The Code2Html Plugin#

Chinese Instruction Code2Html_zh_CN

This plugin using the jEdit(jedit.org)'s syntax engine to parse source code and rederered it to HTML output, it supports up to 130 different syntaxes as much as the jEdit supports. See readme.txt(info) for the fully instruction.

Install#

Unzip CodePluginBin.zip(info) and jeditsyntax.zip(info) to your jspwiki web app install dir. Make any required changes to WEB-INF/classes/code2html.properties; then restart the web app server or reload the app.

News:#

May 17 2010 --Arno Brouwer: I made the following changes:
  • Merged Manuel's changes with the latest (packaged) Code2Html class; moved the codeplugin.cssPath property to the code2html.properties file and renamed to cssPath.
  • When you put the code in the body of the plugin, and did not specify "showsrc=false", the plugin would print an empty line at the top of the code block; this is fixed.
  • Empty lines in the code would not be displayed by the plugin; this is fixed.
  • The JSPWiki path can now contain spaces and all sorts of other strange characters.

To install, simply unzip CodePluginBin.zip(info) and jeditsyntax.zip(info) to your JSPWiki web app install dir (I had to split the zip file up because this wiki has an upload limit of 500K). This will create or overwrite the following files:

WEB-INF/lib/Code2Html.jar Jar file with the plugin class, and a jspwiki_module.xml file
WEB-INF/lib/beansoft_lib.jar Utility classes used by the plugin
WEB-INF/lib/jeditsyntax.jar The JEdit library used to parse the code
WEB-INF/classes/code2html.properties Property file for the plugin
WEB-INF/classes/modes/* Directory containing the language files

If you had a custom CSS file defined in jspwiki.properties with the codeplugin.cssPath parameter, then you will have to set the parameter cssPath in WEB-INF/classes/code2html.properties. The codeplugin.cssPath parameter in jspwiki.properties is no longer used.

Since the Code2Html jar file contains a jspwiki_module.xml file, you no longer have to set the jspwiki.plugin.searchPath property in jspwiki.properties.

If you are upgrading from an earlier version, you will have to manually delete the Code.class files from WEB-INF/classes and WEB-INF/classes/beansoft/jspwiki/plugins.

I marked my changes in the source code(info) with FIX: PG tags.

Nov 22 2009 --Manuel: I made some changes that:

  • take the baseURL in order to generate the link to the CSS stylesheet. That way, the plugin works also with the ShortURLConstructor
  • make it possible to customize the path to the stylesheet: you can add a property in jspwiki.properties to customize the path to the CSS stylesheet: codeplugin.cssPath = my/custom/relative/path/stylesheet.css (I didn't test this one, if it does not work please ping me)

You can download the updated source here: Code.java(info)

Dec 8 2006 Code2Html Plugin 1.1

We realeased a new version of Code2Html plugin, thanks Jürgen Weber and Venk to testing it and give me some great ideas.
This version supports reading from attachment file(like the core Image plugin) and local file system(while, there is a security issue, but I use a config file to determine whether allow the plugin to access local file system). The source code is here: Code.java(info)

Dec 5 2006 CodePluginBinPatch.zip(info) is a file contains the plugin file with a package name. I have compiled a packaged version for that, just extract this patch file to the wiki root dir. This class now is beansoft.jspwiki.plugins.Code. Change the jspwiki.properties file for the property called:
jspwiki.plugin.searchPath = beansoft.jspwiki.plugins,...other packages
or use full class name:

[{beansoft.jspwiki.plugins.Code ...
}]

Wanna change output color format? Just edit the css file css/syntax.css and u'll got it.

Usage:#

[{Code lang='language name' src='attachment file src' version='attachment file version'
file = 'file path of local file system(on the server)' encoding='UTF-8,etc' showsrc='true/false'
<CR or LN, just a empty line here>
--- The source code body here, might have multi lines. ---
}]
Parameters:
  • lang(optional):language name, default value is java, remember don't use values that not exists in the language list:
java jsp actionscript ada95 antlr apacheconf apdl applescript asp aspect-j assembly-m68k assembly-macro32 
assembly-r2000 assembly-parrot assembly-x86 awk b batch bbj bcel beanshell bibtex c chill cil cobol coldfusion
c++ c# css cvs-commit d doxygen dsssl embperl erlang eiffel factor forth fortran foxpro freemarker gettext groovy
haskell hex html i4gl icon idl inform inno-setup ini interlis io javascript jcl jhtml jmk latex lilypond lisp 
literate-haskell lotos lua mail makefile maple ml modula3 moin mqsc netrexx nqc nsis2 objective-c objectrexx 
occam omnimark pascal patch perl php pike pl-sql pl1 pop11 postscript powerdynamo povray prolog progress 
properties psp ptl pvwave pyrex python rebol redcode relax-ng-compact renderman-rib rd rest rhtml rpm-spec 
rtf ruby rview s+ s# sas scheme sgml shellscript shtml slate smalltalk sdl/pr smi-mib sqr squidconf 
svn-commit swig tcl texinfo tex text tpl transact-sql twiki uscript vbscript velocity verilog vhdl xml xsl zpt
  • source code (optional), is putting in the body of that Tag(all JSPWiki Plugin supports a special parameter called '_body'), the first line must be an empty line.
  • src(optional):the attachment's src on the wiki, might with full path or not, eg: Code2Html/Code.java or Code.java
  • version(optional):only in case you use a src, then this will specify the attachment's version, default is latest, eg: 0,1
  • file(optional):file path on the local file system you want to read, eg: c:
    \My.java or c:/My.java, by default this is not opened
  • encoding(optional):the content's encoding of attachment or local file, default to server file system's encoding
  • showsrc(optional):whether display the file/attachment's src to the output, default value is "true"

We load the code content in following sequence: _body > src > file

  • Security issue:
If you want let the plugin to read the local file system, please edit code2html.properties and change following line:
allowReadLocalFile = false
to
allowReadLocalFile = true

Eg:

[{Code lang=javascript

/** A JavaScript function */
function test() {
  var a = "abc";
  alert(a);
}
}]

