(for SnipSnap 0.3.5)
Introduction
SnipSnap uses Macros and Filters to render the text markup to XHTML. Macros are commands and filters usually replace markup with XHTML, for example the BoldFilter replaces
__bold__ with <b>bold</b>. You shoud understand the SnipSnap architecture and have read the
Macro Tutorial. You should also be familiar with regular expressions.
Filter Interface
The basis for all filters is the Filter interface. All classes that want to act as filters have to implement it.
public interface Filter {
public String filter(String input, FilterContext context); public String[] before();
}
The important method is filter(). This method gets a string as an input and
a FilterContext object. It takes the input string, applies it's filtering and then returns the filterd string. FilterContext stores some references, e.g. to the rendering engine.
Example Filter
To write your own filter and implement Filter there are some support classes. The most basic is FilterSupport which does not much, but implement the before() method so you do not have to care. Most of the time though you want to use regular expressions (regex) to filter your content. For this there are two classes you can inherit from
- RegexReplaceFilter
- RegexTokenFilter
RegexReplaceFilter takes a regex and replaces this with some output like sed or perls s/.../.../g. RegexTokenFilter calls a subroutine for every match. This is explained in Advanced Filter Tutorial.
We want to write a smily replace filter as an example. Suppose we want to replace every
Frowny :-( with a smiley :-)
The only thing you have to do is supply the regular expressions:
import org.radeox.filter.regex.RegexReplaceFilter;public class SmileyFilter extends RegexReplaceFilter {
public SmileyFilter() {
super(":-\(", ":-)");
}
}The \ is for quoting the ( in the regular expression.
Caching
Radeox supports caching. To tell the engine that the output from your filter is cachable (= produces the same output for the same input which is usually the case with static filters) add the marker interface CacheFilter:
public class SmileyFilter extends RegexReplaceFilter implements CacheFilter {
...
Deployment
To make this filter work you have to create a jar file with
META-INF/services/org.radeox.filter.Filter
example/SmileyFilter.class
META-INF/services/org.radeox.filter.Filter should contain lines with the class names of your filters, for example
Put this jar into
applications/<AppName>/WEB-INF/lib for example if your application is named
MyBlog then
applications/MyBlog/WEB-INF/lib. Restart the server and the filter should work.
For advanced filters take a look at Advanced Filter Tutorial.