use of com.vladsch.flexmark.util.sequence.BasedSequence in project flexmark-java by vsch.
the class AbbreviationNodeRenderer method render.
private void render(Abbreviation node, NodeRendererContext context, HtmlWriter html) {
String text = node.getChars().unescape();
BasedSequence abbreviation = node.getAbbreviation();
String tag;
if (options.useLinks) {
html.attr("href", "#");
tag = "a";
} else {
tag = "abbr";
}
html.attr("title", abbreviation);
html.srcPos(node.getChars()).withAttr().tag(tag);
html.text(text);
html.closeTag(tag);
}
use of com.vladsch.flexmark.util.sequence.BasedSequence in project flexmark-java by vsch.
the class AbbreviationParagraphPreProcessor method preProcessBlock.
@Override
public int preProcessBlock(Paragraph block, ParserState state) {
BasedSequence trySequence = block.getChars();
Matcher matcher = ABBREVIATION_BLOCK.matcher(trySequence);
int lastFound = 0;
while (matcher.find()) {
// abbreviation definition
if (matcher.start() != lastFound)
break;
lastFound = matcher.end();
int openingStart = matcher.start(1);
int openingEnd = matcher.end(1);
int textEnd = lastFound;
BasedSequence openingMarker = trySequence.subSequence(openingStart, openingStart + 2);
BasedSequence text = trySequence.subSequence(openingStart + 2, openingEnd - 2).trim();
BasedSequence closingMarker = trySequence.subSequence(openingEnd - 2, openingEnd);
AbbreviationBlock abbreviationBlock = new AbbreviationBlock();
abbreviationBlock.setOpeningMarker(openingMarker);
abbreviationBlock.setText(text);
abbreviationBlock.setClosingMarker(closingMarker);
abbreviationBlock.setAbbreviation(trySequence.subSequence(openingEnd, textEnd).trim());
abbreviationBlock.setCharsFromContent();
block.insertBefore(abbreviationBlock);
state.blockAdded(abbreviationBlock);
abbreviationMap.put(abbreviationMap.normalizeKey(abbreviationBlock.getText()), abbreviationBlock);
}
return lastFound;
}
use of com.vladsch.flexmark.util.sequence.BasedSequence in project flexmark-java by vsch.
the class AttributesInlineParserExtension method parse.
@Override
public boolean parse(final InlineParser inlineParser) {
if (inlineParser.peek(1) != '{') {
int index = inlineParser.getIndex();
BasedSequence input = inlineParser.getInput();
Matcher matcher = inlineParser.matcher(parsing.ATTRIBUTES_TAG);
if (matcher != null) {
BasedSequence attributesOpen = input.subSequence(matcher.start(), matcher.end());
// see what we have
// open, see if open/close
BasedSequence attributesText = input.subSequence(matcher.start(1), matcher.end(1));
AttributesNode attributes = new AttributesNode(attributesOpen.subSequence(0, 1), attributesText, attributesOpen.endSequence(1));
attributes.setCharsFromContent();
int leadingSpaces = attributesText.countLeading(" \t");
// give it to the text node
if (leadingSpaces > 0) {
inlineParser.appendText(attributesText, 0, leadingSpaces);
attributesText = attributesText.subSequence(leadingSpaces);
}
inlineParser.flushTextNode();
inlineParser.getBlock().appendChild(attributes);
BasedSequence attributeText = attributesText.trim();
if (!attributeText.isEmpty()) {
// have some attribute text
// parse attributes
Matcher attributeMatcher = parsing.ATTRIBUTE.matcher(attributeText);
while (attributeMatcher.find()) {
BasedSequence attributeName = attributeText.subSequence(attributeMatcher.start(1), attributeMatcher.end(1));
BasedSequence attributeSeparator = attributeMatcher.groupCount() == 1 || attributeMatcher.start(2) == -1 ? BasedSequence.NULL : attributeText.subSequence(attributeMatcher.end(1), attributeMatcher.start(2)).trim();
BasedSequence attributeValue = attributeMatcher.groupCount() == 1 || attributeMatcher.start(2) == -1 ? BasedSequence.NULL : attributeText.subSequence(attributeMatcher.start(2), attributeMatcher.end(2));
boolean isQuoted = attributeValue.length() >= 2 && (attributeValue.charAt(0) == '"' && attributeValue.endCharAt(1) == '"' || attributeValue.charAt(0) == '\'' && attributeValue.endCharAt(1) == '\'');
BasedSequence attributeOpen = !isQuoted ? BasedSequence.NULL : attributeValue.subSequence(0, 1);
BasedSequence attributeClose = !isQuoted ? BasedSequence.NULL : attributeValue.endSequence(1, 0);
if (isQuoted) {
attributeValue = attributeValue.midSequence(1, -1);
}
AttributeNode attribute;
if (attributeSeparator.isNull() && attributeSeparator.isNull() && attributeValue.isNull() && AttributeNode.isImplicitName(attributeName)) {
attribute = new AttributeNode(attributeName.subSequence(0, 1), attributeSeparator, attributeOpen, attributeName.subSequence(1), attributeClose);
} else {
attribute = new AttributeNode(attributeName, attributeSeparator, attributeOpen, attributeValue, attributeClose);
}
attributes.appendChild(attribute);
}
return true;
}
// did not process, reset to where we started
inlineParser.setIndex(index);
}
}
return false;
}
use of com.vladsch.flexmark.util.sequence.BasedSequence in project flexmark-java by vsch.
the class CoreNodeFormatter method render.
private void render(IndentedCodeBlock node, NodeFormatterContext context, MarkdownWriter markdown) {
markdown.blankLine();
String prefix = RepeatedCharSequence.of(" ", listOptions.getCodeIndent()).toString();
if (options.emulationProfile == ParserEmulationProfile.GITHUB_DOC) {
if (node.getParent() instanceof ListItem) {
BasedSequence marker = ((ListItem) node.getParent()).getOpeningMarker();
prefix = RepeatedCharSequence.of(" ", Utils.minLimit(8 - marker.length() - 1, 4)).toString();
}
}
markdown.pushPrefix().addPrefix(prefix);
markdown.openPreFormatted(true);
if (options.indentedCodeMinimizeIndent) {
List<BasedSequence> lines = node.getContentLines();
int[] leadColumns = new int[lines.size()];
int minSpaces = Integer.MAX_VALUE;
int i = 0;
for (BasedSequence line : lines) {
leadColumns[i] = line.countLeadingColumns(0, " \t");
minSpaces = Utils.min(minSpaces, leadColumns[i]);
i++;
}
if (minSpaces > 0) {
i = 0;
// markdown.append(out);
for (BasedSequence line : lines) {
if (leadColumns[i] > minSpaces)
markdown.repeat(' ', leadColumns[i] - minSpaces);
markdown.append(line.trimStart());
i++;
}
} else {
markdown.append(node.getContentChars());
}
} else {
markdown.append(node.getContentChars());
}
markdown.closePreFormatted();
markdown.popPrefix();
markdown.tailBlankLine();
}
use of com.vladsch.flexmark.util.sequence.BasedSequence in project flexmark-java by vsch.
the class CoreNodeFormatter method render.
private void render(FencedCodeBlock node, NodeFormatterContext context, MarkdownWriter markdown) {
markdown.blankLine();
CharSequence openingMarker = node.getOpeningMarker();
CharSequence closingMarker = node.getClosingMarker();
char openingMarkerChar = openingMarker.charAt(0);
char closingMarkerChar = closingMarker.length() > 0 ? closingMarker.charAt(0) : '\0';
int openingMarkerLen = openingMarker.length();
int closingMarkerLen = closingMarker.length();
switch(options.fencedCodeMarkerType) {
case ANY:
break;
case BACK_TICK:
openingMarkerChar = '`';
closingMarkerChar = openingMarkerChar;
break;
case TILDE:
openingMarkerChar = '~';
closingMarkerChar = openingMarkerChar;
break;
}
if (openingMarkerLen < options.fencedCodeMarkerLength)
openingMarkerLen = options.fencedCodeMarkerLength;
if (closingMarkerLen < options.fencedCodeMarkerLength)
closingMarkerLen = options.fencedCodeMarkerLength;
openingMarker = RepeatedCharSequence.of(String.valueOf(openingMarkerChar), openingMarkerLen);
if (options.fencedCodeMatchClosingMarker || closingMarkerChar == '\0')
closingMarker = openingMarker;
else
closingMarker = RepeatedCharSequence.of(String.valueOf(closingMarkerChar), closingMarkerLen);
markdown.append(openingMarker);
if (options.fencedCodeSpaceBeforeInfo)
markdown.append(' ');
markdown.append(node.getInfo());
markdown.line();
markdown.openPreFormatted(true);
if (options.fencedCodeMinimizeIndent) {
List<BasedSequence> lines = node.getContentLines();
int[] leadColumns = new int[lines.size()];
int minSpaces = Integer.MAX_VALUE;
int i = 0;
for (BasedSequence line : lines) {
leadColumns[i] = line.countLeadingColumns(0, " \t");
minSpaces = Utils.min(minSpaces, leadColumns[i]);
i++;
}
ArrayList<BasedSequence> trimmedLines = new ArrayList<BasedSequence>();
if (minSpaces > 0) {
i = 0;
// markdown.append(out);
for (BasedSequence line : lines) {
if (leadColumns[i] > minSpaces)
markdown.repeat(' ', leadColumns[i] - minSpaces);
markdown.append(line.trimStart());
i++;
}
} else {
markdown.append(node.getContentChars());
}
} else {
markdown.append(node.getContentChars());
}
markdown.closePreFormatted();
markdown.line().append(closingMarker).line();
markdown.tailBlankLine();
}
Aggregations