Created
July 20, 2014 15:54
-
-
Save lordofthejars/35f6b8b6d1456ee10a21 to your computer and use it in GitHub Desktop.
Nashorn and Asciidoctor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Currently I am developing the integration between asciidoctorjs and nashorn. But I have found a problem which because of my no experience on Nashorn I don't know how to fix it. Let me paste the code: | |
| ScriptEngineManager engineManager = | |
| new ScriptEngineManager(); | |
| ScriptEngine engine = | |
| engineManager.getEngineByName("nashorn"); | |
| SimpleScriptContext simpleScriptContext = new SimpleScriptContext(); | |
| Bindings bindings = new SimpleBindings(); | |
| simpleScriptContext.setBindings(bindings, ScriptContext.ENGINE_SCOPE); | |
| try { | |
| engine.eval(new InputStreamReader(RenderDocumentWithRhino.class.getResourceAsStream("/opal.js")), simpleScriptContext); | |
| engine.eval(new InputStreamReader(RenderDocumentWithRhino.class.getResourceAsStream("/asciidoctor.js")), simpleScriptContext); | |
| engine.eval(new InputStreamReader(RenderDocumentWithRhino.class.getResourceAsStream("/asciidoctor_extensions.js")), simpleScriptContext); | |
| } catch (ScriptException e) { | |
| throw new IllegalArgumentException(e); | |
| } | |
| String content = "*Hello World*"; | |
| Map<String, Object> options = new HashMap<>(); | |
| options.put("header_footer", true); | |
| simpleScriptContext.getBindings(ScriptContext.ENGINE_SCOPE).put("listOptions", options.keySet().toArray()); | |
| simpleScriptContext.getBindings(ScriptContext.ENGINE_SCOPE).put("options", options); | |
| Invocable invocable = (Invocable) engine; | |
| Object eval = engine.eval("Opal.hash2(listOptions, options)", simpleScriptContext); | |
| Object function = engine.eval("function render(a, b) {Opal.Asciidoctor.$render(a, b)}", simpleScriptContext); | |
| AsciidoctorJs asciidoctorJs = | |
| invocable.getInterface( | |
| AsciidoctorJs.class); | |
| System.out.println(content); | |
| System.out.println(eval); | |
| System.out.println(asciidoctorJs); | |
| System.out.println( | |
| asciidoctorJs.render(content, eval)); | |
| After running the code a NullPointerException is thrown because asciidoctorJs var is null. Anyone may know why this happens? I am sure it should be something I am missing but for example a hello world example worked but not when I add asciidoctorjs. I suspect something about simpleScriptContext but not sure. | |
| Thanks :) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment