HandleSourceInSkin
Description
Ever wanted to see your page source within your skin? Instead of getting a text/plain page in the browser?
Then redirect action=source to a function that works almost like PmWiki's HandleBrowse() but instead of calling MarkupToHTML applies htmlspecialchars() to the page text.
Installation
Place the following code into your local/config.php
$HandleActions["source"] = "HandleSourceInSkin";
function HandleSourceInSkin($pagename, $auth = 'read') {
global $HandleSourceFmt, $PageStartFmt, $PageEndFmt, $FmtV, $ShowSourceHeaderFmt;
$page = RetrieveAuthPage($pagename, $auth, true, READPAGE_CURRENT);
if (!$page) Abort("?cannot show source of $pagename");
PCache($pagename,$page);
# heading
SDV($ShowSourceHeaderFmt, "<h2 class='wikiaction'>$[ShowSource {\$FullName}]</h2>");
# text
$class='sourcecode';
if (PageExists($pagename)) {
$text = @$page['text'];
if (trim($text) == '') {
$text= 'Page contains no text';
$class.=' nosource';
}
}
else {
$text= "Page doesn't exist";
$class.=' nosource';
}
# wrap with <div>
$FmtV['$PageText'] = "<div class='$class'>".str_replace("\n","<br />",htmlspecialchars($text))."</div>";
SDV($HandleSourceFmt,array(&$PageStartFmt, &$ShowSourceHeaderFmt, '$PageText', &$PageEndFmt));
PrintFmt($pagename,$HandleSourceFmt);
}
Define the CSS classes for the <div> wrap in your custom CSS in pub/css/local.css or in your skin's stylesheet. For example:
#wikitext .sourcecode {
font-family:monospace;
}
#wikitext .nosource {
padding:1em;
margin:2em 0;
font-weight:bold;
color:#800;
border:2px dotted #800;
}
Notes
Of course you can customize the above function by using SDVs or XLPage Strings instead of fix texts.
Instead of redirecting action=source you can invent a new one e.g.
$HandleActions["sourceinskin"] = "HandleSourceInSkin";
But why should you?
To make the redirected action available in your skin add something like that to your Site.PageActions:
(:if ( ! action source ) :)
* %item rel=nofollow class=source accesskey='$[ak_source]'%[[{*$FullName}?action=source | $[Source] ]]
(:if:)
Release Notes
- (2009-03-01) Initial version
See Also
Contributors
Comments
See discussion at HandleSourceInSkin-Talk
User notes : If you use, used or reviewed this recipe, you can add your name. These statistics appear in the Cookbook listings and will help newcomers browsing through the wiki.