Search in sources :

Example 1 with BlockType

use of org.eclipse.mylyn.wikitext.parser.DocumentBuilder.BlockType in project mylyn.docs by eclipse.

the class ListBlock method processLineContent.

/**
 * AsciiDoc line process with given offset.
 */
@Override
protected int processLineContent(String line, int offset) {
    boolean closeItem = true;
    // first line processed in current block
    if (blockLineCount == 0) {
        listState = new Stack<>();
        ListAttributes attributes = new ListAttributes();
        String listSpec = normalizeListSpec(matcher.group(1));
        BlockType type = calculateType(listSpec);
        if (type == BlockType.NUMERIC_LIST) {
            List<String> positionalParameters = new ArrayList<>();
            positionalParameters.add(PARAM_NAME_STYLE);
            Map<String, String> lastProperties = getAsciiDocState().getLastProperties(positionalParameters);
            getAsciiDocState().setLastPropertiesText("");
            String startProperty = lastProperties.get(PARAM_NAME_START);
            if (startProperty != null) {
                attributes.setStart(startProperty);
            }
            String styleProperty = lastProperties.get(PARAM_NAME_STYLE);
            updateStyleAttribute(attributes, listSpec, styleProperty);
        }
        // first line of the block could be "** " or more
        offset = matcher.start(2);
        listState.push(new ListState(listSpec, 1, type));
        builder.beginBlock(type, attributes);
        adjustLevel(matcher, type, listSpec);
    } else if (line.isEmpty()) {
        if (!listContinuation) {
            blankSeparator = true;
        }
        return -1;
    } else if (isListContinuation(line)) {
        // list continuation
        blankSeparator = false;
        listContinuation = true;
        closeItem = false;
        return -1;
    } else {
        Matcher matcher = startPattern.matcher(line);
        if (!matcher.matches()) {
            if (blankSeparator) {
                setClosed(true);
                blankSeparator = false;
                return 0;
            }
            closeItem = false;
            Matcher leadingBlankMatcher = leadingBlankPattern.matcher(line);
            if (leadingBlankMatcher.find()) {
                offset = leadingBlankMatcher.end();
            }
            // $NON-NLS-1$
            markupLanguage.emitMarkupText(getParser(), state, " ");
        } else {
            String listSpec = normalizeListSpec(matcher.group(1));
            BlockType type = calculateType(listSpec);
            offset = matcher.start(2);
            adjustLevel(matcher, type, listSpec);
        }
        blankSeparator = false;
        listContinuation = false;
    }
    ++blockLineCount;
    ListState listState = this.listState.peek();
    if (closeItem) {
        if (listState.openItem) {
            builder.endBlock();
        }
        listState.openItem = true;
        builder.beginBlock(BlockType.LIST_ITEM, new Attributes());
    }
    markupLanguage.emitMarkupLine(getParser(), state, line, offset);
    // entire line processed
    return -1;
}
Also used : BlockType(org.eclipse.mylyn.wikitext.parser.DocumentBuilder.BlockType) Matcher(java.util.regex.Matcher) ListAttributes(org.eclipse.mylyn.wikitext.parser.ListAttributes) ArrayList(java.util.ArrayList) Attributes(org.eclipse.mylyn.wikitext.parser.Attributes) ListAttributes(org.eclipse.mylyn.wikitext.parser.ListAttributes)

Example 2 with BlockType

use of org.eclipse.mylyn.wikitext.parser.DocumentBuilder.BlockType in project mylyn.docs by eclipse.

the class HtmlParserTest method assertParseEventOrder.

private void assertParseEventOrder(String content, Object... expectedEventTypes) {
    final List<Object> actualEventTypes = new ArrayList<>();
    DocumentBuilder builder = new NoOpDocumentBuilder() {

        @Override
        public void beginBlock(BlockType type, Attributes attributes) {
            actualEventTypes.add(type);
        }

        @Override
        public void beginSpan(SpanType type, Attributes attributes) {
            actualEventTypes.add(type);
        }

        @Override
        public void characters(String text) {
            actualEventTypes.add(text);
        }

        @Override
        public void endBlock() {
            actualEventTypes.add(END_BLOCK);
        }

        @Override
        public void endSpan() {
            actualEventTypes.add(END_SPAN);
        }
    };
    parse(content, builder);
    assertEquals(ImmutableList.copyOf(expectedEventTypes), actualEventTypes);
}
Also used : NoOpDocumentBuilder(org.eclipse.mylyn.wikitext.parser.builder.NoOpDocumentBuilder) SpanType(org.eclipse.mylyn.wikitext.parser.DocumentBuilder.SpanType) DocumentBuilder(org.eclipse.mylyn.wikitext.parser.DocumentBuilder) NoOpDocumentBuilder(org.eclipse.mylyn.wikitext.parser.builder.NoOpDocumentBuilder) HtmlDocumentBuilder(org.eclipse.mylyn.wikitext.parser.builder.HtmlDocumentBuilder) BlockType(org.eclipse.mylyn.wikitext.parser.DocumentBuilder.BlockType) ArrayList(java.util.ArrayList) Attributes(org.eclipse.mylyn.wikitext.parser.Attributes)

