Skip to content

Commit

Permalink
[asciidoc-java] adding asciidoc-java module, for now it is not yet in…
Browse files Browse the repository at this point in the history
…tegrated with any other module until we switch to java17 as baseline
  • Loading branch information
rmannibucau committed Dec 9, 2023
1 parent 769e2f1 commit 6291d9c
Show file tree
Hide file tree
Showing 40 changed files with 3,914 additions and 1 deletion.
6 changes: 6 additions & 0 deletions _documentation/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>asciidoc-java</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-settings</artifactId>
Expand Down
33 changes: 33 additions & 0 deletions _documentation/src/main/minisite/content/asciidoc-java.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
= Ascii2SVG
:minisite-index: 400
:minisite-index-title: Asciidoc Java
:minisite-index-icon: font
:minisite-index-description: Asciidoc Java native parser.

The module `asciidoc-java` implements a light Asciidoc syntax parser in plain Java.
It aims at making it easier and lighter than using JRuby to embed Asciidoc in any Java based (native or not) application.

Its usage is pretty simple:

. You get a `Parser` instance to load the model of your document in memory (AST),
. You visit the model as you want (or reusing provided `Visitor<?>`) to render the loaded `Document`.

TIP: the `Document` instance is thread safe and can safely be reused accross multiple threads. The parser is also thread save but the visitors must be created per rendering instance.

Here is a basic example:

[source,java]
----
final var parser = new Parser(); <1>
final var doc = parser.parse(myDocContent, new Parser.ParserContext(ContextResolver.of(Path.of("asciidoc/content")))); <2>
final var renderer = new SimpleHtmlRenderer(); <3>
renderer.visit(doc); <4>
final var html = renderer.result(); <5>
----
<.> Create a parser instance, can be a singleton in an application,
<.> Load the document model, can also be a singletong in an application when the conditions depends on attributes,
<.> Create a renderer (here a html renderer, optionally you can pass it attributes using the other constructor),
<.> Visit the document,
<.> Get back the output of the renderer (html output here).

IMPORTANT: the asciidoc syntax is not 100% implemented yet, if you encounter anything you miss, feel free to open an issue or pull request on our bugtracker.
57 changes: 57 additions & 0 deletions asciidoc-java/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.yupiik.maven</groupId>
<artifactId>yupiik-tools-maven-plugin-parent</artifactId>
<version>1.1.9-SNAPSHOT</version>
</parent>

<artifactId>asciidoc-java</artifactId>
<name>Yupiik Tools :: Asciidoc</name>
<description>Simple library to convert an asciidoc content to something else (mainly html).</description>

<dependencies>
<!-- are you sure? probably think one more time -->
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>17</source>
<target>17</target>
<release>17</release>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<configuration>
<source>17</source>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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 static io.yupiik.asciidoc.model.Element.ElementType.ADMONITION;

public record Admonition(Level level, Element content) implements Element {
@Override
public ElementType type() {
return ADMONITION;
}

public enum Level {
NOTE,
TIP,
IMPORTANT,
CAUTION,
WARNING
}
}
27 changes: 27 additions & 0 deletions asciidoc-java/src/main/java/io/yupiik/asciidoc/model/Anchor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* 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 static io.yupiik.asciidoc.model.Element.ElementType.ANCHOR;

// just a pointer to a section by "id" so rendering must resolve the section
// to use its label as title once all model is resolved if not provided
public record Anchor(String value, String label) implements Element {
@Override
public ElementType type() {
return ANCHOR;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* 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.List;
import java.util.function.Function;

import static io.yupiik.asciidoc.model.Element.ElementType.ATTRIBUTE;

public record Attribute(String attribute, Function<String, List<Element>> evaluator) implements Element {
@Override
public ElementType type() {
return ATTRIBUTE;
}
}
19 changes: 19 additions & 0 deletions asciidoc-java/src/main/java/io/yupiik/asciidoc/model/Author.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* 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;

public record Author(String name, String mail) {
}
22 changes: 22 additions & 0 deletions asciidoc-java/src/main/java/io/yupiik/asciidoc/model/Body.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* 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.List;

public record Body(List<Element> children) {
}
25 changes: 25 additions & 0 deletions asciidoc-java/src/main/java/io/yupiik/asciidoc/model/Code.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* 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;

public record Code(String value, Map<String, String> options, boolean inline) implements Element {
@Override
public ElementType type() {
return ElementType.CODE;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* 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.List;
import java.util.Map;
import java.util.function.Predicate;

import static io.yupiik.asciidoc.model.Element.ElementType.CONDITIONAL_BLOCK;

public record ConditionalBlock(Predicate<Context> evaluator,
List<Element> children,
Map<String, String> options) implements Element {
@Override
public ElementType type() {
return CONDITIONAL_BLOCK;
}

@FunctionalInterface
public interface Context {
String attribute(String key);
}

public record Ifdef(String attribute) implements Predicate<Context> {
@Override
public boolean test(final Context context) {
return context.attribute(attribute) != null;
}
}

public record Ifndef(String attribute) implements Predicate<Context> {
@Override
public boolean test(final Context context) {
return context.attribute(attribute) == null;
}
}

public record Ifeval(String todo) implements Predicate<Context> {
@Override
public boolean test(final Context context) {
return context.attribute(todo) == null;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* 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;

import static io.yupiik.asciidoc.model.Element.ElementType.DESCRIPTION_LIST;

public record DescriptionList(Map<String, Element> children) implements Element {
@Override
public ElementType type() {
return DESCRIPTION_LIST;
}
}
19 changes: 19 additions & 0 deletions asciidoc-java/src/main/java/io/yupiik/asciidoc/model/Document.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* 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;

public record Document(Header header, Body body) {
}

0 comments on commit 6291d9c

Please sign in to comment.