Now using gst-lib.

This commit is contained in:
JesseBrault0709 2023-06-11 13:38:30 +02:00
parent f5697fb99b
commit bc28a00cfc
7 changed files with 77 additions and 56 deletions

View File

@ -7,6 +7,9 @@ repositories {
}
dependencies {
// https://archiva.jessebrault.com/#artifact/com.jessebrault.gst/lib
implementation 'com.jessebrault.gst:lib:0.0.1'
// https://mvnrepository.com/artifact/org.apache.groovy/groovy-templates
implementation 'org.apache.groovy:groovy-templates:4.0.12'

View File

@ -19,20 +19,14 @@ final class GspPageRenderer implements PageRenderer {
RenderContext context
) {
def diagnostics = []
try {
def result = this.gspRenderer.render(specialPage.text, context) {
it.diagnosticsConsumer = diagnostics.&addAll
it.loggerName = "GspSpecialPage(${ specialPage.path })"
}
Result.of(diagnostics, result.toString())
} catch (Exception e) {
Result.of(
[*diagnostics, new Diagnostic(
"An exception occurred while rendering specialPage ${ specialPage.path }:\n${ e }",
e
)],
''
)
def result = this.gspRenderer.render(specialPage.text, context) {
it.diagnosticsConsumer = diagnostics.&addAll
it.loggerName = "GspSpecialPage(${ specialPage.path })"
}
if (result.hasDiagnostics()) {
Result.ofDiagnostics(diagnostics + result.diagnostics)
} else {
Result.of(diagnostics, result.get())
}
}

View File

@ -26,24 +26,18 @@ final class GspPartRenderer implements PartRenderer {
requireNonNull(binding)
requireNonNull(context)
def diagnostics = []
try {
def result = this.gspRenderer.render(part.text, context) {
it.putCustom('binding', binding)
it.diagnosticsConsumer = diagnostics.&addAll
it.loggerName = "GspPart(${ part.path })"
if (text) {
it.text = text
}
def result = this.gspRenderer.render(part.text, context) {
it.putCustom('binding', binding)
it.diagnosticsConsumer = diagnostics.&addAll
it.loggerName = "GspPart(${ part.path })"
if (text) {
it.text = text
}
Result.of(diagnostics, result.toString())
} catch (Exception e) {
Result.of(
[*diagnostics, new Diagnostic(
"An exception occurred while rendering part ${ part.path }:\n${ e }",
e
)],
''
)
}
if (result.hasDiagnostics()) {
Result.ofDiagnostics(diagnostics + result.diagnostics)
} else {
Result.of(diagnostics, result.get())
}
}

View File

@ -1,28 +1,39 @@
package com.jessebrault.ssg.render
import com.jessebrault.gst.TemplateCreator
import com.jessebrault.gst.groovy.GroovyTemplateCreator
import com.jessebrault.gst.parser.ExtendedGstParser
import com.jessebrault.ssg.dsl.StandardDslMap
import groovy.text.GStringTemplateEngine
import groovy.text.TemplateEngine
import org.codehaus.groovy.control.CompilerConfiguration
import com.jessebrault.ssg.util.Diagnostic
import com.jessebrault.ssg.util.Result
import java.util.function.Consumer
final class StandardGspRenderer {
private final TemplateEngine engine
private final TemplateCreator templateCreator
StandardGspRenderer(ClassLoader parentClassLoader) {
def cc = new CompilerConfiguration() // TODO: investigate if this makes any difference on the ultimate template
def gcl = new GroovyClassLoader(parentClassLoader, cc)
this.engine = new GStringTemplateEngine(gcl)
this.templateCreator = new GroovyTemplateCreator(ExtendedGstParser::new, [], parentClassLoader, true)
}
String render(
String template,
Result<String> render(
String templateText,
RenderContext context,
Consumer<StandardDslMap.Builder> dslMapBuilderConsumer
) {
this.engine.createTemplate(template).make(StandardDslMap.get(context, dslMapBuilderConsumer)).toString()
try {
def templateCreateResult = this.templateCreator.create(templateText)
if (templateCreateResult.hasDiagnostics()) {
Result.ofDiagnostics(templateCreateResult.diagnostics.collect {
new Diagnostic(it.message, it.exception)
})
} else {
Result.of(templateCreateResult.get().make(StandardDslMap.get(context, dslMapBuilderConsumer)))
}
} catch (Exception e) {
Result.ofDiagnostics([new Diagnostic(e.message, e)])
}
}
}

View File

@ -21,21 +21,15 @@ final class GspTemplateRenderer implements TemplateRenderer {
RenderContext context
) {
def diagnostics = []
try {
def result = this.gspRenderer.render(template.text, context) {
it.diagnosticsConsumer = diagnostics.&addAll
it.loggerName = "GspTemplate(${ template.path })"
it.text = text
}
Result.of(diagnostics, result)
} catch (Exception e) {
Result.of(
[*diagnostics, new Diagnostic(
"An exception occurred while rendering Template ${ template.path }:\n${ e }",
e
)],
''
)
def result = this.gspRenderer.render(template.text, context) {
it.diagnosticsConsumer = diagnostics.&addAll
it.loggerName = "GspTemplate(${ template.path })"
it.text = text
}
if (result.hasDiagnostics()) {
Result.ofDiagnostics(diagnostics + result.diagnostics)
} else {
Result.of(diagnostics, result.get())
}
}

View File

@ -1,3 +1,27 @@
plugins {
id 'groovy-gradle-plugin'
}
repositories {
maven {
url 'https://archiva.jessebrault.com/repository/internal/'
credentials {
username System.getenv('JBARCHIVA_USERNAME')
password System.getenv('JBARCHIVA_PASSWORD')
}
}
maven {
url 'https://archiva.jessebrault.com/repository/snapshots/'
credentials {
username System.getenv('JBARCHIVA_USERNAME')
password System.getenv('JBARCHIVA_PASSWORD')
}
}
}
dependencies {
implementation 'com.jessebrault.jbarchiva:jbarchiva:0.1.0'
}

View File

@ -1,4 +1,5 @@
plugins {
id 'com.jessebrault.jbarchiva'
id 'groovy'
id 'java-library'
id 'java-test-fixtures'