Search in sources :

Example 61 with BasedSequence

use of com.vladsch.flexmark.util.sequence.BasedSequence in project flexmark-java by vsch.

the class SpecExampleNodeRenderer method render.

private void render(SpecExampleAst node, NodeRendererContext context, HtmlWriter html) {
    BasedSequence text = node.getChars();
    switch(options.renderAs) {
        case DEFINITION_LIST:
            html.tag("dt").text("AST").tag("/dt").line();
            html.tag("dd");
            render(text, "text", context, html);
            html.tag("/dd").line();
            break;
        case SECTIONS:
            if (!text.isEmpty()) {
                html.tagVoidLine("hr");
                render(text, "text", context, html);
            }
            break;
        case FENCED_CODE:
        default:
            break;
    }
}
Also used : BasedSequence(com.vladsch.flexmark.util.sequence.BasedSequence)

Example 62 with BasedSequence

use of com.vladsch.flexmark.util.sequence.BasedSequence in project flexmark-java by vsch.

the class SpecExampleNodeRenderer method render.

private void render(SpecExampleSource node, NodeRendererContext context, HtmlWriter html) {
    BasedSequence text = node.getChars();
    switch(options.renderAs) {
        case DEFINITION_LIST:
            html.tag("dt").text("Source").tag("/dt").line();
            html.tag("dd");
            render(text, "markdown", context, html);
            html.tag("/dd").line();
            break;
        case SECTIONS:
            if (!text.isEmpty()) {
                html.tagVoidLine("hr");
                render(text, "markdown", context, html);
            }
            break;
        case FENCED_CODE:
        default:
            break;
    }
}
Also used : BasedSequence(com.vladsch.flexmark.util.sequence.BasedSequence)

Example 63 with BasedSequence

use of com.vladsch.flexmark.util.sequence.BasedSequence in project flexmark-java by vsch.

the class JekyllTagInlineParserExtension method parse.

@Override
public boolean parse(final InlineParser inlineParser) {
    if (inlineParser.peek(1) == '%' && (inlineParser.peek(2) == ' ' || inlineParser.peek(2) == '\t')) {
        BasedSequence input = inlineParser.getInput();
        Matcher matcher = inlineParser.matcher(parsing.MACRO_TAG);
        if (matcher != null) {
            BasedSequence tag = input.subSequence(matcher.start(), matcher.end());
            BasedSequence tagName = input.subSequence(matcher.start(1), matcher.end(1));
            BasedSequence parameters = input.subSequence(matcher.end(1), matcher.end() - 2).trim();
            JekyllTag macro = new JekyllTag(tag.subSequence(0, 2), tagName, parameters, tag.endSequence(2));
            macro.setCharsFromContent();
            if (!listIncludesOnly || tagName.equals("includes")) {
                List<JekyllTag> tagList = JekyllTagExtension.TAG_LIST.getFrom(inlineParser.getDocument());
                tagList.add(macro);
            }
            inlineParser.flushTextNode();
            inlineParser.getBlock().appendChild(macro);
            return true;
        }
    }
    return false;
}
Also used : Matcher(java.util.regex.Matcher) BasedSequence(com.vladsch.flexmark.util.sequence.BasedSequence) JekyllTag(com.vladsch.flexmark.ext.jekyll.tag.JekyllTag)

Example 64 with BasedSequence

use of com.vladsch.flexmark.util.sequence.BasedSequence in project flexmark-java by vsch.

the class Table method finalizeTable.

