SourceBlock
Question answered by this recipe
I tried the (abandoned) Beautifier recipe to perform syntax highlighting for my source code but still wasn't very satisfied... What's next?
Description
Here you'll find SourceBlock: a script recipe dedicated to syntax highlighting, which offers more features:
- Numbered lines on some source code.
- Ordered lists item number preserved when mixed with source code excerpts.
- Extra highlighting to point-out specific source lines.
- Page layout preserved with auto/forced word wrapping of source code lines.
- Language neutral mode without errors when the language is not specified or not recognized.
- Link to view/download individual source code excerpts.
Notes and Comments
SourceBlock is based on GeSHi, a source code highlighting script last updated on 19th of Aug, 2012, already used in several other wiki systems. In the meantime GeSHi development and downloads have moved to https://github.com/GeSHi/geshi-1.0.
Installation
- Download and copy sourceblock.phpΔ to the cookbook/ directory.
- Download and unpack the current GeSHi distribution to a newly created cookbook/geshi directory.
- The 'cookbook/geshi' directory should contain the geshi.php file and the geshi subdirectory containing the ~250 syntax files.
- Enable the recipe in your local/config.php file with:
include_once("$FarmD/cookbook/sourceblock.php");
Markup
Once installed, it adds markup of the form:
(:source PARAMETERS... :) [@
source code
@]
and:
(:source PARAMETERS... :)
source code
(:sourceend:)
with the following optional PARAMETERS:
lang=LANGUAGE
to select one of the supported highlighters,linenum{=LINE_NUMBER}
to enable line numbers and optionally specify styling alternative,linestart=START_NUMBER
to specify the initial line number.tabwidth=TAB_WIDTH
to set the number of spaces to use for tab replacement(*).header=TEXT
andfooter=TEXT
to surround the source code(*).highlight='LINE_NUMBER1, LINE_NUMBER2,...'
to specify the line numbers to point-out (separators are spaces, comma and semicolon).-trim
to disable the automatic trimming of source.-link
to disable the automatic generation of documentation links(*).-getcode
,+getcode
to disable or enable the "Get Code" view/download code link (see below).id=ID
,class=CLASS
to overload default styles definitions.
lang=LANGUAGE
defined.
SourceBlock configures GeSHi to use <div>
and style sheets then word wrapping is less problematic.
In order to be a full alternative to CodeBlock, SourceBlock provides following extra markup to generate <pre>
(formatted) sections (with optional wrapping):
(:code wrap=OUTPUT_WIDTH :) [@
source code
@]
and:
(:code wrap=OUTPUT_WIDTH :)
source code
(:codeend:)
Both markups don't need to start a new line, and may be appended to list elements without altering their order.
The extra "Get Code" link provides a direct access to the unformatted code. The markup also handle the related parameters:
filename=<name>
to provide a relevant name to the block.-inline
to force browsers to download the block content (ie. sole available behavior in Internet Explorer).
Blocks appearance is controlled via style sheets parts overrideable in config.php
. Defaults style are given as recipe samples in the sandbox below.
An additional:
(:source langs:)
markup form is available to list all the languages supported by the GeSHi installation.
Optional external file handling
The recipe script detects any previous installation of the Linked Resource Extras recipe and if so, provide the two more markup directive to render external files:
(:source FILE_RESOURCE PARAMETERS... :)
and:
(:code FILE_RESOURCE PARAMETERS... :)
The markup parameters have the same meaning. Please refer to Linked Resource Extras recipe page for more details on the FILE_RESOURCE specification itself.
SandBox
(:source lang=css header="$HTMLStylesFmt['sourceblock'] = '" footer="';":) [= .sourceblocklink { text-align: right; font-size: smaller; } .sourceblocktext { padding: 0.5em; border: 1px solid #808080; color: #000000; background-color: #F1F0ED; } .sourceblocktext div { font-family: monospace; font-size: small; line-height: 1; height: 1%; } .sourceblocktext div.head, .sourceblocktext div.foot { font: italic medium serif; padding: 0.5em; }=] |
$HTMLStylesFmt['sourceblock'] = ' .sourceblocklink {text-align: right; font-size: smaller; } .sourceblocktext { padding: 0.5em; border: 1px solid #808080; color: #000000; background-color: #F1F0ED; } .sourceblocktext div { font-family: monospace; font-size: small; line-height: 1; height: 1%; } .sourceblocktext div.head, .sourceblocktext div.foot { font: italic medium serif; padding: 0.5em; } '; |
(:source lang=css header="$HTMLStylesFmt['codeblock'] = '" footer="';":) .codeblocklink { text-align: right; font-size: smaller; } .codeblocktext { text-align: left; padding: 0.5em; border: 1px solid #808080; color: #000000; background-color: #F1F0ED; } .codeblocktext pre { font-family: monospace; font-size: small; line-height: 1; } (:sourcend:) |
$HTMLStylesFmt['codeblock'] = ' .codeblocklink {text-align: right; font-size: smaller; } .codeblocktext { text-align: left; padding: 0.5em; border: 1px solid #808080; color: #000000; background-color: #F1F0ED; } .codeblocktext pre { font-family: monospace; font-size: small; line-height: 1; } '; |
(:source lang=bash:) #!/bin/sh # petit script qui genere la cle et le certificat d'un site https DIR=/etc/apache-ssl/ssl-cert # nom du certif/du site NAME=$1 SSL=`which openssl` # check if dir is OK if [ ! -d $DIR ]; then echo "Dir $DIR not found, building it." mkdir -p $DIR fi (:sourcend:) |
#!/bin/sh # petit script qui genere la cle et le certificat d'un site https DIR=/etc/apache-ssl/ssl-cert # nom du certif/du site NAME=$1 SSL=`which openssl` # check if dir is OK if [ ! -d $DIR ]; then echo "Dir $DIR not found, building it." mkdir -p $DIR fi |
(:source lang=sql linenum tabwidth=4 :) -- Find all Boards and the schools they Govern -- Include the status of the school SELECT sch.orgname AS 'SchoolName', bot.orgName AS 'BoardName', sch.[OrgEventStatusCode] AS Status FROM organisation AS sch, orgGroup AS og , organisation AS bot WHERE bot.BusinessEntityID = og.OrgBusinessEntityID -- get the org that is the OWNER AND og.MemberBusinessEntityID = sch.BusinessEntityID -- get the org that is the MEMBER AND og.OrgGroupEndDate is NULL -- NOT an ended group membership AND og.orgRoleGroupCode = 'GOVERN' -- Group for school boards AND og.OrgRoleCode = 'SCHBRD' -- Role for school board members ORDER BY BoardName (:sourcend:) |
|
# blupp blupp blupp\\ (:source lang=php:) <? echo "here's some code"; ?> (:sourcend:) # blipp blipp |
|
(:source lang=xml:) <skos:Concept rdfs:label="root" xmlns:skos="http://www.w3.org/2004/02/skos/core" xmlns:my="http://www.ontologies.thomson.com/MyVocabulary" xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema" tr:index="1"> </skos:Concept> (:sourcend:) |
<skos:Concept rdfs:label="root" xmlns:skos="http://www.w3.org/2004/02/skos/core" xmlns:my="http://www.ontologies.thomson.com/MyVocabulary" xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema" tr:index="1"> </skos:Concept> |
Release Notes
- 2019-07-16
- PHP 7.2 fix
- 2014-09-08
- Fixed stupid bug
- 2014-08-26
- Updated recipe for PHP 5.5
- 2012-07-06
- Fixed bug in style handling (multiple blocks on the same page).
- 2012-07-05
- Integrated suggested modifications (trimming reduced to line-breaks, sorted languages list).
- 2009-01-27
- Fixed multiple blocks of code with different languages on the same page bug. Fixed '
linenum=...
' option and introduced 'linestart=...
'. - 2008-09-16
- Fixed the '
-getcode
' option handling for the(:source:)
markup. - 2008-06-12
- Fixed bugs introduced by previous style features and some code cleanup.
- 2008-06-11
- Added the '
id=...
' andclass=...
style related options. Reduced PHP notice messages. Fixed bug in option handling. - 2008-01-02
- Added
(:source langs:)
markup to list supported languages. - 2007-12-05
- Fixed XHTML validation issue.
- 2007-10-18
- Fixed external file handling (was limited to Attachments).
- 2007-09-13
- Fixed trimming bug. Added ';' as highlighting line separator
- 2007-09-10
- Added the '
-getcode
' option. Renamed the confusing '-attach
' to '-inline
' option. - 2007-05-07
- Added forgotten
<:vspace>
fix. - 2007-04-30
- Code cleanup. Added a fix to "Get Code" feature. Added support for external files.
- 2006-10-25
- Added RecipeInfo data.
- 2006-09-07
- Code cleanup. Added highlight feature.
- 2006-03-07
- Fixed IE css glitch. Updated licensing terms.
- 2006-02-28
- Heavily revamped to allow view/download of individual source code excerpts.
- 2005-09-04
- Fixed
<:vspace>
oddities with 2.0.0 and above. - 2005-06-15
- Fixed bug eating last line when using
source
withoutlang
attribute. - 2005-04-01
- Fixed bug preventing different languages on the same page.
- 2005-03-29
- Synced with 2.0.beta27 markup directive syntax.
See Also
- Cookbook /
- Ape Embed videos, maps, documents, and more in wiki pages (Stable)
- Beautifier Perform syntax highlighting for source code displayed on wiki pages
- CodeMirror An enhanced page editor for PmWiki (Stable)
- Gists Markup to allow easy embedding of syntax-highlighted github gists (active)
- LinkedResourceExtras Linked resource helper functions and scripts for recipe writers (Beta)
- PITS The PmWiki Issue Tracking System (Beta)
- PmPygment Display code highlighting using Python pygment (Stable)
- Precode General utility block markup for displaying codes with syntax highlighting (Active/Stable)
- Syntaxlove Syntax highlighting with good look and cool JavaScript-effects
- ZCode Extract and highlight source code files from 7z, TAR, ZIP, or RAR archives (Experimental)
Contributors
Comments
See discussion at SourceBlock-Talk
List of all supported languages
Geshi Version: 1.0.9.0
lang | full name |
---|---|
4cs | GADV 4CS |
6502acme | MOS 6502 (6510) ACME Cross Assembler format |
6502kickass | MOS 6502 (6510) Kick Assembler format |
6502tasm | MOS 6502 (6510) TASM/64TASS 1.46 Assembler format |
68000devpac | Motorola 68000 - HiSoft Devpac ST 2 Assembler format |
abap | ABAP |
actionscript | ActionScript |
actionscript3 | ActionScript 3 |
ada | Ada |
aimms | AIMMS3 |
algol68 | ALGOL 68 |
apache | Apache configuration |
applescript | AppleScript |
apt_sources | Apt sources |
arm | ARM ASSEMBLER |
asm | ASM |
asp | ASP |
asymptote | asymptote |
autoconf | Autoconf |
autohotkey | Autohotkey |
autoit | AutoIt |
avisynth | AviSynth |
awk | awk |
bascomavr | BASCOM AVR |
bash | Bash |
basic4gl | Basic4GL |
batch | Windows Batch file |
bf | Brainfuck |
biblatex | BibTeX |
bibtex | BibTeX |
blitzbasic | BlitzBasic |
bnf | bnf |
boo | Boo |
c | C |
c_loadrunner | C (LoadRunner) |
c_mac | C (Mac) |
c_winapi | C (WinAPI) |
caddcl | CAD DCL |
cadlisp | CAD Lisp |
ceylon | Ceylon |
cfdg | CFDG |
cfm | ColdFusion |
chaiscript | ChaiScript |
chapel | Chapel |
cil | CIL |
clojure | Clojure |
cmake | CMake |
cobol | COBOL |
coffeescript | CoffeeScript |
cpp | C |
cpp-qt | C++ (Qt) |
cpp-winapi | C++ (WinAPI) |
csharp | C# |
css | CSS |
cuesheet | Cuesheet |
d | D |
dart | Dart |
dcl | DCL |
dcpu16 | DCPU-16 Assembly |
dcs | DCS |
delphi | Delphi |
diff | Diff |
div | DIV |
dos | DOS |
dot | dot |
e | E |
ecmascript | ECMAScript |
eiffel | Eiffel |
email | eMail (mbox) |
epc | EPC |
erlang | Erlang |
euphoria | Euphoria |
ezt | EZT |
f1 | Formula One |
falcon | Falcon |
fo | FO (abas-ERP) |
fortran | Fortran |
freebasic | FreeBasic |
freeswitch | FreeSWITCH |
fsharp | F# |
gambas | GAMBAS |
gdb | GDB |
genero | genero |
genie | Genie |
gettext | GNU Gettext |
glsl | glSlang |
gml | GML |
gnuplot | Gnuplot |
go | Go |
groovy | Groovy |
gwbasic | GwBasic |
haskell | Haskell |
haxe | Haxe |
hicest | HicEst |
hq9plus | HQ9+ |
html4strict | HTML |
html5 | HTML5 |
icon | Icon |
idl | Uno Idl |
ini | INI |
inno | Inno |
intercal | INTERCAL |
io | Io |
ispfpanel | ISPF Panel |
j | J |
java | Java |
java5 | Java(TM) 2 Platform Standard Edition 5.0 |
javascript | Javascript |
jcl | JCL |
jquery | jQuery |
julia | Julia |
kixtart | KiXtart |
klonec | KLone C |
klonecpp | KLone C |
kotlin | Kotlin |
latex | LaTeX |
lb | Liberty BASIC |
ldif | LDIF |
lisp | Lisp |
llvm | LLVM Intermediate Representation |
locobasic | Locomotive Basic |
logtalk | Logtalk |
lolcode | LOLcode |
lotusformulas | Lotus Notes @Formulas |
lotusscript | LotusScript |
lscript | LScript |
lsl2 | LSL2 |
lua | Lua |
m68k | Motorola 68000 Assembler |
magiksf | MagikSF |
make | GNU make |
mapbasic | MapBasic |
mathematica | Mathematica |
matlab | Matlab M |
mercury | Mercury |
metapost | MetaPost |
mirc | mIRC Scripting |
mk-61 | МК-61/52 |
mmix | MMIX |
modula2 | Modula-2 |
modula3 | Modula-3 |
mpasm | Microchip Assembler |
mxml | MXML |
mysql | MySQL |
nagios | Nagios |
netrexx | NetRexx |
newlisp | newlisp |
nginx | nginx |
nimrod | Nimrod |
nsis | NSIS |
oberon2 | Oberon-2 |
objc | Objective-C |
objeck | Objeck Programming Language |
ocaml | OCaml |
ocaml-brief | OCaml (brief) |
octave | GNU/Octave |
oobas | OpenOffice.org Basic |
oorexx | ooRexx |
oracle11 | Oracle 11 SQL |
oracle8 | Oracle 8 SQL |
oxygene | Oxygene |
oz | OZ |
parasail | ParaSail |
parigp | PARI/GP |
pascal | Pascal |
pcre | PCRE |
per | per |
perl | Perl |
perl6 | Perl 6 |
pf | OpenBSD Packet Filter |
phix | Phix |
php | PHP |
php-brief | PHP (brief) |
pic16 | PIC16 |
pike | Pike |
pixelbender | Pixel Bender 1.0 |
pli | PL/I |
plsql | PL/SQL |
postgresql | PostgreSQL |
postscript | PostScript |
povray | POVRAY |
powerbuilder | PowerBuilder |
powershell | PowerShell |
proftpd | ProFTPd configuration |
progress | Progress |
prolog | Prolog |
properties | PROPERTIES |
providex | ProvideX |
purebasic | PureBasic |
pycon | Python (console mode) |
pys60 | Python for S60 |
python | Python |
q | q/kdb+ |
qbasic | QBasic/QuickBASIC |
qml | QML |
racket | Racket |
rails | Rails |
rbs | RBScript |
rebol | REBOL |
reg | Microsoft Registry |
rexx | rexx |
robots | robots.txt |
rpmspec | RPM Specification File |
rsplus | R / S+ |
ruby | Ruby |
rust | Rust |
sas | SAS |
sass | Sass |
scala | Scala |
scheme | Scheme |
scilab | SciLab |
scl | SCL |
sdlbasic | sdlBasic |
smalltalk | Smalltalk |
smarty | Smarty |
spark | SPARK |
sparql | SPARQL |
sql | SQL |
standardml | StandardML |
stonescript | StoneScript |
swift | Swift |
systemverilog | SystemVerilog |
tcl | TCL |
tclegg | TCLEGG |
teraterm | Tera Term Macro |
texgraph | TeXgraph |
text | Text |
thinbasic | thinBasic |
tsql | T-SQL |
twig | Twig |
typoscript | TypoScript |
unicon | Unicon (Unified Extended Dialect of Icon) |
upc | UPC |
urbi | Urbi |
uscript | Unreal Script |
vala | Vala |
vb | Visual Basic |
vbnet | vb.net |
vbscript | VBScript |
vedit | Vedit macro language |
verilog | Verilog |
vhdl | VHDL |
vim | Vim Script |
visualfoxpro | Visual Fox Pro |
visualprolog | Visual Prolog |
whitespace | Whitespace |
whois | Whois (RPSL format) |
winbatch | Winbatch |
xbasic | XBasic |
xml | XML |
xojo | Xojo |
xorg_conf | Xorg configuration |
xpp | X |
yaml | YAML |
z80 | ZiLOG Z80 Assembler |
zxbasic | ZXBasic |
User notes +9: 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.