use of com.vladsch.flexmark.ast.util.TextCollectingVisitor in project flexmark-java by vsch.
the class CoreNodeDocxRenderer method render.
private void render(final ImageRef node, final DocxRendererContext docx) {
ResolvedLink resolvedLink = null;
if (!node.isDefined() && recheckUndefinedReferences) {
if (node.getReferenceNode(referenceRepository) != null) {
node.setDefined(true);
}
}
Reference reference = null;
if (node.isDefined()) {
reference = node.getReferenceNode(referenceRepository);
String url = reference.getUrl().unescape();
resolvedLink = docx.resolveLink(LinkType.IMAGE, url, null, null);
if (reference.getTitle().isNotNull()) {
resolvedLink.getNonNullAttributes().replaceValue(Attribute.TITLE_ATTR, reference.getTitle().unescape());
} else {
resolvedLink.getNonNullAttributes().remove(Attribute.TITLE_ATTR);
}
} else {
// see if have reference resolver and this is resolved
String normalizeRef = referenceRepository.normalizeKey(node.getReference());
resolvedLink = docx.resolveLink(LinkType.IMAGE_REF, normalizeRef, null, null);
if (resolvedLink.getStatus() == UNKNOWN) {
resolvedLink = null;
}
}
if (resolvedLink == null) {
// empty ref, we treat it as text
docx.text(node.getChars().unescape());
if (options.logImageProcessing) {
System.out.println("render image ref of " + referenceRepository.normalizeKey(node.getReference()) + " skipped because it was not defined");
}
} else {
String altText = new TextCollectingVisitor().collectAndGetText(node);
String url = resolvedLink.getUrl();
Attributes attributes = resolvedLink.getNonNullAttributes();
if (!altText.isEmpty()) {
attributes.replaceValue("alt", altText);
}
if (reference != null) {
attributes = docx.extendRenderingNodeAttributes(reference, AttributablePart.NODE, attributes);
}
attributes = docx.extendRenderingNodeAttributes(AttributablePart.NODE, attributes);
renderImage(docx, url, attributes);
}
}
use of com.vladsch.flexmark.ast.util.TextCollectingVisitor in project flexmark-java by vsch.
the class CoreNodeDocxRenderer method render.
private void render(final Image node, final DocxRendererContext docx) {
String altText = new TextCollectingVisitor().collectAndGetText(node);
ResolvedLink resolvedLink = docx.resolveLink(LinkType.IMAGE, node.getUrl().unescape(), null, null);
String url = resolvedLink.getUrl();
Attributes attributes = resolvedLink.getNonNullAttributes();
if (!node.getUrlContent().isEmpty()) {
// reverse URL encoding of =, &
String content = Escaping.percentEncodeUrl(node.getUrlContent()).replace("+", "%2B").replace("%3D", "=").replace("%26", "&");
url += content;
}
if (!altText.isEmpty()) {
attributes.replaceValue("alt", altText);
}
attributes = docx.extendRenderingNodeAttributes(AttributablePart.NODE, attributes);
// String alt = node.getText().unescape();
renderImage(docx, url, attributes);
}
use of com.vladsch.flexmark.ast.util.TextCollectingVisitor in project flexmark-java by vsch.
the class MarkdownToText method main.
// use the PARSER to parse and RENDERER to parse pegdown indentation rules and render CommonMark
public static void main(String[] args) {
final String pegdown = "#Heading\n" + "-----\n" + "paragraph text \n" + "lazy continuation\n" + "\n" + "* list item\n" + " > block quote\n" + " lazy continuation\n" + "\n" + "~~~info\n" + " with uneven indent\n" + " with uneven indent\n" + " indented code\n" + "~~~ \n" + "\n" + " with uneven indent\n" + " with uneven indent\n" + " indented code\n" + "1. numbered item 1 \n" + "1. numbered item 2 \n" + "1. numbered item 3 \n" + " - bullet item 1 \n" + " - bullet item 2 \n" + " - bullet item 3 \n" + " 1. numbered sub-item 1 \n" + " 1. numbered sub-item 2 \n" + " 1. numbered sub-item 3 \n" + " \n" + " ~~~info\n" + " with uneven indent\n" + " with uneven indent\n" + " indented code\n" + " ~~~ \n" + " \n" + " with uneven indent\n" + " with uneven indent\n" + " indented code\n" + "";
System.out.println("pegdown\n");
System.out.println(pegdown);
Node document = PARSER.parse(pegdown);
TextCollectingVisitor textCollectingVisitor = new TextCollectingVisitor();
String text = textCollectingVisitor.collectAndGetText(document);
System.out.println("\n\nText\n");
System.out.println(text);
}
use of com.vladsch.flexmark.ast.util.TextCollectingVisitor in project flexmark-java by vsch.
the class TitleExtract method findTitle.
private static String findTitle(Node root) {
if (root instanceof Heading) {
Heading h = (Heading) root;
if (h.getLevel() == 1 && h.hasChildren()) {
TextCollectingVisitor collectingVisitor = new TextCollectingVisitor();
return collectingVisitor.collectAndGetText(h);
}
}
if (root instanceof Block && root.hasChildren()) {
Node child = root.getFirstChild();
while (child != null) {
String title = findTitle(child);
if (title != null) {
return title;
}
child = child.getNext();
}
}
return null;
}
use of com.vladsch.flexmark.ast.util.TextCollectingVisitor in project flexmark-java by vsch.
the class JiraConverterNodeRenderer method render.
private void render(Image node, NodeRendererContext context, HtmlWriter html) {
if (!context.isDoNotRenderLinks()) {
String altText = new TextCollectingVisitor().collectAndGetText(node);
ResolvedLink resolvedLink = context.resolveLink(LinkType.IMAGE, node.getUrl().unescape(), null);
html.raw("!").raw(resolvedLink.getUrl()).raw("!");
}
}
Aggregations