Search in sources :

Example 16 with MutableDataSet

use of com.vladsch.flexmark.util.options.MutableDataSet in project flexmark-java by vsch.

the class SyntheticLinkFormatterSample method main.

public static void main(String[] args) {
    MutableDataSet options = new MutableDataSet();
    // set optional extensions
    options.set(Parser.EXTENSIONS, Arrays.asList(SyntheticLinkExtension.create()));
    // uncomment to convert soft-breaks to hard breaks
    // options.set(HtmlRenderer.SOFT_BREAK, "<br />\n");
    Parser parser = Parser.builder(options).build();
    HtmlRenderer renderer = HtmlRenderer.builder(options).build();
    Formatter formatter = Formatter.builder(options).build();
    // You can re-use parser and renderer instances
    Node document = parser.parse("Some markdown content");
    String markdown = formatter.render(document);
    System.out.println(markdown);
}
Also used : CustomNodeFormatter(com.vladsch.flexmark.formatter.CustomNodeFormatter) Node(com.vladsch.flexmark.ast.Node) HtmlRenderer(com.vladsch.flexmark.html.HtmlRenderer) MutableDataSet(com.vladsch.flexmark.util.options.MutableDataSet) Parser(com.vladsch.flexmark.parser.Parser)

Example 17 with MutableDataSet

use of com.vladsch.flexmark.util.options.MutableDataSet in project flexmark-java by vsch.

the class TextCollectingVisitorTest method test_basic.

@Test
public void test_basic() {
    DataHolder options = new MutableDataSet().set(Parser.EXTENSIONS, Arrays.asList(TablesExtension.create()));
    Parser parser = Parser.builder(options).build();
    String markdown = "| First Header  | Second Header |\n" + "| ------------- | ------------- |\n" + "| Content Cell  | Content Cell  |\n" + "\n" + "| Left-aligned | Center-aligned | Right-aligned |\n" + "| :---         |     :---:      |          ---: |\n" + "| git status   | git status     | git status    |\n" + "| git diff     | git diff       | git diff      |\n" + "";
    Node document = parser.parse(markdown);
    TextCollectingVisitor collectingVisitor = new TextCollectingVisitor();
    final String text = collectingVisitor.collectAndGetText(document);
    System.out.println(text);
    final String astText = new AstCollectingVisitor().collectAndGetAstText(document);
    System.out.println(astText);
}
Also used : TextCollectingVisitor(com.vladsch.flexmark.ast.util.TextCollectingVisitor) DataHolder(com.vladsch.flexmark.util.options.DataHolder) AstCollectingVisitor(com.vladsch.flexmark.test.AstCollectingVisitor) Node(com.vladsch.flexmark.ast.Node) MutableDataSet(com.vladsch.flexmark.util.options.MutableDataSet) Parser(com.vladsch.flexmark.parser.Parser) Test(org.junit.Test)

Example 18 with MutableDataSet

use of com.vladsch.flexmark.util.options.MutableDataSet in project flexmark-java by vsch.

the class RenderingTestCase method getOptions.

/**
 * process comma separated list of option sets and combine them for final set to use
 *
 * @param example    spec example instance for which options are being processed
 * @param optionSets comma separate list of option set names
 * @return combined set from applying these options together
 */
public DataHolder getOptions(SpecExample example, String optionSets) {
    if (optionSets == null)
        return null;
    String[] optionNames = optionSets.replace('\u00A0', ' ').split(",");
    DataHolder options = null;
    boolean isFirst = true;
    for (String optionName : optionNames) {
        String option = optionName.trim();
        if (option.isEmpty() || option.startsWith("-"))
            continue;
        if (option.equals(IGNORE_OPTION_NAME)) {
            // noinspection ConstantConditions
            throwIgnoredOption(example, optionSets, option);
        } else if (option.equals(FAIL_OPTION_NAME)) {
            if (options == null) {
                options = new MutableDataSet().set(FAIL, true);
            } else {
                options = new MutableDataSet(options).set(FAIL, true);
            }
        } else if (option.equals(NO_FILE_EOL_OPTION_NAME)) {
            if (options == null) {
                options = new MutableDataSet().set(NO_FILE_EOL, true);
            } else {
                options = new MutableDataSet(options).set(NO_FILE_EOL, true);
            }
        } else if (option.equals(FILE_EOL_OPTION_NAME)) {
            if (options == null) {
                options = new MutableDataSet().set(NO_FILE_EOL, false);
            } else {
                options = new MutableDataSet(options).set(NO_FILE_EOL, true);
            }
        } else {
            if (options == null) {
                options = options(option);
                if (options == null) {
                    throw new IllegalStateException("Option " + option + " is not implemented in the RenderingTestCase subclass");
                }
            } else {
                DataHolder dataSet = options(option);
                if (dataSet != null) {
                    if (isFirst) {
                        options = new MutableDataSet(options);
                        isFirst = false;
                    }
                    ((MutableDataSet) options).setAll(dataSet);
                } else {
                    throw new IllegalStateException("Option " + option + " is not implemented in the RenderingTestCase subclass");
                }
            }
            if (IGNORE.getFrom(options)) {
                // noinspection ConstantConditions
                throwIgnoredOption(example, optionSets, option);
            }
        }
    }
    return options;
}
Also used : DataHolder(com.vladsch.flexmark.util.options.DataHolder) MutableDataSet(com.vladsch.flexmark.util.options.MutableDataSet)

