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 01de21a5 Add simple demo servlet
01de21a5 is described below

commit 01de21a5940d60a2eed28f77be0d68d9499946c0
Author: Carsten Ziegeler <[email protected]>
AuthorDate: Mon Oct 2 10:57:59 2023 +0200

    Add simple demo servlet
---
 mdresourcedecorator/README.md                      |  8 +-
 mdresourcedecorator/pom.xml                        |  6 ++
 .../apache/sling/mdresource/impl/HtmlServlet.java  | 99 ++++++++++++++++++++++
 .../mdresource/impl/MarkdownResourceDecorator.java | 17 +---
 .../mdresource/impl/md/MarkdownProcessor.java      |  1 +
 .../sling/mdresource/impl/md/ProcessingResult.java |  3 +
 .../mdresource/impl/md/handler/HeadingHandler.java |  3 +-
 .../impl/MarkdownResourceWrapperTest.java          |  4 +-
 8 files changed, 117 insertions(+), 24 deletions(-)

diff --git a/mdresourcedecorator/README.md b/mdresourcedecorator/README.md
index a1e9920e..156bca8a 100644
--- a/mdresourcedecorator/README.md
+++ b/mdresourcedecorator/README.md
@@ -11,16 +11,16 @@ A resource decorator can decorate resources provided by any 
resource provider. F
 ## Installation
 
 Install this bundle alongside with these two bundles:
-- org.jsoup:jsoup:1.13.1
-- com.vladsch.flexmark:flexmark-osgi:0.62.2
+- org.jsoup:jsoup:1.16.1
+- com.vladsch.flexmark:flexmark-osgi:0.64.8
 
 ## Configuration
 
 As the decorator is decorating resources from an already existing resource 
provider, you need to have a resource provider providing markdown files. You 
could use the file resource provider or store the files in the JCR repository.
 
-Configure a `org.apache.sling.resource.MarkdownResourceDecorator`, e.g.
+Configure a `org.apache.sling.resource.MarkdownResourceDecorator`. As the 
decorator is a factory, you can configure it multiple times and you need to use 
a factory configuration, e.g.
 