The result like this:

syntaxscreenshoot.png

More egs: Read attachment from same page: [{Code src='Code.java'}] Read local files: [{Code file='E:\\OpenSource\\JSPWikiPlugins\\src\\Test.java' showsrc=false}]

Touch me: beansoftstudio@msn.com


I am getting lots extra spaces in the code output. I have tried with both XML and Java and if a word is highlighted in some way, it also looks like its getting padded. I used the bin dist available here on Tomcat 5.5.20 with JSPWiki 2.4.71

--AnonymousCoward, 08-Nov-2006


I just output the original source code like this:

<link rel='stylesheet' type='text/css' href='css/syntax.css'/><pre><span class=comment>/* comment here</span> </pre>

Also I've tested on v2.4.71, using the default css both under IE and Firefox, it looks same, I think your problem might is:

1. source code include too many tab chars(one tab take 8 spaces);

2. the css template difines the span's padding.

Maybe you can see the css file or might change span output to div by change the source code of studio.beansoft.syntax.sample.HtmlOutputTokenHandler.

--Jacky Liu, 08-Nov-2006


The Code2Html Plugin is great.

What about adding the feature to code2html files in the filesystem or inline attachements like inlining images?

--Jürgen Weber, 30-Nov-2006


Allowing access to the file system would be a big security risk...

--JanneJalkanen, 30-Nov-2006


Adding the feature to code2html files in the filesystem is really a security risk, esepecially when u put a site on the internet. But inline attachements like inlining images is really a good idea, also i'm cosidering to add line number display switch in the next coming version.

--Jacky Liu, 01-Dec-2006


Of course, it would be a risk to give internet users access to the filesystem.

Still, it would be useful for intranets, e.g. you could access a subversion repository checked out to the filesystem.

So, to address the security concerns:

  • filesystem access should be restricted to a given folder and its subfolders
  • linking to the filesystem should be restricted to certain user groups

--Jürgen Weber, 01-Dec-2006

What is the full path to the class I need to enter in the jspwiki.properties file for the property called: jspwiki.plugin.searchPath

Currently I have it set to: jspwiki.plugin.searchPath = lu.intrasoft.jspwiki.plugin

Plugin insertion failed: Could not find plugin CodePluginPlugin insertion failed: Could not find plugin CodePlugin

-- Venki, 04-Dec-2006


Hi Venki, the file has no package, so sometimes might be a issue.

Try: 1) reload the web app;

2) recompile the file by move Code.java to beansoft.jspwiki.plugins.Code.java, then add to search path.

I've created a small patch fileCodePluginBinPatch.zip(info) of this issue, see UPD above. Please test it and send a feedback.

Next version i'll trying to move it to a new package so this issue might be fixed.

--Jacky Liu, 05-Dec-2006


Jacky, thanks very much for implementing the attachment feature, it works like a breeze. Wonder how I got by with plain text source code for so long.

8-) Jürgen

--Jürgen Weber, 11-Dec-2006


Hi Jacky

I'm using jspwiki 2.4.87

It prints everything as preformatted text?

Looking at the page source it makes sense, i.e. I see this:

<pre><span class=reservedWord></span><br><span class=comment>/*</span><span class=comment>*</span><span class=comment> </span><span class=comment>A</span><span class=comment> </span><span class=comment>JavaScript</span><span class=comment> </span><span class=comment>function</span><span class=comment> </span><span class=comment>*/</span>

