
Manual
Go back to the manual's table of contents
Part 6: Extending Scripturian
Scriptlet Plugins
You can enhance the scriptlet parsing environment via scriptlet plugins. As an example, let's install a "shout" tag that prints out the content of the tag with an exclamation point at the end:
class MyPlugin implements ScriptletPlugin { public String getScriptlet( String code, LanguageAdapter languageAdapter, String content ) { if( code.equals( "!!!" ) ) { return "print('" + content.trim().replace( "'", "\\'" ) + "' + '!')"; } throw new RuntimeException( "We do not support tag: " + code ); } } parsingContext.getScriptletPlugins().put( "!!!", new MyPlugin() );We would then be able to use the new "shout" scriptlet tag like so:
This is going to be <%!!! shouted out >
Our "shout" tag in this example is not very useful, but of course you can do anything with the content. You are not required to return any code: the tag can be used to perform special operations during the parsing phase of the exectuable. Further points to note:
- Whatever "getScriptlet" returns is inserted into the executable during parsing only. Once the executable is ready, these plugins will never be called again for this document, so there is no performance loss for using them. It is simply a convenience for parsing.
- You do not have to return scriptlet source code, but if you do, it's up to you to make sure that it fits the syntax of your language. In the example above we've made sure to escape the content's quotation marks so that it could be including in a literal string.
- The languageAdapter value refers to the last language used (or the default): it's up to your to parse the 'code' for a language tag if you wish to support multiple languages.
- You can use the same plugin class to implement multiple tag codes. In this case, we only supported "!!!".
Supporting Other Language Engines
To add a new language to Scripturian, you will first need to create a language adapter for it. If the language supports the Java scripting standard (JSR-223), then you can use the JSR-223 adapter. However, our experience has shown that adherence to the Java scripting standard is inconsistent, and that it's often better to create your own adapter.
Go back to the manual's table of contents