Skip to content

Commit

Permalink
Merge pull request #554 from ancho/feature/fix-relative-image-source
Browse files Browse the repository at this point in the history
fix relative image source transformation
  • Loading branch information
jonbullock authored Oct 24, 2018
2 parents 66b6bb0 + 3d452da commit 058d403
Show file tree
Hide file tree
Showing 2 changed files with 219 additions and 187 deletions.
130 changes: 74 additions & 56 deletions jbake-core/src/main/java/org/jbake/util/HtmlUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,69 +10,87 @@
import java.util.Map;

/**
*
* @author Manik Magar
*
*/
public class HtmlUtil {

/**
* Image paths are specified as w.r.t. assets folder. This function prefix site host to all img src except
* the ones that starts with http://, https://.
*
* If image path starts with "./", i.e. relative to the source file, then it first replace that with output file directory and the add site host.
*
* @param fileContents Map representing file contents
* @param configuration Configuration object
*/
public static void fixImageSourceUrls(Map<String, Object> fileContents, JBakeConfiguration configuration){

String htmlContent = fileContents.get(Attributes.BODY).toString();

boolean prependSiteHost = configuration.getImgPathPrependHost();
private HtmlUtil() {
}

/**
* Image paths are specified as w.r.t. assets folder. This function prefix site host to all img src except
* the ones that starts with http://, https://.
* <p>
* If image path starts with "./", i.e. relative to the source file, then it first replace that with output file directory and the add site host.
*
* @param fileContents Map representing file contents
* @param configuration Configuration object
*/
public static void fixImageSourceUrls(Map<String, Object> fileContents, JBakeConfiguration configuration) {
String htmlContent = fileContents.get(Attributes.BODY).toString();
boolean prependSiteHost = configuration.getImgPathPrependHost();
String siteHost = configuration.getSiteHost();
String uri = fileContents.get(Attributes.URI).toString();

if (fileContents.get(Attributes.NO_EXTENSION_URI) != null){
uri = fileContents.get(Attributes.NO_EXTENSION_URI).toString();

//remove trailing "/"
if(uri.endsWith("/")) {
uri = uri.substring(0, uri.length() - 1);
}
}

if (uri.contains("/")){
//strip that file name, leaving end "/"
uri = uri.substring(0, uri.lastIndexOf("/") + 1);
String uri = getDocumentUri(fileContents);

Document document = Jsoup.parseBodyFragment(htmlContent);
Elements allImgs = document.getElementsByTag("img");

for (Element img : allImgs) {
transformImageSource(img, uri, siteHost, prependSiteHost);
}

Document document = Jsoup.parseBodyFragment(htmlContent);
Elements allImgs = document.getElementsByTag("img");

for (Element img : allImgs) {
String source = img.attr("src");

// Now add the root path
if (!source.startsWith("http://") && !source.startsWith("https://")){

if (!source.startsWith("/")){
source = uri + source.replaceFirst("./", "");
}

if (!siteHost.endsWith("/") && !source.startsWith("/")) {
siteHost = siteHost.concat("/");
}

if (prependSiteHost) {
source = siteHost + source;
//Use body().html() to prevent adding <body></body> from parsed fragment.
fileContents.put(Attributes.BODY, document.body().html());
}

private static String getDocumentUri(Map<String, Object> fileContents) {
String uri = fileContents.get(Attributes.URI).toString();

if (fileContents.get(Attributes.NO_EXTENSION_URI) != null) {
uri = fileContents.get(Attributes.NO_EXTENSION_URI).toString();
uri = removeTrailingSlash(uri);
}

if (uri.contains("/")) {
uri = removeFilename(uri);
}
return uri;
}

private static void transformImageSource(Element img, String uri, String siteHost, boolean prependSiteHost) {
String source = img.attr("src");

// Now add the root path
if (!source.startsWith("http://") && !source.startsWith("https://")) {

if (isRelative(source)) {
source = uri + source.replaceFirst("\\./", "");
}

if (prependSiteHost) {
if (!siteHost.endsWith("/") && isRelative(source)) {
siteHost = siteHost.concat("/");
}

img.attr("src", source);
}
}

//Use body().html() to prevent adding <body></body> from parsed fragment.
fileContents.put(Attributes.BODY, document.body().html());
source = siteHost + source;
}

img.attr("src", source);
}
}

private static String removeFilename(String uri) {
uri = uri.substring(0, uri.lastIndexOf('/') + 1);
return uri;
}

private static String removeTrailingSlash(String uri) {
if (uri.endsWith("/")) {
uri = uri.substring(0, uri.length() - 1);
}
return uri;
}

private static boolean isRelative(String source) {
return !source.startsWith("/");
}
}
Loading

0 comments on commit 058d403

Please sign in to comment.