Skip to content

Commit

Permalink
Implement Make "relative <img src> points to assets" optional jbake-o…
Browse files Browse the repository at this point in the history
…rg#502

Implement Make URL fixing optional jbake-org#500
These two are hitting the same code, so it's hard to split them.
  • Loading branch information
OndraZizka committed Jul 25, 2018
1 parent 2251844 commit 43e35b8
Show file tree
Hide file tree
Showing 12 changed files with 142 additions and 45 deletions.
11 changes: 7 additions & 4 deletions jbake-core/src/main/java/org/jbake/app/ConfigUtil.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package org.jbake.app;

import org.apache.commons.collections.IteratorUtils;
import java.io.File;
import org.apache.commons.configuration.CompositeConfiguration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.configuration.SystemConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;

/**
* Provides Configuration related functions.
*
Expand Down Expand Up @@ -207,9 +205,14 @@ public interface Keys {
*/
String INPUT_CHARSET = "input.charset";
/**
* Should JBake prefix <img src="..."> with site base URL?
* Should JBake prefix <img src="..."> with site base URL for all URLs?
*/
String IMAGES_URL_MAKE_ABSOLUTE = "images.url.makeAbsolute";
/**
* Should JBake prefix <img src="..."> with site base URL relative URLs?
* This is not disjunctive from IMAGES_URL_MAKE_ABSOLUTE.
*/
String IMAGES_URL_RELATIVE_POINTS_TO_ASSETS = "images.url.relativePointsToAssets";
}

