Search in sources :

Example 1 with TableRowAttributes

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

the class TableBlock method handleCellContent.

private void handleCellContent(String fullCellContent, int positionInLine) {
    closeCellBlockIfNeeded();
    String cellContent = fullCellContent.trim();
    String blockContent;
    if (format == TableFormat.COMMA_SEPARATED_VALUES) {
        if (cellContent.startsWith("\"") && cellContent.endsWith("\"")) {
            blockContent = cellContent.substring(1, cellContent.length() - 1).replaceAll("\"\"", "\"");
        } else {
            blockContent = cellContent;
        }
    } else {
        String delimiter = getCellSeparator();
        // $NON-NLS-1$
        blockContent = cellContent.replaceAll("\\\\" + Pattern.quote(delimiter), delimiter);
    }
    if (isColFormatKnown() && cellsCount % colsAttribute.size() == 0) {
        TableRowAttributes tableRowAttributes = new TableRowAttributes();
        builder.beginBlock(BlockType.TABLE_ROW, tableRowAttributes);
    }
    TableCellAttributes attributes;
    if (colsAttribute.isEmpty()) {
        attributes = new TableCellAttributes();
    } else {
        attributes = colsAttribute.get(cellsCount % colsAttribute.size());
    }
    if (hasHeader && isFirstRow()) {
        builder.beginBlock(BlockType.TABLE_CELL_HEADER, attributes);
    } else {
        builder.beginBlock(BlockType.TABLE_CELL_NORMAL, attributes);
    }
    cellBlockIsOpen = true;
    int offset = fullCellContent.indexOf(cellContent);
    markupLanguage.emitMarkupLine(parser, state, offset + positionInLine, blockContent, 0);
}
Also used : TableRowAttributes(org.eclipse.mylyn.wikitext.parser.TableRowAttributes) TableCellAttributes(org.eclipse.mylyn.wikitext.parser.TableCellAttributes)

Example 2 with TableRowAttributes

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

the class TableBlock method openCell.

/**
 * Open a cell block.
 *
 * @param lineOffset
 *            line offset
 * @param type
 *            type of cell (expecting {@link BlockType#TABLE_CELL_HEADER} or {@link BlockType#TABLE_CELL_NORMAL}).
 * @param attributes
 *            attributes of the cell
 */
private void openCell(int lineOffset, BlockType type, TableCellAttributes attributes) {
    closeCell();
    if (!openRow) {
        openRow(lineOffset, new TableRowAttributes());
    }
    state.setLineCharacterOffset(lineOffset);
    builder.beginBlock(type, attributes);
    openCell = true;
}
Also used : TableRowAttributes(org.eclipse.mylyn.wikitext.parser.TableRowAttributes)

Example 3 with TableRowAttributes

use of org.eclipse.mylyn.wikitext.parser.TableRowAttributes 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 4 with TableRowAttributes

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

the class TableBlock method processBlockContent.

@Override
protected void processBlockContent(String line) {
    if (!line.trim().isEmpty()) {
        if (!isColFormatKnown() && !cellBlockIsOpen) {
            TableRowAttributes tableRowAttributes = new TableRowAttributes();
            builder.beginBlock(BlockType.TABLE_ROW, tableRowAttributes);
        }
        int offset = 0;
        boolean firstCellInLine = true;
        Matcher rowCellMatcher = createRowCellMatcher(line);
        while (offset <= line.length()) {
            boolean found = rowCellMatcher.find();
            int endOffset = found ? rowCellMatcher.start() : line.length();
            String cellContent = line.substring(offset, endOffset);
            if (offset == 0 && format == TableFormat.PREFIX_SEPARATED_VALUES) {
                if (!cellContent.isEmpty()) {
                    if (isFirsCellOfTable()) {
                        handleCellContent(cellContent, offset);
                        firstCellInLine = false;
                    } else {
                        appendCellContent(cellContent, endOffset);
                        firstCellInLine = false;
                    }
                }
            } else {
                if (!isColFormatKnown() && firstCellInLine && !isFirsCellOfTable()) {
                    colsAttribute = LanguageSupport.createDefaultColumnsAttributeList(cellsCount + 1);
                }
                // start of new cell
                handleCellContent(cellContent, offset);
                firstCellInLine = false;
            }
            offset = found ? rowCellMatcher.end() : line.length() + 1;
        }
    }
}
Also used : TableRowAttributes(org.eclipse.mylyn.wikitext.parser.TableRowAttributes) Matcher(java.util.regex.Matcher)

