-
Notifications
You must be signed in to change notification settings - Fork 330
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #367 from manikmagar/image-src-path-fix
Prevent image source path from breaking
- Loading branch information
Showing
4 changed files
with
244 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package org.jbake.util; | ||
|
||
import java.util.Map; | ||
|
||
import org.apache.commons.configuration.CompositeConfiguration; | ||
import org.jbake.app.ConfigUtil.Keys; | ||
import org.jbake.app.Crawler.Attributes; | ||
import org.jsoup.Jsoup; | ||
import org.jsoup.nodes.Document; | ||
import org.jsoup.nodes.Element; | ||
import org.jsoup.select.Elements; | ||
|
||
/** | ||
* | ||
* @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 | ||
*/ | ||
public static void fixImageSourceUrls(Map<String, Object> fileContents, CompositeConfiguration config){ | ||
|
||
String htmlContent = fileContents.get(Attributes.BODY).toString(); | ||
|
||
String siteHost = config.getString(Keys.SITE_HOST); | ||
|
||
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); | ||
} | ||
|
||
Document document = Jsoup.parseBodyFragment(htmlContent); | ||
|
||
Elements allImgs = document.getElementsByTag("img"); | ||
|
||
for (Element img : allImgs) { | ||
String source = img.attr("src"); | ||
|
||
if(source.startsWith("./")){ | ||
// image relative to current content is specified, | ||
// lets add current url to it. | ||
source = source.replaceFirst("./", uri); | ||
} | ||
|
||
// Now add the root path | ||
if(!source.startsWith("http://") | ||
&& !source.startsWith("https://")){ | ||
|
||
if (!siteHost.endsWith("/") && !source.startsWith("/")) siteHost = siteHost.concat("/"); | ||
|
||
String fullUrl = siteHost + source; | ||
|
||
img.attr("src", fullUrl); | ||
|
||
} | ||
} | ||
|
||
|
||
//Use body().html() to prevent adding <body></body> from parsed fragment. | ||
fileContents.put(Attributes.BODY, document.body().html()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
package org.jbake.util; | ||
|
||
import static org.assertj.core.api.Assertions.*; | ||
|
||
import java.io.File; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import org.apache.commons.configuration.CompositeConfiguration; | ||
import org.jbake.app.ConfigUtil; | ||
import org.jbake.app.Crawler.Attributes; | ||
import org.jbake.util.HtmlUtil; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
|
||
|
||
public class HtmlUtilTest { | ||
|
||
private CompositeConfiguration config; | ||
|
||
@Before | ||
public void setUp() throws Exception{ | ||
config = ConfigUtil.load(new File(this.getClass().getResource("/fixture").getFile())); | ||
} | ||
|
||
@Test | ||
public void shouldNotAddBodyHTMLElement(){ | ||
Map<String, Object> fileContent = new HashMap<String, Object>(); | ||
fileContent.put(Attributes.ROOTPATH, "../../../"); | ||
fileContent.put(Attributes.URI, "blog/2017/05/first_post.html"); | ||
fileContent.put(Attributes.BODY, "<div> Test <img src='blog/2017/05/first.jpg' /></div>"); | ||
|
||
HtmlUtil.fixImageSourceUrls(fileContent, config); | ||
|
||
String body = fileContent.get(Attributes.BODY).toString(); | ||
|
||
assertThat(body).doesNotContain("<body>"); | ||
assertThat(body).doesNotContain("</body>"); | ||
|
||
} | ||
|
||
@Test | ||
public void shouldAddRootpath(){ | ||
Map<String, Object> fileContent = new HashMap<String, Object>(); | ||
fileContent.put(Attributes.ROOTPATH, "../../../"); | ||
fileContent.put(Attributes.URI, "blog/2017/05/first_post.html"); | ||
fileContent.put(Attributes.BODY, "<div> Test <img src='blog/2017/05/first.jpg' /></div>"); | ||
|
||
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 shouldAddContentpath(){ | ||
Map<String, Object> fileContent = new HashMap<String, Object>(); | ||
fileContent.put(Attributes.ROOTPATH, "../../../"); | ||
fileContent.put(Attributes.URI, "blog/2017/05/first_post.html"); | ||
fileContent.put(Attributes.BODY, "<div> Test <img src='./first.jpg' /></div>"); | ||
|
||
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<String, Object> fileContent = new HashMap<String, Object>(); | ||
fileContent.put(Attributes.ROOTPATH, "../../../"); | ||
fileContent.put(Attributes.URI, "blog/2017/05/first_post.html"); | ||
fileContent.put(Attributes.BODY, "<div> Test <img src='/blog/2017/05/first.jpg' /></div>"); | ||
|
||
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 shouldAddRootPathForNoExtension(){ | ||
Map<String, Object> fileContent = new HashMap<String, Object>(); | ||
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, "<div> Test <img src='blog/2017/05/first.jpg' /></div>"); | ||
|
||
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<String, Object> fileContent = new HashMap<String, Object>(); | ||
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, "<div> Test <img src='./first.jpg' /></div>"); | ||
|
||
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<String, Object> fileContent = new HashMap<String, Object>(); | ||
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, "<div> Test <img src='http://example.com/first.jpg' /></div>"); | ||
|
||
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<String, Object> fileContent = new HashMap<String, Object>(); | ||
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, "<div> Test <img src='https://example.com/first.jpg' /></div>"); | ||
|
||
HtmlUtil.fixImageSourceUrls(fileContent, config); | ||
|
||
String body = fileContent.get(Attributes.BODY).toString(); | ||
|
||
assertThat(body).contains("src=\"https://example.com/first.jpg\""); | ||
} | ||
} |