Skip to content

Commit

Permalink
build: replace remark with flexmark
Browse files Browse the repository at this point in the history
Signed-off-by: Fred Bricon <[email protected]>
  • Loading branch information
fbricon authored and angelozerr committed Dec 21, 2023
1 parent 0d3437e commit 3dd00d1
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 89 deletions.
8 changes: 5 additions & 3 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,11 @@ sourceSets {

dependencies {
implementation("org.zeroturnaround:zt-zip:1.14")
implementation("com.kotcrab.remark:remark:1.2.0") //FIXME use lsp4ij's flexmark instead
implementation("org.jsoup:jsoup:1.14.2") //FIXME use lsp4ij's jsoup instead
implementation("com.google.code.gson:gson:2.10.1")
implementation("org.jsoup:jsoup:1.17.1")
implementation("com.vladsch.flexmark:flexmark-html2md-converter:0.64.8") {
exclude(group="com.vladsch.flexmark", module= "flexmark-jira-converter")
}
implementation("com.google.code.gson:gson:2.10.1") //Need to ensure we don't get telemetry's old gson version
implementation("io.quarkus:quarkus-core:$quarkusVersion") {
isTransitive = false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public String getJavadoc(PsiMethod method, DocumentFormat documentFormat) {

@Override
public String getJavadoc(PsiMember method, com.redhat.qute.commons.DocumentFormat documentFormat) {
boolean markdown = DocumentFormat.Markdown.equals(documentFormat);
boolean markdown = DocumentFormat.Markdown.name().toLowerCase().equals(documentFormat.name().toLowerCase());
Reader reader = markdown ? JavadocContentAccess.getMarkdownContentReader(method)
: JavadocContentAccess.getPlainTextContentReader(method);
return reader != null ? toString(reader) : null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@


import java.io.Reader;
import java.lang.reflect.Field;

import org.jsoup.safety.Cleaner;
import org.jsoup.safety.Safelist;
import com.overzealous.remark.Options;
import com.overzealous.remark.Options.Tables;
import com.overzealous.remark.Remark;
import com.vladsch.flexmark.html2md.converter.FlexmarkHtmlConverter;
import com.vladsch.flexmark.parser.PegdownExtensions;
import com.vladsch.flexmark.util.data.DataKey;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -30,34 +30,29 @@
public class JavaDoc2MarkdownConverter extends AbstractJavaDocConverter {
private static final Logger LOGGER = LoggerFactory.getLogger(JavaDoc2MarkdownConverter.class);

private static Remark remark;

static {
Options options = new Options();
options.tables = Tables.MULTI_MARKDOWN;
options.hardwraps = true;
options.inlineLinks = true;
options.autoLinks = true;
options.reverseHtmlSmartPunctuation = true;
remark = new Remark(options);
//Stop remark from stripping file and jdt protocols in an href
try {
Field cleanerField = Remark.class.getDeclaredField("cleaner");
cleanerField.setAccessible(true);

Cleaner c = (Cleaner) cleanerField.get(remark);

Field safelistField = Cleaner.class.getDeclaredField("safelist");
safelistField.setAccessible(true);
private static final String LINE_SEPARATOR = "\n";

Safelist s = (Safelist) safelistField.get(c);

s.addProtocols("a", "href", "file", "jdt");
s.addProtocols("img", "src", "file");
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
LOGGER.error("Unable to modify jsoup to include file and jdt protocols", e);
}
}
final static public DataKey<Integer> HTML_EXTENSIONS = new DataKey<>("HTML_EXTENSIONS", 0
//| Extensions.ABBREVIATIONS
//| Extensions.EXTANCHORLINKS /*| Extensions.EXTANCHORLINKS_WRAP*/
//| Extensions.AUTOLINKS
//| Extensions.DEFINITIONS
| PegdownExtensions.FENCED_CODE_BLOCKS
//| Extensions.FORCELISTITEMPARA
//| Extensions.HARDWRAPS
//| Extensions.ATXHEADERSPACE
//| Extensions.QUOTES
//| Extensions.SMARTS
//| Extensions.RELAXEDHRULES
//| Extensions.STRIKETHROUGH
//| Extensions.SUPPRESS_HTML_BLOCKS
//| Extensions.SUPPRESS_INLINE_HTML
//| Extensions.TABLES
//| Extensions.TASKLISTITEMS
//| Extensions.WIKILINKS
//| Extensions.TRACE_PARSER
);
private static final FlexmarkHtmlConverter CONVERTER = FlexmarkHtmlConverter.builder().build();

public JavaDoc2MarkdownConverter(Reader reader) {
super(reader);
Expand All @@ -68,7 +63,53 @@ public JavaDoc2MarkdownConverter(String javadoc) {
}

@Override
String convert(String rawHtml) {
return remark.convert(rawHtml);
public String convert(String html) {
Document document = Jsoup.parse(html);
//Add missing table headers if necessary, else most Markdown renderers will crap out
document.select("table").forEach(JavaDoc2MarkdownConverter::addMissingTableHeaders);

String markdown = CONVERTER.convert(document);
if (markdown.endsWith(LINE_SEPARATOR)) {// FlexmarkHtmlConverter keeps adding an extra line
markdown = markdown.substring(0, markdown.length() - LINE_SEPARATOR.length());
}

return markdown;
}

/**
* Adds a new row header if the given table doesn't have any.
*
* @param table
* the HTML table to check for a header
*/
private static void addMissingTableHeaders(Element table) {
int numCols = 0;
for (Element child : table.children()) {
if ("thead".equals(child.nodeName())) {
// Table already has a header, nothing else to do
return;
}
if ("tbody".equals(child.nodeName())) {
Elements rows = child.getElementsByTag("tr");
if (!rows.isEmpty()) {
for (Element row : rows) {
int colSize = row.getElementsByTag("td").size();
//Keep the biggest column size
if (colSize > numCols) {
numCols = colSize;
}
}
}
}
}
if (numCols > 0) {
//Create a new header row based on the number of columns already found
Element newHeader = new Element("tr");
for (int i = 0; i < numCols; i++) {
newHeader.appendChild(new Element("th"));
}
//Insert header row in 1st position in the table
table.insertChildren(0, newHeader);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ protected void processClass(PsiClass configType, String className, SearchContext
// see
// https://github.com/quarkusio/quarkus/blob/44e5e2e3a642d1fa7af9ddea44b6ff8d37e862b8/extensions/kubernetes/deployment/src/main/java/io/quarkus/kubernetes/deployment/KubernetesProcessor.java#L94
super.addItemMetadata(collector, "kubernetes.deployment.target", "java.lang.String", //
"To enable the generation of OpenShift resources, you need to include OpenShift in the target platforms: `kubernetes.deployment.target=openshift`."
+ System.lineSeparator()
+ "If you need to generate resources for both platforms (vanilla Kubernetes and OpenShift), then you need to include both (coma separated)."
+ System.lineSeparator() + "`kubernetes.deployment.target=kubernetes, openshift`.",
"""
To enable the generation of OpenShift resources, you need to include OpenShift in the target platforms: `kubernetes.deployment.target=openshift`.
If you need to generate resources for both platforms (vanilla Kubernetes and OpenShift), then you need to include both (comma-separated).
`kubernetes.deployment.target=kubernetes, openshift`.""",
null, null, null, KUBERNETES_PREFIX, null, true);
// kubernetes.registry
// see
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void testgetFieldJavadoc() throws Exception {
DocumentFormat.Markdown);

String actual = QuteSupportForTemplate.getInstance().getJavadoc(params, getJDTUtils(), new EmptyProgressIndicator());
String expected = " The name of the item ";
String expected = "The name of the item";
assertEquals(expected, actual);
}

Expand All @@ -57,9 +57,11 @@ public void testgetMethodJavadoc() throws Exception {
DocumentFormat.Markdown);

String actual = QuteSupportForTemplate.getInstance().getJavadoc(params, getJDTUtils(), new EmptyProgressIndicator());
String expected = " Returns the derived items. \n" +
" * Returns:\n" +
" - the derived items";
String expected = """
Returns the derived items.
* **Returns:**
* the derived items""";
assertEquals(expected, actual);
}

Expand Down
Loading

0 comments on commit 3dd00d1

Please sign in to comment.