How would you like to always know that your Rails .rhtml templates were valid XHTML? I have to admit: I'm kind of anal about my standards-compliance. If I write code, I want it to conform to the coding conventions for whatever language I'm using. If I'm writing XML, it damn well better validate against the particular XML application's schema. Which is what make James Clark's nXML mode for Emacs so great.
In my opinion, Emacs in nXML mode is bar-none the best text-oriented XML editor available1. It's main trick is incrementally parsing your XML and validating it against the RELAX NG compact syntax2 version of the schema. So you know with every keypress whether or not your document is valid. Plus you get fancy features like tab-completion on only elements valid in the current context and automatic tag closing.
The one teensy, tiny problem I've had with it is that it completely barfs all over the ERb sections in your Rails .rhtml templates. You never know whether or not your document is valid, because it's never really valid! Teh suck!
Ah, but there is a fix, if you are... mad. Last night I finally did as I've been meaning to do and dove into the core of nXML's XML parser. It turns out to be regexp-based, but in a good way3 — Emacs regexps are probably the only way in Emacs to gobble characters fast enough to make parser responsive. And the way James Clark writes those regexps... His regexp-generation code is difficult to understand, but the actually regexps to do the parsing are crystal-clear and laser-precise. Here's the man himself and a snippet of his code:
;; A symbolic regexp is represented by a list whose CAR is the string
;; containing the regexp and whose cdr is a list of symbolic names
;; for the groups in the string.
(comment (xmltok+ (xmltok-g markup-declaration "!") (xmltok-g comment-first-dash "-" (xmltok-g comment-open "-") opt) opt))
Anyway, the upshot is that I was able to patch nXML's XML parser to parse ERB
sections with only 14 lines of code. It reports them back up to nXML mode as
'erb-section tokens, which the higher levels of the mode happily completely
ignore. This doesn't help with templates which are only XHTML fragments, but
I'm working on a solution for that too... So iff4 you're ready to get valid,
grab my patch5 and go to it!
1 Ok — I've used too few other tools to claim this with much authority, but nXML mode is pretty useful.
2 RELAX NG is also (mainly) James Clark's.
3 Unlike say, ahem REXML. I love and use the API, but wish it wouldn't accept invalid XML.
4 sic
5 If all the eval-when-compiles in xmltok.el don't make it
immediately obvious, you will need to byte-compile the patched file to get
the benefits of the modified parser.
Commentary most sage
Great work, congrats! Nxhtml mode solved xhtml fragment problem. But if there is a fix with using php-tpl files, especially for smarty files, it will be more than great:)