Skip to content

Commit

Permalink
Merge pull request #16 from refinedmods/release/0.3.0
Browse files Browse the repository at this point in the history
Release v0.3.0
  • Loading branch information
raoulvdberge authored Dec 25, 2023
2 parents 520370c + 31b0ab4 commit 8477acc
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 14 deletions.
10 changes: 9 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

## [0.3.0] - 2023-12-25

### Added

- Support for images.

## [0.2.3] - 2023-12-24

### Fixed
Expand Down Expand Up @@ -49,7 +55,9 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

- Initial release.

[Unreleased]: https://github.com/refinedmods/refinedsites/compare/v0.2.3...HEAD
[Unreleased]: https://github.com/refinedmods/refinedsites/compare/v0.3.0...HEAD

[0.3.0]: https://github.com/refinedmods/refinedsites/compare/v0.2.3...v0.3.0

[0.2.3]: https://github.com/refinedmods/refinedsites/compare/v0.2.2...v0.2.3

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.refinedmods.refinedsites.render;

import java.nio.file.Path;
import java.util.List;
import java.util.Map;

import lombok.RequiredArgsConstructor;
import org.asciidoctor.ast.Document;
import org.asciidoctor.ast.StructuralNode;
import org.asciidoctor.extension.Treeprocessor;

