Skip to content

Commit

Permalink
Refactor Head Parsing to ease porting to main branch
Browse files Browse the repository at this point in the history
  • Loading branch information
abelsromero committed Feb 9, 2024
1 parent 589b744 commit b5b676a
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 50 deletions.
11 changes: 6 additions & 5 deletions docs/modules/site-integration/pages/exposed-metadata.adoc
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
= Exposed metadata
:asciidoctor-docs-url: https://docs.asciidoctor.org/asciidoc/latest
:maven-site-plugin-docs-url: https://maven.apache.org/plugins/maven-site-plugin

The Asciidoctor Maven Site integration integrates with Doxia to expose some of its information.
The Asciidoctor Maven Site integration collaborates with Doxia to expose some of its information.

== Document Header metadata

The following information from the {asciidoctor-docs-url}/document/header/[header] is extracted:
The following elements from the {asciidoctor-docs-url}/document/header/[header] are integrated:

document title:: used to inform the breadcrumb line when these are enabled.
document title:: used to inform the {maven-site-plugin-docs-url}/examples/sitedescriptor.html#Breadcrumbs[breadcrumb] line when these are enabled.

author(s):: full representation (full name and email) will be present as HTML `<meta name="author" ... >` tags inside the HTML `<head>`.
In case of multiple authors, each one will appear in a distinct meta element.
In case of multiple authors, each one will appear in a distinct `meta` element.

revision date:: the header revision date value wll be presented as-is in a `<meta name="date" ... >` element.
revision date:: the header revision date value will be presented as-is in a `<meta name="date" ... >` element.
Alternatively, if not set, the generated value of `docdatetime` will be used.
2 changes: 1 addition & 1 deletion src/it/maven-site-plugin/validate.groovy
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import java.nio.charset.StandardCharsets
import java.nio.file.Files
import java.util.regex.Matcher
import java.util.regex.Pattern

File outputDir = new File(basedir, "target/site")
Expand All @@ -14,6 +13,7 @@ String[] unexpectedFiles = [
]

