Hi all!
During the hackathon, I worked on a patch that would make it possible for non-textual content to be included on wikitext pages using the template syntax. The idea is that if we have a content handler that e.g. generates awesome diagrams from JSON data, like the extension Dan Andreescu wrote, we want to be able to use that output on a wiki page. But until now, that would have required the content handler to generate wikitext for the transclusion - not easily done.
So, I came up with a way for ContentHandler to wrap the HTML generated by another ContentHandler so it can be used for transclusion.
Have a look at the patch at https://gerrit.wikimedia.org/r/#/c/132710/. Note that I have completely rewritten it since my first version at the hackathon.
It would be great to get some feedback on this, and have it merged soon, so we can start using non-textual content to its full potential.
Here is a quick overview of the information flow. Let's assume we have a "template" page T that is supposed to be transcluded on a "target" page P; the template page uses the non-text content model X, while the target page is wikitext. So:
* When Parser parses P, it encounters {{T}} * Parser loads the Content object for T (an XContent object, for model X), and calls getTextForTransclusion() on it, with CONTENT_MODEL_WIKITEXT as the target format. * getTextForTransclusion() calls getContentForTransclusion() * getContentForTransclusion() calls convert( CONTENT_MODEL_WIKITEXT ) which fails (because content model X doesn't provide a wikitext representation). * getContentForTransclusion() then calls convertContentViaHtml() * convertContentViaHtml() calls getTextForTransclusion( CONTENT_MODEL_HTML ) to get the HTML representation. * getTextForTransclusion() calls getContentForTransclusion() calls convert() which handles the conversion to HTML by calling getHtml() directly. * convertContentViaHtml() takes the HTML and calls makeContentFromHtml() on the ContentHandler for wikitext. * makeContentFromHtml() replaces the actual HTML by a parser strip mark, and returns a WikitextContent containing this strip mark. * The strip mark is eventually returns to the original Parser instances, and used to replace {{T}} on the original page.
This essentialyl means that any content can be converted to HTML, and can be transcluded into any content that provides an implementation of makeContentFromHtml(). This actually changes how transclusion of JS and CSS pages into wikitext pages work. You can try this out by transclusing a JS page like MediaWiki:Test.js as a template on a wikitext page.
The old getWikitextForTransclusion() is now a shorthand for getTextForTransclusion( CONTENT_MODEL_WIKITEXT ).
As Brion pointed out in a comment to my original, there is another caveat: what should the expandtemplates module do when expanding non-wikitext templates? I decided to just wrap the HTML in <html>...</html> tags instead of using a strip mark in this case. The resulting wikitext is however only "correct" if $wgRawHtml is enabled, otherwise, the HTML will get mangled/escaped by wikitext parsing. This seems acceptable to me, but please let me know if you have a better idea.
So, let me know what you think! Daniel