Example 3 with BlockType

use of org.eclipse.mylyn.wikitext.parser.DocumentBuilder.BlockType in project mylyn.docs by eclipse.

the class ListBlock method processLineContent.

@Override
public int processLineContent(String line, int offset) {
    boolean continuation = false;
    if (blockLineCount == 0) {
        listState = new Stack<ListState>();
        Attributes attributes = new Attributes();
        String listSpec = matcher.group(1);
        char lastChar = listSpec.charAt(listSpec.length() - 1);
        int level = calculateLevel(listSpec);
        BlockType type = calculateType(lastChar);
        BlockType itemType = calculateItemType(lastChar);
        if (type == BlockType.BULLETED_LIST && '-' == lastChar) {
            // $NON-NLS-1$
            attributes.setCssStyle("list-style: square");
        }
        offset = matcher.start(LINE_REMAINDER_GROUP_OFFSET);
        listState.push(new ListState(1, type, itemType));
        builder.beginBlock(type, attributes);
        adjustLevel(lastChar, level, type, itemType);
    } else {
        Matcher matcher = startPattern.matcher(line);
        if (!matcher.matches()) {
            setClosed(true);
            return 0;
        }
        String listSpec = matcher.group(1);
        char lastChar = listSpec.charAt(listSpec.length() - 1);
        int lineLevel = calculateLevel(listSpec);
        BlockType type = calculateType(lastChar);
        BlockType itemType = calculateItemType(lastChar);
        offset = matcher.start(LINE_REMAINDER_GROUP_OFFSET);
        continuation = adjustLevel(lastChar, lineLevel, type, itemType);
    }
    ++blockLineCount;
    ListState listState = this.listState.peek();
    if (!continuation && listState.openItem) {
        listState.openItem = false;
        builder.endBlock();
    }
    if (!listState.openItem) {
        listState.openItem = true;
        builder.beginBlock(listState.itemType, new Attributes());
    }
    String definition = null;
    int definitionOffset = -1;
    if (listState.itemType == BlockType.DEFINITION_TERM) {
        // detect definition on same line as term
        Matcher definitionMatcher = definitionPattern.matcher(line);
        if (offset > 0) {
            definitionMatcher.region(offset, line.length());
        }
        if (definitionMatcher.find()) {
            line = line.substring(offset, definitionMatcher.start(1));
            offset = 0;
            definition = definitionMatcher.group(2);
            definitionOffset = definitionMatcher.start(2);
        }
    }
    if (definition == null) {
        markupLanguage.emitMarkupLine(getParser(), state, line, offset);
    } else {
        markupLanguage.emitMarkupLine(getParser(), state, offset, line, 0);
    }
    if (definition != null) {
        listState.openItem = false;
        builder.endBlock();
        adjustLevel(' ', listState.level, BlockType.DEFINITION_LIST, BlockType.DEFINITION_ITEM);
        listState = this.listState.peek();
        if (listState.openItem) {
            builder.endBlock();
        }
        listState.openItem = true;
        builder.beginBlock(listState.itemType, new Attributes());
        markupLanguage.emitMarkupLine(parser, state, definitionOffset, definition, 0);
    }
    return -1;
}
Also used : BlockType(org.eclipse.mylyn.wikitext.parser.DocumentBuilder.BlockType) Matcher(java.util.regex.Matcher) Attributes(org.eclipse.mylyn.wikitext.parser.Attributes)

Example 4 with BlockType

use of org.eclipse.mylyn.wikitext.parser.DocumentBuilder.BlockType in project mylyn.docs by eclipse.

the class TableBlock method processLineContent.

