diff --git a/jbake-core/src/main/java/org/jbake/util/HtmlUtil.java b/jbake-core/src/main/java/org/jbake/util/HtmlUtil.java index 36c688639..7a411cfd3 100644 --- a/jbake-core/src/main/java/org/jbake/util/HtmlUtil.java +++ b/jbake-core/src/main/java/org/jbake/util/HtmlUtil.java @@ -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 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://. + *

+ * 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 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 from parsed fragment. - fileContents.put(Attributes.BODY, document.body().html()); + + img.attr("src", source); + } + } + + //Use body().html() to prevent adding 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("/"); } } diff --git a/jbake-core/src/test/java/org/jbake/util/HtmlUtilTest.java b/jbake-core/src/test/java/org/jbake/util/HtmlUtilTest.java index 43355f4fb..84722d2c3 100644 --- a/jbake-core/src/test/java/org/jbake/util/HtmlUtilTest.java +++ b/jbake-core/src/test/java/org/jbake/util/HtmlUtilTest.java @@ -13,34 +13,33 @@ import static org.assertj.core.api.Assertions.assertThat; - public class HtmlUtilTest { - - private DefaultJBakeConfiguration config; - - @Before - public void setUp() throws Exception{ - config = (DefaultJBakeConfiguration) new ConfigUtil().loadConfig(new File(this.getClass().getResource("/fixture").getFile())); - } - - @Test - public void shouldNotAddBodyHTMLElement(){ - Map fileContent = new HashMap(); - fileContent.put(Attributes.ROOTPATH, "../../../"); - fileContent.put(Attributes.URI, "blog/2017/05/first_post.html"); - fileContent.put(Attributes.BODY, "

