11package io .markdom .handler .commonmark .atlassian ;
22
3- import java .util .Optional ;
4-
5- import org .commonmark .node .AbstractVisitor ;
6- import org .commonmark .node .BlockQuote ;
7- import org .commonmark .node .BulletList ;
8- import org .commonmark .node .Code ;
9- import org .commonmark .node .CustomBlock ;
10- import org .commonmark .node .CustomNode ;
113import org .commonmark .node .Document ;
12- import org .commonmark .node .Emphasis ;
13- import org .commonmark .node .FencedCodeBlock ;
14- import org .commonmark .node .HardLineBreak ;
15- import org .commonmark .node .Heading ;
16- import org .commonmark .node .HtmlBlock ;
17- import org .commonmark .node .HtmlInline ;
18- import org .commonmark .node .Image ;
19- import org .commonmark .node .IndentedCodeBlock ;
20- import org .commonmark .node .Link ;
21- import org .commonmark .node .ListItem ;
22- import org .commonmark .node .Node ;
23- import org .commonmark .node .OrderedList ;
24- import org .commonmark .node .Paragraph ;
25- import org .commonmark .node .SoftLineBreak ;
26- import org .commonmark .node .StrongEmphasis ;
27- import org .commonmark .node .Text ;
28- import org .commonmark .node .ThematicBreak ;
294
30- import io .markdom .common .MarkdomEmphasisLevel ;
31- import io .markdom .common .MarkdomException ;
32- import io .markdom .common .MarkdomHeadingLevel ;
335import io .markdom .handler .MarkdomDispatcher ;
346import io .markdom .handler .MarkdomHandler ;
35- import io .markdom .handler .SimpleMarkdomHandler ;
367import io .markdom .util .ObjectHelper ;
378
389public final class AtlassianCommonmarkDocumentMarkdomDispatcher implements MarkdomDispatcher {
3910
40- private static final String COMMENT_BEGIN = "<!--" ;
41-
42- private static final String COMMENT_END = "-->" ;
43-
4411 private final Document document ;
4512
4613 public AtlassianCommonmarkDocumentMarkdomDispatcher (Document document ) throws IllegalArgumentException {
@@ -49,167 +16,8 @@ public AtlassianCommonmarkDocumentMarkdomDispatcher(Document document) throws Il
4916
5017 @ Override
5118 public <Result > Result handle (MarkdomHandler <Result > handler ) {
52- ObjectHelper .notNull ("handler" , handler );
53- visitDocument (new SimpleMarkdomHandler <Result >(handler ));
19+ document .accept (new MarkdomHandlerCommonmarkVisitor <>(ObjectHelper .notNull ("handler" , handler )));
5420 return handler .getResult ();
5521 }
5622
57- private void visitDocument (final SimpleMarkdomHandler <?> handler ) {
58- document .accept (new AbstractVisitor () {
59-
60- @ Override
61- public void visit (CustomNode customNode ) {
62- throw new AssertionError ("Encountered custom node: " + customNode );
63- }
64-
65- @ Override
66- public void visit (CustomBlock customBlock ) {
67- throw new AssertionError ("Encountered custom block: " + customBlock );
68- }
69-
70- @ Override
71- public void visit (Text text ) {
72- handler .onTextContent (text .getLiteral ());
73- }
74-
75- @ Override
76- public void visit (StrongEmphasis strongEmphasis ) {
77- handler .onEmphasisContentBegin (MarkdomEmphasisLevel .LEVEL_2 );
78- visitChildren (strongEmphasis );
79- handler .onEmphasisContentEnd ();
80- }
81-
82- @ Override
83- public void visit (SoftLineBreak softLineBreak ) {
84- handler .onLineBreakContent (false );
85- }
86-
87- @ Override
88- public void visit (Paragraph paragraph ) {
89- handler .onParagraphBlockBegin ();
90- visitChildren (paragraph );
91- handler .onParagraphBlockEnd ();
92- }
93-
94- @ Override
95- public void visit (OrderedList orderedList ) {
96- int startIndex = orderedList .getStartNumber ();
97- handler .onOrderedListBlockBegin (startIndex );
98- visitChildren (orderedList );
99- handler .onOrderedListBlockEnd ();
100- }
101-
102- @ Override
103- public void visit (ListItem listItem ) {
104- handler .onListItemBegin ();
105- visitChildren (listItem );
106- handler .onListItemEnd ();
107- }
108-
109- @ Override
110- public void visit (Link link ) {
111- String uri = link .getDestination ();
112- Optional <String > title = Optional .ofNullable (link .getTitle ());
113- handler .onLinkContentBegin (uri , title );
114- visitChildren (link );
115- handler .onLinkContentEnd ();
116- }
117-
118- @ Override
119- public void visit (IndentedCodeBlock indentedCodeBlock ) {
120- handler .onCodeBlock (indentedCodeBlock .getLiteral (), Optional .empty ());
121- }
122-
123- @ Override
124- public void visit (Image image ) {
125- String uri = image .getDestination ();
126- Optional <String > title = Optional .ofNullable (image .getTitle ());
127- Optional <String > alternative = Optional .ofNullable (visitText (image ));
128- handler .onImageContent (uri , title , alternative );
129- }
130-
131- private String visitText (Node parent ) {
132- StringBuilder builder = new StringBuilder ();
133- parent .accept (new CommonmarkTextVisitor (builder ));
134- return 0 == builder .length () ? null : builder .toString ();
135- }
136-
137- @ Override
138- public void visit (HtmlBlock htmlBlock ) {
139- String htmlText = htmlBlock .getLiteral ();
140- if (null != htmlText && htmlText .length () >= COMMENT_BEGIN .length () + COMMENT_END .length ()
141- && htmlText .startsWith (COMMENT_BEGIN ) && htmlText .endsWith (COMMENT_END )) {
142- handler .onCommentBlock (htmlText .substring (COMMENT_BEGIN .length (), htmlText .length () - COMMENT_END .length ()));
143- } else {
144- throw new MarkdomException ("Encountered HTML block: " + htmlBlock .getLiteral ());
145- }
146- }
147-
148- @ Override
149- public void visit (HtmlInline htmlInline ) {
150- throw new MarkdomException ("Encountered HTML inline: " + htmlInline .getLiteral ());
151- }
152-
153- @ Override
154- public void visit (ThematicBreak thematicBreak ) {
155- handler .onDivisionBlock ();
156- }
157-
158- @ Override
159- public void visit (Heading heading ) {
160- MarkdomHeadingLevel level = MarkdomHeadingLevel .values ()[heading .getLevel () - 1 ];
161- handler .onHeadingBlockBegin (level );
162- visitChildren (heading );
163- handler .onHeadingBlockEnd ();
164- }
165-
166- @ Override
167- public void visit (HardLineBreak hardLineBreak ) {
168- handler .onLineBreakContent (true );
169- }
170-
171- @ Override
172- public void visit (FencedCodeBlock fencedCodeBlock ) {
173- String code = fencedCodeBlock .getLiteral ();
174- Optional <String > hint = Optional .ofNullable (fencedCodeBlock .getInfo ());
175- handler .onCodeBlock (code , hint );
176- }
177-
178- @ Override
179- public void visit (Emphasis emphasis ) {
180- handler .onEmphasisContentBegin (MarkdomEmphasisLevel .LEVEL_1 );
181- visitChildren (emphasis );
182- handler .onEmphasisContentEnd ();
183- }
184-
185- @ Override
186- public void visit (Document document ) {
187- handler .onDocumentBegin ();
188- visitChildren (document );
189- handler .onDocumentEnd ();
190- }
191-
192- @ Override
193- public void visit (Code code ) {
194- handler .onCodeContent (code .getLiteral ());
195- }
196-
197- @ Override
198- public void visit (BulletList bulletList ) {
199- handler .onUnorderedListBlockBegin ();
200- visitChildren (bulletList );
201- handler .onUnorderedListBlockEnd ();
202- }
203-
204- @ Override
205- public void visit (BlockQuote blockQuote ) {
206- handler .onQuoteBlockBegin ();
207- visitChildren (blockQuote );
208- handler .onQuoteBlockEnd ();
209- }
210-
211- });
212-
213- }
214-
21523}
0 commit comments