public void finalizeTable() {
    // remove null cells
    heading.cleanup();
    body.cleanup();
    if (options.fillMissingColumns) {
        int minColumns = getMinColumns();
        int maxColumns = getMaxColumns();
        if (minColumns < maxColumns) {
            // add empty cells to rows that have less
            TableCell empty = new TableCell("", 1, 1);
            for (TableRow row : heading.rows) {
                row.expandTo(maxColumns - 1, empty);
            }
            for (TableRow row : body.rows) {
                row.expandTo(maxColumns - 1, empty);
            }
        }
    }
    int sepColumns = getMaxColumns();
    alignments = new CellAlignment[sepColumns];
    columnWidths = new int[sepColumns];
    BitSet spanAlignment = new BitSet(sepColumns);
    List<ColumnSpan> columnSpans = new ArrayList<ColumnSpan>();
    Ref<Integer> delta = new Ref<Integer>(0);
    if (separator.rows.size() > 0) {
        TableRow row = separator.rows.get(0);
        int j = 0;
        int jSpan = 0;
        delta.value = 0;
        for (TableCell cell : row.cells) {
            // set alignment if not already set or was set by a span and this column is not a span
            if ((alignments[jSpan] == null || cell.columnSpan == 1 && spanAlignment.get(jSpan)) && cell.alignment != CellAlignment.NONE) {
                alignments[jSpan] = cell.alignment;
                if (cell.columnSpan > 1)
                    spanAlignment.set(jSpan);
            }
            j++;
            jSpan += cell.columnSpan;
        }
    }
    if (heading.rows.size() > 0) {
        int i = 0;
        for (TableRow row : heading.rows) {
            int j = 0;
            int jSpan = 0;
            delta.value = 0;
            for (TableCell cell : row.cells) {
                // set alignment if not already set or was set by a span and this column is not a span
                if ((alignments[jSpan] == null || cell.columnSpan == 1 && spanAlignment.get(jSpan)) && cell.alignment != CellAlignment.NONE) {
                    alignments[jSpan] = cell.alignment;
                    if (cell.columnSpan > 1)
                        spanAlignment.set(jSpan);
                }
                BasedSequence cellText = cellText(cell.text, true, 0, null, delta);
                int width = options.charWidthProvider.charWidth(cellText) + options.spacePad + options.pipeWidth * cell.columnSpan;
                if (cell.columnSpan > 1) {
                    columnSpans.add(new ColumnSpan(j, cell.columnSpan, width));
                } else {
                    if (columnWidths[jSpan] < width)
                        columnWidths[jSpan] = width;
                }
                j++;
                jSpan += cell.columnSpan;
            }
            i++;
        }
    }
    if (body.rows.size() > 0) {
        int i = 0;
        delta.value = 0;
        for (TableRow row : body.rows) {
            int j = 0;
            int jSpan = 0;
            for (TableCell cell : row.cells) {
                BasedSequence cellText = cellText(cell.text, false, 0, null, delta);
                int width = options.charWidthProvider.charWidth(cellText) + options.spacePad + options.pipeWidth * cell.columnSpan;
                if (cell.columnSpan > 1) {
                    columnSpans.add(new ColumnSpan(jSpan, cell.columnSpan, width));
                } else {
                    if (columnWidths[jSpan] < width)
                        columnWidths[jSpan] = width;
                }
                j++;
                jSpan += cell.columnSpan;
            }
            i++;
        }
    }
    // add separator column widths to the calculation
    if (separator.rows.size() == 0 || body.rows.size() > 0 || heading.rows.size() > 0) {
        int j = 0;
        delta.value = 0;
        for (CellAlignment alignment : alignments) {
            CellAlignment alignment1 = adjustCellAlignment(alignment);
            int colonCount = alignment1 == CellAlignment.LEFT || alignment1 == CellAlignment.RIGHT ? 1 : alignment1 == CellAlignment.CENTER ? 2 : 0;
            int dashCount = 0;
            int dashesOnly = Utils.minLimit(dashCount, options.minSeparatorColumnWidth - colonCount, options.minSeparatorDashes);
            if (dashCount < dashesOnly)
                dashCount = dashesOnly;
            int width = dashCount * options.dashWidth + colonCount * options.colonWidth + options.pipeWidth;
            if (columnWidths[j] < width)
                columnWidths[j] = width;
            j++;
        }
    } else {
        // keep as is
        int j = 0;
        delta.value = 0;
        for (TableCell cell : separator.rows.get(0).cells) {
            CellAlignment alignment = adjustCellAlignment(cell.alignment);
            int colonCount = alignment == CellAlignment.LEFT || alignment == CellAlignment.RIGHT ? 1 : alignment == CellAlignment.CENTER ? 2 : 0;
            int dashCount = cell.text.trim(":").length();
            int dashesOnly = Utils.minLimit(dashCount, options.minSeparatorColumnWidth - colonCount, options.minSeparatorDashes);
            if (dashCount < dashesOnly)
                dashCount = dashesOnly;
            int width = dashCount * options.dashWidth + colonCount * options.colonWidth + options.pipeWidth;
            if (columnWidths[j] < width)
                columnWidths[j] = width;
            j++;
        }
    }
    if (!columnSpans.isEmpty()) {
        // now need to distribute extra width from spans to contained columns
        int[] additionalWidths = new int[sepColumns];
        BitSet unfixedColumns = new BitSet(sepColumns);
        List<ColumnSpan> newColumnSpans = new ArrayList<ColumnSpan>(columnSpans.size());
        for (ColumnSpan columnSpan : columnSpans) {
            int spanWidth = spanWidth(columnSpan.startColumn, columnSpan.columnSpan);
            if (spanWidth < columnSpan.width) {
                // not all fits, need to distribute the remainder
                unfixedColumns.set(columnSpan.startColumn, columnSpan.startColumn + columnSpan.columnSpan);
                newColumnSpans.add(columnSpan);
            }
        }
        // we now distribute additional width equally between columns that are spanned to unfixed columns
        while (!newColumnSpans.isEmpty()) {
            columnSpans = newColumnSpans;
            BitSet fixedColumns = new BitSet(sepColumns);
            newColumnSpans.clear();
            // remove spans that already fit into fixed columns
            for (ColumnSpan columnSpan : columnSpans) {
                int spanWidth = spanWidth(columnSpan.startColumn, columnSpan.columnSpan);
                int fixedWidth = spanFixedWidth(unfixedColumns, columnSpan.startColumn, columnSpan.columnSpan);
                if (spanWidth <= fixedWidth) {
                    fixedColumns.set(columnSpan.startColumn, columnSpan.startColumn + columnSpan.columnSpan);
                } else {
                    newColumnSpans.add(columnSpan);
                }
            }
            // reset fixed columns
            unfixedColumns.andNot(fixedColumns);
            columnSpans = newColumnSpans;
            newColumnSpans.clear();
            for (ColumnSpan columnSpan : columnSpans) {
                int spanWidth = spanWidth(columnSpan.startColumn, columnSpan.columnSpan);
                int fixedWidth = spanFixedWidth(unfixedColumns, columnSpan.startColumn, columnSpan.columnSpan);
                if (spanWidth > fixedWidth) {
                    // not all fits, need to distribute the remainder to unfixed columns
                    int distributeWidth = spanWidth - fixedWidth;
                    int unfixedColumnCount = unfixedColumns.get(columnSpan.startColumn, columnSpan.startColumn + columnSpan.columnSpan).cardinality();
                    int perSpanWidth = distributeWidth / unfixedColumnCount;
                    int extraWidth = distributeWidth - perSpanWidth * unfixedColumnCount;
                    for (int i = 0; i < columnSpan.columnSpan; i++) {
                        if (unfixedColumns.get(columnSpan.startColumn + i)) {
                            columnWidths[columnSpan.startColumn + i] += perSpanWidth;
                            if (extraWidth > 0) {
                                columnWidths[columnSpan.startColumn + i]++;
                                extraWidth--;
                            }
                        }
                    }
                    newColumnSpans.add(columnSpan);
                }
            }
        }
    }
}
Also used : BasedSequence(com.vladsch.flexmark.util.sequence.BasedSequence) BitSet(java.util.BitSet) ArrayList(java.util.ArrayList) CellAlignment(com.vladsch.flexmark.util.html.CellAlignment) Ref(com.vladsch.flexmark.util.Ref)

