Search in sources :

Example 1 with FencedCodeBlock

use of org.commonmark.node.FencedCodeBlock in project hippo by NHS-digital-website.

the class FencedCodeBlockNodeRenderer method render.

@Override
public void render(Node node) {
    // We only handle one type as per getNodeTypes, so we can just cast it here.
    final FencedCodeBlock codeBlock = (FencedCodeBlock) node;
    final String codeLanguage = codeBlock.getInfo();
    final String literal = codeBlock.getLiteral();
    html.tag("article", Collections.singletonMap("class", "nhsd-o-code-viewer nhsd-t-body"));
    html.tag("div", ImmutableMap.of("class", "nhsd-o-code-viewer__tab-content", "role", "tabpanel", "aria-hidden", "true"));
    if (codeLanguage != null) {
        html.tag("p", ImmutableMap.of("class", "nhsd-t-heading-s nhsd-!t-margin-3", "data-hide-tab-header", ""));
        html.text(codeLanguage.toUpperCase());
        html.tag("/p");
    }
    html.tag("div", Collections.singletonMap("class", "nhsd-o-code-viewer__code nhsd-o-code-viewer__code__slim"));
    html.tag("div", Collections.singletonMap("class", "nhsd-o-code-viewer__code-content nhsd-o-code-viewer__code-content__slim"));
    html.tag("pre", Collections.singletonMap("class", "line-numbers"));
    if (codeLanguage != null) {
        html.tag("code", Collections.singletonMap("class", String.format("language-%s", codeLanguage)));
    } else {
        html.tag("code");
    }
    html.text(literal);
    html.tag("/code");
    html.tag("/pre");
    html.tag("/div");
    html.tag("/div");
    html.tag("/div");
    html.tag("/article");
}
Also used : FencedCodeBlock(org.commonmark.node.FencedCodeBlock)

Example 2 with FencedCodeBlock

use of org.commonmark.node.FencedCodeBlock in project hippo by NHS-digital-website.

the class FencedCodeBlockNodeRendererTest method rendersCodeBlock.

@Test
public void rendersCodeBlock() {
    // Arrange
    final String literal = "<button class=\"nhsd-a-button\" type=\"button\">\n  <span class=\"nhsd-a-button__label\">Take primary action</span>\n</button>";
    final String language = "html";
    final String languageClass = String.format("language-%s", language);
    FencedCodeBlock node = new FencedCodeBlock();
    node.setInfo(language);
    node.setLiteral(literal);
    // Act
    renderer.render(node);
    // Assert
    InOrder inOrder = Mockito.inOrder(html);
    inOrder.verify(html).tag("article", Collections.singletonMap("class", "nhsd-o-code-viewer nhsd-t-body"));
    inOrder.verify(html).tag("div", ImmutableMap.of("class", "nhsd-o-code-viewer__tab-content", "role", "tabpanel", "aria-hidden", "true"));
    inOrder.verify(html).tag("p", ImmutableMap.of("class", "nhsd-t-heading-s nhsd-!t-margin-3", "data-hide-tab-header", ""));
    inOrder.verify(html).text(language.toUpperCase());
    inOrder.verify(html).tag("/p");
    inOrder.verify(html).tag("div", Collections.singletonMap("class", "nhsd-o-code-viewer__code nhsd-o-code-viewer__code__slim"));
    inOrder.verify(html).tag("div", Collections.singletonMap("class", "nhsd-o-code-viewer__code-content nhsd-o-code-viewer__code-content__slim"));
    inOrder.verify(html).tag("pre", Collections.singletonMap("class", "line-numbers"));
    inOrder.verify(html).tag("code", Collections.singletonMap("class", languageClass));
    inOrder.verify(html).text(literal);
    inOrder.verify(html).tag("/code");
    inOrder.verify(html).tag("/pre");
    inOrder.verify(html, times(3)).tag("/div");
    inOrder.verify(html).tag("/article");
    inOrder.verifyNoMoreInteractions();
}
Also used : InOrder(org.mockito.InOrder) FencedCodeBlock(org.commonmark.node.FencedCodeBlock) Test(org.junit.Test)

Example 3 with FencedCodeBlock

use of org.commonmark.node.FencedCodeBlock in project PocketHub by pockethub.

the class MarkwonUtils method createMarkwon.

