Search in sources :

Example 1 with HtmlRenderer

use of org.commonmark.renderer.html.HtmlRenderer in project symja_android_library by axkr.

the class AJAXDocServlet method generateHTMLString.

public static String generateHTMLString(final String markdownStr) {
    List<Extension> EXTENSIONS = // 
    Arrays.asList(TeXExtension.create(), TablesExtension.create());
    Parser parser = // 
    Parser.builder().extensions(EXTENSIONS).build();
    Node document = parser.parse(markdownStr);
    HtmlRenderer renderer = // 
    HtmlRenderer.builder().extensions(EXTENSIONS).nodeRendererFactory(new HtmlNodeRendererFactory() {

        @Override
        public NodeRenderer create(HtmlNodeRendererContext context) {
            return new DocNodeRenderer(context);
        }
    }).build();
    return renderer.render(document);
}
Also used : TablesExtension(org.commonmark.ext.gfm.tables.TablesExtension) Extension(org.commonmark.Extension) CustomNode(org.commonmark.node.CustomNode) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Node(org.commonmark.node.Node) HtmlRenderer(org.commonmark.renderer.html.HtmlRenderer) HtmlNodeRendererContext(org.commonmark.renderer.html.HtmlNodeRendererContext) HtmlNodeRendererFactory(org.commonmark.renderer.html.HtmlNodeRendererFactory) Parser(org.commonmark.parser.Parser) ExprParser(org.matheclipse.core.parser.ExprParser)

Example 2 with HtmlRenderer

use of org.commonmark.renderer.html.HtmlRenderer in project symja_android_library by axkr.

the class MarkdownToHTML method generateHTMLString.

/**
 * Generate markdown links for Symja function reference.
 *
 * @param sourceLocation source directory for funtions (*.md) files
 */
