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 Oct 30, 2018
1 parent 00ff5bd commit 5837c9a
Show file tree
Hide file tree
Showing 14 changed files with 194 additions and 65 deletions.
96 changes: 83 additions & 13 deletions jbake-core/src/main/java/org/jbake/app/Oven.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
package org.jbake.app;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.ServiceLoader;
import org.apache.commons.configuration.CompositeConfiguration;
import org.jbake.app.configuration.DefaultJBakeConfiguration;
import org.jbake.app.configuration.JBakeConfiguration;
Expand All @@ -10,16 +17,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.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.ServiceLoader;

/**
* All the baking happens in the Oven!
*
Expand Down Expand Up @@ -139,11 +140,13 @@ public void bake() {
// render content
renderContent();

// copy assets
asset.copy();
asset.copyAssetsFromContent(config.getContentFolder());
{
// copy assets
asset.copy();
asset.copyAssetsFromContent(config.getContentFolder());

errors.addAll(asset.getErrors());
errors.addAll(asset.getErrors());
}

LOGGER.info("Baking finished!");
long end = new Date().getTime();
Expand All @@ -157,6 +160,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 Expand Up @@ -186,13 +223,46 @@ private void renderContent() {
Renderer renderer = utensils.getRenderer();
ContentStore contentStore = utensils.getContentStore();

for (RenderingTool tool : ServiceLoader.load(RenderingTool.class)) {
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.getMakeImagesUrlAbolute();

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

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

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

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


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,13 @@ public boolean getMakeImagesUrlAbolute()
return getAsBoolean(JBakeProperty.IMG_URL_MAKE_ABSOLUTE);
}

@Override
public boolean getRelativeImagePathsPointToAssets()
{
return getAsBoolean(JBakeProperty.IMG_PATH_RELATIVE_POINTS_TO_ASSETS);
}



public void setDestinationFolderName(String folderName) {
setProperty(JBakeProperty.DESTINATION_FOLDER, folderName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,13 @@ public interface JBakeConfiguration {
*/
boolean getMakeImagesUrlAbolute();

/**
* Should JBake prefix <img src="..."> with site base URL relative URLs?
* This is not disjunctive from IMAGES_URL_MAKE_ABSOLUTE.
*/
boolean getRelativeImagePathsPointToAssets();


/**
* Set a property value for the given key
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public class JBakeProperty {
public static final String IMG_PATH_UPDATE = "img.path.update";
public static final String IMG_PATH_PREPEND_HOST = "img.path.prepend.host";
public static final String IMG_URL_MAKE_ABSOLUTE = "img.url.makeAbsolute";
public static final String IMG_PATH_RELATIVE_POINTS_TO_ASSETS = "img.path.relativePointsToAssets";

public static final String VERSION = "version";

public static final String EXTRACT_TITLE_FROM_DOC = "extract.title";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +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 @@ -10,7 +11,7 @@
import java.io.File;


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

@Override
public int render(Renderer renderer, ContentStore db, JBakeConfiguration config) throws RenderingException {
Expand All @@ -33,4 +34,4 @@ public int render(Renderer renderer, ContentStore db, File destination, File tem
return render(renderer, db, configuration);
}

}
}
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.
}
}
44 changes: 25 additions & 19 deletions jbake-core/src/main/java/org/jbake/render/DocumentsRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@

public class DocumentsRenderer implements RenderingTool {


@Override
public int render(Renderer renderer, ContentStore db, File destination, File templatesPath, CompositeConfiguration config) throws RenderingException {
return render(renderer, db, null);
}

@Override
public int render(Renderer renderer, ContentStore db, JBakeConfiguration config) throws RenderingException {
int renderedCount = 0;
Expand Down Expand Up @@ -71,22 +77,22 @@ public int render(Renderer renderer, ContentStore db, JBakeConfiguration config)
}
}

/**
* Creates a simple content model to use in individual post navigations.
*
* @param document
* @return
*/
private Map<String, Object> getContentForNav(Map<String, Object> document) {
Map<String, Object> navDocument = new HashMap<>();
navDocument.put(Attributes.NO_EXTENSION_URI, document.get(Attributes.NO_EXTENSION_URI));
navDocument.put(Attributes.URI, document.get(Attributes.URI));
navDocument.put(Attributes.TITLE, document.get(Attributes.TITLE));
return navDocument;
}

