-
Notifications
You must be signed in to change notification settings - Fork 36
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
CCS-3388 Preprocessing the adoc to create an xref using uuid #248
base: master
Are you sure you want to change the base?
Changes from all commits
c4b2f6a
16371b3
da6a934
e7eb7cd
34241b8
8066523
d48b0c5
2443f6e
2c4d674
d468249
9909af1
1ce3b58
e06360f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package com.redhat.pantheon.asciidoctor.extension; | ||
|
||
import org.asciidoctor.ast.Document; | ||
import org.asciidoctor.extension.Preprocessor; | ||
import org.asciidoctor.extension.PreprocessorReader; | ||
import com.redhat.pantheon.jcr.JcrQueryHelper; | ||
import org.apache.sling.api.resource.ResourceResolver; | ||
import java.util.Optional; | ||
|
||
import org.apache.sling.api.resource.Resource; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class UuidPreProcessor extends Preprocessor { // (1) | ||
|
||
private static Resource module; | ||
private static final Logger log = LoggerFactory.getLogger(UuidPreProcessor.class); | ||
private static String newModulePath; | ||
Comment on lines
+18
to
+20
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These fields cannot be static. I'm happy to explain why, if you'd like me to. |
||
|
||
public UuidPreProcessor(Resource module) { | ||
this.module = module; | ||
} | ||
|
||
@Override | ||
public void process(Document document, PreprocessorReader reader) { | ||
|
||
List<String> lines = reader.readLines(); | ||
List<String> newLines = new ArrayList<String>(); | ||
String[] split; | ||
String uuid, newLink; | ||
|
||
for (String line: lines) { | ||
if(line.startsWith("xref:")){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a good start but it needs to be updated so that it works for all of these scenarios:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes Ben, |
||
split = line.split(",pantheon-id="); | ||
uuid = split[1].replace(split[1].substring(split[1].length()-1), ""); | ||
Comment on lines
+36
to
+37
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not quite sure what this does. Are we assuming that the 'pantheon-id' parameter is the final parameter of the xref, and then the last character is the closing Don't spend too much time making the code update "perfect" - I'm not sure that we'll stick with UUIDs for the final solution, so just getting the next N characters (N = however long a UUID is) from the string would be good enough for this POC. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This approach was done considering the last character to be ']' and assuming 'pantheon-id' as the final parameter of xref. |
||
resolveActualPath(module.getResourceResolver(), uuid); | ||
newLink = split[0].replaceAll(":.*?\\[", ":"+newModulePath+"["); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OOF, regex syntax! I think I can read this. It mostly makes sense, but what is the purpose of the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Explained the use of "?" in the below example: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ahhhh, the "?" makes it non-greedy! Ok, that makes sense, thank you very much! |
||
newLink = newLink + "]"; | ||
newLines.add(newLink); | ||
Comment on lines
+38
to
+41
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would like to see this logic being conditional based on whether or not the UUID query returns a result. It looks like the current behavior is that if the UUID fails to resolve, the xref is totally wiped out and replaced with nothing. What the code should do is leave the original author-supplied xref target intact. |
||
}else { | ||
newLines.add(line); | ||
} | ||
} | ||
reader.restoreLines(newLines); | ||
} | ||
|
||
private static void resolveActualPath(ResourceResolver resolver, String uuid) { | ||
JcrQueryHelper qh = new JcrQueryHelper(resolver); | ||
try { | ||
Optional<Resource> result = | ||
qh.query("select * from [nt:base] WHERE [jcr:uuid] = '" + uuid + "'") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know we talked about having There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This makes sense, since this is searching for all the UUIDs across the hierarchy, we might have to switch to [pant:module] |
||
.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); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,6 +57,7 @@ protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse r | |
Optional<Content> content; | ||
if (draft) { | ||
content = module.getDraftContent(LocaleUtils.toLocale(locale)); | ||
log.info("asciidoctorservlet content"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You shouldn't need to touch this file. I am 90% sure that this servlet is unused in our project... but no one has gotten around to deleting it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I might have to delete this |
||
} else { | ||
content = module.getReleasedContent(LocaleUtils.toLocale(locale)); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -68,44 +68,6 @@ void encodeAllImageLocations() { | |
}); | ||
} | ||
|
||
@Test | ||
void dereferenceAllHyperlinks() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It definitely makes sense to remove this test since you're removing the code that it targets, but don't forget to add appropriate tests of your own for the new code that you're introducing! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes Ben, |
||
// 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 = "<html>" + | ||
"<head><title>This is the head</title></head>" + | ||
"<body>This is the body" + | ||
"<a href='1234'>vanilla hyperlink</a>" + | ||
"<a href='abcd'><!-- " + resourceUuid + " -->link with a valid uuid</a>" + | ||
"<a href='xyz'><!-- 123e4567-e89b-12d3-a456-426655440000 -->link with a random uuid</a>" + | ||
"</body>" + | ||
"</html>"; | ||
|
||
// 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<Element> 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 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove these lines. You are calling
.convert()
twice here - the first result gets overwritten by the second, so it must be unnecessary. We probably don't need thelog
statement here either.