Papyrix's XHTML5 "parser" (it does layout & styling too) is organized within its ePub support library.
There's several arrays of special tagnames, a util that checks for whitespace, a util which reformats styling properties from into a new struct, a util which performs an in-array operation, a helper which reformats styling properties & which flushes the current word (NFC-normalized) into the paragraph class, & a helper which splits a block between pages.
1/?
There's a helper which initializes a new text-block & (via another helper) possibly page checking if we're exiting out, or if we should tweak the current empty text-block.
Upon a start element it checks if the nesting's too deep or if we're skipping everything in an open tag, checks for image tags to reformat their referenced file & synthesizes an image block falling back to a text-block with the alt-text, outputs a placeholder for tables, sets the properties to skip certain tags, ...
2/?