public static void generateHTMLString(final File sourceLocation, String function, boolean javadoc) {
    if (sourceLocation.exists()) {
        // Get the list of the files contained in the package
        final String[] files = sourceLocation.list();
        if (files != null) {
            for (int i = 0; i < files.length; i++) {
                if (files[i].endsWith(".md")) {
                    String className = files[i].substring(0, files[i].length() - 3);
                    if (className.equals(function)) {
                        File file = new File(sourceLocation + "/" + files[i]);
                        String html;
                        try {
                            Set<Extension> EXTENSIONS = Collections.singleton(TablesExtension.create());
                            Parser parser = Parser.builder().extensions(EXTENSIONS).build();
                            Node document = parser.parse(Files.asCharSource(file, StandardCharsets.UTF_8).read());
                            HtmlRenderer renderer = HtmlRenderer.builder().extensions(EXTENSIONS).build();
                            html = renderer.render(document);
                            if (javadoc) {
                                html = html.replace("<blockquote>", "");
                                html = html.replace("</blockquote>", "");
                                String[] lines = html.split("\\n");
                                System.out.println("/**");
                                for (int j = 0; j < lines.length; j++) {
                                    if (lines[j].startsWith("<h3>Github</h3>")) {
                                        // don't include link to github implementation
                                        break;
                                    }
                                    if (!lines[j].startsWith("<h2>")) {
                                        System.out.println(" * " + lines[j]);
                                    }
                                }
                                System.out.println(" */");
                            } else {
                                System.out.println(html);
                            }
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }
}
Also used : Extension(org.commonmark.Extension) TablesExtension(org.commonmark.ext.gfm.tables.TablesExtension) Node(org.commonmark.node.Node) HtmlRenderer(org.commonmark.renderer.html.HtmlRenderer) IOException(java.io.IOException) File(java.io.File) Parser(org.commonmark.parser.Parser)

Example 3 with HtmlRenderer

use of org.commonmark.renderer.html.HtmlRenderer in project diary by billthefarmer.

the class DiaryWidgetProvider method getText.

// getText
@SuppressWarnings("deprecation")
private CharSequence getText(Context context) {
    // Get preferences
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
    // Get folder
    folder = preferences.getString(Settings.PREF_FOLDER, Diary.DIARY);
    markdown = preferences.getBoolean(Settings.PREF_MARKDOWN, true);
    // Get date
    DateFormat format = DateFormat.getDateInstance(DateFormat.MEDIUM);
    String date = format.format(new Date());
    // Get text
    CharSequence text = Diary.read(getFile());
    if (markdown) {
        // Use commonmark
        Parser parser = Parser.builder().build();
        Node document = parser.parse(text.toString());
        HtmlRenderer renderer = HtmlRenderer.builder().build();
        String html = renderer.render(document);
        text = Html.fromHtml(html);
    }
    return text;
}
Also used : SharedPreferences(android.content.SharedPreferences) DateFormat(java.text.DateFormat) Node(org.commonmark.node.Node) HtmlRenderer(org.commonmark.renderer.html.HtmlRenderer) Date(java.util.Date) Parser(org.commonmark.parser.Parser)

Example 4 with HtmlRenderer

use of org.commonmark.renderer.html.HtmlRenderer in project codeu-2018-team12 by codeu-2018-team12.

the class TextFormatter method formatForDisplay.

public static String formatForDisplay(String input) {
    // Parses input and replaces markdown characters with the appropriate HTML
    Parser parser = Parser.builder().build();
    Node document = parser.parse(input);
    HtmlRenderer renderer = HtmlRenderer.builder().build();
    String parsedInput = renderer.render(document);
    // Removes unneeded <p> tags
    int parsedInputLength = parsedInput.length();
    if (parsedInput.startsWith("<p>")) {
        parsedInput = parsedInput.substring(3, parsedInputLength - 5);
    }
    // Ensures that new line characters are displayed correctly
    parsedInput = parsedInput.replaceAll("(\r\n|\n\r|\r|\n)", "<br />");
    // Replaces emoji aliases with Hexadecimal values ready for display in HTML
    String finalOutput = EmojiParser.parseToUnicode(parsedInput);
    finalOutput = EmojiParser.parseToHtmlHexadecimal(finalOutput);
    return finalOutput;
}
Also used : HtmlRenderer(org.commonmark.renderer.html.HtmlRenderer) Parser(org.commonmark.parser.Parser) EmojiParser(com.vdurmont.emoji.EmojiParser)

Example 5 with HtmlRenderer

use of org.commonmark.renderer.html.HtmlRenderer in project xian by happyyangyuan.

the class MdToHtml method mdToHtml.

/**
 * @param md the markdown string.
 * @return converted html
 */
public static String mdToHtml(String md) {
    Parser parser = Parser.builder().build();
    Node document = parser.parse(md);
    HtmlRenderer renderer = HtmlRenderer.builder().build();
    return renderer.render(document);
}
Also used : Node(org.commonmark.node.Node) HtmlRenderer(org.commonmark.renderer.html.HtmlRenderer) Parser(org.commonmark.parser.Parser)

Aggregations

Parser (org.commonmark.parser.Parser)13 HtmlRenderer (org.commonmark.renderer.html.HtmlRenderer)13 Node (org.commonmark.node.Node)12 Extension (org.commonmark.Extension)5 TablesExtension (org.commonmark.ext.gfm.tables.TablesExtension)5 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 IOException (java.io.IOException)2 DateFormat (java.text.DateFormat)2 AppWidgetManager (android.appwidget.AppWidgetManager)1 ComponentName (android.content.ComponentName)1 SharedPreferences (android.content.SharedPreferences)1 RemoteViews (android.widget.RemoteViews)1 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 MagiskManager (com.topjohnwu.magisk.MagiskManager)1 EmojiParser (com.vdurmont.emoji.EmojiParser)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 InputStream (java.io.InputStream)1 Calendar (java.util.Calendar)1 Date (java.util.Date)1