Skip to content

Commit

Permalink
fix merge and docx link resolvers
Browse files Browse the repository at this point in the history
  • Loading branch information
vsch committed Apr 2, 2020
1 parent f2aa458 commit 9719ed1
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 41 deletions.
1 change: 1 addition & 0 deletions VERSION-TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ Please give feedback on the upcoming changes if you have concerns about breaking
Will only include files if `JekyllTagExtension.LINK_RESOLVER_FACTORIES` is not empty, in which
case the link resolvers will be used to resolve `includeFile` to full file path. Use singleton
list of `DocxLinkResolver.Factory` instance to resolve using doc relative and root url paths.
* Fix: `MergeLinkResolver` and `DocxLinkResolver`

## 0.60.2

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.File;
import java.util.Set;

public class DocxLinkResolver implements LinkResolver {
Expand All @@ -26,31 +27,29 @@ public class DocxLinkResolver implements LinkResolver {

public DocxLinkResolver(LinkResolverBasicContext context) {
// can use context for custom settings
// context.getDocument();
// context.getHtmlOptions();
String docRelativeURL = DocxRenderer.DOC_RELATIVE_URL.get(context.getOptions());
if (docRelativeURL != null) {
docRelativeURL = Utils.removePrefix(docRelativeURL, '/');
}

String docRootURL = DocxRenderer.DOC_ROOT_URL.get(context.getOptions());
if (docRootURL != null) {
docRootURL = Utils.removePrefix(docRootURL, '/');
}
this.docRelativeURL = docRelativeURL;
this.docRootURL = docRootURL;

docRelativeURL = Utils.removePrefix(docRelativeURL, '/');

relativeParts = docRelativeURL.split("/");
prefixWwwLinks = DocxRenderer.PREFIX_WWW_LINKS.get(context.getOptions());
}

@NotNull
@Override
public ResolvedLink resolveLink(@NotNull Node node, @NotNull LinkResolverBasicContext context, @NotNull ResolvedLink link) {
// NOTE: node is document when resolving include urls
if (node instanceof Image || node instanceof Link || node instanceof Reference || node instanceof JekyllTagBlock) {
// resolve wiki image link
String url = link.getUrl();

if (docRelativeURL.isEmpty() && docRootURL.isEmpty()) {
// resolve url, return one of LinkStatus other than LinkStatus.UNKNOWN
return link.withStatus(LinkStatus.VALID)
.withUrl(url);
}

if (url.startsWith("http://") || url.startsWith("https://") || url.startsWith("ftp://") || url.startsWith("sftp://")) {
// resolve url, return one of LinkStatus other than LinkStatus.UNKNOWN
return link.withStatus(LinkStatus.VALID)
Expand All @@ -67,7 +66,7 @@ public ResolvedLink resolveLink(@NotNull Node node, @NotNull LinkResolverBasicCo
if (docRootURL != null && !docRootURL.isEmpty()) {
// this one is root url, prefix with root url, without the trailing /
url = docRootURL + url;

if (!url.startsWith("file:")) url = "file://" + url;
return link.withStatus(LinkStatus.VALID)
.withUrl(url);
}
Expand Down Expand Up @@ -117,17 +116,19 @@ public ResolvedLink resolveLink(@NotNull Node node, @NotNull LinkResolverBasicCo

// prefix with remaining docParts
sep = docRelativeURL.startsWith("/") ? "/" : "";
StringBuilder resolvedURL = new StringBuilder();
StringBuilder resolvedPath = new StringBuilder();
iMax = docParts;
for (int i = 0; i < iMax; i++) {
resolvedURL.append(sep);
resolvedURL.append(relativeParts[i]);
resolvedPath.append(sep);
resolvedPath.append(relativeParts[i]);
sep = "/";
}

resolvedURL.append('/').append(resolved).append(suffix);
resolvedPath.append('/').append(resolved).append(suffix);
String resolvedUri = resolvedPath.toString();
if (!resolvedUri.startsWith("file:")) resolvedUri = "file://" + resolvedUri;
return link.withStatus(LinkStatus.VALID)
.withUrl(resolvedURL.toString());
.withUrl(resolvedUri);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ public abstract class ComboDocxConverterSpecTestBase extends ComboSpecTestCase {
WikiLinkExtension.create()
))
.set(DocxRenderer.RENDER_BODY_ONLY, true)
.set(DocxRenderer.DOC_RELATIVE_URL, String.format("file://%s", PROJECT_ROOT_DIRECTORY))
.set(DocxRenderer.DOC_ROOT_URL, String.format("file://%s/assets", PROJECT_ROOT_DIRECTORY))
.set(DocxRenderer.DOC_RELATIVE_URL, String.format("%s", PROJECT_ROOT_DIRECTORY))
.set(DocxRenderer.DOC_ROOT_URL, String.format("%s/assets", PROJECT_ROOT_DIRECTORY))
.set(SharedDataKeys.RUNNING_TESTS, SKIP_IGNORED_TESTS)
.set(JekyllTagExtension.ENABLE_INLINE_TAGS, false)
.set(JekyllTagExtension.LINK_RESOLVER_FACTORIES, Collections.singletonList(new DocxLinkResolver.Factory()))
Expand All @@ -116,14 +116,23 @@ public abstract class ComboDocxConverterSpecTestBase extends ComboSpecTestCase {
optionsMap.put("form-controls-input", new MutableDataSet().set(DocxRenderer.FORM_CONTROLS, "input"));
optionsMap.put("form-controls-form", new MutableDataSet().set(DocxRenderer.FORM_CONTROLS, "form"));
}

private static String removeFileUri(String uri) {
if (uri.startsWith("file://")) return uri.substring("file://".length());
if (uri.startsWith("file:/")) return uri.substring("file:/".length());
return uri;
}

protected static DataHolder getDefaultOptions(ResourceLocation resourceLocation) {
String fileUrl = resourceLocation.getFileDirectoryUrl();
return new MutableDataSet().set(DocxRenderer.DOC_RELATIVE_URL, fileUrl);
String filePath = removeFileUri(fileUrl);
return new MutableDataSet().set(DocxRenderer.DOC_RELATIVE_URL, filePath);
}

protected static DataHolder getDefaultOptions(ResourceLocation resourceLocation, DataHolder options) {
String fileUrl = resourceLocation.getFileDirectoryUrl();
return new MutableDataSet(options).set(DocxRenderer.DOC_RELATIVE_URL, fileUrl);
String filePath = removeFileUri(fileUrl);
return new MutableDataSet(options).set(DocxRenderer.DOC_RELATIVE_URL, filePath);
}

public ComboDocxConverterSpecTestBase(@NotNull SpecExample example, @Nullable Map<String, ? extends DataHolder> optionMap, @Nullable DataHolder... defaultOptions) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -361,3 +361,14 @@ Term 1
````````````````````````````````


## Jekyll Include

```````````````````````````````` example Jekyll Include: 1
{% include test.md %}
.
{% include test.md %}
````````````````````````````````


Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public class Formatter implements IRender {
final public static DataKey<String> TRANSLATION_HTML_BLOCK_TAG_PATTERN = Parser.TRANSLATION_HTML_BLOCK_TAG_PATTERN;
final public static DataKey<String> TRANSLATION_HTML_INLINE_TAG_PATTERN = Parser.TRANSLATION_HTML_INLINE_TAG_PATTERN;

// list of documents across which to uniquify the reference ids if translating
// link resolver info for doc relative and doc root urls
final public static DataKey<String> DOC_RELATIVE_URL = new DataKey<>("DOC_RELATIVE_URL", "");
final public static DataKey<String> DOC_ROOT_URL = new DataKey<>("DOC_ROOT_URL", "");
final public static DataKey<Boolean> DEFAULT_LINK_RESOLVER = new DataKey<>("DEFAULT_LINK_RESOLVER", false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,20 @@ public class MergeLinkResolver implements LinkResolver {

public MergeLinkResolver(LinkResolverBasicContext context) {
// can use context for custom settings
// context.getDocument();
// context.getHtmlOptions();
String docRelativeURL = Formatter.DOC_RELATIVE_URL.get(context.getOptions());
if (docRelativeURL != null) {
docRelativeURL = Utils.removePrefix(docRelativeURL, '/');
}

String docRootURL = Formatter.DOC_ROOT_URL.get(context.getOptions());
if (docRootURL != null) {
docRootURL = Utils.removePrefix(docRootURL, '/');
}

this.docRelativeURL = docRelativeURL;
this.docRootURL = docRootURL;

docRelativeURL = Utils.removePrefix(docRelativeURL, '/');

relativeParts = docRelativeURL.split("/");
}

@NotNull
@Override
public ResolvedLink resolveLink(@NotNull Node node, @NotNull LinkResolverBasicContext context, @NotNull ResolvedLink link) {
if (node instanceof Image || node instanceof Link || node instanceof Reference) {
// resolve link
String url = link.getUrl();

if (docRelativeURL.isEmpty() && docRootURL.isEmpty()) {
Expand All @@ -65,7 +57,6 @@ public ResolvedLink resolveLink(@NotNull Node node, @NotNull LinkResolverBasicCo
if (docRootURL != null && !docRootURL.isEmpty()) {
// this one is root url, prefix with merged root url, without the trailing /
url = (!docRootURL.startsWith("/") ? "/" : "") + docRootURL + url;

return link.withStatus(LinkStatus.VALID)
.withUrl(url);
}
Expand Down Expand Up @@ -110,20 +101,19 @@ public ResolvedLink resolveLink(@NotNull Node node, @NotNull LinkResolverBasicCo
}

// prefix with remaining docParts
StringBuilder resolvedURL = new StringBuilder();

sep = docRelativeURL.startsWith("/") ? "/" : "";

StringBuilder resolvedPath = new StringBuilder();
iMax = docParts;
for (int i = 0; i < iMax; i++) {
resolvedURL.append(sep);
resolvedURL.append(relativeParts[i]);
resolvedPath.append(sep);
resolvedPath.append(relativeParts[i]);
sep = "/";
}

resolvedURL.append('/').append(resolved).append(suffix);
resolvedPath.append('/').append(resolved).append(suffix);
String resolvedUri = resolvedPath.toString();
return link.withStatus(LinkStatus.VALID)
.withUrl(resolvedURL.toString());
.withUrl(resolvedUri);
}
}
}
Expand Down

0 comments on commit 9719ed1

Please sign in to comment.