Skip to content

Commit

Permalink
refactor HtmlUtil
Browse files Browse the repository at this point in the history
  • Loading branch information
ancho committed Oct 17, 2018
1 parent e6da353 commit 3d452da
Showing 1 changed file with 33 additions and 23 deletions.
56 changes: 33 additions & 23 deletions jbake-core/src/main/java/org/jbake/util/HtmlUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
*/
public class HtmlUtil {

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://.
Expand All @@ -24,11 +27,23 @@ public class HtmlUtil {
* @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 = getDocumentUri(fileContents);

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

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

//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) {
Expand All @@ -39,37 +54,32 @@ public static void fixImageSourceUrls(Map<String, Object> fileContents, JBakeCon
if (uri.contains("/")) {
uri = removeFilename(uri);
}
return uri;
}

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

for (Element img : allImgs) {
String source = img.attr("src");
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://")) {
// Now add the root path
if (!source.startsWith("http://") && !source.startsWith("https://")) {

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

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

img.attr("src", source);
source = siteHost + source;
}
}

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

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

Expand Down

0 comments on commit 3d452da

Please sign in to comment.