Test
"); - - HtmlUtil.fixImageSourceUrls(fileContent, config); - - String body = fileContent.get(Attributes.BODY).toString(); - - assertThat(body).doesNotContain(""); - assertThat(body).doesNotContain(""); - - } + + private DefaultJBakeConfiguration config; + + @Before + public void setUp() throws Exception { + config = (DefaultJBakeConfiguration) new ConfigUtil().loadConfig(new File(this.getClass().getResource("/fixture").getFile())); + } + + @Test + public void shouldNotAddBodyHTMLElement() { + Map fileContent = new HashMap(); + fileContent.put(Attributes.ROOTPATH, "../../../"); + fileContent.put(Attributes.URI, "blog/2017/05/first_post.html"); + fileContent.put(Attributes.BODY, "
Test
"); + + HtmlUtil.fixImageSourceUrls(fileContent, config); + + String body = fileContent.get(Attributes.BODY).toString(); + + assertThat(body).doesNotContain(""); + assertThat(body).doesNotContain(""); + + } @Test - public void shouldNotAddSiteHost(){ + public void shouldNotAddSiteHost() { Map fileContent = new HashMap(); fileContent.put(Attributes.ROOTPATH, "../../../"); fileContent.put(Attributes.URI, "blog/2017/05/first_post.html"); @@ -55,110 +54,125 @@ public void shouldNotAddSiteHost(){ } - @Test - public void shouldAddContentPath(){ - Map fileContent = new HashMap(); - fileContent.put(Attributes.ROOTPATH, "../../../"); - fileContent.put(Attributes.URI, "blog/2017/05/first_post.html"); - fileContent.put(Attributes.BODY, "
Test
"); - - HtmlUtil.fixImageSourceUrls(fileContent, config); - - String body = fileContent.get(Attributes.BODY).toString(); - - assertThat(body).contains("src=\"http://www.jbake.org/blog/2017/05/first.jpg\""); - - } - - @Test - public void shouldAddContentPathForCurrentDirectory(){ - Map fileContent = new HashMap(); - fileContent.put(Attributes.ROOTPATH, "../../../"); - fileContent.put(Attributes.URI, "blog/2017/05/first_post.html"); - fileContent.put(Attributes.BODY, "
Test
"); - - HtmlUtil.fixImageSourceUrls(fileContent, config); - - String body = fileContent.get(Attributes.BODY).toString(); - - assertThat(body).contains("src=\"http://www.jbake.org/blog/2017/05/first.jpg\""); - - } - - @Test - public void shouldNotAddRootPath(){ - Map fileContent = new HashMap(); - fileContent.put(Attributes.ROOTPATH, "../../../"); - fileContent.put(Attributes.URI, "blog/2017/05/first_post.html"); - fileContent.put(Attributes.BODY, "
Test
"); - - HtmlUtil.fixImageSourceUrls(fileContent,config); - - String body = fileContent.get(Attributes.BODY).toString(); - - assertThat(body).contains("src=\"http://www.jbake.org/blog/2017/05/first.jpg\""); - - } - - @Test - public void shouldNotAddRootPathForNoExtension(){ - Map fileContent = new HashMap(); - fileContent.put(Attributes.ROOTPATH, "../../../"); - fileContent.put(Attributes.URI, "blog/2017/05/first_post.html"); - fileContent.put(Attributes.NO_EXTENSION_URI, "blog/2017/05/first_post/"); - fileContent.put(Attributes.BODY, "
Test
"); - - HtmlUtil.fixImageSourceUrls(fileContent, config); - - String body = fileContent.get(Attributes.BODY).toString(); - - assertThat(body).contains("src=\"http://www.jbake.org/blog/2017/05/first.jpg\""); - - } - - @Test - public void shouldAddContentPathForNoExtension(){ - Map fileContent = new HashMap(); - fileContent.put(Attributes.ROOTPATH, "../../../"); - fileContent.put(Attributes.URI, "blog/2017/05/first_post.html"); - fileContent.put(Attributes.NO_EXTENSION_URI, "blog/2017/05/first_post/"); - fileContent.put(Attributes.BODY, "
Test
"); - - HtmlUtil.fixImageSourceUrls(fileContent, config); - - String body = fileContent.get(Attributes.BODY).toString(); - - assertThat(body).contains("src=\"http://www.jbake.org/blog/2017/05/first.jpg\""); - } - - @Test - public void shouldNotChangeForHTTP(){ - Map fileContent = new HashMap(); - fileContent.put(Attributes.ROOTPATH, "../../../"); - fileContent.put(Attributes.URI, "blog/2017/05/first_post.html"); - fileContent.put(Attributes.NO_EXTENSION_URI, "blog/2017/05/first_post/"); - fileContent.put(Attributes.BODY, "
Test
"); - - HtmlUtil.fixImageSourceUrls(fileContent, config); - - String body = fileContent.get(Attributes.BODY).toString(); - - assertThat(body).contains("src=\"http://example.com/first.jpg\""); - - } - - @Test - public void shouldNotChangeForHTTPS(){ - Map fileContent = new HashMap(); - fileContent.put(Attributes.ROOTPATH, "../../../"); - fileContent.put(Attributes.URI, "blog/2017/05/first_post.html"); - fileContent.put(Attributes.NO_EXTENSION_URI, "blog/2017/05/first_post/"); - fileContent.put(Attributes.BODY, "
Test
"); - - HtmlUtil.fixImageSourceUrls(fileContent, config); - - String body = fileContent.get(Attributes.BODY).toString(); - - assertThat(body).contains("src=\"https://example.com/first.jpg\""); - } + @Test + public void shouldAddSiteHostWithRelativeImageToDocument() { + Map fileContent = new HashMap<>(); + fileContent.put(Attributes.ROOTPATH, "../../../"); + fileContent.put(Attributes.URI, "blog/2017/05/first_post.html"); + fileContent.put(Attributes.BODY, "
Test
"); + config.setImgPathPrependHost(true); + + HtmlUtil.fixImageSourceUrls(fileContent, config); + + String body = fileContent.get(Attributes.BODY).toString(); + + assertThat(body).contains("src=\"http://www.jbake.org/blog/2017/05/img/deeper/underground.jpg\""); + } + + @Test + public void shouldAddContentPath() { + Map fileContent = new HashMap(); + fileContent.put(Attributes.ROOTPATH, "../../../"); + fileContent.put(Attributes.URI, "blog/2017/05/first_post.html"); + fileContent.put(Attributes.BODY, "
Test
"); + + HtmlUtil.fixImageSourceUrls(fileContent, config); + + String body = fileContent.get(Attributes.BODY).toString(); + + assertThat(body).contains("src=\"http://www.jbake.org/blog/2017/05/first.jpg\""); + + } + + @Test + public void shouldAddContentPathForCurrentDirectory() { + Map fileContent = new HashMap(); + fileContent.put(Attributes.ROOTPATH, "../../../"); + fileContent.put(Attributes.URI, "blog/2017/05/first_post.html"); + fileContent.put(Attributes.BODY, "
Test
"); + + HtmlUtil.fixImageSourceUrls(fileContent, config); + + String body = fileContent.get(Attributes.BODY).toString(); + + assertThat(body).contains("src=\"http://www.jbake.org/blog/2017/05/first.jpg\""); + + } + + @Test + public void shouldNotAddRootPath() { + Map fileContent = new HashMap(); + fileContent.put(Attributes.ROOTPATH, "../../../"); + fileContent.put(Attributes.URI, "blog/2017/05/first_post.html"); + fileContent.put(Attributes.BODY, "
Test
"); + + HtmlUtil.fixImageSourceUrls(fileContent, config); + + String body = fileContent.get(Attributes.BODY).toString(); + + assertThat(body).contains("src=\"http://www.jbake.org/blog/2017/05/first.jpg\""); + + } + + @Test + public void shouldNotAddRootPathForNoExtension() { + Map fileContent = new HashMap(); + fileContent.put(Attributes.ROOTPATH, "../../../"); + fileContent.put(Attributes.URI, "blog/2017/05/first_post.html"); + fileContent.put(Attributes.NO_EXTENSION_URI, "blog/2017/05/first_post/"); + fileContent.put(Attributes.BODY, "
Test
"); + + HtmlUtil.fixImageSourceUrls(fileContent, config); + + String body = fileContent.get(Attributes.BODY).toString(); + + assertThat(body).contains("src=\"http://www.jbake.org/blog/2017/05/first.jpg\""); + + } + + @Test + public void shouldAddContentPathForNoExtension() { + Map fileContent = new HashMap(); + fileContent.put(Attributes.ROOTPATH, "../../../"); + fileContent.put(Attributes.URI, "blog/2017/05/first_post.html"); + fileContent.put(Attributes.NO_EXTENSION_URI, "blog/2017/05/first_post/"); + fileContent.put(Attributes.BODY, "
Test
"); + + HtmlUtil.fixImageSourceUrls(fileContent, config); + + String body = fileContent.get(Attributes.BODY).toString(); + + assertThat(body).contains("src=\"http://www.jbake.org/blog/2017/05/first.jpg\""); + } + + @Test + public void shouldNotChangeForHTTP() { + Map fileContent = new HashMap(); + fileContent.put(Attributes.ROOTPATH, "../../../"); + fileContent.put(Attributes.URI, "blog/2017/05/first_post.html"); + fileContent.put(Attributes.NO_EXTENSION_URI, "blog/2017/05/first_post/"); + fileContent.put(Attributes.BODY, "
Test
"); + + HtmlUtil.fixImageSourceUrls(fileContent, config); + + String body = fileContent.get(Attributes.BODY).toString(); + + assertThat(body).contains("src=\"http://example.com/first.jpg\""); + + } + + @Test + public void shouldNotChangeForHTTPS() { + Map fileContent = new HashMap(); + fileContent.put(Attributes.ROOTPATH, "../../../"); + fileContent.put(Attributes.URI, "blog/2017/05/first_post.html"); + fileContent.put(Attributes.NO_EXTENSION_URI, "blog/2017/05/first_post/"); + fileContent.put(Attributes.BODY, "
Test
"); + + HtmlUtil.fixImageSourceUrls(fileContent, config); + + String body = fileContent.get(Attributes.BODY).toString(); + + assertThat(body).contains("src=\"https://example.com/first.jpg\""); + } }