Skip to content

Commit

Permalink
#12 load extensions from theme
Browse files Browse the repository at this point in the history
  • Loading branch information
Thorsten Marx committed Nov 13, 2023
1 parent 8dfc0db commit 1a41a31
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public class ExtensionHolder implements AutoCloseable {

@Getter
private final Context context;
private final Context themeContext;

public void registerHttpExtension(final String method, final String path, final ExtensionHttpHandler handler) {
httpHandlerExtensions.add(new HttpHandlerExtension(method, path, handler));
Expand All @@ -75,5 +76,8 @@ public void addTag(final String tag, final Function<ContentTags.Parameter, Strin
@Override
public void close() throws Exception {
context.close();
if (themeContext != null) {
themeContext.close();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public class ExtensionManager implements AutoCloseable {
private Engine engine;

List<Source> sources = new ArrayList<>();
List<Source> theme_sources = new ArrayList<>();

private ClassLoader getClassLoader() throws IOException {
Path libs = fileSystem.resolve("libs/");
Expand All @@ -80,30 +81,41 @@ public void init() throws IOException {
.option("engine.WarnInterpreterOnly", "false")
.build();

if (!theme.empty()) {
var themeExtPath = theme.extensionsPath();
if (Files.exists(themeExtPath)) {
log.debug("load extensions from theme");
loadExtensions(themeExtPath, theme_sources);
}
}
var extPath = fileSystem.resolve("extensions/");
if (Files.exists(extPath)) {
log.debug("try to find extensions");
Files.list(extPath)
.filter(path -> !Files.isDirectory(path) && path.getFileName().toString().endsWith(".js"))
.forEach(extFile -> {
try {
log.debug("load extension {}", extFile.getFileName().toString());
Source source = Source.newBuilder(
"js",
Files.readString(extFile, StandardCharsets.UTF_8),
extFile.getFileName().toString() + ".mjs")
.encoding(StandardCharsets.UTF_8)
.build();

sources.add(source);
} catch (IOException ex) {
log.error("", ex);
}
});
log.debug("load extensions from site");
loadExtensions(extPath, sources);
}
}
}

protected void loadExtensions(Path extPath, List<Source> sources) throws IOException {
Files.list(extPath)
.filter(path -> !Files.isDirectory(path) && path.getFileName().toString().endsWith(".js"))
.forEach(extFile -> {
try {
log.debug("load extension {}", extFile.getFileName().toString());
Source source = Source.newBuilder(
"js",
Files.readString(extFile, StandardCharsets.UTF_8),
extFile.getFileName().toString() + ".mjs")
.encoding(StandardCharsets.UTF_8)
.build();

sources.add(source);
} catch (IOException ex) {
log.error("", ex);
}
});
}

public ExtensionHolder newContext() throws IOException {
var context = Context.newBuilder()
.allowAllAccess(true)
Expand All @@ -116,7 +128,21 @@ public ExtensionHolder newContext() throws IOException {
.build())
.engine(engine).build();

ExtensionHolder holder = new ExtensionHolder(context);
Context themeContext = null;
if (!theme.empty()) {
themeContext = Context.newBuilder()
.allowAllAccess(true)
.allowHostClassLookup(className -> true)
.allowHostAccess(HostAccess.ALL)
.allowValueSharing(true)
.hostClassLoader(getClassLoader())
.allowIO(IOAccess.newBuilder()
.fileSystem(new ExtensionFileSystem(theme.extensionsPath()))
.build())
.engine(engine).build();
}

ExtensionHolder holder = new ExtensionHolder(context, themeContext);

final Value bindings = context.getBindings("js");
bindings.putMember("extensions", holder);
Expand All @@ -125,6 +151,16 @@ public ExtensionHolder newContext() throws IOException {

sources.forEach(context::eval);

if (!theme.empty()) {
final Value themeBindings = themeContext.getBindings("js");
themeBindings.putMember("extensions", holder);
themeBindings.putMember("fileSystem", fileSystem);
themeBindings.putMember("theme", theme);

theme_sources.forEach(themeContext::eval);
}


return holder;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ public abstract class TemplateEngineTest {

protected RequestContext requestContext () {
var markdownRenderer = TestHelper.getRenderer();
return new RequestContext("", Map.of(), new RenderContext(new ExtensionHolder(null), markdownRenderer, new ContentTags(Map.of())));
return new RequestContext("", Map.of(), new RenderContext(new ExtensionHolder(null, null), markdownRenderer, new ContentTags(Map.of())));
}
}

0 comments on commit 1a41a31

Please sign in to comment.