Search in sources :

Example 1 with Node

use of com.vladsch.flexmark.util.ast.Node in project zeppelin by apache.

the class FlexmarkParser method render.

@Override
public String render(String markdownText) {
    Node document = parser.parse(markdownText);
    String html = renderer.render(document);
    return wrapWithMarkdownClassDiv(html);
}
Also used : Node(com.vladsch.flexmark.util.ast.Node)

Example 2 with Node

use of com.vladsch.flexmark.util.ast.Node in project gerrit by GerritCodeReview.

the class MarkdownFormatter method markdownToDocHtml.

public byte[] markdownToDocHtml(String md, String charEnc) throws UnsupportedEncodingException {
    Node root = parseMarkdown(md);
    HtmlRenderer renderer = HtmlRenderer.builder(markDownOptions()).build();
    String title = findTitle(root);
    StringBuilder html = new StringBuilder();
    html.append("<html>");
    html.append("<head>");
    if (!Strings.isNullOrEmpty(title)) {
        html.append("<title>").append(title).append("</title>");
    }
    html.append("<style type=\"text/css\">\n");
    if (css != null) {
        html.append(css);
    } else {
        html.append(readCSS());
    }
    html.append("\n</style>");
    html.append("</head>");
    html.append("<body>\n");
    html.append(renderer.render(root));
    html.append("\n</body></html>");
    return html.toString().getBytes(charEnc);
}
Also used : Node(com.vladsch.flexmark.util.ast.Node) HtmlRenderer(com.vladsch.flexmark.html.HtmlRenderer)

Example 3 with Node

use of com.vladsch.flexmark.util.ast.Node in project gerrit by GerritCodeReview.

the class MarkdownFormatter method findTitle.

private String findTitle(Node root) {
    if (root instanceof Heading) {
        Heading h = (Heading) root;
        if (h.getLevel() == 1 && h.hasChildren()) {
            TextCollectingVisitor collectingVisitor = new TextCollectingVisitor();
            return collectingVisitor.collectAndGetText(h);
        }
    }
    if (root instanceof Block && root.hasChildren()) {
        Node child = root.getFirstChild();
        while (child != null) {
            String title = findTitle(child);
            if (title != null) {
                return title;
            }
            child = child.getNext();
        }
    }
    return null;
}
Also used : TextCollectingVisitor(com.vladsch.flexmark.ast.util.TextCollectingVisitor) Heading(com.vladsch.flexmark.ast.Heading) Node(com.vladsch.flexmark.util.ast.Node) Block(com.vladsch.flexmark.util.ast.Block)

Example 4 with Node

use of com.vladsch.flexmark.util.ast.Node in project gerrit by GerritCodeReview.

the class MarkdownFormatter method parseMarkdown.

private Node parseMarkdown(String md) {
    Parser parser = Parser.builder(markDownOptions()).build();
    Node document = parser.parse(md);
    return document;
}
Also used : Node(com.vladsch.flexmark.util.ast.Node) Parser(com.vladsch.flexmark.parser.Parser)

Example 5 with Node

use of com.vladsch.flexmark.util.ast.Node in project zeppelin by apache.

the class UMLBlockQuoteParser method tryContinue.

@Override
public BlockContinue tryContinue(ParserState state) {
    if (hadClose) {
        return BlockContinue.none();
    }
    final int index = state.getIndex();
    BasedSequence line = state.getLineWithEOL();
    final Matcher matcher = YUML_BLOCK_END.matcher(line.subSequence(index));
    if (!matcher.matches()) {
        return BlockContinue.atIndex(index);
    } else {
        // if have open gitlab block quote last child then let them handle it
        Node lastChild = block.getLastChild();
        if (lastChild instanceof UMLBlockQuote) {
            final BlockParser parser = state.getActiveBlockParser((Block) lastChild);
            if (parser instanceof UMLBlockQuoteParser && !((UMLBlockQuoteParser) parser).hadClose) {
                // let the child handle it
                return BlockContinue.atIndex(index);
            }
        }
        hadClose = true;
        block.setClosingMarker(state.getLine().subSequence(index, index + 3));
        block.setClosingTrailing(state.getLineWithEOL().subSequence(matcher.start(1), matcher.end(1)));
        return BlockContinue.atIndex(state.getLineEndIndex());
    }
}
Also used : BlockParser(com.vladsch.flexmark.parser.block.BlockParser) AbstractBlockParser(com.vladsch.flexmark.parser.block.AbstractBlockParser) MatchedBlockParser(com.vladsch.flexmark.parser.block.MatchedBlockParser) Matcher(java.util.regex.Matcher) BasedSequence(com.vladsch.flexmark.util.sequence.BasedSequence) Node(com.vladsch.flexmark.util.ast.Node)

Aggregations

Node (com.vladsch.flexmark.util.ast.Node)7 TextCollectingVisitor (com.vladsch.flexmark.ast.util.TextCollectingVisitor)2 FencedCodeBlock (com.vladsch.flexmark.ast.FencedCodeBlock)1 Heading (com.vladsch.flexmark.ast.Heading)1 HtmlRenderer (com.vladsch.flexmark.html.HtmlRenderer)1 Parser (com.vladsch.flexmark.parser.Parser)1 AbstractBlockParser (com.vladsch.flexmark.parser.block.AbstractBlockParser)1 BlockParser (com.vladsch.flexmark.parser.block.BlockParser)1 MatchedBlockParser (com.vladsch.flexmark.parser.block.MatchedBlockParser)1 Block (com.vladsch.flexmark.util.ast.Block)1 MutableDataSet (com.vladsch.flexmark.util.data.MutableDataSet)1 BasedSequence (com.vladsch.flexmark.util.sequence.BasedSequence)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 ArrayList (java.util.ArrayList)1 Matcher (java.util.regex.Matcher)1