private final static Logger LOGGER = LoggerFactory.getLogger(ConfigUtil.class);
Expand Down
90 changes: 78 additions & 12 deletions jbake-core/src/main/java/org/jbake/app/Oven.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
package org.jbake.app;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.ServiceLoader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.configuration.CompositeConfiguration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.io.FilenameUtils;
Expand All @@ -10,14 +20,10 @@
import org.jbake.template.ModelExtractors;
import org.jbake.template.ModelExtractorsDocumentTypeListener;
import org.jbake.template.RenderingException;
import org.jbake.util.HtmlUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* All the baking happens in the Oven!
*
Expand Down Expand Up @@ -150,18 +156,44 @@ public void bake() {

Renderer renderer = new Renderer(db, destination, templatesPath, config);

for(RenderingTool tool : ServiceLoader.load(RenderingTool.class)) {
try {
renderedCount += tool.render(renderer, db, destination, templatesPath, config);
} catch(RenderingException e) {
errors.add(e);
}
ServiceLoader<RenderingTool> renderTools = ServiceLoader.load(RenderingTool.class);

// If this is enabled, then this already happened in Crawler.
// TODO: Remove the fixing from Crawler.
// We should keep the pristine doc body as long as possible, or change it locally.
boolean fixedAlready = config.getBoolean(Keys.IMAGES_URL_MAKE_ABSOLUTE);

// 1st pass without altered URLs.
for(RenderingTool tool : renderTools) {
if (!tool.isRendersInPlace() || fixedAlready) continue;
try {
renderedCount += tool.render(renderer, db, destination, templatesPath, config);
} catch(RenderingException e) {
errors.add(e);
}
}

// Make the URLs absolute.
if (!fixedAlready) {
makeUrlsAbsolute(db, config);

// 2nd pass with absolutized URLs.
for (RenderingTool tool : renderTools) {
if (tool.isRendersInPlace()) continue;
try {
renderedCount += tool.render(renderer, db, destination, templatesPath, config);
}
catch (RenderingException e) {
errors.add(e);
}
}
}

// mark docs as rendered
for (String docType : DocumentTypes.getDocumentTypes()) {
db.markContentAsRendered(docType);
db.markContentAsRendered(docType);
}

// copy assets
Asset asset = new Asset(source, destination, config);
asset.copy(assetsPath);
Expand All @@ -180,6 +212,40 @@ public void bake() {
}
}

/**
* Replaces the URLs to resources in documents so that they are pointing to the resources even if the rendered document is placed
* somewhere else than the source markup.
*/
private void makeUrlsAbsolute(ContentStore db, CompositeConfiguration config)
{
int renderedCount = 0;
final List<String> errors = new LinkedList<String>();
for (String docType : DocumentTypes.getDocumentTypes()) {
DocumentList documentList = db.getUnrenderedContent(docType);
if(documentList == null) continue;

for (Map<String, Object> documentMap : documentList) {
try {
HtmlUtil.fixImageSourceUrls(documentMap, config);
// Save
documentMap = db.mergeDocument(documentMap).toMap();
}
catch (Exception e) {
errors.add(e.getMessage());
}
}
}

if (!errors.isEmpty()) {
StringBuilder sb = new StringBuilder();
sb.append("Failed to render documents. Cause(s):");
for (String error : errors) {
sb.append("\n ").append(error);
}
throw new RuntimeException(sb.toString());
}
}

/**
* Iterates over the configuration, searching for keys like "template.index.file=..."
* in order to register new document types.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package org.jbake.render;

import java.io.File;

import org.apache.commons.configuration.CompositeConfiguration;
import org.jbake.app.ConfigUtil.Keys;
import org.jbake.app.ContentStore;
import org.jbake.app.Renderer;
import org.jbake.template.RenderingException;


public class ArchiveRenderer implements RenderingTool {
public class ArchiveRenderer extends BaseRenderingTool implements RenderingTool {

@Override
public int render(Renderer renderer, ContentStore db, File destination, File templatesPath, CompositeConfiguration config) throws RenderingException {
Expand All @@ -25,4 +24,4 @@ public int render(Renderer renderer, ContentStore db, File destination, File tem
}
}

}
}
10 changes: 10 additions & 0 deletions jbake-core/src/main/java/org/jbake/render/BaseRenderingTool.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.jbake.render;

public abstract class BaseRenderingTool implements RenderingTool
{
@Override
public boolean isRendersInPlace()
{
return false; // Most renderers put the result elsewhere.
}
}
17 changes: 11 additions & 6 deletions jbake-core/src/main/java/org/jbake/render/DocumentsRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.apache.commons.configuration.CompositeConfiguration;
import org.jbake.app.ContentStore;
import org.jbake.app.Crawler.Attributes;
Expand All @@ -23,7 +22,7 @@ public int render(Renderer renderer, ContentStore db, File destination, File tem
final List<String> errors = new LinkedList<String>();
for (String docType : DocumentTypes.getDocumentTypes()) {
DocumentList documentList = db.getUnrenderedContent(docType);

if(documentList == null) continue;

int index = 0;
Expand All @@ -35,7 +34,7 @@ public int render(Renderer renderer, ContentStore db, File destination, File tem
Map<String, Object> document = documentList.get(index);
document.put("nextContent", null);
document.put("previousContent", null);

if (index > 0) {
document.put("nextContent", getContentForNav(nextDocument));
}
Expand All @@ -44,9 +43,9 @@ public int render(Renderer renderer, ContentStore db, File destination, File tem
Map<String, Object> tempNext = documentList.get(index + 1);
document.put("previousContent", getContentForNav(tempNext));
}

nextDocument = document;

renderer.render(document);
renderedCount++;

Expand All @@ -69,6 +68,12 @@ public int render(Renderer renderer, ContentStore db, File destination, File tem
}
}

@Override
public boolean isRendersInPlace()
{
return true;
}

/**
* Creates a simple content model to use in individual post navigations.
* @param document
Expand All @@ -81,4 +86,4 @@ private Map<String, Object> getContentForNav(Map<String, Object> document) {
navDocument.put(Attributes.TITLE, document.get(Attributes.TITLE));
return navDocument;
}
}
}
5 changes: 2 additions & 3 deletions jbake-core/src/main/java/org/jbake/render/FeedRenderer.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package org.jbake.render;

import java.io.File;

import org.apache.commons.configuration.CompositeConfiguration;
import org.jbake.app.ConfigUtil.Keys;
import org.jbake.app.ContentStore;
import org.jbake.app.Renderer;
import org.jbake.template.RenderingException;


public class FeedRenderer implements RenderingTool {
public class FeedRenderer extends BaseRenderingTool implements RenderingTool {

@Override
public int render(Renderer renderer, ContentStore db, File destination, File templatesPath, CompositeConfiguration config) throws RenderingException {
Expand All @@ -25,4 +24,4 @@ public int render(Renderer renderer, ContentStore db, File destination, File tem
}
}

}
}
7 changes: 3 additions & 4 deletions jbake-core/src/main/java/org/jbake/render/IndexRenderer.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package org.jbake.render;

import java.io.File;
import org.apache.commons.configuration.CompositeConfiguration;
import org.jbake.app.ConfigUtil.Keys;
import org.jbake.app.ContentStore;
import org.jbake.app.Renderer;
import org.jbake.template.RenderingException;

import java.io.File;

public class IndexRenderer implements RenderingTool {
public class IndexRenderer extends BaseRenderingTool implements RenderingTool {

@Override
public int render(Renderer renderer, ContentStore db, File destination, File templatesPath, CompositeConfiguration config) throws RenderingException {
Expand All @@ -32,4 +31,4 @@ private boolean shouldPaginateIndex(CompositeConfiguration config) {
return config.containsKey(Keys.PAGINATE_INDEX) && config.getBoolean(Keys.PAGINATE_INDEX);
}

}
}
8 changes: 6 additions & 2 deletions jbake-core/src/main/java/org/jbake/render/RenderingTool.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.jbake.render;

import java.io.File;

import org.apache.commons.configuration.CompositeConfiguration;
import org.jbake.app.ContentStore;
import org.jbake.app.Renderer;
Expand All @@ -11,4 +10,9 @@ public interface RenderingTool {

int render(Renderer renderer, ContentStore db, File destination, File templatesPath, CompositeConfiguration config) throws RenderingException;

}
/**
* Does this renderer create a file that will be situated in the same directory as the source markup?
* Serves to keep the URLs intact for renderers that do.
*/
boolean isRendersInPlace();
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package org.jbake.render;

import java.io.File;

import org.apache.commons.configuration.CompositeConfiguration;
import org.jbake.app.ConfigUtil.Keys;
import org.jbake.app.ContentStore;
import org.jbake.app.Renderer;
import org.jbake.template.RenderingException;


public class SitemapRenderer implements RenderingTool {
public class SitemapRenderer extends BaseRenderingTool implements RenderingTool {

@Override
public int render(Renderer renderer, ContentStore db, File destination, File templatesPath, CompositeConfiguration config) throws RenderingException {
Expand All @@ -25,4 +24,4 @@ public int render(Renderer renderer, ContentStore db, File destination, File tem
}
}

}
}
5 changes: 2 additions & 3 deletions jbake-core/src/main/java/org/jbake/render/TagsRenderer.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package org.jbake.render;

import java.io.File;

import org.apache.commons.configuration.CompositeConfiguration;
import org.jbake.app.ConfigUtil.Keys;
import org.jbake.app.ContentStore;
import org.jbake.app.Renderer;
import org.jbake.template.RenderingException;


public class TagsRenderer implements RenderingTool {
public class TagsRenderer extends BaseRenderingTool implements RenderingTool {

@Override
public int render(Renderer renderer, ContentStore db, File destination, File templatesPath, CompositeConfiguration config) throws RenderingException {
Expand All @@ -24,4 +23,4 @@ public int render(Renderer renderer, ContentStore db, File destination, File tem
}
}

}
}
Loading

0 comments on commit 43e35b8

Please sign in to comment.