Skip to content

Commit 138897c

Browse files
authored
Add INFO message to site modules showing the filename in case of issues (#961)
This PR uses the new 'reference' parameter in Doxia Parsers to print the source filename when an issue is detected. This is compatible and extends the features in 'logHandler'. Fixes #323
1 parent 136dacb commit 138897c

File tree

6 files changed

+55
-25
lines changed

6 files changed

+55
-25
lines changed

CHANGELOG.adoc

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Improvements::
2929
* Sections are now wrapped in <div> in (asciidoctor-parser-doxia-module) (#944)
3030
* Add support for inline and Example blocks (asciidoctor-parser-doxia-module) (#938)
3131
* Add support for captioned titles in appendixes, tables, listing, figure, and examples (asciidoctor-parser-doxia-module) (#938)
32-
32+
* Add INFO message to site modules showing the filename in case of issues (#323)
3333

3434
Build / Infrastructure::
3535

asciidoctor-converter-doxia-module/src/main/java/org/asciidoctor/maven/site/AsciidoctorConverterDoxiaParser.java

+12-6
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import org.asciidoctor.Options;
1818
import org.asciidoctor.OptionsBuilder;
1919
import org.asciidoctor.SafeMode;
20+
import org.asciidoctor.maven.commons.StringUtils;
2021
import org.asciidoctor.maven.log.LogHandler;
2122
import org.asciidoctor.maven.log.LogRecordFormatter;
2223
import org.asciidoctor.maven.log.LogRecordsProcessors;
@@ -82,15 +83,21 @@ public void parse(Reader reader, Sink sink, String reference) throws ParseExcept
8283
}
8384

8485
final LogHandler logHandler = getLogHandlerConfig(siteConfig);
85-
final MemoryLogHandler memoryLogHandler = asciidoctorLoggingSetup(asciidoctor, logHandler, siteDirectory);
86+
final MemoryLogHandler memoryLogHandler = asciidoctorLoggingSetup(asciidoctor, siteDirectory);
8687

8788
final SiteConverterDecorator siteConverter = new SiteConverterDecorator(asciidoctor);
8889
final Result headerMetadata = siteConverter.process(source, conversionConfig.getOptions());
8990

9091
try {
9192
// process log messages according to mojo configuration
92-
new LogRecordsProcessors(logHandler, siteDirectory, errorMessage -> logger.error(errorMessage))
93-
.processLogRecords(memoryLogHandler);
93+
if (!memoryLogHandler.isEmpty()) {
94+
logger.info("Issues found in: {}", reference);
95+
if (logHandler.getOutputToConsole() && StringUtils.isNotBlank(reference)) {
96+
memoryLogHandler.processAll();
97+
}
98+
new LogRecordsProcessors(logHandler, siteDirectory, errorMessage -> logger.error(errorMessage))
99+
.processLogRecords(memoryLogHandler);
100+
}
94101
} catch (Exception exception) {
95102
throw new ParseException(exception.getMessage(), exception);
96103
}
@@ -101,10 +108,9 @@ public void parse(Reader reader, Sink sink, String reference) throws ParseExcept
101108
sink.rawText(headerMetadata.getHtml());
102109
}
103110

111+
private MemoryLogHandler asciidoctorLoggingSetup(Asciidoctor asciidoctor, File siteDirectory) {
104112

105-
private MemoryLogHandler asciidoctorLoggingSetup(Asciidoctor asciidoctor, LogHandler logHandler, File siteDirectory) {
106-
107-
final MemoryLogHandler memoryLogHandler = new MemoryLogHandler(logHandler.getOutputToConsole(),
113+
final MemoryLogHandler memoryLogHandler = new MemoryLogHandler(false,
108114
logRecord -> logger.info(LogRecordFormatter.format(logRecord, siteDirectory)));
109115
asciidoctor.registerLogHandler(memoryLogHandler);
110116
// disable default console output of AsciidoctorJ

asciidoctor-maven-commons/src/main/java/org/asciidoctor/maven/log/MemoryLogHandler.java

+28-7
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@
1414
* AsciidoctorJ LogHandler that stores records in memory.
1515
*
1616
* @author abelsromero
17+
* @since 1.5.7
1718
*/
1819
public class MemoryLogHandler implements LogHandler {
1920

20-
final List<LogRecord> records = new ArrayList<>();
21+
private final List<LogRecord> records = new ArrayList<>();
2122

2223
private final Boolean outputToConsole;
2324
private final Consumer<LogRecord> recordConsumer;
@@ -46,8 +47,8 @@ public void clear() {
4647
*/
4748
public List<LogRecord> filter(Severity severity) {
4849
return this.records.stream()
49-
.filter(record -> severityIsHigher(record, severity))
50-
.collect(Collectors.toList());
50+
.filter(record -> severityIsHigher(record, severity))
51+
.collect(Collectors.toList());
5152
}
5253

5354
/**
@@ -58,8 +59,8 @@ public List<LogRecord> filter(Severity severity) {
5859
*/
5960
public List<LogRecord> filter(String text) {
6061
return this.records.stream()
61-
.filter(record -> messageContains(record, text))
62-
.collect(Collectors.toList());
62+
.filter(record -> messageContains(record, text))
63+
.collect(Collectors.toList());
6364
}
6465

6566
/**
@@ -71,8 +72,27 @@ public List<LogRecord> filter(String text) {
7172
*/
7273
public List<LogRecord> filter(Severity severity, String text) {
7374
return this.records.stream()
74-
.filter(record -> severityIsHigher(record, severity) && messageContains(record, text))
75-
.collect(Collectors.toList());
75+
.filter(record -> severityIsHigher(record, severity) && messageContains(record, text))
76+
.collect(Collectors.toList());
77+
}
78+
79+
/**
80+
* Returns whether error messages have been captured or no.
81+
*
82+
* @return true if no error messages are present
83+
* @since 3.1.0
84+
*/
85+
public boolean isEmpty() {
86+
return records.isEmpty();
87+
}
88+
89+
/**
90+
* Processes all stored log records.
91+
*
92+
* @since 3.1.0
93+
*/
94+
public void processAll() {
95+
records.forEach(recordConsumer::accept);
7696
}
7797

7898
private static boolean severityIsHigher(LogRecord record, Severity severity) {
@@ -82,4 +102,5 @@ private static boolean severityIsHigher(LogRecord record, Severity severity) {
82102
private static boolean messageContains(LogRecord record, String text) {
83103
return record.getMessage().contains(text);
84104
}
105+
85106
}

asciidoctor-parser-doxia-module/src/main/java/org/asciidoctor/maven/site/parser/AsciidoctorAstDoxiaParser.java

+12-7
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.asciidoctor.OptionsBuilder;
2121
import org.asciidoctor.SafeMode;
2222
import org.asciidoctor.ast.Document;
23+
import org.asciidoctor.maven.commons.StringUtils;
2324
import org.asciidoctor.maven.log.LogHandler;
2425
import org.asciidoctor.maven.log.LogRecordFormatter;
2526
import org.asciidoctor.maven.log.LogRecordsProcessors;
@@ -90,7 +91,7 @@ public void parse(Reader reader, Sink sink, String reference) throws ParseExcept
9091
}
9192

9293
final LogHandler logHandler = getLogHandlerConfig(siteConfig);
93-
final MemoryLogHandler memoryLogHandler = asciidoctorLoggingSetup(asciidoctor, logHandler, siteDirectory);
94+
final MemoryLogHandler memoryLogHandler = asciidoctorLoggingSetup(asciidoctor, siteDirectory);
9495

9596
if (isNotBlank(reference))
9697
logger.debug("Document loaded: {}", reference);
@@ -99,14 +100,18 @@ public void parse(Reader reader, Sink sink, String reference) throws ParseExcept
99100

100101
try {
101102
// process log messages according to mojo configuration
102-
new LogRecordsProcessors(logHandler, siteDirectory, errorMessage -> logger.error(errorMessage))
103-
.processLogRecords(memoryLogHandler);
104-
103+
if (!memoryLogHandler.isEmpty()) {
104+
logger.info("Issues found in: {}", reference);
105+
if (logHandler.getOutputToConsole() && StringUtils.isNotBlank(reference)) {
106+
memoryLogHandler.processAll();
107+
}
108+
new LogRecordsProcessors(logHandler, siteDirectory, errorMessage -> logger.error(errorMessage))
109+
.processLogRecords(memoryLogHandler);
110+
}
105111
} catch (Exception exception) {
106112
throw new ParseException(exception.getMessage(), exception);
107113
}
108114

109-
110115
HeaderMetadata headerMetadata = HeaderMetadata.from(document);
111116

112117
new HeadParser(sink)
@@ -116,9 +121,9 @@ public void parse(Reader reader, Sink sink, String reference) throws ParseExcept
116121
.sink(document);
117122
}
118123

119-
private MemoryLogHandler asciidoctorLoggingSetup(Asciidoctor asciidoctor, LogHandler logHandler, File siteDirectory) {
124+
private MemoryLogHandler asciidoctorLoggingSetup(Asciidoctor asciidoctor, File siteDirectory) {
120125

121-
final MemoryLogHandler memoryLogHandler = new MemoryLogHandler(logHandler.getOutputToConsole(),
126+
final MemoryLogHandler memoryLogHandler = new MemoryLogHandler(false,
122127
logRecord -> logger.info(LogRecordFormatter.format(logRecord, siteDirectory)));
123128
asciidoctor.registerLogHandler(memoryLogHandler);
124129
// disable default console output of AsciidoctorJ

docs/modules/site-integration/pages/converter-module-setup-and-configuration.adoc

+1-2
Original file line numberDiff line numberDiff line change
@@ -157,5 +157,4 @@ Enables processing of Asciidoctor messages.
157157
For example to hide them, enable finer detail or fail the build on certain scenarios (e.g. missing included files).
158158
To see all options refer to the main plugin xref:plugin:goals/http.adoc#configuration-logHandler[logHandler configuration].
159159
+
160-
IMPORTANT: Due to limitations in how Maven site integration works, it is not possible to provide the filename in the error message.
161-
We are aware this is not ideal and are tracking any development on the Maven side towards this goal (https://issues.apache.org/jira/browse/DOXIA-555[DOXIA-555]).
160+
NOTE: Since v3.1.0+ the source filename will be displayed alongside issues found during conversion.

docs/modules/site-integration/pages/parser-module-setup-and-configuration.adoc

+1-2
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,7 @@ Enables processing of Asciidoctor messages.
159159
For example to hide them, enable finer detail or fail the build on certain scenarios (e.g. missing included files).
160160
To see all options refer to the main plugin xref:plugin:goals/http.adoc#configuration-logHandler[logHandler configuration].
161161
+
162-
IMPORTANT: Due to limitations in how Maven site integration works, it is not possible to provide the filename in the error message.
163-
We are aware this is not ideal and are tracking any development on the Maven side towards this goal (https://issues.apache.org/jira/browse/DOXIA-555[DOXIA-555]).
162+
NOTE: Since v3.1.0+ the source filename will be displayed alongside issues found during conversion.
164163

165164
[#supported-asciidoc-elements]
166165
== Supported AsciiDoc elements

0 commit comments

Comments
 (0)