Skip to content

Commit

Permalink
[asciidoc-java] fix header/body delimitation when there is no attribu…
Browse files Browse the repository at this point in the history
…tes + source headers
  • Loading branch information
rmannibucau committed Dec 10, 2023
1 parent b71fde8 commit 499a2af
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 6 deletions.
77 changes: 77 additions & 0 deletions asciidoc-java/src/main/java/io/yupiik/asciidoc/launcher/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright (c) 2020 - 2023 - Yupiik SAS - https://www.yupiik.com
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package io.yupiik.asciidoc.launcher;

import io.yupiik.asciidoc.model.Document;
import io.yupiik.asciidoc.parser.Parser;
import io.yupiik.asciidoc.parser.resolver.ContentResolver;
import io.yupiik.asciidoc.renderer.Visitor;
import io.yupiik.asciidoc.renderer.html.SimpleHtmlRenderer;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

public final class Main {
private Main() {
// no-op
}

public static void main(final String... args) throws IOException { // todo: complete impl, this is just an undocumented boostrap main for testing purposes
final var attributes = new HashMap<String, String>();
Function<Map<String, String>, Visitor<String>> renderer = SimpleHtmlRenderer::new;
ContentResolver resolver = null;
Path input = null;
Path output = null;

for (int i = 0; i < args.length; i++) {
if ("-a".equals(args[i]) || "--attribute".equals(args[i])) {
final int sep = args[i + 1].indexOf('=');
attributes.put(args[i + 1].substring(0, sep), args[i + 1].substring(sep + 1));
} else if ("-i".equals(args[i]) || "--input".equals(args[i])) {
input = Path.of(args[i + 1]);
} else if ("-o".equals(args[i]) || "--output".equals(args[i])) {
output = !"-".equals(args[i + 1]) ? Path.of(args[i + 1]) : null /* means stdout */;
} else if ("-b".equals(args[i]) || "--base".equals(args[i])) {
resolver = ContentResolver.of(Path.of(args[i + 1]));
}
}

if (input == null) {
throw new IllegalArgumentException("No --input argument, ensure to set --input <path>");
}
if (resolver == null) {
resolver = ContentResolver.of(input.toAbsolutePath().getParent());
}

final var parser = new Parser();
final Document document;
try (final var reader = Files.newBufferedReader(input)) {
document = parser.parse(reader, new Parser.ParserContext(resolver));
}

final var html = renderer.apply(attributes);
html.visit(document);
if (output != null) {
Files.writeString(output, html.result());
} else {
System.out.println(html.result());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/*
* Copyright (c) 2020 - 2023 - Yupiik SAS - https://www.yupiik.com
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package io.yupiik.asciidoc.model;

import java.util.Map;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,12 @@ public Header parseHeader(final Reader reader) {
var revision = NO_REVISION;

final var authorLine = reader.nextLine();
if (authorLine != null) {
if (authorLine != null && !authorLine.isBlank()) {
if (!ATTRIBUTE_DEFINITION.matcher(authorLine).matches()) { // author line
author = parseAuthorLine(authorLine);

final var revisionLine = reader.nextLine();
if (revisionLine != null) {
if (revisionLine != null && !revisionLine.isBlank()) {
if (!ATTRIBUTE_DEFINITION.matcher(revisionLine).matches()) { // author line
revision = parseRevisionLine(revisionLine);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ void parseHeader() {
assertEquals(Map.of("attr-1", "v1", "attr-2", "v2"), header.attributes());
}

@Test
void parseHeaderAndContent() {
final var doc = new Parser().parse(List.of("= Title", "", "++++", "pass", "++++"), new Parser.ParserContext(null));
assertEquals("Title", doc.header().title());
assertEquals(Map.of(), doc.header().attributes());
assertEquals(List.of(new PassthroughBlock("pass", Map.of())), doc.body().children());
}

@Test
void parseMultiLineAttributesHeader() {
final var header = new Parser().parseHeader(new Reader(List.of(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ public record Foo() {}
<body>
<div id="content">
<h1>Main title</h1>
<div class="details">
<span id="revnumber">Some text.</span>
</div>
<span>
Some text.
</span>
<div>
<h2>Second part</h2>
<span>
Expand All @@ -68,7 +68,7 @@ public record Foo() {}
</div>
</body>
</html>
""");
""");
}

@Test
Expand Down

0 comments on commit 499a2af

Please sign in to comment.