@RequiredArgsConstructor
public class ImageTreeprocessor extends Treeprocessor {
private final Path currentPageSourcePath;
private final Path currentPageOutputPath;
private final Map<Path, Path> sourceToDestinationAssets;

@Override
public Document process(final Document document) {
processBlock(document);
return document;
}

private void processBlock(final StructuralNode block) {
final List<StructuralNode> blocks = block.getBlocks();
for (int i = 0; i < blocks.size(); i++) {
final StructuralNode currentBlock = blocks.get(i);
if ("image".equals(currentBlock.getContext())) {
final Path sourcePath = currentPageSourcePath.getParent().resolve(
(String) currentBlock.getAttribute("target")
).normalize();
final Path destinationPath = sourceToDestinationAssets.get(sourcePath);
if (destinationPath == null) {
throw new RuntimeException("Asset " + sourcePath + " not found");
}
final Path relativePath = currentPageOutputPath.getParent().relativize(destinationPath);
currentBlock.setAttribute("target", relativePath.toString(), true);
} else {
processBlock(currentBlock);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.refinedmods.refinedsites.render;

import java.nio.file.Path;
import java.util.List;

import lombok.Builder;
Expand All @@ -10,5 +11,6 @@ record PageInfo(String title,
String relativePath,
String icon,
IconReferences iconReferences,
List<TableOfContents> tableOfContents) {
List<TableOfContents> tableOfContents,
Path pageOutputPath) {
}
44 changes: 32 additions & 12 deletions src/main/java/com/refinedmods/refinedsites/render/Renderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,11 @@ private void renderComponent(final Component component,
sitemapBaseUrl,
componentOutputPath
);
final Map<Path, Path> sourceToDestinationAssets;
if (component.getAssetsPath() != null) {
copyComponentAssets(component, assetsOutputPath);
sourceToDestinationAssets = copyComponentAssets(component, assetsOutputPath);
} else {
sourceToDestinationAssets = Collections.emptyMap();
}
final Releases releases = site.getReleases(component);
if (component.isLatest() && releases != null) {
Expand All @@ -165,7 +168,13 @@ private void renderComponent(final Component component,
final PageAttributeCache pageAttributeCache = new PageAttributeCache();
final Map<Path, PageInfo> pageInfo = new HashMap<>();
for (final Path pagePath : component.getPages()) {
pageInfo.put(pagePath, renderPagePre(pagePath, component, pageAttributeCache));
pageInfo.put(pagePath, renderPagePre(
pagePath,
component,
pageAttributeCache,
sourceToDestinationAssets,
componentOutputPath
));
}
prepareNavigationItems(component.getNavigationItems(), pageInfo);
for (final Path pagePath : component.getPages()) {
Expand Down Expand Up @@ -300,38 +309,49 @@ private void prepareNavigationItems(final List<NavigationItem> navigationItems,
});
}

private void copyComponentAssets(final Component component,
final Path assetsOutputPath) throws IOException {
private Map<Path, Path> copyComponentAssets(final Component component,
final Path assetsOutputPath) throws IOException {
if (!Files.exists(component.getAssetsPath())) {
return;
return Collections.emptyMap();
}
final Path componentAssetsPath = assetsOutputPath.resolve(
component.getAssetsOutputPath() + "/"
);
final Map<Path, Path> sourceToDestinationAssets = new HashMap<>();
Files.walk(component.getAssetsPath()).forEach(path -> {
final Path relativePath = component.getAssetsPath().relativize(path);
final Path targetPath = componentAssetsPath.resolve(relativePath);
try {
Files.createDirectories(targetPath.getParent());
Files.copy(path, targetPath);
sourceToDestinationAssets.put(path.normalize(), targetPath);
} catch (final IOException e) {
throw new RuntimeException(e);
}
});
return sourceToDestinationAssets;
}

private PageInfo renderPagePre(final Path pagePath,
final Component component,
final PageAttributeCache attributeCache) {
final PageAttributeCache attributeCache,
final Map<Path, Path> sourceToDestinationAssets,
final Path componentOutputPath) {
log.info("Parsing Asciidoc for page {}", pagePath);
final String relativePath = component.getRelativePagePath(component.getPagesPath(), pagePath);
final Path pageOutputPath = componentOutputPath.resolve(relativePath);
final IconReferences icons = new IconReferences();
try (Asciidoctor asciidoctor = Asciidoctor.Factory.create()) {
final PageAttributeCache.PageAttributes pageAttributes = attributeCache.getAttributes(
asciidoctor,
pagePath
);
asciidoctor.javaExtensionRegistry().includeProcessor(new IncludeProcessorImpl());
asciidoctor.javaExtensionRegistry().treeprocessor(new ImageTreeprocessor(
pagePath,
pageOutputPath,
sourceToDestinationAssets
));
asciidoctor.javaExtensionRegistry().inlineMacro(new XRefInlineMacroProcessor(
component,
pagePath,
Expand All @@ -353,6 +373,7 @@ private PageInfo renderPagePre(final Path pagePath,
.replace("<table class=\"", "<table class=\"table table-striped table-bordered "))
.relativePath(relativePath)
.icon(pageAttributes.icon().orElse(null))
.pageOutputPath(pageOutputPath)
.build();
}
}
Expand Down Expand Up @@ -380,14 +401,13 @@ private void renderPage(final Path pagePath,
log.info("Rendering page {}", pagePath);
final Context context = new Context();
final PageInfo info = pageInfo.get(pagePath);
final Path pageOutputPath = componentOutputPath.resolve(info.relativePath());
Files.createDirectories(pageOutputPath.getParent());
final FileWriter fileWriter = new FileWriter(pageOutputPath.toFile());
Files.createDirectories(info.pageOutputPath().getParent());
final FileWriter fileWriter = new FileWriter(info.pageOutputPath().toFile());
context.setVariable("title", info.title());
context.setVariable("icon", info.icon());
final String iconsHtml = info.iconReferences().getHtml(
assetsOutputPath.resolve(component.getAssetsOutputPath() + "/"),
pageOutputPath
info.pageOutputPath()
);
context.setVariable("toc", info.tableOfContents());
context.setVariable("content", info.parsedContent() + iconsHtml);
Expand All @@ -402,12 +422,12 @@ private void renderPage(final Path pagePath,
.collect(Collectors.toList());
Collections.reverse(otherReleases);
context.setVariable("otherReleases", otherReleases);
linkBuilder.setCurrentPageOutputPath(pageOutputPath);
linkBuilder.setCurrentPageOutputPath(info.pageOutputPath());
final String template = getTemplate(pagePath);
templateEngine.process(template, context, fileWriter);
if (sitemapGenerator != null && !info.relativePath().contains("404")) {
sitemapGenerator.addUrl(new WebSitemapUrl.Options(
baseUrl + "/" + componentOutputPath.relativize(pageOutputPath)
baseUrl + "/" + componentOutputPath.relativize(info.pageOutputPath())
).lastMod(renderDate).changeFreq(ChangeFreq.DAILY).build());
}
}
Expand Down

0 comments on commit 8477acc

Please sign in to comment.