<span class=reservedWord>function</span> test() <span class=operator>{</span>
  <span class=reservedWord>var</span> a <span class=operator>=</span> <span class=literal>"</span><span class=literal>abc</span><span class=literal>"</span><span class=operator>;</span>
<span class=operator>}</span>

</pre>

Why are there <pre></pre> tags surrounding it?

Can a patch be done, or could you tell me how to fix this

Thanks

--GregP 25th Jan 2007


Yes, it do use pre to keep the original code format, but the span with class name which will display different colors of code.

The output code is like this: <link rel='stylesheet' type='text/css' href='css/syntax.css'/> <pre><span class=reservedWord....>res</span> </pre>

Don't forget to check the css file, without it the code is just displayed as preformatted text.

It should be put at: $tomcat_home/webapps/JSPWiki/css/syntax.css then the app can use it.

Or enter http://localhost:8080/JSPWiki/css/syntax.css to check the file whether could be accessble.

--Jacky Liu, 26-Jan-2007


--GregP, 16-Feb-2007

FYI, it seems that the Brushed Template, Typography functionality interferes with your plugin

Nice plugin by the way... very useful



Plugin doesn't like it when JSPWiki has spaces in its path. It uses "%20" instead of ' '. I've tracked that down to StringUtil.getRealFilePath and just inserted .replaceAll("%20", " ") after inputURL.getFile(), but there should be a better solution.

--ShadowIce, 01-Mar-2007


Hi,

on my wiki the plugin does not seem to work. i checked the reachability of the syntax.css and the code, but the result for:

[{Code lang=javascript

/** A JavaScript function */
function test() {
  var a = "abc";
}
}]

is :

<span class="reservedWord"></span><br><span class="comment">/*</span><span class="comment">*</span><span class="comment"> </span><span class="comment">A</span><span class="comment"> </span><span class="comment">JavaScript</span><span class="comment"> </span><span class="comment">function</span><span class="comment"> </span><span class="comment">*/</span>
<span class="reservedWord">function</span> test() <span class="operator">{</span>
  <span class="reservedWord">var</span> a <span class="operator">=</span> <span class="literal">"</span><span class="literal">abc</span><span class="literal">"</span><span class="operator">;</span>
<span class="operator">}</span>

any idea, what could be the problem?

--Lukas Weberruss, 23-Mar-2007


