Embedding mediawiki templates inside <pre>

I’ve experimented with this a few times, but never found something that works.  My intent was to use some mediawiki templates inside code/file samples on the wiki. On the wiki, we typically use <pre> tags to properly format code/file samples. By definition, the html <pre> tag allows for pre-formatted text to be displayed as-is.  Meaning, most html tags inside <pre> will be displayed and not processed. There are exceptions for some font mark-up tags, but I’m not fully aware of which html tags are explicitly allowed inside <pre>.  Let’s try an example using the following html markup …

This is <strong>bold <span style="color: red;">red</span></strong> text
<a href="#link">This is a link</a>
<span style="color:green;
                font-family:'Times New Roman', Times, serif;
                font-weight:bolder;">big and green times</span>

This will display in a browser as …

This is bold red text
This is a link
big and green times

Introducing mediawiki into the mix complicates things slightly. By design (as security precaution I’d guess), mediawiki does not honor all HTML tags on a wiki page. However, it does recognize and permit some tags, <pre> being one of them. As noted previously, we use the <pre> quite a bit to call out command execution or file contents.

Where the wrinkle comes is trying to use mediawiki templates inside a <pre> block. Well why would you want to do that? Often, our command samples or file contents include Fedora release version specific information. Since Fedora releases come out about every 6 months, for every wiki page that doesn’t need to reference an explicit Fedora release, someone would need to modify every wiki page that references a Fedora version. At a quick glance, that comes to about ~179 wiki pages. As I mentioned, not all of those pages would need an update when a new release comes out, but it at least gives a rough estimate.

After some searching, I came across a forum post which recommends a solution that works. To demonstrate using a mediawiki template inside a <pre> block, I’ll use the wiki page Creating_a_Test_Day_Live_Image. On that page, we recommend checking out a git branch of spin-kickstarts whose name matches the upcoming Fedora release. I’d like to use the FedoraVersion template so we don’t need to constantly update this page each time a new version of Fedora is released.

To make use of the FedoraVersion mediawiki template inside a <pre> block, you would enter the following …

git clone 'git://git.fedorahosted.org/spin-kickstarts.git' -b F-{{FedoraVersion|number|next}}

This instructs mediawiki to format the page as follows:

git clone 'git://git.fedorahosted.org/spin-kickstarts.git' -b F-14

I have no idea why the magic combination of noinclude mediawiki tags works. Bonus points to anyone who can answer: why does this work?


8 responses to “Embedding mediawiki templates inside <pre>

  1. And just to prove that escaping is not a universally-solved problem, the title of this post appears as “Embedding mediawiki templates inside” on Planet Fedora…

  2. note that when the command is a one-liner, <pre> is really overkill. You can just use the {{command}} template. {{command|git clone ‘git://git.fedorahosted.org/spin-kickstarts.git’ -b F-{{FedoraVersion|number|next}}}} should work, I think.

  3. I’m guessing it works because the MediaWiki passes over the text several times and it lets the parser parse what’s inside the pre before it finds the pre tag itself.

      git clone 'git://git.fedorahosted.org/spin-kickstarts.git' -b F-{{FedoraVersion|number|next}}

    Turns into

    git clone ‘git://git.fedorahosted.org/spin-kickstarts.git’ -b F-14

    Turns into

      git clone 'git://git.fedorahosted.org/spin-kickstarts.git' -b F-14

    Is then displayed as

    git clone ‘git://git.fedorahosted.org/spin-kickstarts.git’ -b F-14

  4. Even for multi-line pre sections there is a better way:
    {{#tag:pre| text text {{template}}
    text text {{template|foo|bar}} text}}
    It seems to work the same, but looks much cleaner.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s