diff --git a/CHANGELOG.md b/CHANGELOG.md index 4a6668d..62f7404 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,13 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] +## [0.4.1] - 2024-08-16 + +### Fixed + +- Articles are retrieved from snapshot version if possible. +- Playbook filename is now a command line argument. Default is `playbook.json`. + ## [0.4.0] - 2024-08-15 ### Added @@ -70,7 +77,9 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). - Initial release. -[Unreleased]: https://github.com/refinedmods/refinedsites/compare/v0.4.0...HEAD +[Unreleased]: https://github.com/refinedmods/refinedsites/compare/v0.4.1...HEAD + +[0.4.1]: https://github.com/refinedmods/refinedsites/compare/v0.4.0...v0.4.1 [0.4.0]: https://github.com/refinedmods/refinedsites/compare/v0.3.0...v0.4.0 diff --git a/src/main/java/com/refinedmods/refinedsites/RefinedSites.java b/src/main/java/com/refinedmods/refinedsites/RefinedSites.java index 69b3a05..0c118a2 100644 --- a/src/main/java/com/refinedmods/refinedsites/RefinedSites.java +++ b/src/main/java/com/refinedmods/refinedsites/RefinedSites.java @@ -18,7 +18,7 @@ public static void main(final String[] args) { log.info("Loading playbook from {}", args[0]); final Path rootPath = Paths.get(args[0]); final SiteFactory siteFactory = new SiteFactory(rootPath); - final Site site = siteFactory.getSite(); + final Site site = siteFactory.getSite(args.length == 2 ? args[1] : "playbook.json"); log.info("Loaded site {}", site); final Renderer renderer = new Renderer(rootPath, rootPath.resolve("output/"), site.getUrl()); renderer.render(site); diff --git a/src/main/java/com/refinedmods/refinedsites/model/Component.java b/src/main/java/com/refinedmods/refinedsites/model/Component.java index b389af4..62e942d 100644 --- a/src/main/java/com/refinedmods/refinedsites/model/Component.java +++ b/src/main/java/com/refinedmods/refinedsites/model/Component.java @@ -31,4 +31,9 @@ public String getAssetsOutputPath() { public String getRelativePagePath(final Path from, final Path to) { return from.relativize(to).toString().replace(".adoc", ".html"); } + + @Override + public String toString() { + return name + "@" + version; + } } diff --git a/src/main/java/com/refinedmods/refinedsites/playbook/SiteFactory.java b/src/main/java/com/refinedmods/refinedsites/playbook/SiteFactory.java index 7646cdc..e2849b2 100644 --- a/src/main/java/com/refinedmods/refinedsites/playbook/SiteFactory.java +++ b/src/main/java/com/refinedmods/refinedsites/playbook/SiteFactory.java @@ -37,10 +37,10 @@ public class SiteFactory { private final Path rootPath; - public Site getSite() { + public Site getSite(final String playbookJsonFilename) { try { log.info("Loading playbook"); - final Path playbookPath = rootPath.resolve("playbook.json"); + final Path playbookPath = rootPath.resolve(playbookJsonFilename); final PlaybookConfig json = GSON.fromJson(Files.readString(playbookPath), PlaybookConfig.class); log.info("Loaded playbook"); final List components = json.getComponents().stream().flatMap(component -> { diff --git a/src/main/java/com/refinedmods/refinedsites/render/Renderer.java b/src/main/java/com/refinedmods/refinedsites/render/Renderer.java index 2cd1917..5cef539 100644 --- a/src/main/java/com/refinedmods/refinedsites/render/Renderer.java +++ b/src/main/java/com/refinedmods/refinedsites/render/Renderer.java @@ -104,8 +104,27 @@ public void render(final Site site) { for (final Component component : site.getComponents()) { renderComponentPre(component); } - for (final Component component : site.getComponents()) { - renderComponent(component, assetsPath, site, sitemapIndex); + final var components = site.getComponents() + .stream() + .collect(Collectors.groupingBy(Component::getName)); + for (final var entry : components.entrySet()) { + log.info("Rendering versions of component {}", entry.getKey()); + final List componentVersions = entry.getValue(); + final Component snapshotComponent = componentVersions.stream() + .filter(c -> c.getVersion().snapshot()) + .findFirst() + .orElse(null); + List articles = null; + if (snapshotComponent != null) { + articles = renderComponent(snapshotComponent, assetsPath, site, sitemapIndex, null); + log.info("Reusing {} articles from snapshot in other components", articles.size()); + } + for (final Component component : componentVersions) { + if (component == snapshotComponent) { + continue; + } + renderComponent(component, assetsPath, site, sitemapIndex, articles); + } } sitemapIndex.write(); Files.writeString(outputPath.resolve("robots.txt"), "Sitemap: " + url + "/sitemap_index.xml"); @@ -149,10 +168,11 @@ private void renderComponentPre(final Component component) { component.setSlug(componentSlug); } - private void renderComponent(final Component component, - final Path assetsOutputPath, - final Site site, - final SitemapIndexGenerator sitemapIndex) + private List renderComponent(final Component component, + final Path assetsOutputPath, + final Site site, + final SitemapIndexGenerator sitemapIndex, + @Nullable final List articles) throws IOException { log.info("Rendering component {}", component); final Path componentOutputPath = getComponentOutputPath(component); @@ -192,18 +212,9 @@ private void renderComponent(final Component component, infosByPageType.computeIfAbsent(singleInfo.type(), k -> new ArrayList<>()).add(singleInfo); } prepareNavigationItems(component.getNavigationItems(), pageInfo); - final List articles = infosByPageType.getOrDefault("article", Collections.emptyList()) - .stream() - .map(info -> new ArticleRender( - info.title(), - info.description(), - info.relativePath(), - info.date().orElse(LocalDate.EPOCH) - )) - .sorted(Comparator.comparing(ArticleRender::getDate).reversed()) - .toList(); - writeRssFeed(component, sitemapBaseUrl, articles, componentOutputPath); + final List theArticles = articles == null ? getArticles(infosByPageType) : articles; + writeRssFeed(component, sitemapBaseUrl, theArticles, componentOutputPath); for (final Path pagePath : component.getPages()) { renderPage( @@ -217,13 +228,28 @@ private void renderComponent(final Component component, releaseMatchingComponentVersion, sitemapBaseUrl, componentSitemap, - articles + theArticles ); } if (componentSitemap != null) { componentSitemap.write(); sitemapIndex.addUrl(sitemapBaseUrl + "/sitemap.xml", renderDate); } + + return theArticles; + } + + private static List getArticles(final Map> infosByPageType) { + return infosByPageType.getOrDefault("article", Collections.emptyList()) + .stream() + .map(info -> new ArticleRender( + info.title(), + info.description(), + info.relativePath(), + info.date().orElse(LocalDate.EPOCH) + )) + .sorted(Comparator.comparing(ArticleRender::getDate).reversed()) + .toList(); } private static void writeRssFeed(final Component component,