class PatternCapturer {

private final Pattern pattern
private final List<String> hits

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
import org.apache.maven.doxia.parser.Parser;
import org.apache.maven.doxia.sink.Sink;
import org.apache.maven.project.MavenProject;
import org.asciidoctor.*;
import org.asciidoctor.ast.Author;
import org.asciidoctor.Asciidoctor;
import org.asciidoctor.AttributesBuilder;
import org.asciidoctor.OptionsBuilder;
import org.asciidoctor.SafeMode;
import org.asciidoctor.maven.log.LogHandler;
import org.asciidoctor.maven.log.LogRecordFormatter;
import org.asciidoctor.maven.log.LogRecordsProcessors;
import org.asciidoctor.maven.log.MemoryLogHandler;
import org.asciidoctor.maven.site.SiteConverter.HeaderMetadata;
import org.asciidoctor.maven.site.SiteConverter.Result;
import org.asciidoctor.maven.site.SiteConverterDecorator.Result;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.xml.Xpp3Dom;
Expand All @@ -22,7 +23,6 @@
import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.util.Optional;
import java.util.logging.Logger;

/**
Expand Down Expand Up @@ -79,7 +79,7 @@ public void parse(Reader reader, Sink sink, String reference) throws ParseExcept
final LogHandler logHandler = getLogHandlerConfig(siteConfig);
final MemoryLogHandler memoryLogHandler = asciidoctorLoggingSetup(asciidoctor, logHandler, siteDirectory);

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

try {
Expand All @@ -90,28 +90,12 @@ public void parse(Reader reader, Sink sink, String reference) throws ParseExcept
throw new ParseException(exception.getMessage(), exception);
}

sinkHeader(sink, headerMetadata.getHeaderMetadata());
new HeadParser(sink)
.parse(headerMetadata.getHeaderMetadata());

sink.rawText(headerMetadata.getHtml());
}

private static void sinkHeader(Sink sink, HeaderMetadata headerMetadata) {
sink.head();
sink.title();
sink.text(Optional.ofNullable(headerMetadata.getTitle()).orElse("[Untitled]"));
sink.title_();

for (String author : headerMetadata.getAuthors()) {
sink.author();
sink.text(author.toString());
sink.author_();
}

sink.date();
sink.text(headerMetadata.getDateTime());
sink.date_();
sink.head_();
}

private MemoryLogHandler asciidoctorLoggingSetup(Asciidoctor asciidoctor, LogHandler logHandler, File siteDirectory) {

Expand Down
32 changes: 32 additions & 0 deletions src/main/java/org/asciidoctor/maven/site/HeadParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.asciidoctor.maven.site;

import org.apache.maven.doxia.sink.Sink;

import java.util.Optional;

class HeadParser {

private final Sink sink;

HeadParser(Sink sink) {
this.sink = sink;
}

void parse(SiteConverterDecorator.HeaderMetadata headerMetadata) {
sink.head();
sink.title();
sink.text(Optional.ofNullable(headerMetadata.getTitle()).orElse("[Untitled]"));
sink.title_();

for (String author : headerMetadata.getAuthors()) {
sink.author();
sink.text(author.toString());
sink.author_();
}

sink.date();
sink.text(headerMetadata.getDateTime());
sink.date_();
sink.head_();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@
import java.util.stream.Collectors;

/**
* Asciidoctor conversion wrapper to extract metadata and do the HTML conversion.
* Asciidoctor conversion wrapper for maven-site integration.
* In addition to conversion, handles header metadata extraction.
*/
class SiteConverter {
class SiteConverterDecorator {

private final Asciidoctor asciidoctor;

SiteConverter(Asciidoctor asciidoctor) {
SiteConverterDecorator(Asciidoctor asciidoctor) {
this.asciidoctor = asciidoctor;
}

Expand All @@ -42,8 +43,7 @@ private static Options headerProcessingMetadata(Options options) {
}

builder.parseHeaderOnly(Boolean.TRUE);
Options build = builder.build();
return build;
return builder.build();
}

private List<String> extractAuthors(Document document) {
Expand All @@ -68,11 +68,11 @@ final class Result {
this.html = html;
}

public HeaderMetadata getHeaderMetadata() {
HeaderMetadata getHeaderMetadata() {
return headerMetadata;
}

public String getHtml() {
String getHtml() {
return html;
}
}
Expand All @@ -89,15 +89,15 @@ final class HeaderMetadata {
this.dateTime = dateTime;
}

public String getTitle() {
String getTitle() {
return title;
}

public List<String> getAuthors() {
List<String> getAuthors() {
return authors;
}

public String getDateTime() {
String getDateTime() {
return dateTime;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import org.asciidoctor.Attributes;
import org.asciidoctor.Options;
import org.asciidoctor.SafeMode;
import org.asciidoctor.maven.site.SiteConverter.HeaderMetadata;
import org.asciidoctor.maven.site.SiteConverter.Result;
import org.asciidoctor.maven.site.SiteConverterDecorator.HeaderMetadata;
import org.asciidoctor.maven.site.SiteConverterDecorator.Result;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
Expand All @@ -18,7 +18,7 @@

import static org.assertj.core.api.Assertions.assertThat;

class SiteConverterTest {
class SiteConverterDecoratorTest {

private final Asciidoctor asciidoctor = Asciidoctor.Factory.create();

Expand All @@ -29,7 +29,7 @@ class SiteConverterTest {
"Hello, AsciiDoc!\n================"
})
void should_extract_title_from_header(String title) {
SiteConverter siteConverter = new SiteConverter(asciidoctor);
SiteConverterDecorator siteConverter = new SiteConverterDecorator(asciidoctor);

Options options = defaultOptions();
Result result = siteConverter.process(title + "\n", options);
Expand All @@ -41,7 +41,7 @@ void should_extract_title_from_header(String title) {

@Test
void should_extract_title_from_attribute() {
SiteConverter siteConverter = new SiteConverter(asciidoctor);
SiteConverterDecorator siteConverter = new SiteConverterDecorator(asciidoctor);

Options options = Options.builder()
.safe(SafeMode.UNSAFE)
Expand All @@ -63,7 +63,7 @@ void should_extract_title_from_attribute() {
@ParameterizedTest
@MethodSource("authorsProvider")
void should_extract_author(String content, String expected) {
SiteConverter siteConverter = new SiteConverter(asciidoctor);
SiteConverterDecorator siteConverter = new SiteConverterDecorator(asciidoctor);

Options options = defaultOptions();
Result result = siteConverter.process(content + "\n", options);
Expand All @@ -85,7 +85,7 @@ private static Stream<Arguments> authorsProvider() {

@Test
void should_extract_author_from_attribute() {
SiteConverter siteConverter = new SiteConverter(asciidoctor);
SiteConverterDecorator siteConverter = new SiteConverterDecorator(asciidoctor);

String content = "= Hello, AsciiDoc!";
Options options = optionsWithAttributes(Collections.singletonMap("author", "From Attr"));
Expand All @@ -99,7 +99,7 @@ void should_extract_author_from_attribute() {

@Test
void should_extract_multiple_authors() {
SiteConverter siteConverter = new SiteConverter(asciidoctor);
SiteConverterDecorator siteConverter = new SiteConverterDecorator(asciidoctor);

String content = "= Hello, AsciiDoc!\nfirstname1 lastname2; firstname3 middlename4 lastname5";
Result result = siteConverter.process(content + "\n", defaultOptions());
Expand All @@ -112,7 +112,7 @@ void should_extract_multiple_authors() {

@Test
void should_extract_datetime_generated() {
SiteConverter siteConverter = new SiteConverter(asciidoctor);
SiteConverterDecorator siteConverter = new SiteConverterDecorator(asciidoctor);

String content = "= Hello, AsciiDoc!";
Result result = siteConverter.process(content + "\n", defaultOptions());
Expand All @@ -124,7 +124,7 @@ void should_extract_datetime_generated() {

@Test
void should_extract_datetime_from_attribute() {
SiteConverter siteConverter = new SiteConverter(asciidoctor);
SiteConverterDecorator siteConverter = new SiteConverterDecorator(asciidoctor);

String content = "= Hello, AsciiDoc!";
Options options = optionsWithAttributes(Collections.singletonMap("docdatetime", "2024-11-22"));
Expand Down

0 comments on commit b5b676a

Please sign in to comment.