10
10
/**
11
11
* Extension for adding auto generated IDs to headings.
12
12
* <p>
13
- * Create it with {@link #create()} and then configure it on the builder
14
- * {@link HtmlRenderer.Builder#extensions(Iterable)}).
13
+ * Create it with {@link #create()} or {@link #builder()} and then configure it on the
14
+ * renderer builder ( {@link HtmlRenderer.Builder#extensions(Iterable)}).
15
15
* <p>
16
16
* The heading text will be used to create the id. Multiple headings with the
17
17
* same text will result in appending a hyphen and number. For example:
@@ -33,20 +33,22 @@ public class HeadingAnchorExtension implements HtmlRenderer.HtmlRendererExtensio
33
33
private final String idPrefix ;
34
34
private final String idSuffix ;
35
35
36
- private HeadingAnchorExtension (String defaultId , String idPrefix , String idSuffix ) {
37
- this .defaultId = defaultId ;
38
- this .idPrefix = idPrefix ;
39
- this .idSuffix = idSuffix ;
36
+ private HeadingAnchorExtension (Builder builder ) {
37
+ this .defaultId = builder . defaultId ;
38
+ this .idPrefix = builder . idPrefix ;
39
+ this .idSuffix = builder . idSuffix ;
40
40
}
41
41
42
+ /**
43
+ * @return the extension built with default settings
44
+ */
42
45
public static Extension create () {
43
- return create (builder ());
44
- }
45
-
46
- private static Extension create (Builder builder ) {
47
- return new HeadingAnchorExtension (builder .defaultId , builder .idPrefix , builder .idSuffix );
46
+ return new HeadingAnchorExtension (builder ());
48
47
}
49
48
49
+ /**
50
+ * @return a builder to configure the extension settings
51
+ */
50
52
public static Builder builder () {
51
53
return new Builder ();
52
54
}
@@ -62,15 +64,9 @@ public AttributeProvider create(AttributeProviderContext context) {
62
64
}
63
65
64
66
public static class Builder {
65
- private String defaultId ;
66
- private String idPrefix ;
67
- private String idSuffix ;
68
-
69
- public Builder () {
70
- defaultId = "id" ;
71
- idPrefix = "" ;
72
- idSuffix = "" ;
73
- }
67
+ private String defaultId = "id" ;
68
+ private String idPrefix = "" ;
69
+ private String idSuffix = "" ;
74
70
75
71
/**
76
72
* @param value Default value for the id to take if no generated id can be extracted. Default "id"
@@ -92,15 +88,18 @@ public Builder idPrefix(String value) {
92
88
93
89
/**
94
90
* @param value Set the value to be appended to every id generated. Default ""
95
- * @return
91
+ * @return {@code this}
96
92
*/
97
93
public Builder idSuffix (String value ) {
98
94
this .idSuffix = value ;
99
95
return this ;
100
96
}
101
97
98
+ /**
99
+ * @return a configured extension
100
+ */
102
101
public Extension build () {
103
- return HeadingAnchorExtension . create (this );
102
+ return new HeadingAnchorExtension (this );
104
103
}
105
104
}
106
105
}
0 commit comments