public static Markwon createMarkwon(Context context, String baseUrl) {
    final Prism4j prism4j = new Prism4j(new GrammarLocatorDef());
    return Markwon.builder(context).usePlugin(StrikethroughPlugin.create()).usePlugin(TaskListPlugin.create(context)).usePlugin(HtmlPlugin.create()).usePlugin(new AbstractMarkwonPlugin() {

        @Override
        public void configureVisitor(@NonNull MarkwonVisitor.Builder builder) {
            builder.on(FencedCodeBlock.class, (visitor, fencedCodeBlock) -> {
                // We actually won't be applying code spans here, as our custom view will
                // draw background and apply mono typeface
                // 
                // NB the `trim` operation on literal (as code will have a new line at the end)
                final CharSequence code = visitor.configuration().syntaxHighlight().highlight(fencedCodeBlock.getInfo(), fencedCodeBlock.getLiteral().trim());
                visitor.builder().append(code);
            });
        }

        @Override
        public void configureParser(@NonNull Parser.Builder builder) {
            super.configureParser(builder);
            builder.postProcessor(new PostProcessor() {

                @Override
                public Node process(Node node) {
                    Visitor t = new AbstractVisitor() {

                        @Override
                        public void visit(HtmlBlock htmlBlock) {
                            String literal = htmlBlock.getLiteral();
                            if (literal.startsWith("<!--")) {
                                htmlBlock.unlink();
                            } else {
                                super.visit(htmlBlock);
                            }
                        }
                    };
                    node.accept(t);
                    return node;
                }
            });
        }
    }).usePlugin(GlideImagesPlugin.create(new GifAwareGlideStore(context))).usePlugin(new SpanLinkPlugin(baseUrl)).usePlugin(new AbstractMarkwonPlugin() {

        @Override
        public void configure(@NonNull Registry registry) {
            registry.require(HtmlPlugin.class, htmlPlugin -> htmlPlugin.addHandler(new AlignHandler()));
        }
    }).usePlugin(TableEntryPlugin.create(TablePlugin.create(context))).usePlugin(SyntaxHighlightPlugin.create(prism4j, Prism4jThemeDefault.create())).usePlugin(new AsyncDrawableSchedulerPlugin()).build();
}
Also used : HtmlBlock(org.commonmark.node.HtmlBlock) Context(android.content.Context) StrikethroughPlugin(io.noties.markwon.ext.strikethrough.StrikethroughPlugin) MarkwonVisitor(io.noties.markwon.MarkwonVisitor) NonNull(androidx.annotation.NonNull) Prism4jThemeDefault(io.noties.markwon.syntax.Prism4jThemeDefault) Markwon(io.noties.markwon.Markwon) SyntaxHighlightPlugin(io.noties.markwon.syntax.SyntaxHighlightPlugin) Prism4j(io.noties.prism4j.Prism4j) Visitor(org.commonmark.node.Visitor) TableEntryPlugin(io.noties.markwon.recycler.table.TableEntryPlugin) TaskListPlugin(io.noties.markwon.ext.tasklist.TaskListPlugin) FencedCodeBlock(org.commonmark.node.FencedCodeBlock) Parser(org.commonmark.parser.Parser) TablePlugin(io.noties.markwon.ext.tables.TablePlugin) AbstractMarkwonPlugin(io.noties.markwon.AbstractMarkwonPlugin) PrismBundle(io.noties.prism4j.annotations.PrismBundle) Node(org.commonmark.node.Node) HtmlPlugin(io.noties.markwon.html.HtmlPlugin) AbstractVisitor(org.commonmark.node.AbstractVisitor) PostProcessor(org.commonmark.parser.PostProcessor) GlideImagesPlugin(io.noties.markwon.image.glide.GlideImagesPlugin) AbstractVisitor(org.commonmark.node.AbstractVisitor) MarkwonVisitor(io.noties.markwon.MarkwonVisitor) Visitor(org.commonmark.node.Visitor) AbstractVisitor(org.commonmark.node.AbstractVisitor) Node(org.commonmark.node.Node) HtmlPlugin(io.noties.markwon.html.HtmlPlugin) HtmlBlock(org.commonmark.node.HtmlBlock) Prism4j(io.noties.prism4j.Prism4j) NonNull(androidx.annotation.NonNull) AbstractMarkwonPlugin(io.noties.markwon.AbstractMarkwonPlugin) PostProcessor(org.commonmark.parser.PostProcessor)

Aggregations

FencedCodeBlock (org.commonmark.node.FencedCodeBlock)3 Context (android.content.Context)1 NonNull (androidx.annotation.NonNull)1 AbstractMarkwonPlugin (io.noties.markwon.AbstractMarkwonPlugin)1 Markwon (io.noties.markwon.Markwon)1 MarkwonVisitor (io.noties.markwon.MarkwonVisitor)1 StrikethroughPlugin (io.noties.markwon.ext.strikethrough.StrikethroughPlugin)1 TablePlugin (io.noties.markwon.ext.tables.TablePlugin)1 TaskListPlugin (io.noties.markwon.ext.tasklist.TaskListPlugin)1 HtmlPlugin (io.noties.markwon.html.HtmlPlugin)1 GlideImagesPlugin (io.noties.markwon.image.glide.GlideImagesPlugin)1 TableEntryPlugin (io.noties.markwon.recycler.table.TableEntryPlugin)1 Prism4jThemeDefault (io.noties.markwon.syntax.Prism4jThemeDefault)1 SyntaxHighlightPlugin (io.noties.markwon.syntax.SyntaxHighlightPlugin)1 Prism4j (io.noties.prism4j.Prism4j)1 PrismBundle (io.noties.prism4j.annotations.PrismBundle)1 AbstractVisitor (org.commonmark.node.AbstractVisitor)1 HtmlBlock (org.commonmark.node.HtmlBlock)1 Node (org.commonmark.node.Node)1 Visitor (org.commonmark.node.Visitor)1