CodeBlock
Question
I already tried Beautifier? to perform syntax highlighting for source code but I want also:
- Numbered lines on some source code,
- To use source code excerpts to illustrate ordered lists items without loosing item numbers,
- To control the source code line length with word wrapping to preserve the page layout,
- No error/warnings when the language is not specified or not recognized.
Could you help me?
Answer
Yes, you'll need:
- The Beautifier? package script for basic features (beautifier core),
- The code block extension which provide a new improved custom markup.
Discussion
CodeBlock is based on Beautifier? cookbook recipe. Once installed, it adds markup of the form
(:code<=lang> <ln> <wrap=n> :) [=
lang source code
=]
where optional parameters:
lang
is any of the supported beautifiers,ln
enables line numbers,n
forces word wrapping to specified line length.
Installation
- Download and extract Beautifier? package script as described in the relevant recipe,
- Download and extract Attach:codeblock.zip to the
cookbook/
directory.
In your local/config.php
file, you only have to include the codeblock.php
script:
include_once('cookbook/codeblock.php');
There's no need to include the beautifier.php
script neither register any language as required by the Beautifier? recipe.
For more control over the appearance of the formatted code (as does the Beautifier? recipe), you may wish to set $BeautifierOutputType='css'
, which will use Beautifier's CSS writer instead of the default HTML.
You may also set $CodeWrapLen
to control default wrapping line length (applies only to blocks with unspecified wrapping length and without language specified or recognized).
Markup
To enter source code into PmWiki, use the following markup:
(:code=csharp:) [=
// C# source code goes here
=]
The markup don't need to start a new line, and may be appended to list elements without altering their order:
# List elem (:code:) [=
/* code sample */
=]
# Next list elem (:code:) [=
/* next code sample */
=]
In order to allow the appearance tweaking of the block via a stylesheet, the formatted output of Beautifier is placed into a <div class='codeblock'><pre></pre></div>
tag, and optional line numbers are enclosed into <span class='codeblock_ln'></span>
.
Here's a nice one:
.codeblock { padding: 0 0.5em; text-align: center; } .codeblock pre { line-height: 1; border: 1px solid #808080; background-color: white; text-align: left; padding: 0.5em 10px; } .codeblock pre .codeblock_ln { color: #808080; }
Caveat
For now, when forcing word-wrapping of syntaxically highlighted code, original line numbers couldn't be preserved as without any language specification.
Contributors
--Dfaure, and all the previous Beautifier? contributors.
Discussion
Sandbox
(:code=php3 ln:) <?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>"; ?>
(:code=cpp ln:) #include "somefile.h" if (p == 2) { int a = 4; }
Problems with colour coding
I downloaded all the neccessary Beautifier & CodeBook files and had eventually got some nice colour coding of some C++ code I had been writing. When I uploaded all the files to my web site I no longer got colour coding. If this happens to you then change the line in codeblock.php
SDV($BEAUT_PATH, dirname(__FILE__) . "/beautifier/php");
to
SDV($BEAUT_PATH, dirname(__FILE__) . "/Beautifier/php");
The path names appear to be case sensitive. This probably due to different Apache configuration settings on the 2 machines!!
Category: Superceded