Search in sources :

Example 11 with Node

use of org.commonmark.node.Node in project gitiles by GerritCodeReview.

the class Navbar method extractSiteTitle.

private void extractSiteTitle() {
    for (Node c = node.getFirstChild(); c != null; c = c.getNext()) {
        if (c instanceof Heading) {
            Heading h = (Heading) c;
            if (h.getLevel() == 1) {
                siteTitle = MarkdownUtil.getInnerText(h);
                h.unlink();
                break;
            }
        }
    }
}
Also used : Heading(org.commonmark.node.Heading) Node(org.commonmark.node.Node)

Example 12 with Node

use of org.commonmark.node.Node in project gitiles by GerritCodeReview.

the class MarkdownUtil method trimPreviousWhitespace.

static void trimPreviousWhitespace(Node node) {
    Node prev = node.getPrevious();
    if (prev instanceof Text) {
        Text prevText = (Text) prev;
        String s = prevText.getLiteral();
        prevText.setLiteral(CharMatcher.whitespace().trimTrailingFrom(s));
    }
}
Also used : Node(org.commonmark.node.Node) Text(org.commonmark.node.Text)

Example 13 with Node

use of org.commonmark.node.Node 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 14 with Node

use of org.commonmark.node.Node 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 15 with Node

use of org.commonmark.node.Node 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)

Aggregations

Node (org.commonmark.node.Node)42 Parser (org.commonmark.parser.Parser)18 HtmlRenderer (org.commonmark.renderer.html.HtmlRenderer)12 Test (org.junit.Test)9 Extension (org.commonmark.Extension)7 TablesExtension (org.commonmark.ext.gfm.tables.TablesExtension)7 IOException (java.io.IOException)4 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 DateFormat (java.text.DateFormat)2 HashMap (java.util.HashMap)2 TableBlock (org.commonmark.ext.gfm.tables.TableBlock)2 Code (org.commonmark.node.Code)2 CustomNode (org.commonmark.node.CustomNode)2 AppWidgetManager (android.appwidget.AppWidgetManager)1 ActivityNotFoundException (android.content.ActivityNotFoundException)1 ComponentName (android.content.ComponentName)1 Context (android.content.Context)1 DialogInterface (android.content.DialogInterface)1 Intent (android.content.Intent)1 SharedPreferences (android.content.SharedPreferences)1