Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add asciidoc converter to core #1555

Merged
merged 13 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions buildSrc/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ dependencies {
}
implementation("com.networknt:json-schema-validator")
implementation("io.micronaut.rss:micronaut-rss")
implementation("org.asciidoctor:asciidoctorj:3.0.0")
testImplementation("org.skyscreamer:jsonassert:1.5.3")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,9 @@ import groovy.transform.CompileStatic
import groovy.transform.Memoized
import io.micronaut.context.ApplicationContext
import io.micronaut.core.annotation.NonNull
import io.micronaut.core.io.ResourceLoader
import io.micronaut.core.io.scan.DefaultClassPathResourceLoader
import io.micronaut.core.util.StringUtils
import io.micronaut.guides.core.App
import io.micronaut.guides.core.DefaultVersionLoader
import io.micronaut.guides.core.DependencyLines
import io.micronaut.guides.core.Guide
import io.micronaut.guides.core.GuideUtils
import io.micronaut.guides.core.GuidesOption
import io.micronaut.guides.core.VersionLoader
import io.micronaut.guides.GuideProjectGenerator
import io.micronaut.guides.core.*
import io.micronaut.starter.api.TestFramework
import io.micronaut.starter.build.dependencies.Coordinate
import io.micronaut.starter.build.dependencies.PomDependencyVersionResolver
Expand All @@ -29,12 +22,7 @@ import java.util.regex.Pattern
import java.util.stream.Collectors

import static io.micronaut.starter.api.TestFramework.SPOCK
import static io.micronaut.starter.application.ApplicationType.CLI
import static io.micronaut.starter.application.ApplicationType.DEFAULT
import static io.micronaut.starter.application.ApplicationType.FUNCTION
import static io.micronaut.starter.application.ApplicationType.GRPC
import static io.micronaut.starter.application.ApplicationType.MESSAGING
import static io.micronaut.starter.options.Language.GROOVY
import static io.micronaut.starter.application.ApplicationType.*

@CompileStatic
class GuideAsciidocGenerator {
Expand Down Expand Up @@ -183,7 +171,7 @@ class GuideAsciidocGenerator {
String app = matches[1] ?: 'default'
cliCommandForApp(metadata, app)
.orElseThrow {
new GradleException("No CLI command found for app: $app -- should be one of ${metadata.apps().stream().flatMap {"@$it:cli-command@"}.collect(Collectors.joining(", "))}")
new GradleException("No CLI command found for app: $app -- should be one of ${metadata.apps().stream().flatMap { "@$it:cli-command@" }.collect(Collectors.joining(", "))}")
}
}

Expand Down Expand Up @@ -267,12 +255,11 @@ class GuideAsciidocGenerator {
include rawLine, rawLines, projectDir, true
} else if (rawLine.startsWith(EXTERNAL) && rawLine.endsWith(']')) {
include rawLine, rawLines, projectDir, false
}else if (rawLine.startsWith(COMMON_PARAMETRIZED) && rawLine.endsWith(']')){
} else if (rawLine.startsWith(COMMON_PARAMETRIZED) && rawLine.endsWith(']')) {
rawLines.addAll includeParametrized(rawLine, projectDir, true)
} else if (rawLine.startsWith(EXTERNAL_PARAMETRIZED) && rawLine.endsWith(']')){
} else if (rawLine.startsWith(EXTERNAL_PARAMETRIZED) && rawLine.endsWith(']')) {
rawLines.addAll includeParametrized(rawLine, projectDir, false)
}
else {
} else {
rawLines << rawLine
}
}
Expand Down Expand Up @@ -339,9 +326,9 @@ class GuideAsciidocGenerator {
case 'L':
line = line.replace(replaceString, value.toUpperCase())
break
default:
default:
line = line.replace(replaceString, value)
break
break
}
newLines[i] = line
}
Expand Down Expand Up @@ -595,7 +582,7 @@ class GuideAsciidocGenerator {
if (features) {
featureNames = features.tokenize('|')
} else {
featureNames = ([] as List<String>) + GuideUtils.getAppVisibleFeatures(app,guidesOption.language)
featureNames = ([] as List<String>) + GuideUtils.getAppVisibleFeatures(app, guidesOption.language)
}

String featureExcludes = extractFromParametersLine(line, 'featureExcludes')
Expand Down Expand Up @@ -657,12 +644,12 @@ class GuideAsciidocGenerator {
List<String> attrs = line.substring(line.indexOf("[") + 1, line.indexOf("]")).tokenize(",")

return attrs
.stream()
.filter({ it.startsWith(attributeName) })
.map({ it.tokenize("=") })
.map({ it.get(1) })
.findFirst()
.orElse("")
.stream()
.filter({ it.startsWith(attributeName) })
.map({ it.tokenize("=") })
.map({ it.get(1) })
.findFirst()
.orElse("")
}

private static String resolveAsciidoctorLanguage(String fileName) {
Expand Down
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-";
}
}
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-";
}
}
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 "";
}
}
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 "";
}
}
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;
}

}
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@ static List<String> findMacroLines(@NonNull String str, @NonNull String macro) {
.toList();
}

static List<String> findMacroInstances(@NonNull String str, @NonNull String macro) {
static List<String> findMacroInstances(@NonNull String str, @NonNull Pattern pattern) {
List<String> matches = new ArrayList<>();
Pattern pattern = Pattern.compile("@(?:([\\w-]*):)?" + macro + "@");
Matcher matcher = pattern.matcher(str);

while (matcher.find()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io.micronaut.guides.core.asciidoc.PlacheholderMacro;

import java.util.Optional;
import java.util.regex.Pattern;

abstract class PlaceholderWithTargetMacroSubstitution implements MacroSubstitution {

Expand All @@ -12,7 +13,8 @@ abstract class PlaceholderWithTargetMacroSubstitution implements MacroSubstituti
protected abstract String getSubstitution(Guide guide, GuidesOption option, String app);

public String substitute(String str, Guide guide, GuidesOption option) {
for (String instance : MacroUtils.findMacroInstances(str, getMacroName())) {
Pattern pattern = Pattern.compile("@(?:([\\w-]*):)?" + getMacroName() + "@");
for (String instance : MacroUtils.findMacroInstances(str, pattern)) {
Optional<PlacheholderMacro> macroOptional = PlacheholderMacro.of(getMacroName(), instance);
if (macroOptional.isEmpty()) {
continue;
Expand Down
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;
}
}

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();
}
Loading
Loading