Beautifier

Note: The recipes here are for PmWiki versions 0.6 and 1.0 only. For PmWiki 2.0 recipes, see Cookbook.


Goal

Provide syntax highlighting for source code displayed on wiki pages.

Solution

Integrate Mike Jewell's Beautifier into PmWiki, add custom markup for source code blocks.

:Attach:beautifier-3.1.tgz

Discussion

This version of Beautifier requires PmWiki 0.6.1 or later. You can still find the previous versions here, but they are no longer supported (and really, why wouldn't you run 0.6? It rocks!). The markup has changed in this version, but installation has become drastically simpler. Thanks to Pm for the help. --Profiles/JasonPerkins

Changes in this version

  • Turned off auto-indenting, which never worked very well.
  • Added the CSS writer for more control over code appearance.
  • Changed the markup from =beautify (lang) to =(lang)

Installation

Start by downloading and unpacking the add-on file to your local/ directory. This default package provides support for C++, C#, JavaScript, Lua, PHP, Python, VB.NET, and XML (sort of). If you'd like to add support for additional languages, you can grab more HFiles from here. While you're there, give some props to Mike for making this excellent tool available.

In config.php, you must include the main Beautifier file and register the languages you want to use. This bit of code will register all of the default languages:

    include_once('beautifier.php');
    register_beautifier('cpp');
    register_beautifier('csharp');
    register_beautifier('javascript');
    register_beautifier('lua');
    register_beautifier('php3');
    register_beautifier('python');
    register_beautifier('vbdotnet');
    register_beautifier('xml');

If you are hosting your wiki with Microsoft IIS, you should also add the line

   error_reporting(E_ALL ^ E_NOTICE);

otherwise the Beautifier code will spit out some warning messages that will mess up your pages.

For more control over the appearance of the formatted code, you may wish to set $BeautifierOutputType='css', which will use Beautifier's CSS writer instead of the default HTML. If so, you'll need to specify the code styles in your own stylesheet. The following is an example:

    .code-comment { color: green; }
    .code-quote   { color: gray; }
    .code-type1  { color: blue; }   /* keywords */
    .code-type2  { color: blue; }   /* data types */
    .code-type3  { color: maroon; } /* preprocessor */

Markup

To enter source code into PmWiki, use the following markup:

 
=csharp [=
// C# source code goes here
 
 =]

Replace "csharp" with the appropriate language identifier (php3, vbdotnet, etc.) The formatted source code will be placed into a <pre class="sourcecode"></pre> tag, so you can tweak the appearance of the block in a stylesheet. Here's a nice one:

 .sourcecode {
   background-color: #eef;
   border: #666 1px solid;
   padding: 1em;
 }

You can see this addon in action at http://premake.sourceforge.net/PremakeWiki/Snippets.

See Also

Contributors

  • Jason Perkins
  • PedroRabinovitch (not much really, just corrected a typo at the replacement pattern. I'll add more soon)

Are there any plans to port this very useful recipe to PmWiki 2?

Thanks

Already done -- see Cookbook.Beautifier. --Pm

pmwiki-2.3.38 -- Last modified by {{Pm}}

from IP: 85.171.160.186 ip should be disabled by default for security reasons