From ffeb5d6dbe62f78dfc2c66dca741107d195f1f98 Mon Sep 17 00:00:00 2001 From: "manik.magar" Date: Fri, 24 Jun 2016 21:19:23 -0400 Subject: [PATCH] Added Permalink feature --- src/main/java/org/jbake/app/Crawler.java | 83 ++++++++++++++++++++++++ 1 file changed, 83 insertions(+) diff --git a/src/main/java/org/jbake/app/Crawler.java b/src/main/java/org/jbake/app/Crawler.java index 1927d18c2..7977a07fd 100644 --- a/src/main/java/org/jbake/app/Crawler.java +++ b/src/main/java/org/jbake/app/Crawler.java @@ -11,15 +11,18 @@ import static java.io.File.separator; import java.io.File; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Date; import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.Set; import org.apache.commons.io.FilenameUtils; +import org.apache.commons.lang3.time.DateFormatUtils; import com.orientechnologies.orient.core.record.impl.ODocument; @@ -178,6 +181,9 @@ private void crawlSourceFile(final File sourceFile, final String sha1, final Str } } + buildPermalink(fileContents); + + if (config.getBoolean(Keys.URI_NO_EXTENSION)) { fileContents.put(Attributes.NO_EXTENSION_URI, uri.replace("/index.html", "/")); } @@ -191,6 +197,83 @@ private void crawlSourceFile(final File sourceFile, final String sha1, final Str LOGGER.warn("{} has an invalid header, it has been ignored!", sourceFile); } } + + /** + * This function generates permalinks if they are enabled in configuration. + * + * Sample configuration entry: permalink.pattern = + * + * @param fileContents + * @author Manik Magar + * @return + */ + private String buildPermalink(Map fileContents){ + String permalink = (String) fileContents.get(Attributes.URI); + + boolean permalinkEnabled = config.getBoolean("permalink"); + if (permalinkEnabled) { + + String pattern = config.getString("permalink.pattern"); + + String[] parts = pattern.split("/:"); + List pLink = new ArrayList(); + for (String part : parts){ + part = part.trim().replace("/", ""); + if (part.endsWith(":")){ + pLink.add(part.replace(":", "")); + } else if(fileContents.containsKey(part)){ + Object value = fileContents.get(part); + if (value instanceof String){ + pLink.add(value.toString()); + } else if (value.getClass().equals(String[].class)){ + pLink.addAll(Arrays.asList((String[])value)); + } + } else if (Arrays.asList("YEAR","MONTH","DAY").contains(part)) { + Date publishedDate = (Date) fileContents.get("date"); + if(Objects.nonNull(publishedDate)){ + String dateValue = null; + if(part.equalsIgnoreCase("YEAR")){ + dateValue = DateFormatUtils.format(publishedDate, "yyyy"); + } + if(part.equalsIgnoreCase("MONTH")){ + dateValue = DateFormatUtils.format(publishedDate, "MM"); + } + if(part.equalsIgnoreCase("DAY")){ + dateValue = DateFormatUtils.format(publishedDate, "dd"); + } + pLink.add(dateValue); + } + } + } + + permalink = String.join("/", pLink); + permalink = sanitize(permalink).concat("/"); + + boolean noExtensionUri = config.getBoolean(Keys.URI_NO_EXTENSION); + String noExtensionUriPrefix = config.getString(Keys.URI_NO_EXTENSION_PREFIX); + if (noExtensionUri && (noExtensionUriPrefix != null && noExtensionUriPrefix.trim().isEmpty())) { + String uri = permalink; + uri = uri + "/index.html"; + fileContents.put(Attributes.URI, uri); + } else { + permalink = permalink.substring(0, permalink.length() -1 ); + permalink = permalink + config.getString(Keys.OUTPUT_EXTENSION); + } + + } + + + return permalink; + } + + /** + * Replace the spaces with hyphens + * @return + */ + private String sanitize(String input){ + return input.replace(" ", "-"); + } + public String getPathToRoot(File sourceFile) { File rootPath = new File(contentPath);