1
1
package org .asciidoctor .maven .site ;
2
2
3
3
import javax .inject .Inject ;
4
- import javax .inject .Provider ;
5
4
import java .io .File ;
6
5
import java .io .IOException ;
7
6
import java .io .Reader ;
12
11
import org .apache .maven .doxia .sink .Sink ;
13
12
import org .apache .maven .project .MavenProject ;
14
13
import org .asciidoctor .Asciidoctor ;
15
- import org .asciidoctor .Attributes ;
16
- import org .asciidoctor .AttributesBuilder ;
17
- import org .asciidoctor .Options ;
18
- import org .asciidoctor .OptionsBuilder ;
19
- import org .asciidoctor .SafeMode ;
20
14
import org .asciidoctor .maven .commons .StringUtils ;
21
15
import org .asciidoctor .maven .log .LogHandler ;
22
- import org .asciidoctor .maven .log .LogRecordFormatter ;
23
16
import org .asciidoctor .maven .log .LogRecordsProcessors ;
24
17
import org .asciidoctor .maven .log .MemoryLogHandler ;
25
18
import org .asciidoctor .maven .site .SiteConverterDecorator .Result ;
29
22
import org .slf4j .Logger ;
30
23
import org .slf4j .LoggerFactory ;
31
24
32
- import static org .asciidoctor .maven .site .SiteBaseDirResolver .resolveBaseDir ;
33
-
34
25
/**
35
26
* This class is used by <a href="https://maven.apache.org/doxia/overview.html">the Doxia framework</a>
36
27
* to handle the actual parsing of the AsciiDoc input files into HTML to be consumed/wrapped
43
34
@ Component (role = Parser .class , hint = AsciidoctorConverterDoxiaParser .ROLE_HINT )
44
35
public class AsciidoctorConverterDoxiaParser extends AbstractTextParser {
45
36
46
- private static final Logger logger = LoggerFactory .getLogger (AsciidoctorConverterDoxiaParser .class );
47
-
48
- @ Inject
49
- protected Provider <MavenProject > mavenProjectProvider ;
50
-
51
37
/**
52
38
* The role hint for the {@link AsciidoctorConverterDoxiaParser} Plexus component.
53
39
*/
54
- public static final String ROLE_HINT = "asciidoc" ;
40
+ static final String ROLE_HINT = "asciidoc" ;
41
+
42
+ private static final Logger logger = LoggerFactory .getLogger (AsciidoctorConverterDoxiaParser .class );
43
+
44
+ private final MavenProject mavenProject ;
45
+ private final SiteConversionConfigurationParser siteConfigParser ;
46
+ private final LogHandlerFactory logHandlerFactory ;
47
+ private final SiteConverterDecorator siteConverter ;
48
+
49
+ @ Inject
50
+ public AsciidoctorConverterDoxiaParser (MavenProject mavenProject ,
51
+ SiteConversionConfigurationParser siteConfigParser ,
52
+ LogHandlerFactory logHandlerFactory ,
53
+ SiteConverterDecorator siteConverter ) {
54
+ this .mavenProject = mavenProject ;
55
+ this .siteConfigParser = siteConfigParser ;
56
+ this .logHandlerFactory = logHandlerFactory ;
57
+ this .siteConverter = siteConverter ;
58
+ }
55
59
56
60
/**
57
61
* {@inheritDoc}
@@ -68,27 +72,24 @@ public void parse(Reader reader, Sink sink, String reference) throws ParseExcept
68
72
return ;
69
73
}
70
74
71
- final MavenProject project = mavenProjectProvider . get ( );
72
- final Xpp3Dom siteConfig = getSiteConfig ( project );
73
- final File siteDirectory = resolveBaseDir ( project . getBasedir (), siteConfig );
75
+ final SiteConversionConfiguration conversionConfig = siteConfigParser . processAsciiDocConfig ( mavenProject , ROLE_HINT );
76
+ final Xpp3Dom asciidocConfig = conversionConfig . getAsciidocConfig ( );
77
+ final File siteDirectory = conversionConfig . getSiteBaseDir ( );
74
78
75
79
// Doxia handles a single instance of this class and invokes it multiple times.
76
80
// We need to ensure certain elements are initialized only once to avoid errors.
77
81
// Note, this cannot be done in the constructor because mavenProjectProvider in set after construction.
78
82
// And overriding init and other methods form parent classes does not work.
79
83
final Asciidoctor asciidoctor = Asciidoctor .Factory .create ();
80
84
81
- SiteConversionConfiguration conversionConfig = new SiteConversionConfigurationParser (project )
82
- .processAsciiDocConfig (siteConfig , defaultOptions (siteDirectory ), defaultAttributes ());
83
85
for (String require : conversionConfig .getRequires ()) {
84
86
requireLibrary (asciidoctor , require );
85
87
}
86
88
87
- final LogHandler logHandler = getLogHandlerConfig ( siteConfig );
88
- final MemoryLogHandler memoryLogHandler = asciidoctorLoggingSetup (asciidoctor , siteDirectory );
89
+ final LogHandler logHandler = logHandlerFactory . getConfiguration ( asciidocConfig );
90
+ final MemoryLogHandler memoryLogHandler = logHandlerFactory . create (asciidoctor , siteDirectory , logger );
89
91
90
- final SiteConverterDecorator siteConverter = new SiteConverterDecorator (asciidoctor );
91
- final Result headerMetadata = siteConverter .process (source , conversionConfig .getOptions ());
92
+ final Result headerMetadata = siteConverter .process (asciidoctor , source , conversionConfig .getOptions ());
92
93
93
94
try {
94
95
// process log messages according to mojo configuration
@@ -97,7 +98,7 @@ public void parse(Reader reader, Sink sink, String reference) throws ParseExcept
97
98
if (logHandler .getOutputToConsole () && StringUtils .isNotBlank (reference )) {
98
99
memoryLogHandler .processAll ();
99
100
}
100
- new LogRecordsProcessors (logHandler , siteDirectory , errorMessage -> logger . error ( errorMessage ) )
101
+ new LogRecordsProcessors (logHandler , siteDirectory , logger :: error )
101
102
.processLogRecords (memoryLogHandler );
102
103
}
103
104
} catch (Exception exception ) {
@@ -110,45 +111,6 @@ public void parse(Reader reader, Sink sink, String reference) throws ParseExcept
110
111
sink .rawText (headerMetadata .getHtml ());
111
112
}
112
113
113
- private MemoryLogHandler asciidoctorLoggingSetup (Asciidoctor asciidoctor , File siteDirectory ) {
114
-
115
- final MemoryLogHandler memoryLogHandler = new MemoryLogHandler (false ,
116
- logRecord -> logger .info (LogRecordFormatter .format (logRecord , siteDirectory )));
117
- asciidoctor .registerLogHandler (memoryLogHandler );
118
- // disable default console output of AsciidoctorJ
119
- java .util .logging .Logger .getLogger ("asciidoctor" ).setUseParentHandlers (false );
120
- return memoryLogHandler ;
121
- }
122
-
123
- private LogHandler getLogHandlerConfig (Xpp3Dom siteConfig ) {
124
- Xpp3Dom asciidoc = siteConfig == null ? null : siteConfig .getChild ("asciidoc" );
125
- return new SiteLogHandlerDeserializer ().deserialize (asciidoc );
126
- }
127
-
128
- protected Xpp3Dom getSiteConfig (MavenProject project ) {
129
- return project .getGoalConfiguration ("org.apache.maven.plugins" , "maven-site-plugin" , "site" , "site" );
130
- }
131
-
132
-
133
- // The possible baseDir based on configuration are:
134
- //
135
- // with nothing : src/site + /asciidoc
136
- // with locale : src/site + {locale} + /asciidoc
137
- // with siteDirectory : {siteDirectory} + /asciidoc
138
- // with siteDirectory + locale : {siteDirectory} + {locale} + /asciidoc
139
- protected OptionsBuilder defaultOptions (File siteDirectory ) {
140
- return Options .builder ()
141
- .backend ("xhtml" )
142
- .safe (SafeMode .UNSAFE )
143
- .baseDir (new File (siteDirectory , ROLE_HINT ).getAbsoluteFile ());
144
- }
145
-
146
- protected AttributesBuilder defaultAttributes () {
147
- return Attributes .builder ()
148
- .attribute ("idprefix" , "@" )
149
- .attribute ("showtitle" , "@" );
150
- }
151
-
152
114
private void requireLibrary (Asciidoctor asciidoctor , String require ) {
153
115
if (!(require = require .trim ()).isEmpty ()) {
154
116
try {
@@ -158,4 +120,5 @@ private void requireLibrary(Asciidoctor asciidoctor, String require) {
158
120
}
159
121
}
160
122
}
123
+
161
124
}
0 commit comments