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

Allow other extensions such as json, yaml, xml, ... #162

Merged
merged 1 commit into from
Oct 9, 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
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ public static String removeExtension(String path) {
return i > 0 ? path.substring(0, i) : path;
}

public static String getExtension(String path) {
final int i = path.lastIndexOf(".");
return i > 0 ? path.substring(i + 1) : null;
}

public static String fileName(String path) {
final int i = path.lastIndexOf("/");
if (i == -1) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
package io.quarkiverse.roq.plugin.aliases.deployment;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;

import io.quarkiverse.roq.frontmatter.deployment.Link;
import io.quarkiverse.roq.frontmatter.deployment.TemplateLink;
import io.quarkiverse.roq.frontmatter.deployment.data.RoqFrontMatterTemplateBuildItem;
import io.quarkiverse.roq.frontmatter.runtime.RoqSiteConfig;
import io.quarkiverse.roq.frontmatter.runtime.model.RoqUrl;
Expand Down Expand Up @@ -59,8 +55,8 @@ public void consumeTemplates(
}
RoqUrl url = item.url();
for (String alias : aliasesName) {
String aliasLink = Link.pageLink(config.rootPath(), alias, new Link.PageLinkData(
item.raw().info().baseFileName(), item.raw().info().date(), item.raw().collection(), item.data()));
String aliasLink = TemplateLink.pageLink(config.rootPath(), alias, new TemplateLink.PageLinkData(
item.raw().info(), item.raw().collection(), item.data()));
aliasMap.put(aliasLink, url.path());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
import java.util.List;
import java.util.Map;

import io.quarkiverse.roq.frontmatter.deployment.Link;
import io.quarkiverse.roq.frontmatter.deployment.Link.PageLinkData;
import io.quarkiverse.roq.frontmatter.deployment.RoqFrontMatterRootUrlBuildItem;
import io.quarkiverse.roq.frontmatter.deployment.TemplateLink;
import io.quarkiverse.roq.frontmatter.deployment.TemplateLink.PageLinkData;
import io.quarkiverse.roq.frontmatter.deployment.data.RoqFrontMatterDocumentTemplateBuildItem;
import io.quarkiverse.roq.frontmatter.deployment.data.RoqFrontMatterPaginateTemplateBuildItem;
import io.quarkiverse.roq.frontmatter.deployment.publish.RoqFrontMatterPublishDerivedCollectionBuildItem;
Expand Down Expand Up @@ -67,7 +67,7 @@ void publishTagPages(
// For all the tags we create a derivation: tag -> document ids
for (String tag : tags) {
derived.computeIfAbsent(tag, k -> new ArrayList<>())
.add(document.raw().id());
.add(document.raw().resolvedPath());
}
}

Expand All @@ -81,9 +81,9 @@ void publishTagPages(
derivedCollectionProducer
.produce(new RoqFrontMatterPublishDerivedCollectionBuildItem(tagCollection, e.getValue(), data));

final String link = Link.pageLink(config.rootPath(),
final String link = TemplateLink.pageLink(config.rootPath(),
data.getString(LINK_KEY, DEFAULT_TAGGING_COLLECTION_LINK_TEMPLATE),
new PageLinkData(item.info().baseFileName(), item.info().date(), tagCollection, data));
new PageLinkData(item.info(), tagCollection, data));
final RoqUrl url = rootUrl.rootUrl().resolve(link);
// Dealing with pagination is as simple as those two lines:
if (data.containsKey(PAGINATE_KEY)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ void bindQuteTemplates(
}
final Set<String> docTemplates = new HashSet<>();
final Set<String> pageTemplates = new HashSet<>();
final Set<String> layoutTemplates = new HashSet<>();
// Produce generated Qute templates
for (RoqFrontMatterRawTemplateBuildItem item : roqFrontMatterTemplates) {
templatePathProducer
Expand All @@ -52,6 +53,8 @@ void bindQuteTemplates(
} else {
pageTemplates.add(item.info().generatedTemplatePath());
}
} else {
layoutTemplates.add(item.info().generatedTemplatePath());
}
}

Expand All @@ -63,8 +66,10 @@ void bindQuteTemplates(
} else if (pageTemplates.contains(c.getTemplateId())) {
c.addParameter("page", NormalPage.class.getName());
c.addParameter("site", Site.class.getName());
} else if (layoutTemplates.contains(c.getTemplateId())) {
c.addParameter("page", Page.class.getName());
c.addParameter("site", Site.class.getName());
}

}));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,42 +12,49 @@
import java.util.Optional;
import java.util.function.Supplier;

