Search in sources :

Example 31 with Node

use of com.vladsch.flexmark.ast.Node in project flexmark-java by vsch.

the class MacroBlockParser method parseInlines.

@Override
public void parseInlines(InlineParser inlineParser) {
    Node node = block.getLastChild();
    if (node instanceof MacroClose) {
        node.unlink();
    }
    inlineParser.parse(block.getContentChars(), block);
    if (node instanceof MacroClose) {
        block.appendChild(node);
    }
}
Also used : MacroClose(com.vladsch.flexmark.ext.xwiki.macros.MacroClose) Node(com.vladsch.flexmark.ast.Node)

Example 32 with Node

use of com.vladsch.flexmark.ast.Node in project flexmark-java by vsch.

the class SpecExampleBlockParser method closeBlock.

@Override
public void closeBlock(ParserState state) {
    // first line, if not blank, has the info string
    List<BasedSequence> lines = content.getLines();
    if (lines.size() > 0) {
        BasedSequence info = lines.get(0);
        int exampleKeyWordStart = myOptions.exampleBreak.length() + 1;
        int exampleKeyWordEnd = exampleKeyWordStart + EXAMPLE_KEYWORD.length();
        BasedSequence exampleKeyword = info.subSequence(exampleKeyWordStart, exampleKeyWordEnd);
        BasedSequence optionsChars = info.subSequence(exampleKeyWordEnd);
        Matcher options = OPTIONS_PATTERN.matcher(optionsChars.toString().replace('\u00A0', ' '));
        block.setOpeningMarker(info.subSequence(0, myOptions.exampleBreak.length()));
        block.setExampleKeyword(exampleKeyword);
        if (options.matches()) {
            BasedSequence coordOpeningMarker = BasedSequence.NULL;
            BasedSequence section = BasedSequence.NULL;
            BasedSequence numberSeparator = BasedSequence.NULL;
            BasedSequence number = BasedSequence.NULL;
            BasedSequence coordClosingMarker = BasedSequence.NULL;
            BasedSequence optionsKeyword = BasedSequence.NULL;
            BasedSequence optionsOpeningMarker = BasedSequence.NULL;
            BasedSequence optionsText = BasedSequence.NULL;
            BasedSequence optionsClosingMarker = BasedSequence.NULL;
            // @formatter:off
            if (options.group(GROUP_COORD_OPEN) != null && !options.group(GROUP_COORD_OPEN).trim().isEmpty()) {
                coordOpeningMarker = optionsChars.subSequence(options.start(GROUP_COORD_OPEN), options.end(GROUP_COORD_OPEN)).trim(WHITESPACE_NBSP_CHARS);
            }
            if (options.group(GROUP_SECTION) != null && !options.group(GROUP_SECTION).trim().isEmpty()) {
                section = optionsChars.subSequence(options.start(GROUP_SECTION), options.end(GROUP_SECTION)).trim(WHITESPACE_NBSP_CHARS);
            }
            if (options.group(GROUP_NUMBER_SEPARATOR) != null && !options.group(GROUP_NUMBER_SEPARATOR).trim().isEmpty()) {
                numberSeparator = optionsChars.subSequence(options.start(GROUP_NUMBER_SEPARATOR), options.end(GROUP_NUMBER_SEPARATOR)).trim(WHITESPACE_NBSP_CHARS);
            }
            if (options.group(GROUP_NUMBER) != null && !options.group(GROUP_NUMBER).trim().isEmpty()) {
                number = optionsChars.subSequence(options.start(GROUP_NUMBER), options.end(GROUP_NUMBER)).trim(WHITESPACE_NBSP_CHARS);
            }
            if (options.group(GROUP_COORD_CLOSE) != null && !options.group(GROUP_COORD_CLOSE).trim().isEmpty()) {
                coordClosingMarker = optionsChars.subSequence(options.start(GROUP_COORD_CLOSE), options.end(GROUP_COORD_CLOSE)).trim(WHITESPACE_NBSP_CHARS);
            }
            if (options.group(GROUP_OPTION_KEYWORD) != null && !options.group(GROUP_OPTION_KEYWORD).trim().isEmpty()) {
                optionsKeyword = optionsChars.subSequence(options.start(GROUP_OPTION_KEYWORD), options.end(GROUP_OPTION_KEYWORD)).trim(WHITESPACE_NBSP_CHARS);
            }
            if (options.group(GROUP_OPTIONS_OPEN) != null && !options.group(GROUP_OPTIONS_OPEN).trim().isEmpty()) {
                optionsOpeningMarker = optionsChars.subSequence(options.start(GROUP_OPTIONS_OPEN), options.end(GROUP_OPTIONS_OPEN)).trim(WHITESPACE_NBSP_CHARS);
            }
            if (options.group(GROUP_OPTIONS) != null) {
                optionsText = optionsChars.subSequence(options.start(GROUP_OPTIONS), options.end(GROUP_OPTIONS));
            }
            if (options.group(GROUP_OPTIONS_CLOSE) != null && !options.group(GROUP_OPTIONS_CLOSE).trim().isEmpty()) {
                optionsClosingMarker = optionsChars.subSequence(options.start(GROUP_OPTIONS_CLOSE), options.end(GROUP_OPTIONS_CLOSE)).trim(WHITESPACE_NBSP_CHARS);
            }
            // @formatter:on
            if (section.isNotNull() && optionsKeyword.isNull() && numberSeparator.isNull() && coordOpeningMarker.isNull() && section.matchChars("options")) {
                // move all from section to options
                int pos = section.indexOfAny(' ', '\t', '\u00A0');
                if (pos < 0) {
                    optionsKeyword = section;
                } else {
                    optionsKeyword = section.subSequence(0, pos);
                    optionsText = section.subSequence(pos + 1);
                }
                optionsClosingMarker = coordClosingMarker;
                section = BasedSequence.NULL;
                coordClosingMarker = BasedSequence.NULL;
            }
            if (optionsText.isNull()) {
                if (optionsClosingMarker.isNotNull()) {
                    optionsText = optionsClosingMarker.subSequence(0, 0);
                } else if (optionsOpeningMarker.isNotNull()) {
                    optionsText = optionsOpeningMarker.subSequence(1, 1);
                } else if (optionsKeyword.isNotNull()) {
                    optionsText = optionsKeyword.subSequence(optionsKeyword.length(), optionsKeyword.length());
                }
            }
            block.setCoordOpeningMarker(coordOpeningMarker);
            block.setSection(section);
            block.setNumberSeparator(numberSeparator);
            block.setNumber(number);
            block.setCoordClosingMarker(coordClosingMarker);
            block.setOptionsKeyword(optionsKeyword);
            block.setOptionsOpeningMarker(optionsOpeningMarker);
            block.setOptions(optionsText);
            block.setOptionsClosingMarker(optionsClosingMarker);
        }
        // if we create option nodes, we break up the options
        if (myOptions.optionNodes && block.getOptionsKeyword().isNotNull()) {
            Node optionsList = new SpecExampleOptionsList(block.getOptions());
            block.appendChild(optionsList);
            BasedSequence trimmedOptionsList = block.getOptions().trim(WHITESPACE_NBSP_CHARS);
            if (!trimmedOptionsList.isEmpty()) {
                BasedSequence[] list = trimmedOptionsList.split(',', 0, SPLIT_INCLUDE_DELIM_PARTS);
                for (BasedSequence item : list) {
                    BasedSequence option = item.trim(WHITESPACE_NBSP_CHARS);
                    if (!option.isEmpty()) {
                        if (option.matches(",")) {
                            Node optionNode = new SpecExampleOptionSeparator(option);
                            optionsList.appendChild(optionNode);
                        } else {
                            Node optionNode = new SpecExampleOption(option);
                            optionsList.appendChild(optionNode);
                        }
                    }
                }
            }
        }
        BasedSequence chars = content.getSpanningChars();
        BasedSequence spanningChars = chars.baseSubSequence(chars.getStartOffset(), lines.get(0).getEndOffset());
        if (lines.size() > 1) {
            // have more lines
            block.setContent(spanningChars, lines.subList(1, lines.size()));
            // need to find the parts
            boolean inSource = true;
            boolean inHtml = false;
            boolean inAst = false;
            int sectionStart = -1;
            BasedSequence prevLine = BasedSequence.NULL;
            BasedSequence lastLine = lines.get(lines.size() - 1);
            String typeBreak = myOptions.typeBreak;
            int typeBreakLength = typeBreak.length();
            for (BasedSequence line : lines.subList(1, lines.size())) {
                if (line.length() == typeBreakLength + line.countTrailing(BasedSequence.EOL_CHARS) && line.matchChars(typeBreak)) {
                    if (inSource) {
                        inSource = false;
                        if (sectionStart != -1) {
                            block.setSource(line.baseSubSequence(sectionStart, line.getStartOffset() - prevLine.countTrailing(BasedSequence.EOL_CHARS)));
                        } else {
                            block.setSource(line.subSequence(0, 0));
                        }
                        block.setHtmlSeparator(line);
                        inHtml = true;
                        sectionStart = -1;
                    } else if (inHtml) {
                        inHtml = false;
                        if (sectionStart != -1) {
                            block.setHtml(line.baseSubSequence(sectionStart, line.getStartOffset() - prevLine.countTrailing(BasedSequence.EOL_CHARS)));
                        } else {
                            block.setHtml(line.subSequence(0, 0));
                        }
                        block.setAstSeparator(line);
                        inAst = true;
                        sectionStart = -1;
                    } else {
                        if (sectionStart == -1) {
                            sectionStart = line.getStartOffset();
                        }
                    }
                } else {
                    if (sectionStart == -1) {
                        sectionStart = line.getStartOffset();
                    }
                }
                prevLine = line;
                if (line == lastLine) {
                    // done
                    if (inSource) {
                        if (sectionStart != -1) {
                            block.setSource(line.baseSubSequence(sectionStart, line.getEndOffset() - prevLine.countTrailing(BasedSequence.EOL_CHARS)));
                        } else {
                            block.setSource(line.subSequence(line.length(), line.length()));
                        }
                    } else if (inHtml) {
                        if (sectionStart != -1) {
                            block.setHtml(line.baseSubSequence(sectionStart, line.getEndOffset() - prevLine.countTrailing(BasedSequence.EOL_CHARS)));
                        } else {
                            block.setHtml(line.subSequence(line.length(), line.length()));
                        }
                    } else if (inAst) {
                        if (sectionStart != -1) {
                            block.setAst(line.baseSubSequence(sectionStart, line.getEndOffset() - prevLine.countTrailing(BasedSequence.EOL_CHARS)));
                        } else {
                            block.setAst(line.subSequence(line.length(), line.length()));
                        }
                    }
                    break;
                }
            }
            // here if we create section nodes
            if (block.getSource().isNotNull()) {
                Node node = new SpecExampleSource(block.getSource());
                block.appendChild(node);
            }
            if (block.getHtmlSeparator().isNotNull()) {
                Node node = new SpecExampleSeparator(block.getHtmlSeparator());
                block.appendChild(node);
                if (block.getHtml().isNotNull()) {
                    node = new SpecExampleHtml(block.getHtml());
                    block.appendChild(node);
                }
                if (block.getAstSeparator().isNotNull()) {
                    node = new SpecExampleSeparator(block.getAstSeparator());
                    block.appendChild(node);
                    if (block.getAst().isNotNull()) {
                        node = new SpecExampleAst(block.getAst());
                        block.appendChild(node);
                    }
                }
            }
        } else {
            Node node = new SpecExampleSource(block.getClosingMarker().subSequence(0, 0));
            block.appendChild(node);
            block.setContent(spanningChars, BasedSequence.EMPTY_LIST);
        }
    } else {
        Node node = new SpecExampleSource(block.getClosingMarker().subSequence(0, 0));
        block.appendChild(node);
        block.setContent(content);
    }
    block.setCharsFromContent();
    content = null;
}
Also used : Matcher(java.util.regex.Matcher) BasedSequence(com.vladsch.flexmark.util.sequence.BasedSequence) Node(com.vladsch.flexmark.ast.Node)

