-
Notifications
You must be signed in to change notification settings - Fork 299
Expand file tree
/
Copy pathTitleExtract.java
More file actions
180 lines (157 loc) · 7.06 KB
/
TitleExtract.java
File metadata and controls
180 lines (157 loc) · 7.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package com.vladsch.flexmark.java.samples;
import com.vladsch.flexmark.ast.Heading;
import com.vladsch.flexmark.ext.anchorlink.AnchorLink;
import com.vladsch.flexmark.ext.anchorlink.internal.AnchorLinkNodeRenderer;
import com.vladsch.flexmark.html.HtmlRenderer;
import com.vladsch.flexmark.html.HtmlRenderer.HtmlRendererExtension;
import com.vladsch.flexmark.html.HtmlWriter;
import com.vladsch.flexmark.html.renderer.DelegatingNodeRendererFactory;
import com.vladsch.flexmark.html.renderer.NodeRenderer;
import com.vladsch.flexmark.html.renderer.NodeRendererContext;
import com.vladsch.flexmark.html.renderer.NodeRenderingHandler;
import com.vladsch.flexmark.parser.Parser;
import com.vladsch.flexmark.parser.ParserEmulationProfile;
import com.vladsch.flexmark.profile.pegdown.Extensions;
import com.vladsch.flexmark.profile.pegdown.PegdownOptionsAdapter;
import com.vladsch.flexmark.util.ast.Block;
import com.vladsch.flexmark.util.ast.Node;
import com.vladsch.flexmark.util.ast.TextCollectingVisitor;
import com.vladsch.flexmark.util.data.DataHolder;
import com.vladsch.flexmark.util.data.MutableDataHolder;
import org.jetbrains.annotations.NotNull;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class TitleExtract {
final private static DataHolder OPTIONS = PegdownOptionsAdapter.flexmarkOptions(
Extensions.ALL & ~(Extensions.HARDWRAPS)
, HeadingExtension.create()).toMutable()
.set(HtmlRenderer.INDENT_SIZE, 2);
static class HeadingExtension implements HtmlRendererExtension {
@Override
public void rendererOptions(@NotNull MutableDataHolder options) {
// add any configuration settings to options you want to apply to everything, here
}
@Override
public void extend(@NotNull HtmlRenderer.Builder htmlRendererBuilder, @NotNull String rendererType) {
htmlRendererBuilder.nodeRendererFactory(new HeadingNodeRenderer.Factory());
}
static HeadingExtension create() {
return new HeadingExtension();
}
}
static class HeadingNodeRenderer implements NodeRenderer {
public HeadingNodeRenderer(DataHolder options) {
}
@Override
public Set<NodeRenderingHandler<?>> getNodeRenderingHandlers() {
return new HashSet<>(Arrays.asList(
new NodeRenderingHandler<>(AnchorLink.class, this::render),
new NodeRenderingHandler<>(Heading.class, this::render)
));
}
void render(AnchorLink node, NodeRendererContext context, HtmlWriter html) {
Node parent = node.getParent();
if (parent instanceof Heading && ((Heading) parent).getLevel() == 1) {
// render without anchor link
context.renderChildren(node);
} else {
context.delegateRender();
}
}
static boolean haveExtension(int extensions, int flags) {
return (extensions & flags) != 0;
}
static boolean haveAllExtensions(int extensions, int flags) {
return (extensions & flags) == flags;
}
void render(Heading node, NodeRendererContext context, HtmlWriter html) {
if (node.getLevel() == 1) {
// render without anchor link
int extensions = ParserEmulationProfile.PEGDOWN_EXTENSIONS.get(context.getOptions());
if (context.getHtmlOptions().renderHeaderId || haveExtension(extensions, Extensions.ANCHORLINKS) || haveAllExtensions(extensions, Extensions.EXTANCHORLINKS | Extensions.EXTANCHORLINKS_WRAP)) {
String id = context.getNodeId(node);
if (id != null) {
html.attr("id", id);
}
}
if (context.getHtmlOptions().sourcePositionParagraphLines) {
html.srcPos(node.getChars()).withAttr().tagLine("h" + node.getLevel(), () -> {
html.srcPos(node.getText()).withAttr().tag("span");
context.renderChildren(node);
html.tag("/span");
});
} else {
html.srcPos(node.getText()).withAttr().tagLine("h" + node.getLevel(), () -> context.renderChildren(node));
}
} else {
context.delegateRender();
}
}
public static class Factory implements DelegatingNodeRendererFactory {
@NotNull
@Override
public NodeRenderer apply(@NotNull DataHolder options) {
return new HeadingNodeRenderer(options);
}
@Override
public Set<Class<?>> getDelegates() {
Set<Class<?>> delegates = new HashSet<>();
delegates.add(AnchorLinkNodeRenderer.Factory.class);
return delegates;
}
}
}
private static String findTitle(Node root) {
if (root instanceof Heading) {
Heading h = (Heading) root;
if (h.getLevel() == 1 && h.hasChildren()) {
TextCollectingVisitor collectingVisitor = new TextCollectingVisitor();
return collectingVisitor.collectAndGetText(h);
}
}
if (root instanceof Block && root.hasChildren()) {
Node child = root.getFirstChild();
while (child != null) {
String title = findTitle(child);
if (title != null) {
return title;
}
child = child.getNext();
}
}
return null;
}
public static void main(String[] args) {
Parser parser = Parser.builder(OPTIONS).build();
HtmlRenderer renderer = HtmlRenderer.builder(OPTIONS).build();
// You can re-use parser and renderer instances
Node document = parser.parse("\n" +
"# Plugin commit-message-length-validator\n" +
"\n" +
"Vendor\n" +
": Gerrit Code Review\n" +
"\n" +
"Version\n" +
": v2.15-rc2\n" +
"\n" +
"commitmessage.maxSubjectLength\n" +
": Maximum length of the commit message's subject line. Defaults\n" +
" to 50 if not specified or less than 0.\n" +
"\n" +
"## About\n" +
"\n" +
"This plugin checks the length of a commit's commit message subject and body, and reports warnings and errors to the git client if the lentghts are exceeded.\n" +
"\n" +
"## Documentation\n" +
"\n" +
"* [Commit Message Length Configuration](config.md)\n" +
"* More Items\n" +
"\n" +
"");
String title = findTitle(document);
System.out.println("Title: " + title + "\n");
String html = renderer.render(document); // "<p>This is <em>Sparta</em></p>\n"
System.out.println(html);
}
}