-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add asciidoc converter to core (#1555)
- Loading branch information
1 parent
cb8d70a
commit 6ee347c
Showing
43 changed files
with
3,314 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
buildSrc/src/main/java/io/micronaut/guides/core/CalloutMacroSubstitution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package io.micronaut.guides.core; | ||
|
||
import jakarta.inject.Singleton; | ||
|
||
@Singleton | ||
public class CalloutMacroSubstitution extends LineMacroSubstitution { | ||
@Override | ||
protected String getMacroName() { | ||
return "callout"; | ||
} | ||
|
||
@Override | ||
protected String getBaseDirectory() { | ||
return "{calloutsDir}"; | ||
} | ||
|
||
@Override | ||
protected String getPrefix() { | ||
return "callout-"; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
buildSrc/src/main/java/io/micronaut/guides/core/CommonMacroSubstitution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package io.micronaut.guides.core; | ||
|
||
import jakarta.inject.Singleton; | ||
|
||
@Singleton | ||
public class CommonMacroSubstitution extends LineMacroSubstitution { | ||
|
||
@Override | ||
protected String getMacroName() { | ||
return "common"; | ||
} | ||
|
||
@Override | ||
protected String getBaseDirectory() { | ||
return "{commonsDir}"; | ||
} | ||
|
||
@Override | ||
protected String getPrefix() { | ||
return "common-"; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
buildSrc/src/main/java/io/micronaut/guides/core/ExternalMacroSubstitution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package io.micronaut.guides.core; | ||
|
||
import jakarta.inject.Singleton; | ||
|
||
@Singleton | ||
public class ExternalMacroSubstitution extends LineMacroSubstitution { | ||
|
||
@Override | ||
protected String getMacroName() { | ||
return "external"; | ||
} | ||
|
||
@Override | ||
protected String getBaseDirectory() { | ||
return "{guidesDir}"; | ||
} | ||
|
||
@Override | ||
protected String getPrefix() { | ||
return ""; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
buildSrc/src/main/java/io/micronaut/guides/core/ExternalTemplateMacroSubstitution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package io.micronaut.guides.core; | ||
|
||
import jakarta.inject.Singleton; | ||
|
||
@Singleton | ||
public class ExternalTemplateMacroSubstitution extends LineMacroSubstitution { | ||
@Override | ||
protected String getMacroName() { | ||
return "external-template"; | ||
} | ||
|
||
@Override | ||
protected String getBaseDirectory() { | ||
return "{guidesDir}"; | ||
} | ||
|
||
@Override | ||
protected String getPrefix() { | ||
return ""; | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
buildSrc/src/main/java/io/micronaut/guides/core/GuideLinkMacroSubstitution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package io.micronaut.guides.core; | ||
|
||
import jakarta.inject.Singleton; | ||
|
||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
import static io.micronaut.guides.core.MacroUtils.findMacroInstances; | ||
|
||
@Singleton | ||
public class GuideLinkMacroSubstitution implements MacroSubstitution { | ||
private static final Pattern GUIDE_LINK_REGEX = Pattern.compile("guideLink:(.*?)\\[(.*?)]"); | ||
|
||
private static String processGuideLink(String line) { | ||
Matcher matcher = GUIDE_LINK_REGEX.matcher(line); | ||
if (matcher.find()) { | ||
String slug = matcher.group(1).trim(); | ||
String text = matcher.group(2); | ||
return "link:" + slug + ".html[" + text + "]"; | ||
} | ||
return line; | ||
} | ||
|
||
@Override | ||
public String substitute(String str, Guide guide, GuidesOption option) { | ||
for (String instance : findMacroInstances(str, GUIDE_LINK_REGEX)) { | ||
String res = processGuideLink(instance); | ||
str = str.replace(instance, res); | ||
} | ||
|
||
return str; | ||
} | ||
|
||
} |
45 changes: 45 additions & 0 deletions
45
buildSrc/src/main/java/io/micronaut/guides/core/LineMacroSubstitution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package io.micronaut.guides.core; | ||
|
||
import io.micronaut.guides.core.asciidoc.Argument; | ||
import io.micronaut.guides.core.asciidoc.AsciidocMacro; | ||
import io.micronaut.guides.core.asciidoc.Attribute; | ||
import io.micronaut.guides.core.asciidoc.IncludeDirective; | ||
|
||
import java.nio.file.Path; | ||
import java.util.Optional; | ||
|
||
import static io.micronaut.guides.core.MacroUtils.findMacroLines; | ||
|
||
abstract class LineMacroSubstitution implements MacroSubstitution { | ||
protected abstract String getMacroName(); | ||
|
||
protected abstract String getBaseDirectory(); | ||
|
||
protected abstract String getPrefix(); | ||
|
||
@Override | ||
public String substitute(String str, Guide guide, GuidesOption option) { | ||
for (String line : findMacroLines(str, getMacroName())) { | ||
Optional<AsciidocMacro> asciidocMacroOptional = AsciidocMacro.of(getMacroName(), line); | ||
if (asciidocMacroOptional.isEmpty()) { | ||
continue; | ||
} | ||
|
||
AsciidocMacro macro = asciidocMacroOptional.get(); | ||
StringBuilder builder = new StringBuilder(); | ||
|
||
for (Attribute attribute : macro.attributes()) { | ||
Argument argument = new Argument(attribute.key(), attribute.values().get(0)); | ||
builder.append(argument).append("\n"); | ||
} | ||
|
||
Path target = Path.of(getBaseDirectory(), getPrefix() + macro.target()); | ||
|
||
IncludeDirective.Builder includeDirectiveBuilder = IncludeDirective.builder().target(target.toString()); | ||
builder.append(includeDirectiveBuilder.build()); | ||
|
||
str = str.replace(line, builder.toString()); | ||
} | ||
return str; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
buildSrc/src/main/java/io/micronaut/guides/core/asciidoc/Argument.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package io.micronaut.guides.core.asciidoc; | ||
|
||
import io.micronaut.core.annotation.NonNull; | ||
import io.micronaut.core.util.StringUtils; | ||
|
||
import java.util.Optional; | ||
|
||
public record Argument(String key, String value) { | ||
private static final String ARGUMENT_DELIMITATOR = ":"; | ||
|
||
@NonNull | ||
public static Optional<Argument> of(@NonNull String str) { | ||
if (StringUtils.isEmpty(str)) { | ||
return Optional.empty(); | ||
} | ||
|
||
String[] parts = str.split("=", 2); | ||
if (parts.length == 2 && !parts[0].isBlank() && !parts[1].isBlank()) { | ||
return Optional.of(new Argument(parts[0], parts[1])); | ||
} | ||
|
||
return Optional.empty(); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return ARGUMENT_DELIMITATOR + key + ARGUMENT_DELIMITATOR + " " + value; | ||
} | ||
} | ||
|
41 changes: 41 additions & 0 deletions
41
buildSrc/src/main/java/io/micronaut/guides/core/asciidoc/AsciidocConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package io.micronaut.guides.core.asciidoc; | ||
|
||
import org.asciidoctor.Placement; | ||
|
||
import java.io.File; | ||
|
||
public interface AsciidocConfiguration { | ||
String getSourceDir(); | ||
|
||
String getSourceHighlighter(); | ||
|
||
Placement getToc(); | ||
|
||
int getToclevels(); | ||
|
||
boolean getSectnums(); | ||
|
||
String getIdprefix(); | ||
|
||
String getIdseparator(); | ||
|
||
String getIcons(); | ||
|
||
String getImagesdir(); | ||
|
||
boolean isNofooter(); | ||
|
||
String getDocType(); | ||
|
||
String getRuby(); | ||
|
||
File getTemplateDirs(); | ||
|
||
String getCommonsDir(); | ||
|
||
String getBaseDir(); | ||
|
||
String getGuidesDir(); | ||
|
||
String getCalloutsDir(); | ||
} |
Oops, something went wrong.