This is an automated email from the ASF dual-hosted git repository.
cziegeler pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-whiteboard.git
The following commit(s) were added to refs/heads/master by this push:
new 55bc5626 Start support for sections
55bc5626 is described below
commit 55bc56269dd52d13f6c9172ba9e5c59b1484cf4c
Author: Carsten Ziegeler <[email protected]>
AuthorDate: Mon Oct 2 15:13:20 2023 +0200
Start support for sections
---
.../apache/sling/mdresource/impl/HtmlServlet.java | 24 ++++++++++++--
.../mdresource/impl/MarkdownResourceDecorator.java | 5 +++
.../mdresource/impl/ResourceConfiguration.java | 3 ++
.../sling/mdresource/impl/ResourceUtils.java | 38 ++++++++++++++++++++--
.../mdresource/impl/md/MarkdownProcessor.java | 12 -------
.../mdresource/impl/md/ProcessingInstructions.java | 4 ---
6 files changed, 65 insertions(+), 21 deletions(-)
diff --git
a/mdresourcedecorator/src/main/java/org/apache/sling/mdresource/impl/HtmlServlet.java
b/mdresourcedecorator/src/main/java/org/apache/sling/mdresource/impl/HtmlServlet.java
index 51cd68e5..517aa472 100644
---
a/mdresourcedecorator/src/main/java/org/apache/sling/mdresource/impl/HtmlServlet.java
+++
b/mdresourcedecorator/src/main/java/org/apache/sling/mdresource/impl/HtmlServlet.java
@@ -20,6 +20,8 @@ package org.apache.sling.mdresource.impl;
import java.io.IOException;
import java.io.PrintWriter;
+import java.util.List;
+import java.util.Map;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
@@ -57,6 +59,9 @@ public class HtmlServlet extends HttpServlet {
@AttributeDefinition(name = "Head Content", description = "Content to
be added to the <head> section")
String head_contents();
+
+ @AttributeDefinition(name = "HTML Property", description = "Name of
the property holding the HTML")
+ String html_property() default "jcr:description";
}
private final Config cfg;
@@ -67,6 +72,7 @@ public class HtmlServlet extends HttpServlet {
}
@Override
+ @SuppressWarnings("unchecked")
protected void doGet(final HttpServletRequest req, final
HttpServletResponse resp)
throws ServletException, IOException {
final SlingHttpServletRequest request = (SlingHttpServletRequest) req;
@@ -93,9 +99,21 @@ public class HtmlServlet extends HttpServlet {
}
pw.println("</header>");
pw.println("<main>");
- final String desc = props.get("jcr:description", String.class);
- if (desc != null) {
- pw.println(desc);
+ final Object html = props.get(this.cfg.html_property());
+ if (html instanceof String) {
+ pw.println(html.toString());
+ } else if (html instanceof List) {
+ boolean startSection = true;
+ for (final Map.Entry<String, String> element :
(List<Map.Entry<String,String>>) html) {
+ if (startSection) {
+ pw.println("<div class=\"section\">");
+ startSection = false;
+ }
+ pw.print(element.getValue());
+ }
+ if (!startSection) {
+ pw.println("</div>");
+ }
}
pw.println("<footer>");
if (this.cfg.footer_resource() != null) {
diff --git
a/mdresourcedecorator/src/main/java/org/apache/sling/mdresource/impl/MarkdownResourceDecorator.java
b/mdresourcedecorator/src/main/java/org/apache/sling/mdresource/impl/MarkdownResourceDecorator.java
index 21ffeab9..3865046b 100644
---
a/mdresourcedecorator/src/main/java/org/apache/sling/mdresource/impl/MarkdownResourceDecorator.java
+++
b/mdresourcedecorator/src/main/java/org/apache/sling/mdresource/impl/MarkdownResourceDecorator.java
@@ -79,6 +79,10 @@ public class MarkdownResourceDecorator implements
ResourceDecorator {
description = "Name of the property holding the rendered html")
String html_property() default "jcr:description";
+ @AttributeDefinition(name = "Html Elements",
+ description = "Name of the property holding the list of top
level HTML elements (optional)")
+ String html_elements_property();
+
@AttributeDefinition(name = "Markdown Property",
description = "Name of the property holding the read markdown
(optional)")
String markdown_property();
@@ -115,6 +119,7 @@ public class MarkdownResourceDecorator implements
ResourceDecorator {
this.config.resourceType = cfg.resource_type();
this.config.sourceMarkdownProperty =
cleanInput(cfg.source_markdown_property());
this.config.htmlProperty = cleanInput(cfg.html_property());
+ this.config.elementsProperty =
cleanInput(cfg.html_elements_property());
this.config.titleProperty = cleanInput(cfg.title_property());
this.config.markdownProperty = cleanInput(cfg.markdown_property());
this.config.rewriteLinks = cfg.rewrite_links();
diff --git
a/mdresourcedecorator/src/main/java/org/apache/sling/mdresource/impl/ResourceConfiguration.java
b/mdresourcedecorator/src/main/java/org/apache/sling/mdresource/impl/ResourceConfiguration.java
index d43f4cb2..e77bfaf2 100644
---
a/mdresourcedecorator/src/main/java/org/apache/sling/mdresource/impl/ResourceConfiguration.java
+++
b/mdresourcedecorator/src/main/java/org/apache/sling/mdresource/impl/ResourceConfiguration.java
@@ -44,6 +44,9 @@ public class ResourceConfiguration {
/** The property holding the rendered html */
public String htmlProperty;
+ /** The property holding the elements list (pair of element name and html
contents) */
+ public String elementsProperty;
+
/** The property holding the first title (optional) */
public String titleProperty;
diff --git
a/mdresourcedecorator/src/main/java/org/apache/sling/mdresource/impl/ResourceUtils.java
b/mdresourcedecorator/src/main/java/org/apache/sling/mdresource/impl/ResourceUtils.java
index 5af73389..650fc56b 100644
---
a/mdresourcedecorator/src/main/java/org/apache/sling/mdresource/impl/ResourceUtils.java
+++
b/mdresourcedecorator/src/main/java/org/apache/sling/mdresource/impl/ResourceUtils.java
@@ -24,7 +24,11 @@ import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;
+import java.util.AbstractMap;
+import java.util.ArrayList;
+import java.util.Collections;
import java.util.HashMap;
+import java.util.List;
import java.util.Map;
import org.apache.sling.api.resource.Resource;
@@ -36,6 +40,11 @@ import
org.apache.sling.mdresource.impl.ResourceConfiguration.SourceType;
import org.apache.sling.mdresource.impl.md.MarkdownProcessor;
import org.apache.sling.mdresource.impl.md.ProcessingInstructions;
import org.apache.sling.mdresource.impl.md.ProcessingResult;
+import org.apache.sling.mdresource.impl.md.links.CustomLinkResolverFactory;
+import org.slf4j.LoggerFactory;
+
+import com.vladsch.flexmark.html.HtmlRenderer;
+import com.vladsch.flexmark.util.ast.Node;
public class ResourceUtils {
@@ -88,9 +97,7 @@ public class ResourceUtils {
final ProcessingInstructions inst = new ProcessingInstructions();
inst.extractTitle = config.titleProperty != null;
- inst.renderHtml = true;
inst.handleYamlFrontmatter = true;
- inst.rewriteLinks = config.rewriteLinks;
try ( final Reader r = getMarkdownReader(config, rsrc, origProps,
props)) {
@@ -102,6 +109,33 @@ public class ResourceUtils {
if ( result.html != null ) {
props.put(config.htmlProperty, result.html);
}
+ if ( result.document.hasChildren()) {
+ final HtmlRenderer.Builder builder = HtmlRenderer.builder();
+ if ( config.rewriteLinks ) {
+ builder.linkResolverFactory(new
CustomLinkResolverFactory(rsrc));
+ }
+ final HtmlRenderer htmlRenderer = builder.build();
+
+ if (config.htmlProperty != null) {
+ props.put(config.htmlProperty,
htmlRenderer.render(result.document));
+ }
+ if ( config.elementsProperty != null ) {
+ final List<Map.Entry<String, String>> elements = new
ArrayList<>();
+ for(final Node node : result.document.getChildren()) {
+ elements.add(new
AbstractMap.SimpleEntry<>(node.getNodeName(), htmlRenderer.render(node)));
+ }
+ props.put(config.elementsProperty, elements);
+ LoggerFactory.getLogger("foo").info("Setting {} to {}",
config.elementsProperty, elements.size());
+ }
+ } else {
+ if (config.htmlProperty != null) {
+ props.put(config.htmlProperty, "");
+ }
+ if ( config.elementsProperty != null ) {
+ props.put(config.elementsProperty, Collections.emptyMap());
+ }
+ }
+
} catch (final IOException e) {
MarkdownResourceDecorator.LOGGER.error("Unable to read markdown :
" + e.getMessage(), e);
diff --git
a/mdresourcedecorator/src/main/java/org/apache/sling/mdresource/impl/md/MarkdownProcessor.java
b/mdresourcedecorator/src/main/java/org/apache/sling/mdresource/impl/md/MarkdownProcessor.java
index 925510cf..309d96c9 100644
---
a/mdresourcedecorator/src/main/java/org/apache/sling/mdresource/impl/md/MarkdownProcessor.java
+++
b/mdresourcedecorator/src/main/java/org/apache/sling/mdresource/impl/md/MarkdownProcessor.java
@@ -29,10 +29,8 @@ import org.apache.sling.api.resource.Resource;
import org.apache.sling.mdresource.impl.md.handler.HeadingHandler;
import org.apache.sling.mdresource.impl.md.handler.NodeHandler;
import org.apache.sling.mdresource.impl.md.handler.YamlFrontMatterHandler;
-import org.apache.sling.mdresource.impl.md.links.CustomLinkResolverFactory;
import com.vladsch.flexmark.ext.yaml.front.matter.YamlFrontMatterExtension;
-import com.vladsch.flexmark.html.HtmlRenderer;
import com.vladsch.flexmark.parser.Parser;
import com.vladsch.flexmark.util.ast.Document;
import com.vladsch.flexmark.util.ast.Node;
@@ -78,16 +76,6 @@ public class MarkdownProcessor {
currentNode = nextNode;
}
- if ( document.hasChildren() && inst.renderHtml) {
- final HtmlRenderer.Builder builder = HtmlRenderer.builder();
- if ( inst.rewriteLinks ) {
- builder.linkResolverFactory(new
CustomLinkResolverFactory(baseResource));
- }
- final HtmlRenderer htmlRenderer = builder.build();
-
- result.html = htmlRenderer.render(document);
- }
-
result.document = document;
return result;
}
diff --git
a/mdresourcedecorator/src/main/java/org/apache/sling/mdresource/impl/md/ProcessingInstructions.java
b/mdresourcedecorator/src/main/java/org/apache/sling/mdresource/impl/md/ProcessingInstructions.java
index 320bb5cb..590cb5c6 100644
---
a/mdresourcedecorator/src/main/java/org/apache/sling/mdresource/impl/md/ProcessingInstructions.java
+++
b/mdresourcedecorator/src/main/java/org/apache/sling/mdresource/impl/md/ProcessingInstructions.java
@@ -25,9 +25,5 @@ public class ProcessingInstructions {
public boolean extractTitle = false;
- public boolean renderHtml = false;
-
public boolean handleYamlFrontmatter = false;
-
- public boolean rewriteLinks = false;
}