Using Wiki Pages as CSS Files

This action allows any page on your wiki to be used as a CSS file. It will work even if the page contains a bunch of other wiki formatting. It simply extracts all of the code blocks with a type of css and inserts them into a tag in the header action. If the action is placed in the GlobalActions page, the CSS will be applied sitewide. The only downside with the current version is that each page is bloated up a bit by the CSS code. This will be addressed in later versions.

The Changes

You'll need to open up the "header.php" file in your actions directory and insert the following just below the links to the stylesheet:

        $global_actions = $this->LoadSingle('SELECT body FROM ' . $this->config['table_prefix'] . 'pages WHERE tag = \'GlobalActions\' AND latest = \'Y\'');
        $regex = '/\{\{customcss.+="(.+)"\}\}/sU';
        if (preg_match_all($regex, $this->page['body'] . (isset($global_actions['body']) ? $global_actions['body'] : ''), $matches))
            for ($i = 0; $i < count($matches[1]); $i++) $csspages[$matches[1][$i]] = $i + 1;
            foreach ($csspages as $csspage => $id) $params .= " file$id=\"$csspage\"";
            echo $this->Action('customcss apply="true"' . $params);

Then save the following code as "customcss.php" inside of the actions directory:


if ($this->tag == 'GlobalActions' || !$vars['apply']) return;

$tags = array();
foreach ($vars as $var => $value) if (substr($var, 0, 4) == 'file') $tags[] = $value;
$tags = join("','", $tags);

$result = $this->Query('SELECT body FROM ' . $this->config['table_prefix'] . "pages WHERE tag IN ('$tags') AND latest = 'Y'");
if (mysql_num_rows($result)) while ($row = mysql_fetch_array($result)) $pages .= $row['body'];
if (preg_match_all('|% %\(css\)(.*)% %|smU', $pages, $matches)) foreach ($matches[1] as $match) $css .= $match;
echo $css;


Please note: "% %\(css\)(.*)% %" should actually be "%%\(css\)(.*)%%"

The "apply" parameter is just a hack that makes the action fail when the page is run through the formatter. All of the work is actually done in the header.php action. No need to do it again!

Now drop something like this into a wiki page to get the whole thing working...

{{customcss file="PageCSS"}}
{{customcss file="AnotherCSSPage"}}

If you would like the CSS to be included sitewide, drop it into a page named GlobalActions. Don't worry about duplication, since any particular page will only be included once, no matter how many times it's listed.



There are 6 comments on this page. [Show comments]
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki