Skip to content

Commit 2b264be

Browse files
committed
fix: rework naming and improve FrontMatterParser interface
1 parent 614abc1 commit 2b264be

15 files changed

Lines changed: 218 additions & 129 deletions

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ document start here
411411
Use class `YamlFrontMatterExtension` in artifact `commonmark-ext-yaml-front-matter`. To fetch metadata, use `YamlFrontMatterVisitor`:
412412

413413
```java
414-
import org.commonmark.ext.front.matter.extractor.YamlContentExtractor;
414+
import org.commonmark.ext.front.matter.parser.RawContentParser;
415415

416416
List<Extension> extensions = List.of(YamlFrontMatterExtension.create());
417417
Parser parser = Parser.builder()
@@ -422,22 +422,22 @@ Node document = parser.parse(markdownDocument);
422422
Map<String, List<String>> frontMatter = YamlFrontMatterVisitor.readData(document);
423423
```
424424

425-
Alternatively, you can use initialize the extension with `YamlContentExtractor` that saves the YAML front matter content
426-
as a string for further processing with other tools:
425+
Alternatively, you can use initialize the extension with `RawContentParser` to capture the entire front matter as a
426+
string for further processing with other tools:
427427

428428
```java
429-
import org.commonmark.ext.front.matter.extractor.YamlContentExtractor;
429+
import org.commonmark.ext.front.matter.parser.RawContentParser;
430430

431-
List<Extension> extensions = List.of(YamlFrontMatterExtension.create(new YamlContentExtractor.Factory()));
431+
List<Extension> extensions = List.of(YamlFrontMatterExtension.create(new RawContentParser.Factory()));
432432
Parser parser = Parser.builder()
433433
.extensions(extensions)
434434
.build();
435435

436436
Node document = parser.parse(markdownDocument);
437-
String frontMatter = YamlFrontMatterVisitor.readContent(document);
437+
String frontMatter = YamlFrontMatterVisitor.readRawContent(document);
438438
```
439439

440-
You can also write a custom extractor by implementing `YamlFrontMatterExtractor` interface.
440+
You can also write a custom front matter parser by implementing `FrontMatterParser` interface.
441441

442442
### Image Attributes
443443

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package org.commonmark.ext.front.matter;
2+
3+
import org.commonmark.parser.SourceLine;
4+
5+
/**
6+
* Parses the content of the front matter block between `---` separators.
7+
* The implementations should add at least 1 child node to
8+
* {@link YamlFrontMatterBlock} to store the result of the parsing.
9+
*/
10+
public interface FrontMatterParser {
11+
void onNextLine(YamlFrontMatterBlock block, SourceLine line);
12+
13+
/**
14+
* Notifies about finding the line with the ending separator (`---` or `...`, without
15+
* initial whitespace). Advanced parsers may be able to determine that the separator
16+
* is a part of the single-/double-quoted multiline string and return {@link SeparatorRole#CONTENT}
17+
* to include it in the front matter content. In this case, front matter parsing continues until
18+
* finding another separator. To end the parsing, return {@link SeparatorRole#BLOCK_END}.
19+
*
20+
* @param block Main block node
21+
* @param separator `---` or `...`
22+
* @return Separator role: block end or the part of the front matter content
23+
*/
24+
SeparatorRole onEndingSeparator(YamlFrontMatterBlock block, SourceLine separator);
25+
26+
interface Factory {
27+
FrontMatterParser create();
28+
}
29+
30+
enum SeparatorRole {
31+
BLOCK_END, CONTENT
32+
}
33+
}

