PagePmSyntax

Summary: Highlight a full PmWiki source page
Version: 20220817, pmwiki-2.3.37
Prerequisites: PmWiki 2.3.x
Status: Experimental
Maintainer: Petko
License: Public Domain
Users: +2 (view / edit)
Discussion: PagePmSyntax-Talk?

From 2.3.35, this functionality is has been incorporated in the core, simply call ?action=source&highlight=1 to output a HTML page with the wiki source highlighted.

Questions answered by this recipe

With PmSyntax, is it possible to automatically highlighting source (?action=source)?

Description

Highlight a full PmWiki source page.

This adds a new action "pmsyntax" which outputs the wiki source text of the page with syntax highlighting by PmSyntax.

Installation

PmWiki 2.3.35 or newer: this has been enabled in the core.

Before PmWiki 2.3.35, add to config.php:

$HandleActions['pmsyntax'] = 'HandlePmSyntax';
function HandlePmSyntax($pagename, $auth='read') {
  global $EnablePmSyntax, $PageStartFmt, $PageEndFmt;
  $page = RetrieveAuthPage($pagename, $auth, true, READPAGE_CURRENT);
  if (!$page) { Abort("?unable to read $pagename"); }

  $text = "<div class='pmhlt'><pre style='white-space: pre-wrap;'>" 
    . str_replace('$', '&#x24;', PHSC($page['text'])).'</pre></div>';

  $fmt = array( &$PageStartFmt, $text, &$PageEndFmt);

  $EnablePmSyntax = 1;
  DisableSkinParts('header footer left right action title');
  PrintFmt($pagename,$fmt);
}

Configuration

To intercept and handle the ?action=source requests, you can add the following to the above block:

PmWiki 2.3.35 or newer:
if($action == 'source' && !isset($_REQUEST['highlight'])) $_REQUEST['highlight'] = 1;

Before PmWiki 2.3.35:
$HandleActions['source'] = 'HandlePmSyntax';

Note, however, that the original action=source outputs plain text, while the new one outputs HTML to be transformed by the browser. If you have tools that query the wiki for action=source and expect plain text, changing the output to HTML may break them.

Usage

PmWiki 2.3.35 or newer: Browse to Page?action=source&highlight=1.

Before PmWiki 2.3.35: Browse to Page?action=pmsyntax.

The function is enabled on PmWiki.org, feel free to test it on any page, for example PmWiki.DocumentationIndex?action=source&highlight=1.

Notes

To do / some day / maybe

Add markup to include in a normal wiki page the highlighted source of another page.

Change log / Release notes

  • 20240613 - added to the core ?action=source for PmWiki 2.3.35. If you upgrade, you can remove this recipe.
  • 20220817 - first public release, ready to be tested.

See also

Contributors

Written and maintained by Petko.

Comments

See discussion at PagePmSyntax-Talk?

User notes +2: 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.