Search in sources :

Example 6 with TextSegment

use of org.eclipse.mylyn.wikitext.commonmark.internal.TextSegment in project mylyn.docs by eclipse.

the class AtxHeaderBlock method process.

@Override
public void process(ProcessingContext context, DocumentBuilder builder, LineSequence lineSequence) {
    Line currentLine = lineSequence.getCurrentLine();
    Matcher matcher = PATTERN.matcher(currentLine.getText());
    checkState(matcher.matches());
    lineSequence.advance();
    builder.setLocator(currentLine.toLocator());
    int contentOffset = matcher.start(2);
    int contentEnd = matcher.end(2);
    int headingLevel = headingLevel(matcher);
    if (contentEnd > contentOffset) {
        Line headerContent = currentLine.segment(contentOffset, contentEnd - contentOffset);
        TextSegment textSegment = new TextSegment(Collections.singletonList(headerContent));
        HeadingAttributes attributes = new HeadingAttributes();
        InlineParser inlineParser = context.getInlineParser();
        String headingText = inlineParser.toStringContent(context, textSegment);
        attributes.setId(context.generateHeadingId(headingLevel, headingText));
        builder.beginHeading(headingLevel, attributes);
        inlineParser.emit(context, builder, textSegment);
        builder.endHeading();
    } else {
        builder.beginHeading(headingLevel, new HeadingAttributes());
        builder.endHeading();
    }
}
Also used : Line(org.eclipse.mylyn.wikitext.commonmark.internal.Line) Matcher(java.util.regex.Matcher) HeadingAttributes(org.eclipse.mylyn.wikitext.parser.HeadingAttributes) InlineParser(org.eclipse.mylyn.wikitext.commonmark.internal.inlines.InlineParser) TextSegment(org.eclipse.mylyn.wikitext.commonmark.internal.TextSegment)

Example 7 with TextSegment

use of org.eclipse.mylyn.wikitext.commonmark.internal.TextSegment in project mylyn.docs by eclipse.

the class ParagraphBlock method processInlines.

void processInlines(ProcessingContext context, DocumentBuilder builder, LineSequence lineSequence, boolean asBlock) {
    TextSegment textSegment = extractTextSegment(lineSequence);
    List<Inline> inlines = context.getInlineParser().parse(context, textSegment);
    if (!emptyParagraph(inlines)) {
        builder.setLocator(textSegment.getLines().get(0).toLocator());
        if (asBlock) {
            builder.beginBlock(BlockType.PARAGRAPH, new Attributes());
        }
        InlineParser.emit(builder, inlines);
        if (asBlock) {
            builder.endBlock();
        }
    }
}
Also used : Attributes(org.eclipse.mylyn.wikitext.parser.Attributes) TextSegment(org.eclipse.mylyn.wikitext.commonmark.internal.TextSegment) Inline(org.eclipse.mylyn.wikitext.commonmark.internal.inlines.Inline)

Example 8 with TextSegment

use of org.eclipse.mylyn.wikitext.commonmark.internal.TextSegment in project mylyn.docs by eclipse.

the class AbstractSourceSpanTest method assertParseToHtml.

public void assertParseToHtml(String expected, String markup) {
    StringWriter writer = new StringWriter();
    HtmlDocumentBuilder builder = new SimplifiedHtmlDocumentBuilder(writer);
    builder.setEmitAsDocument(false);
    builder.beginDocument();
    InlineParser parser = new InlineParser(span, new AllCharactersSpan());
    List<Inline> inlines = parser.parse(ProcessingContext.builder().build(), new TextSegment(ImmutableList.of(new Line(1, 0, markup))));
    for (Inline inline : inlines) {
        inline.emit(builder);
    }
    builder.endDocument();
    assertEquals(expected, writer.toString());
}
Also used : Line(org.eclipse.mylyn.wikitext.commonmark.internal.Line) StringWriter(java.io.StringWriter) SimplifiedHtmlDocumentBuilder(org.eclipse.mylyn.wikitext.commonmark.internal.spec.SimplifiedHtmlDocumentBuilder) TextSegment(org.eclipse.mylyn.wikitext.commonmark.internal.TextSegment) HtmlDocumentBuilder(org.eclipse.mylyn.wikitext.parser.builder.HtmlDocumentBuilder) SimplifiedHtmlDocumentBuilder(org.eclipse.mylyn.wikitext.commonmark.internal.spec.SimplifiedHtmlDocumentBuilder)

Example 9 with TextSegment

use of org.eclipse.mylyn.wikitext.commonmark.internal.TextSegment in project mylyn.docs by eclipse.

the class InlineParserTest method assertParse.

private void assertParse(String content, Inline... inlines) {
    List<Inline> expected = Arrays.asList(inlines);
    List<Inline> actual = createInlines().parse(ProcessingContext.builder().build(), new TextSegment(LineSequence.create(content)));
    for (int x = 0; x < expected.size() && x < actual.size(); ++x) {
        assertEquivalent(x, expected.get(x), actual.get(x));
    }
    assertEquals(expected, actual);
}
Also used : TextSegment(org.eclipse.mylyn.wikitext.commonmark.internal.TextSegment)

Example 10 with TextSegment

use of org.eclipse.mylyn.wikitext.commonmark.internal.TextSegment in project mylyn.docs by eclipse.

the class InlineParserTest method toStringContent.

@Test
public void toStringContent() {
    InlineParser parser = new InlineParser(new CodeSpan(), new AllCharactersSpan());
    String stringContent = parser.toStringContent(ProcessingContext.builder().build(), new TextSegment(Collections.singletonList(new Line(1, 0, "one `two` three"))));
    assertEquals("one two three", stringContent);
}
Also used : Line(org.eclipse.mylyn.wikitext.commonmark.internal.Line) TextSegment(org.eclipse.mylyn.wikitext.commonmark.internal.TextSegment) Test(org.junit.Test)

Aggregations

TextSegment (org.eclipse.mylyn.wikitext.commonmark.internal.TextSegment)10 Line (org.eclipse.mylyn.wikitext.commonmark.internal.Line)7 InlineParser (org.eclipse.mylyn.wikitext.commonmark.internal.inlines.InlineParser)3 Matcher (java.util.regex.Matcher)2 HeadingAttributes (org.eclipse.mylyn.wikitext.parser.HeadingAttributes)2 Test (org.junit.Test)2 StringWriter (java.io.StringWriter)1 Inline (org.eclipse.mylyn.wikitext.commonmark.internal.inlines.Inline)1 SimplifiedHtmlDocumentBuilder (org.eclipse.mylyn.wikitext.commonmark.internal.spec.SimplifiedHtmlDocumentBuilder)1 Attributes (org.eclipse.mylyn.wikitext.parser.Attributes)1 HtmlDocumentBuilder (org.eclipse.mylyn.wikitext.parser.builder.HtmlDocumentBuilder)1