Example 19 with MutableDataSet

use of com.vladsch.flexmark.util.options.MutableDataSet in project flexmark-java by vsch.

the class ParserTest method indentationWithLines.

@Test
public void indentationWithLines() {
    String given = " - 1 space\n   - 3 spaces\n     - 5 spaces\n\t - tab + space";
    MutableDataHolder options = new MutableDataSet().set(Parser.TRACK_DOCUMENT_LINES, true);
    Parser parser = Parser.builder(options).build();
    Document document = parser.parse(given);
    assertThat(document.getFirstChild(), instanceOf(BulletList.class));
    assertEquals("Document line count", 4, document.getLineCount());
    // first level list
    Node list = document.getFirstChild();
    assertEquals("expect one child", list.getFirstChild(), list.getLastChild());
    assertEquals("1 space", firstText(list.getFirstChild()));
    assertEquals("node start line number", 0, list.getStartLineNumber());
    assertEquals("node end line number", 3, list.getEndLineNumber());
    // second level list
    list = list.getFirstChild().getLastChild();
    assertEquals("expect one child", list.getFirstChild(), list.getLastChild());
    assertEquals("3 spaces", firstText(list.getFirstChild()));
    assertEquals("node start line number", 1, list.getStartLineNumber());
    assertEquals("node end line number", 3, list.getEndLineNumber());
    // third level list
    list = list.getFirstChild().getLastChild();
    assertEquals("5 spaces", firstText(list.getFirstChild()));
    assertEquals("tab + space", firstText(list.getFirstChild().getNext()));
    assertEquals("node start line number", 2, list.getStartLineNumber());
    assertEquals("node end line number", 3, list.getEndLineNumber());
}
Also used : MutableDataHolder(com.vladsch.flexmark.util.options.MutableDataHolder) MutableDataSet(com.vladsch.flexmark.util.options.MutableDataSet) Parser(com.vladsch.flexmark.parser.Parser) Test(org.junit.Test)

Example 20 with MutableDataSet

use of com.vladsch.flexmark.util.options.MutableDataSet in project nzbhydra2 by theotherp.

the class Markdown method renderMarkdownAsHtml.

public static String renderMarkdownAsHtml(String markdown) {
    MutableDataSet options = new MutableDataSet();
    Parser parser = Parser.builder(options).build();
    HtmlRenderer renderer = HtmlRenderer.builder(options).build();
    Node document = parser.parse(markdown);
    return renderer.render(document);
}
Also used : Node(com.vladsch.flexmark.ast.Node) HtmlRenderer(com.vladsch.flexmark.html.HtmlRenderer) MutableDataSet(com.vladsch.flexmark.util.options.MutableDataSet) Parser(com.vladsch.flexmark.parser.Parser)

Aggregations

MutableDataSet (com.vladsch.flexmark.util.options.MutableDataSet)20 Parser (com.vladsch.flexmark.parser.Parser)17 HtmlRenderer (com.vladsch.flexmark.html.HtmlRenderer)15 Node (com.vladsch.flexmark.ast.Node)14 MutableDataHolder (com.vladsch.flexmark.util.options.MutableDataHolder)6 Extension (com.vladsch.flexmark.Extension)4 AutolinkExtension (com.vladsch.flexmark.ext.autolink.AutolinkExtension)2 FootnoteExtension (com.vladsch.flexmark.ext.footnotes.FootnoteExtension)2 TocExtension (com.vladsch.flexmark.ext.toc.TocExtension)2 DataHolder (com.vladsch.flexmark.util.options.DataHolder)2 Test (org.junit.Test)2 Document (com.vladsch.flexmark.ast.Document)1 TextCollectingVisitor (com.vladsch.flexmark.ast.util.TextCollectingVisitor)1 AbbreviationExtension (com.vladsch.flexmark.ext.abbreviation.AbbreviationExtension)1 AnchorLinkExtension (com.vladsch.flexmark.ext.anchorlink.AnchorLinkExtension)1 DefinitionExtension (com.vladsch.flexmark.ext.definition.DefinitionExtension)1 EscapedCharacterExtension (com.vladsch.flexmark.ext.escaped.character.EscapedCharacterExtension)1 StrikethroughExtension (com.vladsch.flexmark.ext.gfm.strikethrough.StrikethroughExtension)1 StrikethroughSubscriptExtension (com.vladsch.flexmark.ext.gfm.strikethrough.StrikethroughSubscriptExtension)1 SubscriptExtension (com.vladsch.flexmark.ext.gfm.strikethrough.SubscriptExtension)1