com.vladsch.flexmark.ast.HtmlBlock Java Examples

The following examples show how to use com.vladsch.flexmark.ast.HtmlBlock. 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: SmartFormat.java    From markdown-writer-fx with BSD 2-Clause "Simplified" License 5 votes vote down vote up
List<Pair<Block, String>> formatParagraphs(Node markdownAST, int wrapLength, IndexRange selection, HashSet<BasedSequence> oldParagraphs) {
	ArrayList<Pair<Block, String>> formattedParagraphs = new ArrayList<>();
	NodeVisitor visitor = new NodeVisitor(Collections.emptyList()) {
		@Override
		public void visit(Node node) {
			if (node instanceof Paragraph || node instanceof HtmlBlock) {
				if (selection != null && !isNodeSelected(node, selection))
					return;

				if (oldParagraphs != null && oldParagraphs.contains(node.getChars()))
					return; // ignore unmodified paragraphs

				String newText = (node instanceof Paragraph)
					? formatParagraph((Paragraph) node, wrapLength)
					: formatHtmlBlock((HtmlBlock) node, wrapLength);

				// append trailing line separator (if necessary)
				if (node.getChars().endsWith("\n"))
					newText += "\n";

				if (!node.getChars().equals(newText, false))
					formattedParagraphs.add(new Pair<>((Block) node, newText));
			} else
				visitChildren(node);
		}
	};
	visitor.visit(markdownAST);
	return formattedParagraphs;
}
 
Example #2
Source File: SmartFormat.java    From markdown-writer-fx with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private String formatHtmlBlock(HtmlBlock htmlBlock, int wrapLength) {
	String text = htmlBlock.getChars().toString();
	String[] lines = text.split("\n");
	for (int i = 0; i < lines.length; i++)
		lines[i] = formatText(lines[i], wrapLength, "", 0);
	return String.join("\n", lines);
}
 
Example #3
Source File: HtmlDocumentInterpreter.java    From camunda-bpm-swagger with Apache License 2.0 4 votes vote down vote up
Map<String, ParameterDescription> getParameterDescription(final HtmlBlock node) {
  return htmlNodeToMap(node);
}
 
Example #4
Source File: HtmlDocumentInterpreter.java    From camunda-bpm-swagger with Apache License 2.0 4 votes vote down vote up
String getText(final HtmlBlock node) {
  final Document document = Jsoup.parseBodyFragment(node.getChars().toString());
  return document.text();
}
 
Example #5
Source File: HtmlDocumentInterpreter.java    From camunda-bpm-swagger with Apache License 2.0 4 votes vote down vote up
private Map<String, ParameterDescription> htmlNodeToMap(final HtmlBlock htmlBlock) {
  final String htmlBlockBody = prepareHTML(htmlBlock);
  final Document document = Jsoup.parseBodyFragment(htmlBlockBody);
  final Elements trs = document.select("tr");
  Integer nameIdx = null;
  Integer descriptionIdx = null;
  Integer typeIdx = null;
  Integer requiredIdx = null;
  final Elements ths = trs.get(0).select("th");

  if(ths.size() == 0) {
    // Workaround for missing table header
    nameIdx = 0;
    switch(trs.get(0).select("td").size()) {
    case 2:
      descriptionIdx = 1;
      break;
    case 3:
      typeIdx = 1;
      descriptionIdx = 2;
      break;
    }
  }
  for (int i = 0; i < ths.size(); i++) {
    final Element element = ths.get(i);
    switch(element.text()) {
    case "Name":
    case "Code":
    case "Form Part Name":
      nameIdx = i;
      break;
    case "Description":
      descriptionIdx = i;
      break;
    case "Media type":
    case "Type":
    case "Content Type":
    case "Value":
      typeIdx = i;
      break;
    case "Required?":
      requiredIdx = i;
      break;
    default:
      log.debug("Fieldname unknown: " + element.text());
      break;
    }
  }
  final HashMap<String, ParameterDescription> result = new HashMap<>();
  for (final Element tr : trs) {
    final Elements tds = tr.select("td");
    if (tds.size() >= 2) {
      final ParameterDescription.ParameterDescriptionBuilder builder = ParameterDescription.builder();
      Optional.ofNullable(nameIdx).map(tds::get).map(Element::text).ifPresent(builder::id);
      Optional.ofNullable(descriptionIdx).map(tds::get).map(Element::text).ifPresent(builder::description);
      Optional.ofNullable(typeIdx).map(tds::get).map(Element::text).ifPresent(builder::type);
      Optional.ofNullable(requiredIdx).map(tds::get).map(Element::text).map(o -> o.equals("Yes")).ifPresent(builder::required);
      final ParameterDescription parameterDescription = builder.build();
      result.put(parameterDescription.getId(), parameterDescription);
    }
  }
  return result;
}
 