@Override
public int render(Renderer renderer, ContentStore db, File destination, File templatesPath, CompositeConfiguration config) throws RenderingException {
return render(renderer, db, null);
}
}
@Override
public boolean isRendersInPlace()
{
return true;
}

/**
* Creates a simple content model to use in individual post navigations.
* @param document
* @return
*/
private Map<String, Object> getContentForNav(Map<String, Object> document) {
Map<String, Object> navDocument = new HashMap<String, Object>();
navDocument.put(Attributes.NO_EXTENSION_URI, document.get(Attributes.NO_EXTENSION_URI));
navDocument.put(Attributes.URI, document.get(Attributes.URI));
navDocument.put(Attributes.TITLE, document.get(Attributes.TITLE));
return navDocument;
}
}
6 changes: 4 additions & 2 deletions jbake-core/src/main/java/org/jbake/render/FeedRenderer.java
Original file line number Diff line number Diff line change
@@ -1,5 +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 @@ -10,7 +11,7 @@
import java.io.File;


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

@Override
public int render(Renderer renderer, ContentStore db, JBakeConfiguration config) throws RenderingException {
Expand All @@ -27,10 +28,11 @@ public int render(Renderer renderer, ContentStore db, JBakeConfiguration config)
}
}

///REBASE isn't this in BaseRenderingTool?
@Override
public int render(Renderer renderer, ContentStore db, File destination, File templatesPath, CompositeConfiguration config) throws RenderingException {
JBakeConfiguration configuration = new JBakeConfigurationFactory().createDefaultJbakeConfiguration(templatesPath.getParentFile(), config);
return render(renderer, db, configuration);
}

}
}
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,15 +1,14 @@
package org.jbake.render;

import java.io.File;
import org.apache.commons.configuration.CompositeConfiguration;
import org.jbake.app.ContentStore;
import org.jbake.app.Renderer;
import org.jbake.app.configuration.JBakeConfiguration;
import org.jbake.app.configuration.JBakeConfigurationFactory;
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, JBakeConfiguration config) throws RenderingException {
Expand Down Expand Up @@ -37,4 +36,4 @@ public int render(Renderer renderer, ContentStore db, File destination, File tem
JBakeConfiguration configuration = new JBakeConfigurationFactory().createDefaultJbakeConfiguration(templatesPath.getParentFile(), config);
return render(renderer, db, configuration);
}
}
}
10 changes: 7 additions & 3 deletions jbake-core/src/main/java/org/jbake/render/RenderingTool.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package org.jbake.render;

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

import java.io.File;

public interface RenderingTool {


Expand All @@ -17,4 +16,9 @@ public interface RenderingTool {
//TODO: remove at 3.0.0
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,16 +1,15 @@
package org.jbake.render;

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

import java.io.File;


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

@Override
public int render(Renderer renderer, ContentStore db, JBakeConfiguration config) throws RenderingException {
Expand All @@ -33,4 +32,4 @@ public int render(Renderer renderer, ContentStore db, File destination, File tem
return render(renderer, db, configuration);
}

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

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

import java.io.File;


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

@Override
public int render(Renderer renderer, ContentStore db, JBakeConfiguration config) throws RenderingException {
Expand All @@ -32,4 +31,4 @@ public int render(Renderer renderer, ContentStore db, File destination, File tem
return render(renderer, db, configuration);
}

}
}
Loading

0 comments on commit 5837c9a

Please sign in to comment.