@Override
public int processLineContent(String line, int offset) {
    if (blockLineCount++ == 0) {
        TableAttributes attributes = new TableAttributes();
        // first line opens table
        String options = matcher.group(1);
        if (options != null) {
            Matcher optionsMatcher = optionsPattern.matcher(options);
            while (optionsMatcher.find()) {
                String optionName = optionsMatcher.group(1);
                String optionValue = optionsMatcher.group(2);
                if (optionName.equalsIgnoreCase("id")) {
                    // $NON-NLS-1$
                    attributes.setId(optionValue);
                } else if (optionName.equalsIgnoreCase("style")) {
                    // $NON-NLS-1$
                    attributes.setCssStyle(optionValue);
                } else if (optionName.equalsIgnoreCase("class")) {
                    // $NON-NLS-1$
                    attributes.setCssClass(optionValue);
                } else if (optionName.equalsIgnoreCase("title")) {
                    // $NON-NLS-1$
                    attributes.setTitle(optionValue);
                } else if (optionName.equalsIgnoreCase("border")) {
                    // $NON-NLS-1$
                    attributes.setBorder(optionValue);
                } else if (optionName.equalsIgnoreCase("summary")) {
                    // $NON-NLS-1$
                    attributes.setSummary(optionValue);
                } else if (optionName.equalsIgnoreCase("width")) {
                    // $NON-NLS-1$
                    attributes.setWidth(optionValue);
                } else if (optionName.equalsIgnoreCase("frame")) {
                    // $NON-NLS-1$
                    attributes.setFrame(optionValue);
                } else if (optionName.equalsIgnoreCase("rules")) {
                    // $NON-NLS-1$
                    attributes.setRules(optionValue);
                } else if (optionName.equalsIgnoreCase("cellspacing")) {
                    // $NON-NLS-1$
                    attributes.setCellspacing(optionValue);
                } else if (optionName.equalsIgnoreCase("cellpadding")) {
                    // $NON-NLS-1$
                    attributes.setCellpadding(optionValue);
                } else if (optionName.equalsIgnoreCase("bgcolor")) {
                    // $NON-NLS-1$
                    attributes.setBgcolor(optionValue);
                }
            }
        }
        builder.beginBlock(BlockType.TABLE, attributes);
        // table open line never has cells
        return -1;
    } else {
        Matcher newRowMatcher = newRowPattern.matcher(line);
        if (newRowMatcher.matches()) {
            TableRowAttributes attributes = new TableRowAttributes();
            String newRowOptions = newRowMatcher.group(1);
            if (newRowOptions != null) {
                Matcher optionsMatcher = optionsPattern.matcher(newRowOptions);
                while (optionsMatcher.find()) {
                    String optionName = optionsMatcher.group(1);
                    String optionValue = optionsMatcher.group(2);
                    if (optionName.equalsIgnoreCase("id")) {
                        // $NON-NLS-1$
                        attributes.setId(optionValue);
                    } else if (optionName.equalsIgnoreCase("style")) {
                        // $NON-NLS-1$
                        attributes.setCssStyle(optionValue);
                    } else if (optionName.equalsIgnoreCase("class")) {
                        // $NON-NLS-1$
                        attributes.setCssClass(optionValue);
                    } else if (optionName.equalsIgnoreCase("title")) {
                        // $NON-NLS-1$
                        attributes.setTitle(optionValue);
                    } else if (optionName.equalsIgnoreCase("align")) {
                        // $NON-NLS-1$
                        attributes.setAlign(optionValue);
                    } else if (optionName.equalsIgnoreCase("valign")) {
                        // $NON-NLS-1$
                        attributes.setValign(optionValue);
                    } else if (optionName.equalsIgnoreCase("bgcolor")) {
                        // $NON-NLS-1$
                        attributes.setBgcolor(optionValue);
                    }
                }
            }
            openRow(newRowMatcher.start(), attributes);
            return -1;
        } else {
            Matcher endMatcher = endPattern.matcher(line);
            if (endMatcher.matches()) {
                setClosed(true);
                return endMatcher.start(1);
            } else {
                Matcher cellMatcher = cellPattern.matcher(line);
                if (cellMatcher.matches()) {
                    String kind = cellMatcher.group(1);
                    // $NON-NLS-1$
                    BlockType type = ("!".equals(kind)) ? BlockType.TABLE_CELL_HEADER : BlockType.TABLE_CELL_NORMAL;
                    String contents = cellMatcher.group(2);
                    if (contents == null) {
                        // cell was just opened, no cell options.
                        openCell(cellMatcher.start(), type, new TableCellAttributes());
                        return -1;
                    }
                    int contentsStart = cellMatcher.start(2);
                    emitCells(contentsStart, type, contents);
                    return -1;
                } else {
                    // in case of cells this will be handled with NestedBlocks
                    return -1;
                }
            }
        }
    }
}
Also used : TableRowAttributes(org.eclipse.mylyn.wikitext.parser.TableRowAttributes) Matcher(java.util.regex.Matcher) BlockType(org.eclipse.mylyn.wikitext.parser.DocumentBuilder.BlockType) TableAttributes(org.eclipse.mylyn.wikitext.parser.TableAttributes) TableCellAttributes(org.eclipse.mylyn.wikitext.parser.TableCellAttributes)