Example 33 with Node

use of com.vladsch.flexmark.ast.Node in project flexmark-java by vsch.

the class TextCollectingVisitorTest method test_basic.

@Test
public void test_basic() {
    Parser parser = Parser.builder().build();
    Node document = parser.parse("Test text");
    TextCollectingVisitor collectingVisitor = new TextCollectingVisitor();
    final String text = collectingVisitor.collectAndGetText(document);
    assertEquals("Test text", text);
}
Also used : Node(com.vladsch.flexmark.ast.Node) Parser(com.vladsch.flexmark.parser.Parser) Test(org.junit.Test)

Example 34 with Node

use of com.vladsch.flexmark.ast.Node in project flexmark-java by vsch.

the class TextCollectingVisitorTest method test_fenced_code.

@Test
public void test_fenced_code() {
    Parser parser = Parser.builder().build();
    Node document = parser.parse("```info\nfenced code\nlines\n```");
    TextCollectingVisitor collectingVisitor = new TextCollectingVisitor();
    final String text = collectingVisitor.collectAndGetText(document);
    assertEquals("fenced code\nlines\n", text);
}
Also used : Node(com.vladsch.flexmark.ast.Node) Parser(com.vladsch.flexmark.parser.Parser) Test(org.junit.Test)

Example 35 with Node

