Skip to content

Instantly share code, notes, and snippets.

@eungjun-yi
Created July 29, 2015 08:55

Revisions

  1. eungjun-yi created this gist Jul 29, 2015.
    24 changes: 24 additions & 0 deletions Markdown.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    // Try to render and wait at most 5 seconds.
    final String[] rendered = new String[1];
    @SuppressWarnings("deprecation")
    Thread marked = new Thread() {
    @Override
    public void run() {
    try {
    rendered[0] = (String) ((Invocable) engine).invokeFunction(
    "marked", source, options);
    } catch (Exception e) {
    play.Logger.error("[Markdown] Failed to render: " + source, e);
    }
    }
    };
    marked.start();
    marked.join(5000);

    if (rendered[0] == null) {
    // This is the only way to stop the script engine. Thread.interrupt does not work.
    marked.stop();
    return "〈pre〉" + StringEscapeUtils.escapeHtml(source) + "〈/pre〉";
    } else {
    return rendered[0];
    }