com.vladsch.flexmark.util.sequence.BasedSequence Java Examples

The following examples show how to use com.vladsch.flexmark.util.sequence.BasedSequence. 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: FlexmarkPreviewRenderer.java    From markdown-writer-fx with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public List<Range> findSequences(int startOffset, int endOffset) {
	ArrayList<Range> sequences = new ArrayList<>();

	Node astRoot = toAstRoot();
	if (astRoot == null)
		return sequences;

	NodeVisitor visitor = new NodeVisitor(Collections.emptyList()) {
		@Override
		public void visit(Node node) {
			BasedSequence chars = node.getChars();
			if (isInSequence(startOffset, endOffset, chars))
				sequences.add(new Range(chars.getStartOffset(), chars.getEndOffset()));

			for (BasedSequence segment : node.getSegments()) {
				if (isInSequence(startOffset, endOffset, segment))
					sequences.add(new Range(segment.getStartOffset(), segment.getEndOffset()));
			}

			visitChildren(node);
		}
	};
	visitor.visit(astRoot);
	return sequences;
}
 
Example #2
Source File: TwitterNodePostProcessor.java    From MarkdownView with Apache License 2.0 6 votes vote down vote up
@Override
public void process(NodeTracker state, Node node) {
    if (node instanceof Link) {
        Node previous = node.getPrevious();

        if (previous instanceof Text) {
            final BasedSequence chars = previous.getChars();

            //Se o nó anterior termina com '#' e é seguido pelo Link
            if (chars.endsWith("#") && chars.isContinuedBy(node.getChars())) {
                //Remove o caractere '#' do nó anterior.
                previous.setChars(chars.subSequence(0, chars.length() - 1));
                Twitter videoLink = new Twitter((Link) node);
                videoLink.takeChildren(node);
                node.unlink();
                previous.insertAfter(videoLink);
                state.nodeRemoved(node);
                state.nodeAddedWithChildren(videoLink);
            }
        }
    }
}
 
Example #3
Source File: MarkdownParser.java    From sarl with Apache License 2.0 5 votes vote down vote up
/** Compute the number of lines for reaching the given node.
 *
 * @param node the node.
 * @return the line number for the node.
 */
protected static int computeLineNo(Node node) {
	final int offset = node.getStartOffset();
	final BasedSequence seq = node.getDocument().getChars();
	int tmpOffset = seq.endOfLine(0);
	int lineno = 1;
	while (tmpOffset < offset) {
		++lineno;
		tmpOffset = seq.endOfLineAnyEOL(tmpOffset + seq.eolStartLength(tmpOffset));
	}
	return lineno;
}
 
Example #4
Source File: MarkdownSyntaxHighlighter.java    From markdown-writer-fx with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private boolean highlightSequence(BasedSequence sequence, String language) {
	SyntaxHighlighter.HighlightConsumer highlighter = new SyntaxHighlighter.HighlightConsumer() {
		private int index = sequence.getStartOffset();

		@Override
		public void accept(int length, String style) {
			if (style != null)
				addStyledRange(styleRanges, index, index + length, StyleClass.custom(style, "token"));
			index += length;
		}
	};
	String text = sequence.baseSubSequence(sequence.getStartOffset(), sequence.getEndOffset()).toString();
	return SyntaxHighlighter.highlight(text, language, highlighter);
}
 
Example #5
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 #6
Source File: Twitter.java    From MarkdownView with Apache License 2.0 5 votes vote down vote up
@Override
public void setTextChars(BasedSequence textChars) {
    int textCharsLength = textChars.length();
    textOpeningMarker = textChars.subSequence(0, 1);
    text = textChars.subSequence(1, textCharsLength - 1).trim();
    textClosingMarker = textChars.subSequence(textCharsLength - 1, textCharsLength);
}
 
Example #7
Source File: EmojiDelimiterProcessor.java    From onedev with MIT License 5 votes vote down vote up
@Override
public void process(Delimiter opener, Delimiter closer, int delimitersUsed) {
    // Normal case, wrap nodes between delimiters in emoji node.
    // don't allow any spaces between delimiters
    if (opener.getInput().subSequence(opener.getEndIndex(), closer.getStartIndex()).indexOfAny(BasedSequence.WHITESPACE_CHARS) == -1) {
        EmojiNode emoji = new EmojiNode(opener.getTailChars(delimitersUsed), BasedSequence.NULL, closer.getLeadChars(delimitersUsed));
        opener.moveNodesBetweenDelimitersTo(emoji, closer);
    } else {
        opener.convertDelimitersToText(delimitersUsed, closer);
    }
}
 
