forked from commonmark/commonmark-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutolinkExtension.java
More file actions
91 lines (75 loc) · 2.66 KB
/
Copy pathAutolinkExtension.java
File metadata and controls
91 lines (75 loc) · 2.66 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
package org.commonmark.ext.autolink;
import java.util.EnumSet;
import java.util.Set;
import org.commonmark.Extension;
import org.commonmark.ext.autolink.internal.AutolinkPostProcessor;
import org.commonmark.parser.Parser;
import org.commonmark.renderer.html.HtmlRenderer;
/**
* Extension for automatically turning plain URLs and email addresses into links.
* <p>
* Create it with {@link #create()} and then configure it on the builders
* ({@link org.commonmark.parser.Parser.Builder#extensions(Iterable)},
* {@link HtmlRenderer.Builder#extensions(Iterable)}).
* </p>
* <p>
* The parsed links are turned into normal {@link org.commonmark.node.Link} nodes.
* </p>
*/
public class AutolinkExtension implements Parser.ParserExtension {
private final Set<AutolinkType> linkTypes;
private AutolinkExtension(Builder builder) {
this.linkTypes = builder.linkTypes;
}
/**
* @return the extension with default options
*/
public static Extension create() {
return builder().build();
}
/**
* @return a builder to configure the behavior of the extension.
*/
public static Builder builder() {
return new Builder();
}
@Override
public void extend(Parser.Builder parserBuilder) {
parserBuilder.postProcessor(new AutolinkPostProcessor(linkTypes));
}
public static class Builder {
private Set<AutolinkType> linkTypes = EnumSet.allOf(AutolinkType.class);
/**
* @param linkTypes the link types that should be converted. By default,
* all {@link AutolinkType}s are converted.
* @return {@code this}
*/
public Builder linkTypes(AutolinkType... linkTypes) {
if (linkTypes == null) {
throw new NullPointerException("linkTypes must not be null");
}
return this.linkTypes(Set.of(linkTypes));
}
/**
* @param linkTypes the link types that should be converted. By default,
* all {@link AutolinkType}s are converted.
* @return {@code this}
*/
public Builder linkTypes(Set<AutolinkType> linkTypes) {
if (linkTypes == null) {
throw new NullPointerException("linkTypes must not be null");
}
if (linkTypes.isEmpty()) {
throw new IllegalArgumentException("linkTypes must not be empty");
}
this.linkTypes = EnumSet.copyOf(linkTypes);
return this;
}
/**
* @return a configured extension
*/
public Extension build() {
return new AutolinkExtension(this);
}
}
}