I was trying to solve issue that path have "%20" in the string, so I am thinking to add some code to ModeLoader , just like below :


    public static void loadModeCatalog(String path, boolean resource)
    {
        path=path.replaceAll("%20"," ");
        /*some orign code here */
will that help the issue ?

thanks a lot...

--TonyQ, 10-Jun-2008


I seems to be getting an error with this plugin

Plugin insertion failed: Can't find resource for bundle java.util.PropertyResourceBundle, key plugin.error.pluginfailed 

and this was using the example on the page. i've tried reinstalling, and adding the search path in the properties file.

checking the log files, i see this error

java.lang.~NullPointerException
        at studio.beansoft.syntax.~ModeLoader.parseTokens(ModeLoader.java:84)
        at studio.beansoft.syntax.sample.~HtmlSyntaxHighlighter.syntaxTextToHtml(HtmlSyntaxHighlighter.java:44)
        at beansoft.jspwiki.plugins.Code.execute(Code.java:173)
        at com.ecyrd.jspwiki.plugin.~PluginManager.execute(PluginManager.java:383)

looking at the source attached, that Null Pointer is here.

  public static void parseTokens(String string, String languageType, TokenHandler tokenHandler) {
		TokenMarker marker = getMode(languageType).getTokenMarker();

		StringTokenizer tokenizer = new StringTokenizer(string, "\r\n");// Parsing line by line
        
		TokenMarker.LineContext context = null;
	
		
	-->	while(tokenizer.hasMoreElements()) 

confused as to why that line would throw a null pointer.

I've taken the patched jar and code.class with properties file.

Any suggestions ?

--AnonymousCoward, 31-Jul-2008


I am also getting the same error when I used this plugin.

But when I see tomcat logs I found this

java.io.FileNotFoundException: C:\Program%20Files\Apache%20Software%20Foundation
\Tomcat%205.5\webapps\JSPWiki\WEB-INF\classes\modes\catalog (The system cannot f
ind the path specified)

catalog file is present at specified path. When I checked its properties I found that this file is blocked as it comes from diff computer. I unblocked it. But still getting the same error. I checked each and every file in zip and its having same issue "beacuse of security reason it is blocked" SO does it need to have some specific properties to each file to run this plugin.

I am running my jspwiki on windows machine.

Thanks for the help. Trupti - 12-feb-09



If I use a wiki with a prefix so that pge url ends NOT like:
http://www.jspwiki.org/Wiki.jsp?page=Code2Html
but
http://www.jspwiki.org/wiki/Code2Html
then including the css doesn't work (and therefore colors are not shown at all). This is because the Code.java contains the css inclusion HTML code with link to CSS like this:
protected static final String CSS_URL = "<link rel='stylesheet' type='text/css' href='css/syntax.css'/>"; and before the css the ../ should be there in such a case. This, though, should be auto-recognised by Code.java from the jspwiki.properties file.

--AndrasBalogh, 17-Aug-2009 10:20


I made a new version that addresses the issue described above --Manuel, 22-Nov-2009


The plugin encloses the rendered code with the <pre> tag, in this case if the code contains angular braces '<' or '>' in the code, the html is scrambled as it needs to be escaped. ex:

[{Code lang='html'

<table>
  <tr>
    <td>testing</td>
  </tr>
</table>
}]

To fix this I had to add the below line of code in the handleToken method of studio.beansoft.syntax.sample.HtmlOutputTokenHandler class

segText = segText.replace("<", "<").replace(">", ">");

--Vignesh Srinivasan, 10-Mar-2011 13:40

Add new attachment

Only authorized users are allowed to upload new attachments.

List of attachments

Kind Attachment Name Size Version Date Modified Author Change note
class
Code.class 7.6 kB 1 22-Nov-2009 19:54 Manuel Updated version that takes the baseURL to generate the CSS stylesheet link
java
Code.java 12.0 kB 2 22-Nov-2009 19:54 Manuel Updated version that takes the baseURL to generate the CSS stylesheet link
zip
CodePluginBin.zip 438.7 kB 2 17-May-2010 22:10 Pizza Geek Merged packaged class and Manuel's class; fixed a couple of bugs
zip
CodePluginBinPatch.zip 8.6 kB 2 08-Dec-2006 09:19 Jacky Liu
zip
CodePluginSrc.zip 153.2 kB 2 17-May-2010 22:34 Pizza Geek Merged packaged class and Manuel's class; fixed a couple of bugs
zip
jeditsyntax.zip 117.9 kB 1 17-May-2010 22:12 Pizza Geek Removed jeditsyntax.jar from the CodePluginBin.zip to keep the file < 500K
txt
readme.txt 2.0 kB 1 01-Nov-2006 13:56 Jacky Liu
png
syntaxscreenshoot.png 1.3 kB 1 01-Nov-2006 13:55 Jacky Liu
« This page (revision-53) was last changed on 10-Mar-2011 13:42 by Vignesh Srinivasan