Example 5 with BlockType

use of org.eclipse.mylyn.wikitext.parser.DocumentBuilder.BlockType in project mylyn.docs by eclipse.

the class ListBlock method processLineContent.

@Override
public int processLineContent(String line, int offset) {
    boolean continuation = false;
    if (blockLineCount == 0) {
        listState = new Stack<>();
        Attributes attributes = new Attributes();
        String listSpec = matcher.group(1);
        int level = calculateLevel(listSpec);
        String typeSpec = matcher.group(2);
        BlockType type = calculateType(typeSpec);
        computeAttributes(attributes, type, typeSpec);
        // 0-offset matches may start with the "*** " prefix.
        offset = matcher.start(3);
        listState.push(new ListState(1, type));
        builder.beginBlock(type, attributes);
        adjustLevel(type, listSpec, level);
    } else {
        Matcher matcher = startPattern.matcher(line);
        if (!matcher.matches()) {
            // FIXME: continuations not yet implemented
            matcher = continuationPattern.matcher(line);
            if (listState.isEmpty() || !matcher.matches()) {
                setClosed(true);
                return 0;
            } else {
                continuation = true;
                // use -1 to get one whitespace character
                offset = matcher.start(1) - 1;
            }
        } else {
            String listSpec = matcher.group(1);
            int level = calculateLevel(listSpec);
            String typeSpec = matcher.group(2);
            BlockType type = calculateType(typeSpec);
            offset = matcher.start(3);
            adjustLevel(type, typeSpec, level);
        }
    }
    ++blockLineCount;
    ListState listState = this.listState.peek();
    if (!continuation && listState.openItem) {
        builder.endBlock();
        listState.openItem = false;
    }
    if (!listState.openItem) {
        listState.openItem = true;
        builder.beginBlock(BlockType.LIST_ITEM, new Attributes());
    }
    markupLanguage.emitMarkupLine(getParser(), state, line, offset);
    return -1;
}
Also used : BlockType(org.eclipse.mylyn.wikitext.parser.DocumentBuilder.BlockType) Matcher(java.util.regex.Matcher) Attributes(org.eclipse.mylyn.wikitext.parser.Attributes)

Aggregations

BlockType (org.eclipse.mylyn.wikitext.parser.DocumentBuilder.BlockType)14 Attributes (org.eclipse.mylyn.wikitext.parser.Attributes)12 Matcher (java.util.regex.Matcher)8 BlockStrategies (org.eclipse.mylyn.wikitext.html.internal.BlockStrategies)3 Test (org.junit.Test)3 ArrayList (java.util.ArrayList)2 ListAttributes (org.eclipse.mylyn.wikitext.parser.ListAttributes)2 HtmlDocumentBuilder (org.eclipse.mylyn.wikitext.parser.builder.HtmlDocumentBuilder)2 PrintWriter (java.io.PrintWriter)1 StringReader (java.io.StringReader)1 StringWriter (java.io.StringWriter)1 IFile (org.eclipse.core.resources.IFile)1 CoreException (org.eclipse.core.runtime.CoreException)1 IPath (org.eclipse.core.runtime.IPath)1 BlockStrategy (org.eclipse.mylyn.wikitext.html.internal.BlockStrategy)1 NoOpBlockStrategy (org.eclipse.mylyn.wikitext.html.internal.NoOpBlockStrategy)1 SubstitutionBlockStrategy (org.eclipse.mylyn.wikitext.html.internal.SubstitutionBlockStrategy)1 SupportedBlockStrategy (org.eclipse.mylyn.wikitext.html.internal.SupportedBlockStrategy)1 UnsupportedBlockStrategy (org.eclipse.mylyn.wikitext.html.internal.UnsupportedBlockStrategy)1 DocumentBuilder (org.eclipse.mylyn.wikitext.parser.DocumentBuilder)1