commonmark-ext-yaml-front-matter/src/main/java/org/commonmark/ext/front/matter/YamlFrontMatterExtension.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import java.util.Objects;
44
import java.util.Set;
55
import org.commonmark.Extension;
6-
import org.commonmark.ext.front.matter.extractor.YamlContentExtractor;
7-
import org.commonmark.ext.front.matter.extractor.YamlDataExtractor;
6+
import org.commonmark.ext.front.matter.parser.RawContentParser;
7+
import org.commonmark.ext.front.matter.parser.YamlSubsetParser;
88
import org.commonmark.ext.front.matter.internal.YamlFrontMatterBlockParser;
99
import org.commonmark.ext.front.matter.internal.YamlFrontMatterMarkdownNodeRenderer;
1010
import org.commonmark.node.Node;
@@ -22,24 +22,24 @@
2222
* org.commonmark.parser.Parser.Builder#extensions(Iterable)}, {@link
2323
* HtmlRenderer.Builder#extensions(Iterable)}).
2424
*
25-
* <p>By default, the extension parses the subset of YAML with a built-int parser.
25+
* <p>By default, the extension parses the subset of YAML with a built-int {@link YamlSubsetParser}.
2626
* The parsed metadata is turned into {@link YamlFrontMatterNode}. You can access
2727
* the metadata using {@link YamlFrontMatterVisitor#readData(Node)}.
2828
*
29-
* <p>Alternatively, you can create the extension with {@link YamlContentExtractor.Factory}.
30-
* It turns the YAML front matter into {@link YamlFrontMatterContent} node, which stores
31-
* the front matter content as a simple string. You can access the content with
32-
* {@link YamlFrontMatterVisitor#readContent(Node)} to process it with other tools.
29+
* <p>Alternatively, you can create the extension with {@link RawContentParser.Factory}.
30+
* It turns the YAML front matter into {@link YamlFrontMatterRawContent} node, which stores
31+
* the entire front matter as a string. You can access the content with
32+
* {@link YamlFrontMatterVisitor#readRawContent(Node)} to process it with other tools.
3333
*
34-
* <p>To create a custom YAML front matter extractor, implement {@link YamlFrontMatterExtractor}
35-
* interface and the corresponding factory.
34+
* <p>Implement {@link FrontMatterParser} interface and the corresponding factory to
35+
* parse the front matter with a custom parser.
3636
*/
3737
public class YamlFrontMatterExtension
3838
implements Parser.ParserExtension, MarkdownRenderer.MarkdownRendererExtension {
3939

40-
private final YamlFrontMatterExtractor.Factory yamlExtractorFactory;
40+
private final FrontMatterParser.Factory yamlExtractorFactory;
4141

42-
private YamlFrontMatterExtension(YamlFrontMatterExtractor.Factory yamlExtractorFactory) {
42+
private YamlFrontMatterExtension(FrontMatterParser.Factory yamlExtractorFactory) {
4343
this.yamlExtractorFactory = Objects.requireNonNull(yamlExtractorFactory);
4444
}
4545

@@ -49,10 +49,10 @@ public void extend(Parser.Builder parserBuilder) {
4949
}
5050

5151
public static Extension create() {
52-
return create(new YamlDataExtractor.Factory());
52+
return create(new YamlSubsetParser.Factory());
5353
}
5454

55-
public static Extension create(YamlFrontMatterExtractor.Factory extractor) {
55+
public static Extension create(FrontMatterParser.Factory extractor) {
5656
return new YamlFrontMatterExtension(extractor);
5757
}
5858

commonmark-ext-yaml-front-matter/src/main/java/org/commonmark/ext/front/matter/YamlFrontMatterExtractor.java

Lines changed: 0 additions & 13 deletions
This file was deleted.

commonmark-ext-yaml-front-matter/src/main/java/org/commonmark/ext/front/matter/YamlFrontMatterContent.java renamed to commonmark-ext-yaml-front-matter/src/main/java/org/commonmark/ext/front/matter/YamlFrontMatterRawContent.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
import org.commonmark.node.CustomNode;
44

5-
public class YamlFrontMatterContent extends CustomNode {
5+
public class YamlFrontMatterRawContent extends CustomNode {
66
private String content;
77

8-
public YamlFrontMatterContent(String content) {
8+
public YamlFrontMatterRawContent(String content) {
99
this.content = content;
1010
}
1111

commonmark-ext-yaml-front-matter/src/main/java/org/commonmark/ext/front/matter/YamlFrontMatterVisitor.java

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
import java.util.List;
55
import java.util.Map;
66

7-
import org.commonmark.ext.front.matter.extractor.YamlDataExtractor;
8-
import org.commonmark.ext.front.matter.extractor.YamlContentExtractor;
7+
import org.commonmark.ext.front.matter.parser.YamlSubsetParser;
8+
import org.commonmark.ext.front.matter.parser.RawContentParser;
99
import org.commonmark.node.AbstractVisitor;
1010
import org.commonmark.node.CustomNode;
1111
import org.commonmark.node.Node;
@@ -23,10 +23,10 @@ public YamlFrontMatterVisitor() {
2323

2424
/**
2525
* Reads the YAML front matter metadata, if the Markdown
26-
* document has the YAML front matter and the extension
27-
* uses {@link YamlDataExtractor} (default).
26+
* document has the front matter and the extension
27+
* uses {@link YamlSubsetParser} (default).
2828
*
29-
* @return The data stored in YAML front matter or empty map
29+
* @return The data stored in YAML front matter or empty map.
3030
*/
3131
public static Map<String, List<String>> readData(Node document) {
3232
YamlFrontMatterVisitor visitor = new YamlFrontMatterVisitor();
@@ -35,16 +35,15 @@ public static Map<String, List<String>> readData(Node document) {
3535
}
3636

3737
/**
38-
* Reads the YAML Front Matter metadata as a string, if the Markdown
39-
* document has the YAML Front Matter and the extension uses
40-
* {@link YamlContentExtractor} (default).
38+
* Reads the raw content of the front matter, if the Markdown document has
39+
* the front matter and the extension uses {@link RawContentParser}.
4140
*
42-
* @return The content of YAML front matter as string or empty string.
41+
* @return Raw content of the front matter as string or empty string.
4342
*/
44-
public static String readContent(Node document) {
43+
public static String readRawContent(Node document) {
4544
YamlFrontMatterVisitor visitor = new YamlFrontMatterVisitor();
4645
document.accept(visitor);
47-
return visitor.getContent();
46+
return visitor.getRawContent();
4847
}
4948

5049
@Override
@@ -55,8 +54,8 @@ public void visit(CustomNode customNode) {
5554
((YamlFrontMatterNode) customNode).getValues()
5655
);
5756
present = true;
58-
} else if (customNode instanceof YamlFrontMatterContent) {
59-
content = ((YamlFrontMatterContent) customNode).getContent();
57+
} else if (customNode instanceof YamlFrontMatterRawContent) {
58+
content = ((YamlFrontMatterRawContent) customNode).getContent();
6059
present = true;
6160
} else {
6261
super.visit(customNode);
@@ -65,7 +64,7 @@ public void visit(CustomNode customNode) {
6564

6665
/**
6766
* Returns the YAML front matter metadata, if the Markdown document has
68-
* the YAML front matter and the extension uses {@link YamlDataExtractor}
67+
* the front matter and the extension uses {@link YamlSubsetParser}
6968
* (default).
7069
*
7170
* @return The data stored in YAML front matter or empty map
@@ -75,13 +74,13 @@ public Map<String, List<String>> getData() {
7574
}
7675

7776
/**
78-
* Returns the YAML Front Matter metadata as a string, if the Markdown
79-
* document has the YAML Front Matter and the extension uses
80-
* {@link YamlContentExtractor} (default).
77+
* Returns the raw content of the front matter, if the Markdown
78+
* document has the front matter and the extension uses
79+
* {@link RawContentParser}.
8180
*
82-
* @return The content of YAML front matter as string or empty string.
81+
* @return Raw content of the front matter as string or empty string.
8382
*/
84-
public String getContent() {
83+
public String getRawContent() {
8584
return content;
8685
}
8786

commonmark-ext-yaml-front-matter/src/main/java/org/commonmark/ext/front/matter/extractor/YamlContentExtractor.java

Lines changed: 0 additions & 32 deletions
This file was deleted.

commonmark-ext-yaml-front-matter/src/main/java/org/commonmark/ext/front/matter/internal/YamlFrontMatterBlockParser.java

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,22 @@
22

33
import java.util.regex.Pattern;
44

5-
import org.commonmark.ext.front.matter.YamlFrontMatterExtractor;
5+
import org.commonmark.ext.front.matter.FrontMatterParser;
66
import org.commonmark.ext.front.matter.YamlFrontMatterBlock;
77
import org.commonmark.node.Block;
88
import org.commonmark.node.Document;
9+
import org.commonmark.parser.SourceLine;
910
import org.commonmark.parser.block.*;
1011

1112
public class YamlFrontMatterBlockParser extends AbstractBlockParser {
1213
private static final Pattern REGEX_BEGIN = Pattern.compile("^-{3}(\\s.*)?");
1314
private static final Pattern REGEX_END = Pattern.compile("^(-{3}|\\.{3})(\\s.*)?");
1415

1516
private YamlFrontMatterBlock block;
16-
private YamlFrontMatterExtractor extractor;
17+
private FrontMatterParser frontMatterParser;
1718

18-
public YamlFrontMatterBlockParser(YamlFrontMatterExtractor extractor) {
19-
this.extractor = extractor;
19+
public YamlFrontMatterBlockParser(FrontMatterParser frontMatterParser) {
20+
this.frontMatterParser = frontMatterParser;
2021
block = new YamlFrontMatterBlock();
2122
}
2223

@@ -27,21 +28,26 @@ public Block getBlock() {
2728

2829
@Override
2930
public BlockContinue tryContinue(ParserState parserState) {
30-
final CharSequence line = parserState.getLine().getContent();
31+
final SourceLine line = parserState.getLine();
3132

32-
if (REGEX_END.matcher(line).matches()) {
33-
return extractor.onBlockEnd(block);
33+
if (REGEX_END.matcher(line.getContent()).matches()) {
34+
switch (frontMatterParser.onEndingSeparator(block, line)) {
35+
case BLOCK_END:
36+
return BlockContinue.finished();
37+
case CONTENT:
38+
return BlockContinue.atIndex(parserState.getIndex());
39+
}
3440
}
3541

36-
extractor.onNextLine(block, line);
42+
frontMatterParser.onNextLine(block, line);
3743
return BlockContinue.atIndex(parserState.getIndex());
3844
}
3945

4046
public static class Factory extends AbstractBlockParserFactory {
41-
private YamlFrontMatterExtractor.Factory yamlExtractorFactory;
47+
private FrontMatterParser.Factory frontMatterParserFactory;
4248

43-
public Factory(YamlFrontMatterExtractor.Factory factory) {
44-
this.yamlExtractorFactory = factory;
49+
public Factory(FrontMatterParser.Factory factory) {
50+
this.frontMatterParserFactory = factory;
4551
}
4652

4753
@Override
@@ -52,7 +58,7 @@ public BlockStart tryStart(ParserState state, MatchedBlockParser matchedBlockPar
5258
if (parentParser.getBlock() instanceof Document
5359
&& parentParser.getBlock().getFirstChild() == null
5460
&& REGEX_BEGIN.matcher(line).matches()) {
55-
return BlockStart.of(new YamlFrontMatterBlockParser(yamlExtractorFactory.create()))
61+
return BlockStart.of(new YamlFrontMatterBlockParser(frontMatterParserFactory.create()))
5662
.atIndex(state.getNextNonSpaceIndex());
5763
}
5864

commonmark-ext-yaml-front-matter/src/main/java/org/commonmark/ext/front/matter/internal/YamlFrontMatterMarkdownNodeRenderer.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import java.util.List;
44

5-
import org.commonmark.ext.front.matter.YamlFrontMatterContent;
5+
import org.commonmark.ext.front.matter.YamlFrontMatterRawContent;
66
import org.commonmark.ext.front.matter.YamlFrontMatterNode;
77
import org.commonmark.node.Node;
88
import org.commonmark.renderer.markdown.MarkdownNodeRendererContext;
@@ -22,8 +22,8 @@ public void render(Node node) {
2222
while (child != null) {
2323
if (child instanceof YamlFrontMatterNode) {
2424
renderNode((YamlFrontMatterNode) child);
25-
} else if (child instanceof YamlFrontMatterContent) {
26-
renderContent((YamlFrontMatterContent) child);
25+
} else if (child instanceof YamlFrontMatterRawContent) {
26+
renderContent((YamlFrontMatterRawContent) child);
2727
}
2828
child = child.getNext();
2929
}
@@ -117,7 +117,7 @@ private boolean isFlowCollection(String value) {
117117
|| (value.startsWith("{") && value.endsWith("}"));
118118
}
119119

120-
private void renderContent(YamlFrontMatterContent content) {
120+
private void renderContent(YamlFrontMatterRawContent content) {
121121
String body = content.getContent();
122122
writer.raw(body);
123123
if (!body.endsWith("\n")) {

0 commit comments

Comments
 (0)