com.vladsch.flexmark.ast.Heading Java Examples

The following examples show how to use com.vladsch.flexmark.ast.Heading. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DocumentParser.java    From camunda-bpm-swagger with Apache License 2.0 6 votes vote down vote up
@SneakyThrows
public HashMap<String, Node> parse(final String fileContents) {
  final Node document = parser.parse(fileContents);
  final ReversiblePeekingIterable<Node> children = document.getChildren();
  final Stack<String> headingStack = new Stack<>();
  final HashMap<String, Node> documentTree = new HashMap<>();
  Paragraph subDocument = new Paragraph();
  documentTree.put("#", subDocument);
  for (final Node next : children) {
    final Optional<Paragraph> newSubDocument = resolveHeading(next)
      .map((Heading heading) -> {
        pushHeading(headingStack, heading);
        final String headingTitle = getHeadingTitle(headingStack);
        final Paragraph subDoc = new Paragraph();
        documentTree.put(headingTitle, subDoc);
        return subDoc;
      });
    if (newSubDocument.isPresent()) {
      subDocument = newSubDocument.get();
    }
    else {
      subDocument.appendChild(next);
    }
  }
  return documentTree;
}
 
Example #2
Source File: MarkdownView.java    From MarkdownView with Apache License 2.0 5 votes vote down vote up
@Override
public void setAttributes(final Node node, final AttributablePart part, final Attributes attributes) {
    if (node instanceof FencedCodeBlock) {
        if (part.getName().equals("NODE")) {
            String language = ((FencedCodeBlock) node).getInfo().toString();
            if (!TextUtils.isEmpty(language) &&
                    !language.equals("nohighlight")) {
                addJavascript(HIGHLIGHTJS);
                addJavascript(HIGHLIGHT_INIT);

                attributes.addValue("language", language);
                //attributes.addValue("onclick", String.format("javascript:android.onCodeTap('%s', this.textContent);",
                //        language));
            }
        }
    } else if (node instanceof MathJax) {
        addJavascript(MATHJAX);
        addJavascript(MATHJAX_CONFIG);
    } else if (node instanceof Abbreviation) {
        addJavascript(TOOLTIPSTER_JS);
        addStyleSheet(TOOLTIPSTER_CSS);
        addJavascript(TOOLTIPSTER_INIT);
        attributes.addValue("class", "tooltip");
    } else if (node instanceof Heading) {
        //attributes.addValue("onclick", String.format("javascript:android.onHeadingTap(%d, '%s');",
        //        ((Heading) node).getLevel(), ((Heading) node).getText()));
    } else if (node instanceof Image) {
        //attributes.addValue("onclick", String.format("javascript: android.onImageTap(this.src, this.clientWidth, this.clientHeight);"));
    } else if (node instanceof Mark) {
        //attributes.addValue("onclick", String.format("javascript: android.onMarkTap(this.textContent)"));
    } else if (node instanceof Keystroke) {
        //attributes.addValue("onclick", String.format("javascript: android.onKeystrokeTap(this.textContent)"));
    } else if (node instanceof Link ||
            node instanceof AutoLink) {
        //attributes.addValue("onclick", String.format("javascript: android.onLinkTap(this.href, this.textContent)"));
    }
}
 
Example #3
Source File: MarkdownResource.java    From sling-whiteboard with Apache License 2.0 5 votes vote down vote up
@Override
public boolean consume(Node n, Map<String, Object> p) {
	if ( n instanceof Heading ) {
		Heading h = (Heading) n;
		if ( h.getLevel() == 1 ) {
			p.put("jcr:title", h.getText().toString());
			return true;
		}
	}
	return false;
}
 
Example #4
Source File: DocumentParser.java    From camunda-bpm-swagger with Apache License 2.0 4 votes vote down vote up
private void pushHeading(final Stack<String> headingStack, final Heading heading) {
  clearHeadingStack(headingStack, heading.getLevel());
  headingStack.push(heading.getText().toString());
}
 
Example #5
Source File: DocumentParser.java    From camunda-bpm-swagger with Apache License 2.0 4 votes vote down vote up
private Optional<Heading> resolveHeading(final Node next) {
  if (!(next instanceof Heading))
    return Optional.empty();
  return Optional.of((Heading) next);
}
 
