incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bdelacre...@apache.org
Subject svn commit: r633528 - in /incubator/sling/trunk: extensions/apt/apt-parser/src/main/java/org/apache/sling/apt/parser/ extensions/apt/apt-parser/src/main/java/org/apache/sling/apt/parser/internal/ extensions/apt/apt-parser/src/test/java/org/apache/sling...
Date Tue, 04 Mar 2008 16:21:41 GMT
Author: bdelacretaz
Date: Tue Mar  4 08:21:39 2008
New Revision: 633528

URL: http://svn.apache.org/viewvc?rev=633528&view=rev
Log:
SLING-305 - APT parser and servlet generate html, head and body elements

Modified:
    incubator/sling/trunk/extensions/apt/apt-parser/src/main/java/org/apache/sling/apt/parser/SlingAptParser.java
    incubator/sling/trunk/extensions/apt/apt-parser/src/main/java/org/apache/sling/apt/parser/internal/CustomAptSink.java
    incubator/sling/trunk/extensions/apt/apt-parser/src/main/java/org/apache/sling/apt/parser/internal/SlingAptParserImpl.java
    incubator/sling/trunk/extensions/apt/apt-parser/src/test/java/org/apache/sling/apt/parser/internal/SlingAptParserImplTest.java
    incubator/sling/trunk/launchpad/launchpad-webapp/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/apt/SimpleAptRenderingTest.java
    incubator/sling/trunk/launchpad/launchpad-webapp/src/test/resources/integration-test/apt/apt-test.apt

Modified: incubator/sling/trunk/extensions/apt/apt-parser/src/main/java/org/apache/sling/apt/parser/SlingAptParser.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/extensions/apt/apt-parser/src/main/java/org/apache/sling/apt/parser/SlingAptParser.java?rev=633528&r1=633527&r2=633528&view=diff
==============================================================================
--- incubator/sling/trunk/extensions/apt/apt-parser/src/main/java/org/apache/sling/apt/parser/SlingAptParser.java
(original)
+++ incubator/sling/trunk/extensions/apt/apt-parser/src/main/java/org/apache/sling/apt/parser/SlingAptParser.java
Tue Mar  4 08:21:39 2008
@@ -21,11 +21,23 @@
 import java.io.IOException;
 import java.io.Reader;
 import java.io.Writer;
+import java.util.Map;
 
 /** APT parser interface for Sling */
 public interface SlingAptParser {
+    
+    /** Set this option to "false" to disable the generation of
+     *  an HTML page skeleton (html, head, body tags)
+     */
+    String OPT_HTML_SKELETON = "apt.parser.html.skeleton";
+    
     /** Parse the given input, which must be in APT format, and
      *  write the HTML result to output.
      */
     void parse(Reader input, Writer output) throws IOException, SlingAptParseException;
+    
+    /** Parse the given input, which must be in APT format, and
+     *  write the HTML result to output, taking specified options into account
+     */
+    void parse(Reader input, Writer output, Map<String, Object> options) throws IOException,
SlingAptParseException;
 }

Modified: incubator/sling/trunk/extensions/apt/apt-parser/src/main/java/org/apache/sling/apt/parser/internal/CustomAptSink.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/extensions/apt/apt-parser/src/main/java/org/apache/sling/apt/parser/internal/CustomAptSink.java?rev=633528&r1=633527&r2=633528&view=diff
==============================================================================
--- incubator/sling/trunk/extensions/apt/apt-parser/src/main/java/org/apache/sling/apt/parser/internal/CustomAptSink.java
(original)
+++ incubator/sling/trunk/extensions/apt/apt-parser/src/main/java/org/apache/sling/apt/parser/internal/CustomAptSink.java
Tue Mar  4 08:21:39 2008
@@ -19,17 +19,36 @@
 package org.apache.sling.apt.parser.internal;
 
 import java.io.Writer;
+import java.util.Map;
 
 import org.apache.maven.doxia.module.apt.AptSink;