Example #8
Source File: VideoLink.java    From MarkdownView with Apache License 2.0 5 votes vote down vote up
@Override
public void setTextChars(BasedSequence textChars) {
    int textCharsLength = textChars.length();
    textOpeningMarker = textChars.subSequence(0, 1);
    text = textChars.subSequence(1, textCharsLength - 1).trim();
    textClosingMarker = textChars.subSequence(textCharsLength - 1, textCharsLength);
}
 
Example #9
Source File: SmartEdit.java    From markdown-writer-fx with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Find single node that completely encloses the current selection and match a predicate.
 */
private <T extends Node> T findNodeAtSelection(FindNodePredicate predicate) {
	IndexRange selection = textArea.getSelection();
	int start = selection.getStart();
	int end = selection.getEnd();
	List<T> nodes = findNodes(start, end, predicate, false, false);
	if (nodes.size() != 1)
		return null;

	T node = nodes.get(0);
	BasedSequence text = (node instanceof DelimitedNode) ? ((DelimitedNode)node).getText() : node.getChars();
	return (start >= text.getStartOffset() && end <= text.getEndOffset()) ? node : null;
}
 
Example #10
Source File: MarkdownParser.java    From sarl with Apache License 2.0 5 votes vote down vote up
/** Apply link transformation on the Markdown links.
 *
 * @param content the original content.
 * @param references the references into the document.
 * @return the result of the transformation.
 */
protected String transformMardownLinks(String content, ReferenceContext references) {
	if (!isMarkdownToHtmlReferenceTransformation()) {
		return content;
	}

	// Prepare replacement data structures
	final Map<BasedSequence, String> replacements = new TreeMap<>((cmp1, cmp2) -> {
		final int cmp = Integer.compare(cmp2.getStartOffset(), cmp1.getStartOffset());
		if (cmp != 0) {
			return cmp;
		}
		return Integer.compare(cmp2.getEndOffset(), cmp1.getEndOffset());
	});

	// Visit the links and record the transformations
	final MutableDataSet options = new MutableDataSet();
	final Parser parser = Parser.builder(options).build();
	final Node document = parser.parse(content);
	final NodeVisitor visitor = new NodeVisitor(
			new VisitHandler<>(Link.class, it -> {
				URL url = FileSystem.convertStringToURL(it.getUrl().toString(), true);
				url = transformURL(url, references);
				if (url != null) {
					replacements.put(it.getUrl(), convertURLToString(url));
				}
			}));
	visitor.visitChildren(document);

	// Apply the replacements
	if (!replacements.isEmpty()) {
		final StringBuilder buffer = new StringBuilder(content);
		for (final Entry<BasedSequence, String> entry : replacements.entrySet()) {
			final BasedSequence seq = entry.getKey();
			buffer.replace(seq.getStartOffset(), seq.getEndOffset(), entry.getValue());
		}
		return buffer.toString();
	}
	return content;
}
 
Example #11
Source File: EmojiDelimiterProcessor.java    From MarkdownView with Apache License 2.0 5 votes vote down vote up
@Override
public void process(Delimiter opener, Delimiter closer, int delimitersUsed) {
    // Normal case, wrap nodes between delimiters in emoji node.
    // don't allow any spaces between delimiters
    if (opener.getInput().subSequence(opener.getEndIndex(), closer.getStartIndex()).indexOfAny(BasedSequence.WHITESPACE_CHARS) == -1) {
        Emoji emoji = new Emoji(opener.getTailChars(delimitersUsed), BasedSequence.NULL, closer.getLeadChars(delimitersUsed));
        opener.moveNodesBetweenDelimitersTo(emoji, closer);
    } else {
        opener.convertDelimitersToText(delimitersUsed, closer);
    }
}
 
Example #12
Source File: FlexmarkPreviewRenderer.java    From markdown-writer-fx with BSD 2-Clause "Simplified" License 4 votes vote down vote up
private boolean isInSequence(int start, int end, BasedSequence sequence) {
	if (end == start)
		end++;
	return start < sequence.getEndOffset() && end > sequence.getStartOffset();
}
 
