Other folks recommend solutions involving regex. That won't be reliable; regex does not work for parsing nested structures. In essence, you need a parser.
You may be able to hack together something in a procedural language using regex, where the procedural language code in essence is the parser, but then that's a solution that uses a little bit of regex and a parser. Writing a full parser for HTML5 this way is going to be a lot of work; you may be able to cheat a lot and build island parsers to pick out just what you want, but island parsers tend to fragile and pretty fiddly.
A parser based solution reads your text, parses it into the HTML5 structures, and then applies the patches you want want to make. Writing a set of procedures that crawl the tree, sniff nodes, and make local tree splices to fix things is possible but awkward, esp. if the set of changes is varied or is large. Then you need to prettyprint the tree with the changes. It is hard to assemble all these bits and make it work esp. for a one-shot task.
An alternative is to use a Program Transformation System (PTS), which has all the parsing and prettyprinting machinery built in, and will let you code surface syntax source-to-source transformations of the form "if you see this, replace it with that". The transformations then match the this part of the rules against the parse tree; where matches are found, the that part is spliced in as a replacement. This makes for accurate matching and replacement; you can only replace the structures you intend to replace. Typically you have to provide a PTS with a grammar (so it can parse) [and often some hints about prettyprinting embedded in the grammar]. PTS are usually applied to systems of files that make up an application.
Our DMS Software Reengineering Toolkit is such a PTS, explicitly designed to carry out mass-change tasks to source code.
DMS has a full HTML4 (with CSS) parser and prettyprinter already available. DMS provides the ability to manage language dialects; it should be straightforward to define HTML5 using the HTML4 base.
With that in place, OP could write his patches directly using DMS rules for HTML5 syntax source-to-source transformations such as:
rule replace_cellcolor(): CSS_attribute -> CSS_attribute
" class=\"cellcolor\" " -> " class=\"CellColor\" ";
This is a rule named replace_cellcolor. It will only match CSS_attributes, as defined by a nonterminal in the grammar; it can't match anything else. The rule transforms a CSS_attribute into a CSS_attribute (you could map it to something else, but that's not useful for this particular activity). The pattern is
class=\"cellcolor\"
written inside rewrite-rule-language metaquotes ".....". Because of the metaquotes we have to meta-escape \" the quotes actually used in the CSS syntax. The rule shows this -> that as source pattern in metaescapes, and target pattern in meta-escapes. That's all that is needed for OP's specific patch. What you write is essentially fragments of the targeted langauge; DMS interprets these as specifications for tree patterns, and uses these tree patterns to match the parsed tree. Since DMS parses these patterns using the same parser as it uses for parsing the source files, the tree patterns it gets are exactly the ones you want.
OP likely has a big set of these capitalization fixes he wants done. He writes one DMS rule per capitalization fix, and organizes them in a set:
ruleset fix_CSS_capitalization =
replace_cellcolor
+ replace_xyx
+ ... ;
DMS will apply the entire set of rules automatically in one of its modes of operation. You need a bit of DMS metaprogramming code (not shown here) to open a designated set of files, parse each file, apply the rules, prettyprint the file; this is pretty standard for uses of DMS. Other applications of DMS that make such changes can run through thousands of files per hour.
Summary, DMS:
- Uses a parser, not regex, to accurately interpret the text
- Allows one to easily express exactly the change desired, using the syntax of the target language (HTML5)
- Allows one to express the set of changes
- Applies the rules automatically
- Prettyprints the answer.
- Can apply changes to multiple files.