diff --git a/pantheon-bundle/src/main/java/com/redhat/pantheon/asciidoctor/AsciidoctorService.java b/pantheon-bundle/src/main/java/com/redhat/pantheon/asciidoctor/AsciidoctorService.java index 68e16f750..8eed78167 100644 --- a/pantheon-bundle/src/main/java/com/redhat/pantheon/asciidoctor/AsciidoctorService.java +++ b/pantheon-bundle/src/main/java/com/redhat/pantheon/asciidoctor/AsciidoctorService.java @@ -1,5 +1,18 @@ package com.redhat.pantheon.asciidoctor; +import com.google.common.base.Charsets; +import com.google.common.hash.HashCode; +import com.google.common.hash.Hashing; +import com.redhat.pantheon.asciidoctor.extension.HtmlModulePostprocessor; +import com.redhat.pantheon.asciidoctor.extension.MetadataExtractorTreeProcessor; +import com.redhat.pantheon.asciidoctor.extension.SlingResourceIncludeProcessor; +import com.redhat.pantheon.asciidoctor.extension.UuidPreProcessor; +import com.redhat.pantheon.conf.GlobalConfig; +import com.redhat.pantheon.model.module.Content; +import com.redhat.pantheon.model.module.Metadata; +import com.redhat.pantheon.model.module.Module; +import com.redhat.pantheon.model.module.ModuleVersion; +import com.redhat.pantheon.sling.ServiceResourceResolverProvider; import static java.util.stream.Collectors.toMap; import java.text.SimpleDateFormat; @@ -26,19 +39,8 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.google.common.base.Charsets; -import com.google.common.hash.HashCode; -import com.google.common.hash.Hashing; -import com.redhat.pantheon.asciidoctor.extension.HtmlModulePostprocessor; -import com.redhat.pantheon.asciidoctor.extension.MetadataExtractorTreeProcessor; -import com.redhat.pantheon.asciidoctor.extension.SlingResourceIncludeProcessor; -import com.redhat.pantheon.conf.GlobalConfig; import com.redhat.pantheon.model.ProductVersion; -import com.redhat.pantheon.model.module.Content; -import com.redhat.pantheon.model.module.Metadata; -import com.redhat.pantheon.model.module.Module; -import com.redhat.pantheon.model.module.ModuleVersion; -import com.redhat.pantheon.sling.ServiceResourceResolverProvider; + /** * Business service class which provides Asciidoctor-related methods which work in conjunction with other @@ -229,18 +231,29 @@ private String buildModule(Module base, ModuleVersion moduleVersion, Map lines = reader.readLines(); + List newLines = new ArrayList(); + String[] split; + String uuid, newLink; + + for (String line: lines) { + if(line.startsWith("xref:")){ + split = line.split(",pantheon-id="); + uuid = split[1].replace(split[1].substring(split[1].length()-1), ""); + resolveActualPath(module.getResourceResolver(), uuid); + newLink = split[0].replaceAll(":.*?\\[", ":"+newModulePath+"["); + newLink = newLink + "]"; + newLines.add(newLink); + }else { + newLines.add(line); + } + } + reader.restoreLines(newLines); + } + + private static void resolveActualPath(ResourceResolver resolver, String uuid) { + JcrQueryHelper qh = new JcrQueryHelper(resolver); + try { + Optional result = + qh.query("select * from [nt:base] WHERE [jcr:uuid] = '" + uuid + "'") + .findFirst(); + + result.ifPresent(output -> { + assignValue(output.getPath()); + log.info("result:", output.getPath()); + }); + }catch (Exception e) { + e.printStackTrace(); + } + }; + + private static void assignValue(String path){ + newModulePath = path + ".preview"; + log.info("newPath2:"+ newModulePath); + } + } \ No newline at end of file diff --git a/pantheon-bundle/src/main/java/com/redhat/pantheon/html/Html.java b/pantheon-bundle/src/main/java/com/redhat/pantheon/html/Html.java index 21c088305..f4ed7e45a 100644 --- a/pantheon-bundle/src/main/java/com/redhat/pantheon/html/Html.java +++ b/pantheon-bundle/src/main/java/com/redhat/pantheon/html/Html.java @@ -56,30 +56,6 @@ public static Function encodeAllImageLocations(final Resourc }; } - public static Function dereferenceAllHyperlinks(ResourceResolver resolver) { - JcrQueryHelper qh = new JcrQueryHelper(resolver); - return document -> { - document.select("a") - .forEach(hyperlink -> { - hyperlink.childNodes().stream() - .filter(child -> "#comment".equals(child.nodeName())) - .map(child -> UUID_PATTERN.matcher(child.outerHtml())) - .filter(matcher -> matcher.find()) - .map(matcher -> matcher.group()) - .forEach(uuid -> { - try { - qh.query("select * from [pant:module] as module WHERE module.[jcr:uuid] = '" + uuid + "'") - .findFirst() - .ifPresent(resource -> hyperlink.attr("href", resource.getPath() + ".preview")); - } catch (RepositoryException e) { - e.printStackTrace(); - } - }); - }); - return document; - }; - } - /** * An extractor function which returns just the body content for the parsed html document. * @return An html extactor function. diff --git a/pantheon-bundle/src/main/java/com/redhat/pantheon/servlet/module/AsciidocContentRenderingServlet.java b/pantheon-bundle/src/main/java/com/redhat/pantheon/servlet/module/AsciidocContentRenderingServlet.java index 25cd3ab8c..97fad42bd 100644 --- a/pantheon-bundle/src/main/java/com/redhat/pantheon/servlet/module/AsciidocContentRenderingServlet.java +++ b/pantheon-bundle/src/main/java/com/redhat/pantheon/servlet/module/AsciidocContentRenderingServlet.java @@ -57,6 +57,7 @@ protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse r Optional content; if (draft) { content = module.getDraftContent(LocaleUtils.toLocale(locale)); + log.info("asciidoctorservlet content"); } else { content = module.getReleasedContent(LocaleUtils.toLocale(locale)); } diff --git a/pantheon-bundle/src/test/java/com/redhat/pantheon/html/HtmlTest.java b/pantheon-bundle/src/test/java/com/redhat/pantheon/html/HtmlTest.java index f52387e88..aa0cb0bda 100644 --- a/pantheon-bundle/src/test/java/com/redhat/pantheon/html/HtmlTest.java +++ b/pantheon-bundle/src/test/java/com/redhat/pantheon/html/HtmlTest.java @@ -68,44 +68,6 @@ void encodeAllImageLocations() { }); } - @Test - void dereferenceAllHyperlinks() { - // Given - sCtx.create().resource("/test", - "name", "a-name", - "jcr:primaryType", "pant:module"); - sCtx.create().resource("/test/child", - "name", "child-name"); - String resourceUuid = sCtx.resourceResolver() - .getResource("/test") - .getValueMap() - .get("jcr:uuid") - .toString(); - - String html = "" + - "This is the head" + - "This is the body" + - "vanilla hyperlink" + - "link with a valid uuid" + - "link with a random uuid" + - "" + - ""; - - // When - String transformedHtml = Html.parse(Charsets.UTF_8.name()) - .andThen(Html.dereferenceAllHyperlinks(sCtx.resourceResolver())) - .andThen(doc -> doc.toString()) - .apply(html); - - // Then - Document doc = Jsoup.parse(transformedHtml, "UTF-8"); - List elms = doc.select("a").stream().collect(Collectors.toList()); - assertFalse(elms.isEmpty()); - assertTrue("1234".equals(elms.get(0).attr("href"))); - assertFalse("abcd".equals(elms.get(1).attr("href"))); - assertTrue("xyz".equals(elms.get(2).attr("href"))); - } - @Test void getBody() { // Given