Custom Content Parser
(→Tag Usage) |
(→Tag Usage) |
||
Line 71: | Line 71: | ||
The tag can be used just by surrounding the part of the template you would like to apply your custom parser for with the following syntax: | The tag can be used just by surrounding the part of the template you would like to apply your custom parser for with the following syntax: | ||
− | <code lang=" | + | <code lang="JSP"> |
<cms:parse parserClass="YourCustomParserClassName"> | <cms:parse parserClass="YourCustomParserClassName"> | ||
... the part of the template you want to parse... | ... the part of the template you want to parse... |
Revision as of 07:49, 12 September 2008
Overview
This document will outline the purpose and usage of <cms:parse> tag, which allows the generated content for any part of your template to be parsed with custom parser class in order to manipulate generated content as desired before final page rendering.
Custom Parser and Visitor Class
In order to implement your custom parser you will need to implement two classes: custom parser and custom visitor. Custom parser class is the one that will be referenced with <cms:parse> parserClass attribute. The easiest way is to extend org.opencms.jsp.parse.A_CmsConfiguredHtmlParser which case the only method you will need to implement is createVisitorInstance() returning your custom visitor class. This can be done as below:
import org.opencms.jsp.parse.A_CmsConfiguredHtmlParser; import org.opencms.main.CmsException; import org.opencms.util.I_CmsHtmlNodeVisitor; public class MyCustomParser extends A_CmsConfiguredHtmlParser { public I_CmsHtmlNodeVisitor createVisitorInstance() throws CmsException{ return new MyCustomVisitor(); } }
Now, your custom visitor is the class that you place all your custom parsing magic. The best way to create your custom visitor class is to extend org.opencms.util.CmsHtmlParser and implement org.opencms.util.I_CmsHtmlNodeVisitor. This can be done as below:
import org.htmlparser.Remark; import org.htmlparser.Tag; import org.htmlparser.Text; import org.opencms.util.I_CmsHtmlNodeVisitor; import org.opencms.util.CmsHtmlParser; public final class CustomParser extends CmsHtmlParser implements I_CmsHtmlNodeVisitor { public CustomParser() { super(true); } public void visitStringNode(Text text) { text = doStuffToHtml(text); super.visitStringNode(text); } public void visitTag(Tag tag) { tag = doStuffToTag(tag); super.visitTag(tag); } public void visitRemarkNode(Remark remark) { remark = doStuffToRemark(remark); super.visitRemarkNode(remark); } }
Now, you do not have to overwrite all of the above methods, but only the ones which content you would like to manipulate. The types are explained below:
visitStringNode(Text text) - This parses trough all text content line at the time.
visitTag(Tag tag) - This parses through all non text nodes such as IMG, A, SPAN... line at the time.
visitRemarkNode(Remark remark) - This parses through all comment nodes like at the time.
Tag Usage
The tag can be used just by surrounding the part of the template you would like to apply your custom parser for with the following syntax:
GeSHi Error: GeSHi could not find the language jsp (using path /var/www/http.www.opencms-wiki.org/extensions/SyntaxHighlight_GeSHi/geshi/geshi/) (code 2)
You need to specify a language like this: <source lang="html">...</source>
Supported languages for syntax highlighting:
actionscript, ada, apache, applescript, asm, asp, autoit, bash, blitzbasic, bnf, c, c_mac, caddcl, cadlisp, cfdg, cfm, cpp, cpp-qt, csharp, css, d, delphi, diff, div, dos, eiffel, fortran, freebasic, gml, groovy, html4strict, idl, ini, inno, io, java, java5, javascript, latex, lisp, lua, matlab, mirc, mpasm, mysql, nsis, objc, ocaml, ocaml-brief, oobas, oracle8, pascal, perl, php, php-brief, plsql, python, qbasic, reg, robots, ruby, sas, scheme, sdlbasic, smalltalk, smarty, sql, tcl, text, thinbasic, tsql, vb, vbnet, vhdl, visualfoxpro, winbatch, xml, z80
Please note that if your template part between the parse tags includes any cms:include tags you must set the cacheable attribute to false for all your cms:includes in order for the custom parser to work.