Parses the supplied markdown into html. - Java java.lang

Java examples for java.lang:String Markdown

Description

Parses the supplied markdown into html.

Demo Code


import java.io.InputStreamReader;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;

public class Main{
    /**//from   w  w w  .jav  a 2s  . c o  m
     * Parses the supplied markdown into html.
     * This calls the PageDown javascript library to do the work for us.
     */
    public static String getSanitizedHtml(String markdown) {

        if (markdown == null) {
            return null;
        }

        //TODO: going through the javascript library seems a bit hackish. If there's a PageDown Java library, we should switch to that.
        //The other way to go would be to eliminate this altogether and do all of the PageDown parsing on the client side.

        try {
            ScriptEngineManager manager = new ScriptEngineManager();
            ScriptEngine engine = manager.getEngineByName("JavaScript");
            engine.eval(new InputStreamReader(PageDownUtils.class
                    .getResourceAsStream("pageDown.js")));
            Invocable inv = (Invocable) engine;
            String s = String.valueOf(inv.invokeFunction(
                    "getSanitizedHtml", markdown));

            return s;
        } catch (Exception e) {
            e.printStackTrace();
            return "ERROR";
        }
    }
}

Related Tutorials