Recent Changes - Search:

Cookbook

PmWiki

pmwiki.org

Beautifier

Summary: Perform syntax highlighting for source code displayed on wiki pages
Version:
Prerequisites: PmWiki version: 2 or later
Status:
Maintainer:
Categories: Layout

Questions answered by this recipe

Can I use PmWiki to perform syntax highlighting for source code displayed on wiki pages?

Answer

Yes, the beautifier.tgzΔ script integrates Mike Jewell's Beautifier (http://www.beautifier.org) into PmWiki and adds custom markup for source code blocks.

(Beautifer website gone as of 2007-08-20)
FWIW, it's still available at Sourceforge

Notes

This version of Beautifier requires PmWiki 2 or later. It's based on the beautifier recipe that was developed for PmWiki 0.6 by Jason Perkins. Once installed, it adds markup of the form

=lang [=
lang source code
=]

where lang is any of the supported beautifiers.

Installation

Start by downloading and unpacking beautifier.tgzΔ to the cookbook/beautifier/ directory. This default package provides support for C++, C#, JavaScript, Lua, Perl, PHP, Python, VB.NET, and XML (sort of). If you want support for other languages, you can grab more HFiles from beautifier.org . While you're there, give some props to Mike for making this excellent tool available.

In your local/config.php file, you must include the beautifier.php script and register any languages you want to use. The following would register all of the language beautifiers distributed with the script:

    include_once('cookbook/beautifier/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');

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. You'll want to create your own CSS entries to support the highlighting.

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.) This page has the beautifier enabled for php3, csharp, and css, so you can play with it in the sandbox below.

The formatted output of Beautifier is 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;
 }

See Also

Contributors

  • Jason Perkins
  • PedroRabinovitch (not much really, just corrected a typo at the replacement pattern. I'll add more soon)
  • Pm - revised for PmWiki 2.0, 2004-12-02

Bugs

  • Thanks Pm for this port, I really appreciate, however ;) There's a slight bug that needs fixing in beautifier.php the code Markup("^=beaut_$lang", "block", "/^=$language\\s+$KeepToken(\\d.*?)$KeepToken/e", "'<:block,1>'.do_beautify('$language', '$1')");
  • Should be:
  
         Markup("^=beaut_$language", "block",
         "/^=$language\\s+$KeepToken(\\d.*?)$KeepToken/e",
         "'<:block,1>'.do_beautify('$language', '$1')");

Note the first string has changed, this allows multiple languages to co-exist on the wiki and be beautified independently, this is obviously a typo and not your intention <g> JJ?

Fixed. --Pm
  • Chinese note in the code can't be display correctly.
PS:Cookbook:Source Block works correctly.
  --~LiGuang
  • Is there a reason why the colors (or colours) from the language configuration file aren't used? It looks like all the values are hard coded in ./cookbook/beautifier/php/Output/Output_HTML.php. -Kurt Devlin December 21, 2005, at 01:39 PM

Auto-indenting doesn't work very well. If you want to disable it, comment out the following lines in core.php:

I found just doing the first step worked for me David Jackson
// Strip leading and trailing spaces
if ($this->highlightfile->notrim==0) $line = trim($line);

and

// Print out the current indent.
$sw = $this->_starts_with($lineorig, $this->highlightfile->unindent);
if ($lineorig != "")
{
        if ($this->context->ind>0 && $sw!="")
        {
                $lineout = str_repeat("        ", ($this->context->ind-1));
        }
        else
        {
                $lineout = str_repeat("        ", $this->context->ind);
        }
}

--thom


I've found that I can get something similar to the old Cookbook-V1/TextArea effect by slightly modifying beautifier.php.

I change this section:

$results = "<pre class='sourcecode'>"
. $h1->highlight_text($code) . '</pre>';

to this:

$results = "<textarea cols='60' rows='20' wrap='no'>"
. $h1->highlight_text($code) . '</textarea>';

and bingo. Text areas for all code samples. Of course, a downside is that the highlighting is lost. Is there somewhere better for me to implement this? --olivera?

I'd use CSS with "overflow: auto", and use border colors to give the text area effect.
--Adam

Here are the default HTML colours converted to CSS, they might be useful for anyone using $BeautifierOutputType='css':

span.linecomment, span.blockcomment {

  color: green;

}

span.prepro {

  color: purple;

}

span.select {

  color: red;

}

span.quote, span.category_1, span.category_2, span.category_3 {

  color: blue;

}

--Adam


Sandbox

<?php

if (!@$pagename) {
        $pagename = 'PmWiki.PmWiki';
        if (substr($_SERVER['HTTP_HOST'],-11,11) == 'michaud.com')
        $pagename = 'Pm.HomePage';
}

include_once('scripts/compat1x.php');
#ConvertV1WikiD("wiki-1.d");
UseV1WikiD("wiki-1.d");

$ScriptUrl = "http://{$_SERVER['HTTP_HOST']}/wiki";
$PubDirUrl = '/pmwiki/pub';

include_once('scripts/urlapprove.php');
$UnapprovedLinkFmt = "\$LinkText<a class='apprlink'
href='\$PageUrl?action=approvesites'>$[(approve links)]</a>";

?>
// C++ source code test
#include <iostream>
struct ShowCpp : public std::unary_function<void,std::string>
{
        public:
        typedef std::unary_function<void,std::string> BaseClass;

        void operator ()(const std::string& words) { std::cout << words << std::endl; }
};
//C# source code test
private void go_button(object sender, System.EventArgs e)
{
        System.Object nullObject = 0;
        string str = "";
        System.Object nullObjStr = str;
        Cursor.Current = Cursors.WaitCursor;
        axWebBrowser1.Navigate(textBox1.Text, ref nullObject, ref nullObjStr, ref nullObjStr, ref nullObjStr);
        Cursor.Current = Cursors.Default;
}

=javascript // Java source code test private String doIt(Object sender, x.y.z.Rototo o) throws HibernateException { Collection c = new ArrayList(); String res = ""; for (Iterator i = c.iterator() ; i.hasNext(); ) { res += i.hasNext(); } return res; }

=xml //<this> <is> <a id="toto"/> <test> <nonsense/> </test> </is> </this>

=lua -- lua source code goes here


SFH:
<?php
phpinfo();
?>

Edit - History - Print - Recent Changes - Search
Page last modified on September 15, 2008, at 03:36 PM