Example #6
Source File: FlexmarkPreviewRenderer.java    From markdown-writer-fx with BSD 2-Clause "Simplified" License 4 votes vote down vote up
private void printAttributes(StringBuilder buf, Node node) {
	if (node instanceof Heading)
		printAttribute(buf, "level", ((Heading)node).getLevel());
}
 
Example #7
Source File: SmartEdit.java    From markdown-writer-fx with BSD 2-Clause "Simplified" License 4 votes vote down vote up
private void updateStateProperties() {
	// avoid too many (and useless) runLater() invocations
	if (updateStatePropertiesRunLaterPending)
		return;
	updateStatePropertiesRunLaterPending = true;

	Platform.runLater(() -> {
		updateStatePropertiesRunLaterPending = false;

		List<Node> nodesAtSelection = findNodesAtSelection((s, e, n) -> true, true, false);

		boolean bold = false;
			boolean italic = false;
			boolean code = false;
			boolean link = false;
			boolean image = false;
			boolean unorderedList = false;
			boolean orderedList = false;
			boolean blockquote = false;
			boolean fencedCode = false;
			boolean header = false;
		for (Node node : nodesAtSelection) {
			if (!bold && node instanceof StrongEmphasis)
				bold = true;
			else if (!italic && node instanceof Emphasis)
				italic = true;
			else if (!code && node instanceof Code)
				code = true;
			else if (!link && (node instanceof Link || node instanceof LinkRef))
				link = true;
			else if (!image && (node instanceof Image || node instanceof ImageRef))
				image = true;
			else if (!unorderedList && node instanceof BulletListItem)
				unorderedList = true;
			else if (!orderedList && node instanceof OrderedListItem)
				orderedList = true;
			else if (!blockquote && node instanceof BlockQuote)
				blockquote = true;
			else if (!fencedCode && node instanceof FencedCodeBlock)
				fencedCode = true;
			else if (!header && node instanceof Heading)
				header = true;
		}
		this.bold.set(bold);
		this.italic.set(italic);
		this.code.set(code);
		this.link.set(link);
		this.image.set(image);
		this.unorderedList.set(unorderedList);
		this.orderedList.set(orderedList);
		this.blockquote.set(blockquote);
		this.fencedCode.set(fencedCode);
		this.header.set(header);
	});
}
 
Example #8
Source File: MarkdownParser.java    From sarl with Apache License 2.0 4 votes vote down vote up
/** Extract all the referencable objects from the given content.
 *
 * @param text the content to parse.
 * @return the referencables objects
 */
@SuppressWarnings("static-method")
protected ReferenceContext extractReferencableElements(String text) {
	final ReferenceContext context = new ReferenceContext();

	// Visit the links and record the transformations
	final MutableDataSet options = new MutableDataSet();
	final Parser parser = Parser.builder(options).build();
	final Node document = parser.parse(text);
	final Pattern pattern = Pattern.compile(SECTION_PATTERN_AUTONUMBERING);
	NodeVisitor visitor = new NodeVisitor(
			new VisitHandler<>(Paragraph.class, it -> {
				final CharSequence paragraphText = it.getContentChars();
				final Matcher matcher = pattern.matcher(paragraphText);
				if (matcher.find()) {
					final String number = matcher.group(2);
					final String title = matcher.group(3);
					final String key1 = computeHeaderId(number, title);
					final String key2 = computeHeaderId(null, title);
					context.registerSection(key1, key2, title);
				}
			}));
	visitor.visitChildren(document);

	final Pattern pattern1 = Pattern.compile(SECTION_PATTERN_TITLE_EXTRACTOR_WITHOUT_MD_PREFIX);
	visitor = new NodeVisitor(
			new VisitHandler<>(Heading.class, it -> {
				String key = it.getAnchorRefId();
				String title = it.getText().toString();
				// Sometimes, the title already contains the section number.
				// It should be removed.
				final Matcher matcher = pattern1.matcher(title);
				if (matcher.find()) {
					final String number = matcher.group(1);
					title = matcher.group(2);
					if (Strings.isEmpty(key)) {
						key = computeHeaderId(number, title);
					}
				}
				final String key2 = computeHeaderId(null, title);
				if (Strings.isEmpty(key)) {
					key = key2;
				}
				context.registerSection(key, key2, title);
			}));
	visitor.visitChildren(document);

	return context;
}