You are here: Foswiki>System Web>SyntaxHighlightingPlugin (26 Apr 2022, micha)Edit Attach

SyntaxHighlightingPlugin

Highlights code fragments for many languages using enscript.

Description

The Syntax Highlighting Plugin is used to emphasize the rendering of your wiki text according to several languages.

It uses enscript to render its output.

It supports the following languages :

  1. ada
  2. asm
  3. awk
  4. bash
  5. c
  6. changelog
  7. cpp (c++)
  8. csh
  9. delphi
  10. diff
  11. diffs
  12. diffu
  13. dylan
  14. eiffel
  15. elisp
  16. f90
  17. forth
  18. fortran
  19. fortran_pp (fortran77)
  20. haskell
  21. html
  22. icon
  23. idl
  24. inf
  25. java
  26. javascript (js)
  27. ksh
  28. lua
  29. m4
  30. mail
  31. makefile
  32. matlab
  33. nroff
  34. oberon2
  35. objc
  36. octave
  37. outline
  38. oz
  39. pascal
  40. perl
  41. postscript
  42. pyrex
  43. python
  44. rfc
  45. ruby
  46. scheme
  47. sh
  48. skill
  49. Smalltalk
  50. sml
  51. sql
  52. states
  53. synopsys
  54. tcl
  55. tcsh
  56. tex
  57. vba
  58. verilog
  59. vhdl
  60. vrml
  61. wmlscript
  62. zsh

Syntax Rules

To use this plugin, use the following syntax:

%CODE{lang="..." [num="10"]}% 
...code...
%ENDCODE%

The language can be any from the above list. The num setting is optional and will set the starting line number. Use <sticky> tags to prevent Foswiki's WYSIWYG editor from removing line breaks inside the code block:

<sticky>
%CODE{...}%
... code ...
%ENDCODE%
</sticky>

Using SyntaxHighlightingPlugin With Other Syntax Highlighters

Currently all the Foswiki syntax highlighting plugins use the same %CODE% syntax. This allows you to switch the highlighter you use without having to update all your topics to the new syntax. However, it also means that if you have two or more highlighting plugins installed on your system, the plugin that is first in $Foswiki::cfg{PluginsOrder} (or by default, first alphabetically) will consume all the %CODE% blocks.

Version 1.2 of this plugin added support for using the %CODE_ENSCRIPT% syntax, which allows you to explicitly which plugin you want to highlight your code.

Clearly this is not the best solution to the problem of using more than one highlighting plugin. You can read the background on why this approach was taken in the task report.

Examples

The following text:

%CODE{"c++"}%
#include <iostream>
int main()
{
  std::cout << "Hello, world." << std::endl;
}
%ENDCODE%

gives (if installed):

bomb Language 'c++' not supported


#include 
int main()
{
  std::cout << "Hello, world." << std::endl;
}

You can also output numbered lines starting at 10 with this text:

%CODE{"sh" num="10"}% 
#!/bin/sh
languages=`enscript --help-highlight | grep 'Name:' | cut -d ' ' -f 2`
for l in $languages; do
    cat << EOF
   * $l
EOF
done
%ENDCODE% 

gives (if installed):

bomb Language 'sh' not supported


#!/bin/sh
languages=`enscript --help-highlight | grep 'Name:' | cut -d ' ' -f 2`
for l in $languages; do
	 cat << EOF
	* $l
EOF
done

Change of Syntax

This plugin used to use the following syntax (pre September 2008):

%begin sh%
...code...
%end%

This has been changed to be more consistent with other Foswiki variables. There is a script included which will replace the old syntax with the new. To use it, copy it from the tools directory and into your data directory. When you run it, it will look through your webs and replace the syntax. Note that its not the best script in the world, so always test it on a copy of your data first!

Installation Instructions

You do not need to install anything in the browser to use this extension. The following instructions are for the administrator who installs the extension on the server.

Open configure, and open the "Extensions" section. "Extensions Operation and Maintenance" Tab -> "Install, Update or Remove extensions" Tab. Click the "Search for Extensions" button. Enter part of the extension name or description and press search. Select the desired extension(s) and click install. If an extension is already installed, it will not show up in the search results.

You can also install from the shell by running the extension installer as the web server user: (Be sure to run as the webserver user, not as root!)
cd /path/to/foswiki
perl tools/extension_installer <NameOfExtension> install

If you have any problems, or if the extension isn't available in configure, then you can still install manually from the command-line. See https://foswiki.org/Support/ManuallyInstallingExtensions for more help.

You should ensure the path to enscript is correctly set in configure before enabling the plugin.

Dependencies

NameVersionDescription
enscript>1.6.4Required. Known to work with version 1.6.4 and not with 1.6.1

Change History

26 Apr 2022: 2.00: rewrite and cleanup -- Foswiki:Main.MichaelDaum
25 Oct 2010: 1.21: Documentation updates. -- Foswiki:Main.AndreasKeil, Foswiki:Main.WillNorris
07 Jun 2009: 1.2: Fixed rendering in numbered lists (Foswikitask:Item8176); Moved configure setting to Extensions heading -- Foswiki:Main.AndrewJones
29 Mar 2009: 1.1: Added %CODE_ENSCRIPT% syntax so it can be used with other syntax highlighters (Foswikitask:Item1353) -- Foswiki:Main.AndrewJones
17 Mar 2009: 1.0: Initial release for Foswiki (Foswikitask:Item1251) -- Foswiki:Main.AndrewJones
11 Sep 2008: Rewritten to work on TWiki 4.2 (TWikibug:Item5995) -- Foswiki:Main.AndrewJones
12 Jul 2002: Initial version

PackageForm edit

Author TWiki:Main.NicolasTisserand, TWiki:Main.NicolasBurrus, Perceval Anichini, Foswiki:Main.AndrewJones, Foswiki:Main.MichaelDaum
Version 2.00
Release 26 Apr 2022
Description Highlights code fragments for many languages using enscript.
Repository https://github.com/foswiki/SyntaxHighlightingPlugin
Copyright © 2008 - 2009, Andrew Jones, © 2000 - 2001, Andrea Sterbini, © 2001, Peter Theony, © 2021-2022 Foswiki Contributors
License GPL (GNU General Public License)
Home https://foswiki.org/Extensions/SyntaxHighlightingPlugin
Support https://foswiki.org/Support/SyntaxHighlightingPlugin
Topic revision: r1 - 26 Apr 2022, micha
This site is powered by FoswikiCopyright © by the contributing authors. All material on this site is the property of the contributing authors.
Ideas, requests, problems regarding Foswiki? Send feedback