Example #13
Source File: Label.java    From MarkdownView with Apache License 2.0 4 votes vote down vote up
public Label(BasedSequence chars, String superscriptBlockText) {
    super(chars);
    this.superscriptBlockText = superscriptBlockText;
}
 
Example #14
Source File: Label.java    From MarkdownView with Apache License 2.0 4 votes vote down vote up
@Override
public BasedSequence[] getSegments() {
    return new BasedSequence[]{openingMarker, text, closingMarker};
}
 
Example #15
Source File: Label.java    From MarkdownView with Apache License 2.0 4 votes vote down vote up
public void setOpeningMarker(BasedSequence openingMarker) {
    this.openingMarker = openingMarker;
}
 
Example #16
Source File: Label.java    From MarkdownView with Apache License 2.0 4 votes vote down vote up
public BasedSequence getOpeningMarker() {
    return openingMarker;
}
 
Example #17
Source File: MathJax.java    From MarkdownView with Apache License 2.0 4 votes vote down vote up
public MathJax(BasedSequence chars, boolean isInline) {
    super(chars);
    this.isInline = isInline;
}
 
Example #18
Source File: Label.java    From MarkdownView with Apache License 2.0 4 votes vote down vote up
public BasedSequence getText() {
    return text;
}
 
Example #19
Source File: MathJaxDelimiterProcessor.java    From MarkdownView with Apache License 2.0 4 votes vote down vote up
@Override
public void process(Delimiter opener, Delimiter closer, int delimitersUsed) {
    // wrap nodes between delimiters in strikethrough.
    MathJax mj = new MathJax(opener.getTailChars(delimitersUsed), BasedSequence.NULL, closer.getLeadChars(delimitersUsed), delimitersUsed == 1);
    opener.moveNodesBetweenDelimitersTo(mj, closer);
}
 
Example #20
Source File: KeystrokeDelimiterProcessor.java    From MarkdownView with Apache License 2.0 4 votes vote down vote up
@Override
public void process(Delimiter opener, Delimiter closer, int delimitersUsed) {
    // wrap nodes between delimiters in strikethrough.
    Keystroke kbd = new Keystroke(opener.getTailChars(delimitersUsed), BasedSequence.NULL, closer.getLeadChars(delimitersUsed));
    opener.moveNodesBetweenDelimitersTo(kbd, closer);
}
 
Example #21
Source File: Mark.java    From MarkdownView with Apache License 2.0 4 votes vote down vote up
public void setClosingMarker(BasedSequence closingMarker) {
    this.closingMarker = closingMarker;
}
 
Example #22
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()));
}
 
Example #23
Source File: Mark.java    From MarkdownView with Apache License 2.0 4 votes vote down vote up
public void setText(BasedSequence text) {
    this.text = text;
}
 
Example #24
Source File: Mark.java    From MarkdownView with Apache License 2.0 4 votes vote down vote up
public BasedSequence getText() {
    return text;
}
 
Example #25
Source File: Mark.java    From MarkdownView with Apache License 2.0 4 votes vote down vote up
public void setOpeningMarker(BasedSequence openingMarker) {
    this.openingMarker = openingMarker;
}
 
Example #26
Source File: Mark.java    From MarkdownView with Apache License 2.0 4 votes vote down vote up
public BasedSequence getOpeningMarker() {
    return openingMarker;
}
 
Example #27
Source File: Mark.java    From MarkdownView with Apache License 2.0 4 votes vote down vote up
public Mark(BasedSequence chars, String superscriptBlockText) {
    super(chars);
    this.superscriptBlockText = superscriptBlockText;
}
 
Example #28
Source File: Mark.java    From MarkdownView with Apache License 2.0 4 votes vote down vote up
public Mark(BasedSequence openingMarker, BasedSequence text, BasedSequence closingMarker) {
    super(openingMarker.baseSubSequence(openingMarker.getStartOffset(), closingMarker.getEndOffset()));
    this.openingMarker = openingMarker;
    this.text = text;
    this.closingMarker = closingMarker;
}
 
Example #29
Source File: Bean.java    From MarkdownView with Apache License 2.0 4 votes vote down vote up
public Bean(BasedSequence chars) {
    super(chars);
}
 
Example #30
Source File: UMLBlockQuoteParser.java    From zeppelin with Apache License 2.0 4 votes vote down vote up
@Override
public void addLine(ParserState state, BasedSequence line) {
  content.add(line, state.getIndent());
}