Skip to content

Commit

Permalink
do not replace every first / and preceding character for relative ima…
Browse files Browse the repository at this point in the history
…ge uri's

fix #553
  • Loading branch information
ancho committed Oct 17, 2018
1 parent 66b6bb0 commit e6da353
Show file tree
Hide file tree
Showing 2 changed files with 206 additions and 184 deletions.
114 changes: 61 additions & 53 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,77 @@
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();
/**
* 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 = 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);
}
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("./", "");

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 (isRelative(source)) {
source = uri + source.replaceFirst("\\./", "");
}

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

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

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

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

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

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

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 e6da353

Please sign in to comment.