-    "org.apache.sling.resource.MarkdownResourceDecorator" : {
+    "org.apache.sling.resource.MarkdownResourceDecorator~files" : {
         "decoration.paths" : "/content/files/**.md"
     }
         
diff --git a/mdresourcedecorator/pom.xml b/mdresourcedecorator/pom.xml
index c0dda10d..84697250 100644
--- a/mdresourcedecorator/pom.xml
+++ b/mdresourcedecorator/pom.xml
@@ -51,6 +51,12 @@
             <artifactId>org.osgi.service.metatype.annotations</artifactId>
             <scope>provided</scope>
         </dependency>
+        <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.servlets.annotations</artifactId>
+            <version>1.2.6</version>
+            <scope>provided</scope>
+        </dependency>
         <dependency>
             <groupId>org.slf4j</groupId>
             <artifactId>slf4j-api</artifactId>
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
new file mode 100644
index 00000000..98070a8f
--- /dev/null
+++ 
b/mdresourcedecorator/src/main/java/org/apache/sling/mdresource/impl/HtmlServlet.java
@@ -0,0 +1,99 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.mdresource.impl;
+
+import java.io.IOException;
+import java.io.PrintWriter;
+
+import javax.servlet.Servlet;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.sling.api.SlingHttpServletRequest;
+import org.apache.sling.api.resource.ValueMap;
+import org.apache.sling.servlets.annotations.SlingServletResourceTypes;
+import org.osgi.service.component.annotations.Activate;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.ConfigurationPolicy;
+import org.osgi.service.metatype.annotations.AttributeDefinition;
+import org.osgi.service.metatype.annotations.Designate;
+import org.osgi.service.metatype.annotations.ObjectClassDefinition;
+
+@SlingServletResourceTypes(
+    resourceTypes="sling/markdown/file",
+    methods="GET",
+    extensions="html"
+)
+@Component(service=Servlet.class, configurationPolicy = 
ConfigurationPolicy.REQUIRE)
+@Designate(ocd = HtmlServlet.Config.class)
+public class HtmlServlet extends HttpServlet {
+
+    @ObjectClassDefinition(name = "Apache Sling Markdown HTML Servlet", 
description = "Servlet to render Markdown files as HTML")
+    public @interface Config {
+
+//        @AttributeDefinition(name = "Navigation Resource", description = 
"Path to the navigation resource")
+//        String nav_resource();
+
+//        @AttributeDefinition(name = "Footer Resource", description = "Path 
to the footer resource")
+//        String footer_resource();
+
+        @AttributeDefinition(name = "Head Content", description = "Content to 
be added to the <head> section")
+        String head_contents();
+    }
+
+    private final Config cfg;
+
+    @Activate
+    public HtmlServlet(final Config cfg) {
+        this.cfg = cfg;
+    }
+
+    @Override
+    protected void doGet(final HttpServletRequest req, final 
HttpServletResponse resp)
+    throws ServletException, IOException {
+        final SlingHttpServletRequest request = (SlingHttpServletRequest) req;
+        resp.setContentType("text/html");
+        resp.setCharacterEncoding("UTF-8");
+        final PrintWriter pw = resp.getWriter();
+        pw.println("<html>");
+        pw.println("<head>");
+        final ValueMap props = request.getResource().getValueMap();
+        final String title = props.get("jcr:title", String.class);
+        if (title != null) {
+            pw.print("<title>");
+            pw.print(title);
+            pw.println("</title>");
+        }
+        if (this.cfg.head_contents() != null) {
+            pw.println(this.cfg.head_contents());
+        }
+        pw.println("</head>");
+        pw.println("<body>");
+        pw.println("<main>");
+        final String desc = props.get("jcr:description", String.class);
+        if (desc != null) {
+            pw.println(desc);
+        }
+        pw.println("</main>");
+        pw.println("</body>");
+        pw.println("</html>");
+    }
+}
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 a2028267..21ffeab9 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
@@ -55,10 +55,6 @@ public class MarkdownResourceDecorator implements 
ResourceDecorator {
                 description = "Resources contained in the tree below these 
paths are decorated. Patterns are supported, e.g. \"/content/**.md\".")
         String[] decoration_paths();
 
-        @AttributeDefinition(name = "Decoration Extensions",
-                description = "Only resources with these extensions are 
decorated. If set to \"*\" all resources match.")
-        String[] decoration_extensions() default {"md"};
-
         @AttributeDefinition(name = "Decoration Resource Types",
                 description = "Only resources with these resource types are 
decorated. If set to \"*\" all resources match.")
         String[] decoration_types() default {RESOURCE_TYPE_FILE};
@@ -103,19 +99,12 @@ public class MarkdownResourceDecorator implements 
ResourceDecorator {
     private final PathSet paths;
     private final Set<String> resourceTypes;
     private final ResourceConfiguration config = new ResourceConfiguration();
-    private final Set<String> extensions;
 
     @Activate
     public MarkdownResourceDecorator(final Config cfg) {
         this.paths = 
PathSet.fromStringCollection(Arrays.stream(cfg.decoration_paths())
                 .map(path -> path.contains("*") ? 
Path.GLOB_PREFIX.concat(path) : path)
                 .collect(Collectors.toList()));
-        final Set<String> exts =  new 
HashSet<>(Arrays.asList(cfg.decoration_extensions()));
-        if (exts.contains("*") ) {
-            this.extensions = null;
-        } else {
-            this.extensions = exts;
-        }
         final Set<String> rts =  new 
HashSet<>(Arrays.asList(cfg.decoration_types()));
         if (rts.contains("*") ) {
             this.resourceTypes = null;
@@ -143,11 +132,7 @@ public class MarkdownResourceDecorator implements 
ResourceDecorator {
         // check resource type and path
         if ( (this.resourceTypes == null || 
this.resourceTypes.contains(resource.getResourceType()))
              && this.paths.matches( resource.getPath() ) != null ) {
-
-            final int pos = resource.getName().lastIndexOf('.');
-            if ( this.extensions == null || (pos != -1 && 
this.extensions.contains(resource.getName().substring(pos + 1))) ) {
-                return new MarkdownResourceWrapper(resource, this.config);
-            }
+            return new MarkdownResourceWrapper(resource, this.config);
         }
         return null;
     }
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 1d99e5ef..925510cf 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
@@ -88,6 +88,7 @@ public class MarkdownProcessor {
             result.html = htmlRenderer.render(document);
         }
 
+        result.document = document;
         return result;
     }
 }
diff --git 
a/mdresourcedecorator/src/main/java/org/apache/sling/mdresource/impl/md/ProcessingResult.java
 
b/mdresourcedecorator/src/main/java/org/apache/sling/mdresource/impl/md/ProcessingResult.java
index 6c19d8fe..a40070ea 100644
--- 
a/mdresourcedecorator/src/main/java/org/apache/sling/mdresource/impl/md/ProcessingResult.java
+++ 
b/mdresourcedecorator/src/main/java/org/apache/sling/mdresource/impl/md/ProcessingResult.java
@@ -20,9 +20,12 @@ package org.apache.sling.mdresource.impl.md;
 
 import java.util.HashMap;
 import java.util.Map;
+import com.vladsch.flexmark.util.ast.Document;
 
 public class ProcessingResult {
 
+    public Document document;
+
     public String title;
 
     public String html;
diff --git 
a/mdresourcedecorator/src/main/java/org/apache/sling/mdresource/impl/md/handler/HeadingHandler.java
 
b/mdresourcedecorator/src/main/java/org/apache/sling/mdresource/impl/md/handler/HeadingHandler.java
index af33194a..ff40f36e 100644
--- 
a/mdresourcedecorator/src/main/java/org/apache/sling/mdresource/impl/md/handler/HeadingHandler.java
+++ 
b/mdresourcedecorator/src/main/java/org/apache/sling/mdresource/impl/md/handler/HeadingHandler.java
@@ -29,12 +29,11 @@ public class HeadingHandler implements NodeHandler {
 
     @Override
     public boolean consume(final Node n, final ProcessingResult result) {
-        if ( n instanceof Heading && !hasTitle) {
+        if ( !hasTitle && n instanceof Heading ) {
             final Heading h = (Heading) n;
             if ( h.getLevel() == 1 ) {
                 result.title = h.getText().toString();
                 this.hasTitle = true;
-                return true;
             }
         }
         return false;
diff --git 
a/mdresourcedecorator/src/test/java/org/apache/sling/mdresource/impl/MarkdownResourceWrapperTest.java
 
b/mdresourcedecorator/src/test/java/org/apache/sling/mdresource/impl/MarkdownResourceWrapperTest.java
index 8cb2cfcf..9f8dd7dc 100644
--- 
a/mdresourcedecorator/src/test/java/org/apache/sling/mdresource/impl/MarkdownResourceWrapperTest.java
+++ 
b/mdresourcedecorator/src/test/java/org/apache/sling/mdresource/impl/MarkdownResourceWrapperTest.java
@@ -61,7 +61,7 @@ public class MarkdownResourceWrapperTest {
                 map.get("jcr:title", String.class));
 
         assertEquals("valueMap[jcr:description]",
-                "<p>This is an example of a simple markdown file</p>\n",
+                "<h1>Simple markdown file</h1>\n<p>This is an example of a 
simple markdown file</p>\n",
                 map.get("jcr:description", String.class));
 
         assertEquals("valueMap[author]", "John Doe", map.get("author", 
String.class));
@@ -125,7 +125,7 @@ public class MarkdownResourceWrapperTest {
 
         assertEquals("valueMap[jcr:title]", "First",
                 map.get("jcr:title", String.class));
-        assertEquals("valueMap[jcr:description]", "<h1>And</h1>\n" +
+        assertEquals("valueMap[jcr:description]", 
"<h1>First</h1>\n<h1>And</h1>\n" +
                 "<h2>Last</h2>\n" +
                 "<h1>And</h1>\n" +
                 "<h1>Always</h1>\n",

Reply via email to