import io.quarkiverse.roq.frontmatter.runtime.model.PageInfo;
import io.quarkiverse.roq.util.PathUtils;
import io.vertx.core.json.JsonObject;

public class Link {
public class TemplateLink {
public static final String DEFAULT_PAGE_LINK_TEMPLATE = "/:name";
public static final String DEFAULT_PAGINATE_LINK_TEMPLATE = "/:collection/page:page";
private static final DateTimeFormatter YEAR_FORMAT = DateTimeFormatter.ofPattern("yyyy");
private static final DateTimeFormatter MONTH_FORMAT = DateTimeFormatter.ofPattern("MM");
private static final DateTimeFormatter DAY_FORMAT = DateTimeFormatter.ofPattern("dd");

public interface LinkData {
String baseFileName();
PageInfo pageInfo();

String collection();

ZonedDateTime date();

JsonObject data();
}

public record PageLinkData(String baseFileName, ZonedDateTime date, String collection,
public record PageLinkData(PageInfo pageInfo, String collection,
JsonObject data) implements LinkData {
}

public record PaginateLinkData(String baseFileName, ZonedDateTime date, String collection, String page,
public record PaginateLinkData(PageInfo pageInfo, String collection, String page,
JsonObject data) implements LinkData {
}

private static Map<String, Supplier<String>> withBasePlaceHolders(LinkData data, Map<String, Supplier<String>> other) {
Map<String, Supplier<String>> result = new HashMap<>(Map.ofEntries(
Map.entry(":collection", data::collection),
Map.entry(":year", () -> Optional.ofNullable(data.date()).orElse(ZonedDateTime.now()).format(YEAR_FORMAT)),
Map.entry(":month", () -> Optional.ofNullable(data.date()).orElse(ZonedDateTime.now()).format(MONTH_FORMAT)),
Map.entry(":day", () -> Optional.ofNullable(data.date()).orElse(ZonedDateTime.now()).format(DAY_FORMAT)),
Map.entry(":name", () -> slugify(data.baseFileName())),
Map.entry(":title", () -> data.data().getString("slug", slugify(data.baseFileName())))));
Map.entry(":year",
() -> Optional.ofNullable(data.pageInfo().date()).orElse(ZonedDateTime.now()).format(YEAR_FORMAT)),
Map.entry(":month",
() -> Optional.ofNullable(data.pageInfo().date()).orElse(ZonedDateTime.now()).format(MONTH_FORMAT)),
Map.entry(":day",
() -> Optional.ofNullable(data.pageInfo().date()).orElse(ZonedDateTime.now()).format(DAY_FORMAT)),
Map.entry(":name", () -> {
if (data.pageInfo().isHtml()) {
return slugify(data.pageInfo().baseFileName());
}
return slugify(data.pageInfo().baseFileName()) + "." + data.pageInfo().getExtension();
}),
Map.entry(":title", () -> data.data().getString("slug", slugify(data.pageInfo().baseFileName())))));
if (other != null) {
result.putAll(other);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ public int order() {

public interface DataModifier {

JsonObject modify(String id, String templatePath, JsonObject fm);
JsonObject modify(String resolvedPath, String sourcePath, JsonObject fm);
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package io.quarkiverse.roq.frontmatter.deployment.data;

import static io.quarkiverse.roq.frontmatter.deployment.Link.DEFAULT_PAGE_LINK_TEMPLATE;
import static io.quarkiverse.roq.frontmatter.deployment.TemplateLink.DEFAULT_PAGE_LINK_TEMPLATE;

import java.util.List;
import java.util.Map;
import java.util.Stack;
import java.util.function.Function;
import java.util.stream.Collectors;

import io.quarkiverse.roq.frontmatter.deployment.Link;
import io.quarkiverse.roq.frontmatter.deployment.RoqFrontMatterRootUrlBuildItem;
import io.quarkiverse.roq.frontmatter.deployment.TemplateLink;
import io.quarkiverse.roq.frontmatter.deployment.publish.RoqFrontMatterPublishPageBuildItem;
import io.quarkiverse.roq.frontmatter.deployment.scan.RoqFrontMatterRawTemplateBuildItem;
import io.quarkiverse.roq.frontmatter.runtime.RoqSiteConfig;
Expand All @@ -35,14 +35,14 @@ void prepareData(HttpBuildTimeConfig httpConfig,
}

final var byKey = roqFrontMatterTemplates.stream()
.collect(Collectors.toMap(RoqFrontMatterRawTemplateBuildItem::id, Function.identity()));
.collect(Collectors.toMap(RoqFrontMatterRawTemplateBuildItem::resolvedPath, Function.identity()));
final RootUrl rootUrl = new RootUrl(config.urlOptional().orElse(""), httpConfig.rootPath);
rootUrlProducer.produce(new RoqFrontMatterRootUrlBuildItem(rootUrl));

for (RoqFrontMatterRawTemplateBuildItem item : roqFrontMatterTemplates) {
JsonObject data = mergeParents(item, byKey);
final String link = Link.pageLink(config.rootPath(), data.getString(LINK_KEY, DEFAULT_PAGE_LINK_TEMPLATE),
new Link.PageLinkData(item.info().baseFileName(), item.info().date(), item.collection(), data));
final String link = TemplateLink.pageLink(config.rootPath(), data.getString(LINK_KEY, DEFAULT_PAGE_LINK_TEMPLATE),
new TemplateLink.PageLinkData(item.info(), item.collection(), data));
RoqFrontMatterTemplateBuildItem templateItem = new RoqFrontMatterTemplateBuildItem(item, rootUrl.resolve(link),
data);
templatesProducer.produce(templateItem);
Expand Down Expand Up @@ -86,7 +86,11 @@ public static JsonObject mergeParents(RoqFrontMatterRawTemplateBuildItem item,
String parent = item.layout();
fms.add(item.data());
while (parent != null) {
if (byPath.containsKey(parent)) {
if (byPath.containsKey(parent + ".html")) {
final RoqFrontMatterRawTemplateBuildItem parentItem = byPath.get(parent + ".html");
parent = parentItem.layout();
fms.push(parentItem.data());
} else if (byPath.containsKey(parent)) {
final RoqFrontMatterRawTemplateBuildItem parentItem = byPath.get(parent);
parent = parentItem.layout();
fms.push(parentItem.data());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.quarkiverse.roq.frontmatter.deployment.publish;

import static io.quarkiverse.roq.frontmatter.deployment.Link.DEFAULT_PAGINATE_LINK_TEMPLATE;
import static io.quarkiverse.roq.frontmatter.deployment.TemplateLink.DEFAULT_PAGINATE_LINK_TEMPLATE;
import static io.quarkiverse.roq.frontmatter.deployment.data.RoqFrontMatterDataProcessor.PAGINATE_KEY;

import java.util.ArrayList;
Expand All @@ -11,9 +11,9 @@

import org.jboss.logging.Logger;

import io.quarkiverse.roq.frontmatter.deployment.Link;
import io.quarkiverse.roq.frontmatter.deployment.Paginate;
import io.quarkiverse.roq.frontmatter.deployment.RoqFrontMatterRootUrlBuildItem;
import io.quarkiverse.roq.frontmatter.deployment.TemplateLink;
import io.quarkiverse.roq.frontmatter.deployment.data.RoqFrontMatterDocumentTemplateBuildItem;
import io.quarkiverse.roq.frontmatter.deployment.data.RoqFrontMatterPaginateTemplateBuildItem;
import io.quarkiverse.roq.frontmatter.deployment.scan.RoqFrontMatterRawTemplateBuildItem;
Expand Down Expand Up @@ -66,11 +66,11 @@ public void paginatePublish(RoqSiteConfig config,
for (RoqFrontMatterPaginateTemplateBuildItem pagination : paginationList) {
final RoqFrontMatterRawTemplateBuildItem item = pagination.raw();
final JsonObject data = pagination.data();
Paginate paginate = readPaginate(item.id(), data, pagination.defaultPaginatedCollection());
Paginate paginate = readPaginate(item.resolvedPath(), data, pagination.defaultPaginatedCollection());
AtomicInteger collectionSize = sizeByCollection.get(paginate.collection());
if (collectionSize == null) {
throw new ConfigurationException(
"Paginate collection not found '" + paginate.collection() + "' in " + item.id());
"Paginate collection not found '" + paginate.collection() + "' in " + item.resolvedPath());
}
final int total = collectionSize.get();
if (paginate.size() <= 0) {
Expand All @@ -85,14 +85,14 @@ public void paginatePublish(RoqSiteConfig config,
if (i == 1) {
paginatedUrl = pagination.url();
} else {
final String link = Link.paginateLink(config.rootPath(), linkTemplate,
new Link.PaginateLinkData(item.info().baseFileName(), item.info().date(),
final String link = TemplateLink.paginateLink(config.rootPath(), linkTemplate,
new TemplateLink.PaginateLinkData(item.info(),
paginate.collection(), Integer.toString(i), data));
paginatedUrl = rootUrl.resolve(link);
}
PageInfo info = item.info();
if (i > 1) {
info = info.changeId(paginatedUrl.path());
info = info.changeResolvedPath(paginatedUrl.path());
}
paginatedPages.add(new PageToPublish(paginatedUrl, info, data));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ void bindCollections(
final Supplier<DocumentPage> document = recorder.createDocument(item.collection(),
url,
item.info(), item.data());
documentsById.put(item.info().id(), document);
pagesProducer.produce(new RoqFrontMatterPageBuildItem(item.info().id(), url, document));
documentsById.put(item.info().resolvedPath(), document);
pagesProducer.produce(new RoqFrontMatterPageBuildItem(item.info().resolvedPath(), url, document));
docs.add(document);
}
collectionsProducer.produce(new RoqFrontMatterCollectionBuildItem(e.getKey(), docs));
Expand All @@ -70,7 +70,7 @@ void bindCollections(
for (String id : i.documentIds()) {
final Supplier<DocumentPage> doc = documentsById.get(id);
if (doc == null) {
throw new IllegalStateException("No document found for id " + id);
throw new IllegalStateException("No document found for resolvedPath " + id);
}
docs.add(doc);
}
Expand All @@ -93,9 +93,10 @@ void bindPages(
for (RoqFrontMatterPublishPageBuildItem page : pages) {
final Supplier<NormalPage> recordedPage = recorder.createPage(page.url(),
page.info(), page.data(), page.paginator());
pagesProducer.produce(new RoqFrontMatterPageBuildItem(page.info().id(), page.url(), recordedPage));
normalPagesProducer.produce(new RoqFrontMatterNormalPageBuildItem(page.info().id(), page.url(), recordedPage));
if (page.info().id().equals("index")) {
pagesProducer.produce(new RoqFrontMatterPageBuildItem(page.info().resolvedPath(), page.url(), recordedPage));
normalPagesProducer
.produce(new RoqFrontMatterNormalPageBuildItem(page.info().resolvedPath(), page.url(), recordedPage));
if (page.info().resolvedPath().equals("index.html")) {
indexPageProducer.produce(new RoqFrontMatterIndexPageBuildItem(recordedPage));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ public boolean isPage() {
return isPage;
}

public String id() {
return info.id();
public String resolvedPath() {
return info.resolvedPath();
}

public PageInfo info() {
Expand Down
Loading