Example #6
Source File: HtmlDocumentInterpreter.java    From camunda-bpm-swagger with Apache License 2.0 4 votes vote down vote up
private String prepareHTML(final HtmlBlock htmlBlock) {
  return htmlBlock
    .getChars().toString()
    .replaceAll("\\{\\{[^}]+\\}\\}", "")
    .replaceAll("\\<\\/tr\\>[\n\t]+\\<tr\\>", "");
}
 
Example #7
Source File: MarkDownDocumentInterpreter.java    From camunda-bpm-swagger with Apache License 2.0 4 votes vote down vote up
HtmlBlock resolveHtmlNode(final Node node) {
  return getOpChildNode(node, HtmlBlock.class).orElse(null);
}
 
Example #8
Source File: MarkDownDocumentInterpreter.java    From camunda-bpm-swagger with Apache License 2.0 4 votes vote down vote up
void nodeToString(final Node node, final StringBuffer sb, final Boolean ignoreHtmlBlocks) {
  if (node != null) {
    switch (node.getClass().getSimpleName()) {
    case "Text":
      sb.append(node.getChars());
      break;
    case "Code":
      sb.append("`");
      nodeToString(node.getFirstChildAny(Text.class), sb, ignoreHtmlBlocks);
      sb.append("`");
      break;
    case "Paragraph":
      for (final Node childNode : node.getChildren()) {
        nodeToString(childNode, sb, ignoreHtmlBlocks);
      }
      break;
    case "SoftLineBreak":
      sb.append(" ");
      break;
    case "HtmlBlock":
      if (!ignoreHtmlBlocks) {
        sb.append(htmlInterpreter.getText((HtmlBlock) node));
        sb.append("\n");
      }
      break;
    case "LinkRef":
    case "Link":
      nodeToString(node.getFirstChildAny(Text.class), sb, ignoreHtmlBlocks);
      break;
    case "BulletList":
      sb.append(node.getChars().toString());
      break;
    case "StrongEmphasis":
    case "Emphasis":
      nodeToString(node.getFirstChildAny(Text.class), sb, ignoreHtmlBlocks);
      break;
    case "HtmlInline":
      if (node.getChars().toString().equals("<br/>") || node.getChars().toString().equals("</br>")) {
        sb.append("\n");
      }
      else {
        log.debug("unknown htmlInline element: (" + node.getChars().toString() + ")");

      }
      break;
    default:
      log.debug("class " + node.getClass().getSimpleName() + " not known: (" + node.getChars().toString() + ")");
    }
  }
}
 
Example #9
Source File: SmartFormat.java    From markdown-writer-fx with BSD 2-Clause "Simplified" License 4 votes vote down vote up
void format(boolean formatSelectionOnly, String oldMarkdown) {
	Node markdownAST = editor.getMarkdownAST();
	if (markdownAST == null)
		return;

	// find paragraphs in old markdown
	HashSet<BasedSequence> oldParagraphs = new HashSet<>();
	if (oldMarkdown != null) {
		Node oldMarkdownAST = editor.parseMarkdown(oldMarkdown);
		NodeVisitor visitor = new NodeVisitor(Collections.emptyList()) {
			@Override
			public void visit(Node node) {
				if (node instanceof Paragraph || node instanceof HtmlBlock) {
					oldParagraphs.add(node.getChars());
				} else
					visitChildren(node);
			}
		};
		visitor.visit(oldMarkdownAST);
	}

	IndexRange selectedLinesRange = formatSelectionOnly ? editor.getSmartEdit().getSelectedLinesRange(false) : null;
	IndexRange selection = textArea.getSelection();
	int wrapLength = Options.getWrapLineLength();

	// find and format paragraphs
	List<Pair<Block, String>> formattedParagraphs = formatParagraphs(markdownAST, wrapLength, selectedLinesRange, oldParagraphs);
	if (formattedParagraphs.isEmpty())
		return;

	// replace text of formatted paragraphs
	MultiChangeBuilder<?, ?, ?> multiChange = textArea.createMultiChange(formattedParagraphs.size());
	for (Pair<Block, String> pair : formattedParagraphs) {
		Block paragraph = pair.getFirst();
		String newText = pair.getSecond();

		int startOffset = paragraph.getStartOffset();
		int endOffset = paragraph.getEndOffset();

		multiChange.replaceText(startOffset, endOffset, newText);
	}
	SmartEdit.commitMultiChange(textArea, multiChange);

	// make sure that selection is not out of bounds if text becomes shorter
	SmartEdit.selectRange(textArea, Math.min(selection.getStart(), textArea.getLength()), Math.min(selection.getEnd(), textArea.getLength()));
}