Example 65 with BasedSequence

use of com.vladsch.flexmark.util.sequence.BasedSequence in project flexmark-java by vsch.

the class Table method cellText.

public BasedSequence cellText(CharSequence chars, final boolean isHeader, int width, CellAlignment alignment, Ref<Integer> accumulatedDelta) {
    BasedSequence text = BasedSequenceImpl.of(chars);
    final int length = options.charWidthProvider.charWidth(text);
    if (length < width && options.adjustColumnWidth) {
        if (!options.applyColumnAlignment || alignment == null || alignment == CellAlignment.NONE)
            alignment = isHeader ? CellAlignment.CENTER : CellAlignment.LEFT;
        int diff = width - length;
        int spaceCount = diff / options.spaceWidth;
        if (accumulatedDelta.value * 2 >= options.spaceWidth) {
            spaceCount++;
            accumulatedDelta.value -= options.spaceWidth;
        }
        switch(alignment) {
            case LEFT:
                text = text.append(PrefixedSubSequence.repeatOf(" ", spaceCount, text.subSequence(0, 0)));
                break;
            case RIGHT:
                text = PrefixedSubSequence.repeatOf(" ", spaceCount, text);
                break;
            case CENTER:
                int count = spaceCount / 2;
                text = PrefixedSubSequence.repeatOf(" ", count, text).append(PrefixedSubSequence.repeatOf(" ", spaceCount - count, text.subSequence(0, 0)));
                break;
        }
    }
    return text;
}
Also used : BasedSequence(com.vladsch.flexmark.util.sequence.BasedSequence)

Aggregations

BasedSequence (com.vladsch.flexmark.util.sequence.BasedSequence)91 Matcher (java.util.regex.Matcher)13 Node (com.vladsch.flexmark.ast.Node)6 ArrayList (java.util.ArrayList)5 MacroClose (com.vladsch.flexmark.ext.xwiki.macros.MacroClose)3 ReplacedTextMapper (com.vladsch.flexmark.util.sequence.ReplacedTextMapper)3 Text (com.vladsch.flexmark.ast.Text)2 AttributesNode (com.vladsch.flexmark.ext.attributes.AttributesNode)2 FootnoteBlock (com.vladsch.flexmark.ext.footnotes.FootnoteBlock)2 Macro (com.vladsch.flexmark.ext.xwiki.macros.Macro)2 Pair (com.vladsch.flexmark.util.Pair)2 RepeatedCharSequence (com.vladsch.flexmark.util.sequence.RepeatedCharSequence)2 Block (com.vladsch.flexmark.ast.Block)1 BulletListItem (com.vladsch.flexmark.ast.BulletListItem)1 Link (com.vladsch.flexmark.ast.Link)1 ListItem (com.vladsch.flexmark.ast.ListItem)1 NodeIterator (com.vladsch.flexmark.ast.NodeIterator)1 OrderedListItem (com.vladsch.flexmark.ast.OrderedListItem)1 Parsing (com.vladsch.flexmark.ast.util.Parsing)1 TextCollectingVisitor (com.vladsch.flexmark.ast.util.TextCollectingVisitor)1