Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Honor nested path in tagspath #569

Merged
merged 3 commits into from Jan 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 2 additions & 15 deletions jbake-core/src/main/java/org/jbake/app/Crawler.java
Expand Up @@ -6,6 +6,7 @@
import org.jbake.app.Crawler.Attributes.Status;
import org.jbake.app.configuration.JBakeConfiguration;
import org.jbake.app.configuration.JBakeConfigurationFactory;
import org.jbake.app.configuration.ConfigUtil;
import org.jbake.model.DocumentAttributes;
import org.jbake.model.DocumentStatus;
import org.jbake.model.DocumentTypes;
Expand Down Expand Up @@ -233,21 +234,7 @@ private void crawlSourceFile(final File sourceFile, final String sha1, final Str
}

private String getPathToRoot(File sourceFile) {
File rootPath = config.getContentFolder();
File parentPath = sourceFile.getParentFile();
int parentCount = 0;
while (!parentPath.equals(rootPath)) {
parentPath = parentPath.getParentFile();
parentCount++;
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < parentCount; i++) {
sb.append("../");
}
if (config.getUriWithoutExtension()) {
sb.append("../");
}
return sb.toString();
return ConfigUtil.getPathToContentRoot(config, sourceFile);
}

private DocumentStatus findDocumentStatus(String docType, String uri, String sha1) {
Expand Down
3 changes: 2 additions & 1 deletion jbake-core/src/main/java/org/jbake/app/FileUtil.java
Expand Up @@ -180,5 +180,6 @@ public static String asPath(String path) {
return null;
}
return path.replace(File.separator, "/");
}
}

}
6 changes: 4 additions & 2 deletions jbake-core/src/main/java/org/jbake/app/Renderer.java
Expand Up @@ -2,6 +2,7 @@

import org.apache.commons.configuration.CompositeConfiguration;
import org.jbake.app.Crawler.Attributes;
import org.jbake.app.configuration.ConfigUtil;
import org.jbake.app.configuration.DefaultJBakeConfiguration;
import org.jbake.app.configuration.JBakeConfiguration;
import org.jbake.app.configuration.JBakeConfigurationFactory;
Expand Down Expand Up @@ -272,10 +273,11 @@ public int renderTags(String tagPath) throws Exception {
model.put("renderer", renderingEngine);
model.put(Attributes.TAG, tag);
Map<String, Object> map = buildSimpleModel(Attributes.TAG);
map.put(Attributes.ROOTPATH, "../");
model.put("content", map);

File path = new File(config.getDestinationFolder() + File.separator + tagPath + File.separator + tag + config.getOutputExtension());
map.put(Attributes.ROOTPATH, ConfigUtil.getPathtoDestinationRoot(config, path));

render(new ModelRenderingConfig(path, Attributes.TAG, model, findTemplateName(Attributes.TAG)));

renderedCount++;
Expand All @@ -292,10 +294,10 @@ public int renderTags(String tagPath) throws Exception {
Map<String, Object> model = new HashMap<String, Object>();
model.put("renderer", renderingEngine);
Map<String, Object> map = buildSimpleModel(Attributes.TAGS);
map.put(Attributes.ROOTPATH, "../");
model.put("content", map);

File path = new File(config.getDestinationFolder() + File.separator + tagPath + File.separator + "index" + config.getOutputExtension());
map.put(Attributes.ROOTPATH, ConfigUtil.getPathtoDestinationRoot(config, path));
render(new ModelRenderingConfig(path, "tagindex", model, findTemplateName("tagsindex")));
renderedCount++;
} catch (Exception e) {
Expand Down
Expand Up @@ -9,6 +9,8 @@
import org.slf4j.LoggerFactory;

import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
* Provides Configuration related functions.
Expand Down Expand Up @@ -56,4 +58,40 @@ public JBakeConfiguration loadConfig(File source) throws ConfigurationException
CompositeConfiguration configuration = load(source);
return new DefaultJBakeConfiguration(source, configuration);
}

/**
* Given a file inside content it return
* the relative path to get to the root.
*
* Example: /content and /content/tags/blog will return '../..'
*
* @param sourceFile the file to calculate relative path for
* @return
*/
static public String getPathToRoot(JBakeConfiguration config, File rootPath, File sourceFile) {

Path r = Paths.get(rootPath.toURI());
Path s = Paths.get(sourceFile.getParentFile().toURI());
Path relativePath = s.relativize(r);

StringBuilder sb = new StringBuilder();

sb.append(relativePath.toString());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On Windows the relative path contains "\" which is wrong in context of an URI for image paths.
Causing some tests on appveyor to fail. https://ci.appveyor.com/project/jbake-org/jbake/builds/20288027

org.jbake.app.configuration.ConfigUtilTest > testGetContentRoothPath FAILED
    org.junit.ComparisonFailure: expected:<"..[/]../"> but was:<"..[\]../">
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at org.jbake.app.configuration.ConfigUtilTest.testGetContentRoothPath(ConfigUtilTest.java:352)

We should replace alle backslashes with slashes here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe the methods should be moved to FileUtil and contain a pointer that we get a URI Path back? Something like getUriPath.... ?


if (config.getUriWithoutExtension()) {
sb.append("/..");
}
if(sb.length()>0) { // added as calling logic assumes / at end.
sb.append("/");
}
return sb.toString();
}

static public String getPathtoDestinationRoot(JBakeConfiguration config, File sourceFile) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Little typo getPathtoDestinationRoot -> getUriPathToDestinationRoot. What do you think?

return getPathToRoot(config, config.getDestinationFolder(), sourceFile);
}

static public String getPathToContentRoot(JBakeConfiguration config, File sourceFile) {
return getPathToRoot(config, config.getContentFolder(), sourceFile);
}
}
Expand Up @@ -334,4 +334,23 @@ private File getTestResourcesAsSourceFolder() {
return new File(this.getClass().getResource("/fixture").getFile());
}

@Test
public void testGetContentRoothPath() throws Exception {
File source = getTestResourcesAsSourceFolder();
DefaultJBakeConfiguration config = (DefaultJBakeConfiguration) util.loadConfig(source);

String path = ConfigUtil.getPathToContentRoot(config, new File(config.getContentFolder(), "index.html"));

assertThat(path).isEqualTo("");

path = ConfigUtil.getPathToContentRoot(config, new File(config.getContentFolder(), "/blog/index.html"));

assertThat(path).isEqualTo("../");

path = ConfigUtil.getPathToContentRoot(config, new File(config.getContentFolder(), "/blog/level2/index.html"));

assertThat(path).isEqualTo("../../");


}
}