use of com.vladsch.flexmark.ast.Node in project flexmark-java by vsch.

the class TextCollectingVisitorTest method test_emphasis.

@Test
public void test_emphasis() {
    Parser parser = Parser.builder().build();
    Node document = parser.parse("Test text *emphasis*");
    TextCollectingVisitor collectingVisitor = new TextCollectingVisitor();
    final String text = collectingVisitor.collectAndGetText(document);
    assertEquals("Test text emphasis", text);
}
Also used : Node(com.vladsch.flexmark.ast.Node) Parser(com.vladsch.flexmark.parser.Parser) Test(org.junit.Test)

Aggregations

Node (com.vladsch.flexmark.ast.Node)87 Parser (com.vladsch.flexmark.parser.Parser)29 HtmlRenderer (com.vladsch.flexmark.html.HtmlRenderer)21 Test (org.junit.Test)19 MutableDataSet (com.vladsch.flexmark.util.options.MutableDataSet)14 DataHolder (com.vladsch.flexmark.util.options.DataHolder)11 List (java.util.List)8 BasedSequence (com.vladsch.flexmark.util.sequence.BasedSequence)6 MutableDataHolder (com.vladsch.flexmark.util.options.MutableDataHolder)5 Text (com.vladsch.flexmark.ast.Text)3 TextCollectingVisitor (com.vladsch.flexmark.ast.util.TextCollectingVisitor)3 Extension (com.vladsch.flexmark.Extension)2 Block (com.vladsch.flexmark.ast.Block)2 CustomNode (com.vladsch.flexmark.ast.CustomNode)2 Document (com.vladsch.flexmark.ast.Document)2 Heading (com.vladsch.flexmark.ast.Heading)2 DocxRenderer (com.vladsch.flexmark.docx.converter.internal.DocxRenderer)2 MacroClose (com.vladsch.flexmark.ext.xwiki.macros.MacroClose)2 AstCollectingVisitor (com.vladsch.flexmark.test.AstCollectingVisitor)2 Attributes (com.vladsch.flexmark.util.html.Attributes)2