+import org.apache.sling.apt.parser.SlingAptParser;
 
 /** AptSink which outputs (X)HTML */
 class CustomAptSink extends AptSink {
+
+    private final boolean generateHtmlSkeleton;
+    private String title;
     
-    CustomAptSink(Writer w) {
+    CustomAptSink(Writer w, Map<String, Object> options) {
         super(w);
+        
+        if(options==null) {
+            generateHtmlSkeleton = true;
+        } else {
+            generateHtmlSkeleton = !("false".equals(options.get(SlingAptParser.OPT_HTML_SKELETON)));
+        }
     }
     
     @Override
+    public void title_()
+    {
+        if ( getBuffer().length() > 0 ) {
+            title = getBuffer().toString();
+            resetBuffer();
+        }
+    }
+    @Override
     public void sectionTitle1() {
         write("<h1>");
     }
@@ -92,10 +111,23 @@
     
     @Override
     public void head_() {
+        if(generateHtmlSkeleton) {
+            if(title!=null) {
+                write("\n<title>");
+                write(title);
+                write("</title>");
+            }
+            write("\n</head>");
+        }
+        setHeadFlag(false);
     }
 
     @Override
     public void head() {
+        if(generateHtmlSkeleton) {
+            write("<html>\n<head>");
+        }
+        setHeadFlag(true);
     }
 
     @Override
@@ -123,4 +155,17 @@
         write("<hr/>\n");
     }
 
+    @Override
+    public void body_() {
+        if(generateHtmlSkeleton) {
+            write("</body>\n</html>\n");
+        }
+    }
+
+    @Override
+    public void body() {
+        if(generateHtmlSkeleton) {
+            write("\n<body>");
+        }
+    }
 }

Modified: incubator/sling/trunk/extensions/apt/apt-parser/src/main/java/org/apache/sling/apt/parser/internal/SlingAptParserImpl.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/extensions/apt/apt-parser/src/main/java/org/apache/sling/apt/parser/internal/SlingAptParserImpl.java?rev=633528&r1=633527&r2=633528&view=diff
==============================================================================
--- incubator/sling/trunk/extensions/apt/apt-parser/src/main/java/org/apache/sling/apt/parser/internal/SlingAptParserImpl.java
(original)
+++ incubator/sling/trunk/extensions/apt/apt-parser/src/main/java/org/apache/sling/apt/parser/internal/SlingAptParserImpl.java
Tue Mar  4 08:21:39 2008
@@ -21,6 +21,7 @@
 import java.io.IOException;
 import java.io.Reader;
 import java.io.Writer;
+import java.util.Map;
 
 import org.apache.maven.doxia.module.apt.AptParseException;
 import org.apache.maven.doxia.module.apt.AptParser;
