use of com.vladsch.flexmark.util.sequence.BasedSequence in project flexmark-java by vsch.
the class TableBlockParser method split.
private static List<BasedSequence> split(BasedSequence input) {
BasedSequence line = input.trim();
int lineLength = line.length();
List<BasedSequence> segments = new ArrayList<BasedSequence>();
if (line.startsWith("|")) {
// segments.add(line.subSequence(0, 1));
line = line.subSequence(1, lineLength);
lineLength--;
}
boolean escape = false;
int lastPos = 0;
int cellChars = 0;
for (int i = 0; i < lineLength; i++) {
char c = line.charAt(i);
if (escape) {
escape = false;
cellChars++;
} else {
switch(c) {
case '\\':
escape = true;
// Removing the escaping '\' is handled by the inline parser later, so add it to cell
cellChars++;
break;
case '|':
segments.add(line.subSequence(lastPos, i));
// segments.add(line.subSequence(i, i + 1));
lastPos = i + 1;
cellChars = 0;
break;
default:
cellChars++;
}
}
}
if (cellChars > 0) {
segments.add(line.subSequence(lastPos, lineLength));
}
return segments;
}
use of com.vladsch.flexmark.util.sequence.BasedSequence in project flexmark-java by vsch.
the class TableBlockParser method parseInlines.
@Override
public void parseInlines(InlineParser inlineParser) {
Node section = new TableHead();
block.appendChild(section);
List<TableCell.Alignment> alignments = parseAlignment(separatorLine);
int rowNumber = 0;
int separatorColumns = alignments.size();
for (BasedSequence rowLine : block.getContentLines()) {
if (rowNumber == separatorLineNumber) {
section.setCharsFromContent();
section = new TableSeparator();
block.appendChild(section);
} else if (rowNumber == separatorLineNumber + 1) {
section.setCharsFromContent();
section = new TableBody();
block.appendChild(section);
}
List<BasedSequence> cells = split(rowLine);
TableRow tableRow = new TableRow(rowLine.trimEOL());
int rowCells = countCells(cells);
int maxColumns = rowCells;
if (bodyColumnsTruncatedToHead && maxColumns > separatorColumns)
maxColumns = separatorColumns;
if (rowNumber >= separatorLineNumber) {
if (!bodyColumnsFilledToHead && rowCells < maxColumns)
maxColumns = rowCells;
else if (bodyColumnsFilledToHead && maxColumns < separatorColumns)
maxColumns = separatorColumns;
}
int segmentOffset = 0;
BasedSequence openingMarker = BasedSequence.NULL;
for (int i = 0; i < maxColumns; i++) {
BasedSequence cell = i < rowCells ? cells.get(i + segmentOffset) : BasedSequence.NULL;
if (!isCell(cell)) {
openingMarker = cell;
segmentOffset++;
cell = i < rowCells ? cells.get(i + segmentOffset) : BasedSequence.NULL;
}
TableCell.Alignment alignment = i < alignments.size() ? alignments.get(i) : null;
TableCell tableCell = new TableCell();
tableCell.setHeader(rowNumber < separatorLineNumber);
tableCell.setAlignment(alignment);
tableCell.setOpeningMarker(openingMarker);
openingMarker = BasedSequence.NULL;
// if the next one is not a cell then it is our closing marker
if (i + segmentOffset + 1 < cells.size()) {
BasedSequence closingMarker = cells.get(i + segmentOffset + 1);
if (!isCell(closingMarker)) {
segmentOffset++;
tableCell.setClosingMarker(closingMarker);
}
}
BasedSequence trimmed = cell.trim();
tableCell.setText(trimmed);
inlineParser.parse(trimmed, tableCell);
tableCell.setCharsFromContent();
tableRow.appendChild(tableCell);
}
tableRow.setCharsFromContent();
section.appendChild(tableRow);
rowNumber++;
}
if (section instanceof TableSeparator) {
block.appendChild(new TableBody());
}
section.setCharsFromContent();
}
use of com.vladsch.flexmark.util.sequence.BasedSequence in project flexmark-java by vsch.
the class TaskListNodeFormatter method render.
private void render(final TaskListItem node, final NodeFormatterContext context, final MarkdownWriter markdown) {
BasedSequence markerSuffix = node.getMarkerSuffix();
switch(myOptions.taskListItemCase) {
case AS_IS:
break;
case LOWERCASE:
markerSuffix = markerSuffix.toLowerCase();
break;
case UPPERCASE:
markerSuffix = markerSuffix.toUpperCase();
break;
default:
throw new IllegalStateException("Missing case for TaskListItemCase " + myOptions.taskListItemCase.name());
}
if (node.isItemDoneMarker()) {
switch(myOptions.taskListItemPlacement) {
case AS_IS:
case INCOMPLETE_FIRST:
case INCOMPLETE_NESTED_FIRST:
break;
case COMPLETE_TO_NON_TASK:
case COMPLETE_NESTED_TO_NON_TASK:
markerSuffix = BasedSequence.NULL;
break;
default:
throw new IllegalStateException("Missing case for ListItemPlacement " + myOptions.taskListItemPlacement.name());
}
}
CoreNodeFormatter.renderListItem(node, context, markdown, listOptions, markerSuffix.isEmpty() ? markerSuffix : markerSuffix.append(" "));
}
use of com.vladsch.flexmark.util.sequence.BasedSequence in project flexmark-java by vsch.
the class TaskListNodeRenderer method render.
private void render(final TaskListItem node, final NodeRendererContext context, final HtmlWriter html) {
final BasedSequence sourceText = context.getHtmlOptions().sourcePositionParagraphLines || node.getFirstChild() == null ? node.getChars() : node.getFirstChild().getChars();
if (listOptions.isTightListItem(node)) {
if (!itemClass.isEmpty())
html.attr("class", itemClass);
html.srcPos(sourceText.getStartOffset(), sourceText.getEndOffset()).withAttr(CoreNodeRenderer.TIGHT_LIST_ITEM).withCondIndent().tagLine("li", new Runnable() {
@Override
public void run() {
html.raw(node.isItemDoneMarker() ? TaskListNodeRenderer.this.doneMarker : TaskListNodeRenderer.this.notDoneMarker);
context.renderChildren(node);
}
});
} else {
if (!looseItemClass.isEmpty())
html.attr("class", looseItemClass);
html.withAttr(CoreNodeRenderer.LOOSE_LIST_ITEM).tagIndent("li", new Runnable() {
@Override
public void run() {
if (!TaskListNodeRenderer.this.paragraphClass.isEmpty())
html.attr("class", TaskListNodeRenderer.this.paragraphClass);
html.srcPos(sourceText.getStartOffset(), sourceText.getEndOffset()).withAttr(TASK_ITEM_PARAGRAPH).tagLine("p", new Runnable() {
@Override
public void run() {
html.raw(node.isItemDoneMarker() ? TaskListNodeRenderer.this.doneMarker : TaskListNodeRenderer.this.notDoneMarker);
context.renderChildren(node);
}
});
}
});
}
}
use of com.vladsch.flexmark.util.sequence.BasedSequence in project flexmark-java by vsch.
the class GfmIssuesInlineParserExtension method parse.
@Override
public boolean parse(final InlineParser inlineParser) {
BasedSequence[] matches = inlineParser.matchWithGroups(GITHUB_ISSUE);
if (matches != null) {
BasedSequence input = inlineParser.getInput();
inlineParser.flushTextNode();
BasedSequence openMarker = matches[1];
BasedSequence text = matches[2];
GfmIssue gfmIssue = new GfmIssue(openMarker, text);
inlineParser.getBlock().appendChild(gfmIssue);
return true;
}
return false;
}
Aggregations