Example 5 with TableRowAttributes

use of org.eclipse.mylyn.wikitext.parser.TableRowAttributes 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();
        if (matcher.group(1) != null) {
            // 0-offset matches may start with the "table. " prefix.
            Textile.configureAttributes(attributes, matcher, 2, true);
            offset = line.length();
        }
        builder.beginBlock(BlockType.TABLE, attributes);
    } else if (markupLanguage.isEmptyLine(line)) {
        setClosed(true);
        return 0;
    }
    ++blockLineCount;
    if (offset == line.length()) {
        return -1;
    }
    String textileLine = offset == 0 ? line : line.substring(offset);
    Matcher rowMatcher = TABLE_ROW_PATTERN.matcher(textileLine);
    if (!rowMatcher.find()) {
        setClosed(true);
        return 0;
    }
    {
        TableRowAttributes rowAttributes = new TableRowAttributes();
        int rowStart = rowMatcher.start();
        if (rowStart > 0) {
            // if the row content starts somewhere in the line then it's likely
            // that we have some row-level attributes
            Matcher rowAttributesMatcher = rowAttributesPattern.matcher(textileLine);
            if (rowAttributesMatcher.matches()) {
                Textile.configureAttributes(rowAttributes, rowAttributesMatcher, 1, true);
            }
        }
        builder.beginBlock(BlockType.TABLE_ROW, rowAttributes);
    }
    do {
        int start = rowMatcher.start();
        if (start == textileLine.length() - 1) {
            break;
        }
        String colSpan = rowMatcher.group(1);
        String rowSpan = rowMatcher.group(2);
        String alignment = rowMatcher.group(3);
        String headerIndicator = rowMatcher.group(8);
        String text = rowMatcher.group(9);
        int textLineOffset = rowMatcher.start(9);
        // $NON-NLS-1$ //$NON-NLS-2$
        boolean header = headerIndicator != null && ("_".equals(headerIndicator) || "|".equals(headerIndicator));
        String textAlign = null;
        if (alignment != null) {
            if (alignment.equals("<>")) {
                // $NON-NLS-1$
                // $NON-NLS-1$
                textAlign = "text-align: center;";
            } else if (alignment.equals(">")) {
                // $NON-NLS-1$
                // $NON-NLS-1$
                textAlign = "text-align: right;";
            } else if (alignment.equals("<")) {
                // $NON-NLS-1$
                // $NON-NLS-1$
                textAlign = "text-align: left;";
            } else if (alignment.equals("^")) {
                // $NON-NLS-1$
                // $NON-NLS-1$
                textAlign = "vertical-align: top;";
            }
        }
        TableCellAttributes attributes = new TableCellAttributes();
        attributes.setCssStyle(textAlign);
        attributes.setRowspan(rowSpan);
        attributes.setColspan(colSpan);
        Textile.configureAttributes(attributes, rowMatcher, 4, false);
        state.setLineCharacterOffset(start);
        builder.beginBlock(header ? BlockType.TABLE_CELL_HEADER : BlockType.TABLE_CELL_NORMAL, attributes);
        markupLanguage.emitMarkupLine(getParser(), state, textLineOffset, CharMatcher.WHITESPACE.trimTrailingFrom(text), 0);
        // table cell
        builder.endBlock();
    } while (rowMatcher.find());
    // table row
    builder.endBlock();
    return -1;
}
Also used : TableRowAttributes(org.eclipse.mylyn.wikitext.parser.TableRowAttributes) Matcher(java.util.regex.Matcher) CharMatcher(com.google.common.base.CharMatcher) TableAttributes(org.eclipse.mylyn.wikitext.parser.TableAttributes) TableCellAttributes(org.eclipse.mylyn.wikitext.parser.TableCellAttributes)

Aggregations

TableRowAttributes (org.eclipse.mylyn.wikitext.parser.TableRowAttributes)5 Matcher (java.util.regex.Matcher)3 TableCellAttributes (org.eclipse.mylyn.wikitext.parser.TableCellAttributes)3 TableAttributes (org.eclipse.mylyn.wikitext.parser.TableAttributes)2 CharMatcher (com.google.common.base.CharMatcher)1 BlockType (org.eclipse.mylyn.wikitext.parser.DocumentBuilder.BlockType)1