@@ -52,7 +53,11 @@
     }
     
     public void parse(Reader input, Writer output) throws IOException, SlingAptParseException
{
-        final Sink sink = new CustomAptSink(output);
+        parse(input, output, null);
+    }
+    
+    public void parse(Reader input, Writer output, Map<String, Object> options) throws
IOException, SlingAptParseException {
+        final Sink sink = new CustomAptSink(output, options);
         final AptParser parser = new CustomAptParser(macroProvider);
         try {
             parser.parse(input, sink);

Modified: incubator/sling/trunk/extensions/apt/apt-parser/src/test/java/org/apache/sling/apt/parser/internal/SlingAptParserImplTest.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/extensions/apt/apt-parser/src/test/java/org/apache/sling/apt/parser/internal/SlingAptParserImplTest.java?rev=633528&r1=633527&r2=633528&view=diff
==============================================================================
--- incubator/sling/trunk/extensions/apt/apt-parser/src/test/java/org/apache/sling/apt/parser/internal/SlingAptParserImplTest.java
(original)
+++ incubator/sling/trunk/extensions/apt/apt-parser/src/test/java/org/apache/sling/apt/parser/internal/SlingAptParserImplTest.java
Tue Mar  4 08:21:39 2008
@@ -20,6 +20,8 @@
 
 import java.io.StringReader;
 import java.io.StringWriter;
+import java.util.HashMap;
+import java.util.Map;
 
 import junit.framework.TestCase;
 
@@ -27,6 +29,7 @@
 import org.apache.maven.doxia.macro.MacroExecutionException;
 import org.apache.maven.doxia.macro.MacroRequest;
 import org.apache.maven.doxia.sink.Sink;
+import org.apache.sling.apt.parser.SlingAptParser;
 
 /** Test the SlingAptParserImpl.
  * 
@@ -41,6 +44,13 @@
     
     final SlingAptParserImpl parser = new SlingAptParserImpl(this);
     
+    private final static Map<String, Object> DEFAULT_OPTIONS;
+    
+    static {
+        DEFAULT_OPTIONS = new HashMap<String, Object> ();
+        DEFAULT_OPTIONS.put(SlingAptParser.OPT_HTML_SKELETON, "false");
+    }
+    
     private static class SlingTestMacro implements Macro {
         public void execute(Sink sink, MacroRequest request) throws MacroExecutionException
{
             sink.text("MACRO:" + getClass().getSimpleName());
@@ -55,12 +65,16 @@
         return null;
     }
 
-    protected void parse(String input, String expected) throws Exception {
+    protected void parse(String input, String expected, Map<String, Object> options)
throws Exception {
         final StringWriter out = new StringWriter();
-        parser.parse(new StringReader(input), out);
+        parser.parse(new StringReader(input), out, options);
         assertEquals(expected, out.toString().trim());
     }
     
+    protected void parse(String input, String expected) throws Exception {
+        parse(input, expected, DEFAULT_OPTIONS);
+    }
+    
     public void testNullReader() throws Exception {
         final StringWriter out = new StringWriter();
         try {
@@ -121,5 +135,14 @@
     public void testMacroNotFound() throws Exception {
         final String badName = "MacroThatDoesNotExist"; 
         parse("%{" + badName + "}", "APT macro not found: '" + badName + "'");
+    }
+    
+    public void testTitle() throws Exception {
+        final String input = " ---- \n test title \n ---- \n\nH1 title now";
+        final String expected = 
+            "<html>\n<head>\n<title>test title</title>\n</head>"
+            + "\n<body><h1>H1 title now</h1>\n</body>\n</html>"
+        ;
+        parse(input, expected, null);
     }
 }

Modified: incubator/sling/trunk/launchpad/launchpad-webapp/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/apt/SimpleAptRenderingTest.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/launchpad/launchpad-webapp/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/apt/SimpleAptRenderingTest.java?rev=633528&r1=633527&r2=633528&view=diff
==============================================================================
--- incubator/sling/trunk/launchpad/launchpad-webapp/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/apt/SimpleAptRenderingTest.java
(original)
+++ incubator/sling/trunk/launchpad/launchpad-webapp/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/apt/SimpleAptRenderingTest.java
Tue Mar  4 08:21:39 2008
@@ -29,7 +29,12 @@
             
             // .apt.aptml converts APT to html
             final String content = getContent(HTTP_BASE_URL + "/apt-test/apt-test.apt.aptml",
CONTENT_TYPE_HTML);
-            assertTrue("h1 parsed as expected (" + content + ")", content.contains("<h1>Simple
APT file test"));
+            assertTrue("HTML opening tag present (" + content + ")", content.startsWith("<html>"));
+            assertTrue("HTML closing tag present (" + content + ")", content.endsWith("</html>\n"));
+            assertTrue("title parsed as expected (" + content + ")", content.contains("<title>Simple
APT file test</title>"));
+            assertTrue("body opening tag is present (" + content + ")", content.contains("</head>\n<body>"));
+            assertTrue("body closing tag is present (" + content + ")", content.endsWith("</body>\n</html>\n"));
+            assertTrue("h1 parsed as expected (" + content + ")", content.contains("<h1>h1
heading"));
             assertTrue("h2 parsed as expected (" + content + ")", content.contains("<h2>h2
heading"));
             assertTrue("h3 parsed as expected (" + content + ")", content.contains("<h3>h3
heading"));
         } finally {

Modified: incubator/sling/trunk/launchpad/launchpad-webapp/src/test/resources/integration-test/apt/apt-test.apt
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/launchpad/launchpad-webapp/src/test/resources/integration-test/apt/apt-test.apt?rev=633528&r1=633527&r2=633528&view=diff
==============================================================================
--- incubator/sling/trunk/launchpad/launchpad-webapp/src/test/resources/integration-test/apt/apt-test.apt
(original)
+++ incubator/sling/trunk/launchpad/launchpad-webapp/src/test/resources/integration-test/apt/apt-test.apt
Tue Mar  4 08:21:39 2008
@@ -1,4 +1,8 @@
-Simple APT file test - this is an h1 heading
+   -----------------------------
+   Simple APT file test
+   -----------------------------
+
+h1 heading
 
 * h2 heading
 



Mime
View raw message