admin管理员组文章数量:1290230
I'm using marked on the client side to render markdown code to html.
But now I need to do the same thing on the server side which is Java. In order to get the exact same html code, I have to use marked other than other java markdown libraries.
How can I load the "marked.js" file in java and run the javascript code?
marked.parser(marked.lexer("**hello,world**"));
I'm using marked on the client side to render markdown code to html.
But now I need to do the same thing on the server side which is Java. In order to get the exact same html code, I have to use marked other than other java markdown libraries.
How can I load the "marked.js" file in java and run the javascript code?
marked.parser(marked.lexer("**hello,world**"));
Share
Improve this question
asked Jul 12, 2012 at 3:45
FreewindFreewind
198k163 gold badges452 silver badges734 bronze badges
3 Answers
Reset to default 42 options:
- Have a look at the
Rhino
tutorial. - Then refer to the RunScript example, reproduced below and embed Rhino yourself.
- Then edit it to fit your needs
OR:
Directly use the internal ScriptEngine in Java SE 6 and later, that es bundled with Rhino for you. See the RunMarked
example below adapted to your needs.
RunScript.java
/*
* Licensed under MPL 1.1/GPL 2.0
*/
import org.mozilla.javascript.*;
/**
* RunScript: simplest example of controlling execution of Rhino.
*
* Collects its arguments from the mand line, executes the
* script, and prints the result.
*
* @author Norris Boyd
*/
public class RunScript {
public static void main(String args[])
{
// Creates and enters a Context. The Context stores information
// about the execution environment of a script.
Context cx = Context.enter();
try {
// Initialize the standard objects (Object, Function, etc.)
// This must be done before scripts can be executed. Returns
// a scope object that we use in later calls.
Scriptable scope = cx.initStandardObjects();
// Collect the arguments into a single string.
String s = "";
for (int i=0; i < args.length; i++) {
s += args[i];
}
// Now evaluate the string we've colected.
Object result = cx.evaluateString(scope, s, "<cmd>", 1, null);
// Convert the result to a string and print it.
System.err.println(Context.toString(result));
} finally {
// Exit from the context.
Context.exit();
}
}
}
RunMarked.java
Actually, I noticed Freewind's answer, and I would have written exactly the same (except I'd load the lib directly with Files.toString(File)
using Google Guava). Please refer to his answer (and give him points if you find his answer helpful).
public static String md2html() throws ScriptException, FileNotFoundException, NoSuchMethodException {
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("JavaScript");
File functionscript = new File("public/lib/marked.js");
Reader reader = new FileReader(functionscript);
engine.eval(reader);
Invocable invocableEngine = (Invocable) engine;
Object marked = engine.get("marked");
Object lexer = invocableEngine.invokeMethod(marked, "lexer", "**hello**");
Object result = invocableEngine.invokeMethod(marked, "parser", lexer);
return result.toString();
}
You can use rhino to run JavaScript on the server that runs java.
本文标签: How to load a javascript file and run a javascript method in javaStack Overflow
版权声明:本文标题:How to load a javascript file and run a javascript method in java? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741494445a2381771.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论