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
Add a ment  | 

3 Answers 3

Reset to default 4

2 options:

  1. Have a look at the Rhino tutorial.
  2. Then refer to the RunScript example, reproduced below and embed Rhino yourself.
  3. 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