incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bdelacre...@apache.org
Subject svn commit: r591606 - in /incubator/sling/trunk/microsling/microsling-core: ./ src/main/java/org/apache/sling/microsling/scripting/ src/main/java/org/apache/sling/microsling/scripting/engines/ruby/ src/main/webapp/ src/test/java/org/apache/sling/micros...
Date Sat, 03 Nov 2007 11:59:55 GMT
Author: bdelacretaz
Date: Sat Nov  3 04:59:55 2007
New Revision: 591606

URL: http://svn.apache.org/viewvc?rev=591606&view=rev
Log:
SLING-90 - Ruby Erb templates support, contributed by Shawn Anderson, thanks!

Added:
    incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/scripting/engines/ruby/
    incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/scripting/engines/ruby/ErbScriptEngine.java
  (with props)
    incubator/sling/trunk/microsling/microsling-core/src/main/webapp/erb-scripts.html   (with
props)
    incubator/sling/trunk/microsling/microsling-core/src/test/resources/integration-test/rendering-test.erb
Modified:
    incubator/sling/trunk/microsling/microsling-core/NOTICE
    incubator/sling/trunk/microsling/microsling-core/pom.xml
    incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/scripting/MicroslingScriptResolver.java
    incubator/sling/trunk/microsling/microsling-core/src/main/webapp/index.html
    incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/NodetypeRenderingTest.java
    incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/SlingResourceTypeRenderingTest.java

Modified: incubator/sling/trunk/microsling/microsling-core/NOTICE
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/microsling/microsling-core/NOTICE?rev=591606&r1=591605&r2=591606&view=diff
==============================================================================
--- incubator/sling/trunk/microsling/microsling-core/NOTICE (original)
+++ incubator/sling/trunk/microsling/microsling-core/NOTICE Sat Nov  3 04:59:55 2007
@@ -5,10 +5,15 @@
 The Apache Software Foundation (http://www.apache.org/).
 
 FreeMarker templates support is provided by the FreeMarker
-libraries, which are open-source software licensed under
-a BSD-like license (http://freemarker.org/docs/app_license.html).
+libraries, which are licensed under a BSD-like license 
+(http://freemarker.org/docs/app_license.html).
 The original FreeMarker software can be downloaded at 
 http://freemarker.org/ .
+
+Ruby templates support is provided by the JRuby libraries,
+which are licensed under CPL 1.0 license.
+The original JRuby software can be downloaded at
+http://jruby.codehaus.org/ .  
 
 Server-side javascript support is provided by the Rhino
 libraries, which are open-source software licensed under

Modified: incubator/sling/trunk/microsling/microsling-core/pom.xml
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/microsling/microsling-core/pom.xml?rev=591606&r1=591605&r2=591606&view=diff
==============================================================================
--- incubator/sling/trunk/microsling/microsling-core/pom.xml (original)
+++ incubator/sling/trunk/microsling/microsling-core/pom.xml Sat Nov  3 04:59:55 2007
@@ -56,6 +56,11 @@
 
   <dependencies>
     <dependency>
+      <groupId>org.jruby</groupId>
+      <artifactId>jruby-complete</artifactId>
+      <version>1.0RC3</version>
+    </dependency>
+    <dependency>
       <groupId>org.apache.sling</groupId>
       <artifactId>sling-api</artifactId>
       <version>2.0.0-incubator-SNAPSHOT</version>

Modified: incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/scripting/MicroslingScriptResolver.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/scripting/MicroslingScriptResolver.java?rev=591606&r1=591605&r2=591606&view=diff
==============================================================================
--- incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/scripting/MicroslingScriptResolver.java
(original)
+++ incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/scripting/MicroslingScriptResolver.java
Sat Nov  3 04:59:55 2007
@@ -42,6 +42,7 @@
 import org.apache.sling.api.scripting.SlingScriptResolver;
 import org.apache.sling.microsling.resource.JcrNodeResource;
 import org.apache.sling.microsling.scripting.engines.freemarker.FreemarkerScriptEngine;
+import org.apache.sling.microsling.scripting.engines.ruby.ErbScriptEngine;
 import org.apache.sling.microsling.scripting.engines.rhino.RhinoJavasSriptEngine;
 import org.apache.sling.microsling.scripting.engines.velocity.VelocityTemplatesScriptEngine;
 import org.apache.sling.microsling.scripting.helpers.ScriptFilenameBuilder;
@@ -80,6 +81,7 @@
         addScriptEngine(new RhinoJavasSriptEngine());
         addScriptEngine(new VelocityTemplatesScriptEngine());
         addScriptEngine(new FreemarkerScriptEngine());
+        addScriptEngine(new ErbScriptEngine());
     }
 
     /**

Added: incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/scripting/engines/ruby/ErbScriptEngine.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/scripting/engines/ruby/ErbScriptEngine.java?rev=591606&view=auto
==============================================================================
--- incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/scripting/engines/ruby/ErbScriptEngine.java
(added)
+++ incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/scripting/engines/ruby/ErbScriptEngine.java
Sat Nov  3 04:59:55 2007
@@ -0,0 +1,123 @@
+/*
+ * 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.microsling.scripting.engines.ruby;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.PrintStream;
+import java.io.Writer;
+import java.util.Map;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.sling.api.HttpStatusCodeException;
+import org.apache.sling.api.SlingException;
+import org.apache.sling.api.scripting.SlingScript;
+import org.apache.sling.api.scripting.SlingScriptEngine;
+import org.jruby.Ruby;
+import org.jruby.RubyModule;
+import org.jruby.RubySymbol;
+import org.jruby.javasupport.JavaEmbedUtils;
+import org.jruby.runtime.builtin.IRubyObject;
+
+/**
+ * A ScriptEngine that uses ruby erb templates to render a Resource
+ */
+public class ErbScriptEngine implements SlingScriptEngine {
+
+    public static final String RUBY_SCRIPT_EXTENSION = "erb";
+    public Ruby runtime;
+    RubySymbol bindingSym;
+    RubyModule erbModule;
+
+    public ErbScriptEngine() throws SlingException {
+        runtime = Ruby.getDefaultInstance();
+
+        runtime.evalScript("require 'java';require 'erb';self.send :include, ERB::Util;class
ERB;def get_binding;binding;end;attr_reader :props;def set_props(p);@props = p;"
+            + "for name,v in @props;instance_eval \"def #{name}; @props['#{name}'];end\";end;end;end;");
+
+        erbModule = runtime.getClassFromPath("ERB");
+        bindingSym = RubySymbol.newSymbol(runtime, "get_binding");
+    }
+
+    public String[] getExtensions() {
+        return new String[]{RUBY_SCRIPT_EXTENSION};
+    }
+
+    public String getEngineName() {
+        return "Ruby Erb Script Engine";
+    }
+
+    public String getEngineVersion() {
+        return "0.9";
+    }
+
+    public void eval(SlingScript script, Map<String, Object> props)
+        throws SlingException, IOException {
+        // ensure get method
+        HttpServletRequest request = (HttpServletRequest) props.get(REQUEST);
+        if(!"GET".equals(request.getMethod())) {
+            throw new HttpStatusCodeException(
+                HttpServletResponse.SC_METHOD_NOT_ALLOWED,
+                "Ruby templates only support GET requests");
+        }
+
+        try {
+            final Writer w = ((HttpServletResponse) props.get(RESPONSE)).getWriter();
+            PrintStream stream = new PrintStream(new OutputStream() {
+                public void write(int b) {
+                    try {
+                        w.write(b);
+                    } catch(IOException ex) {
+                    }
+                }
+            });
+
+            StringBuffer scriptString = new StringBuffer();
+            BufferedReader bufferedScript = (BufferedReader) script.getScriptReader();
+            String nextLine = bufferedScript.readLine();
+            while(nextLine != null) {
+                scriptString.append(nextLine);
+                scriptString.append("\n");
+                nextLine = bufferedScript.readLine();
+            }
+
+            IRubyObject scriptRubyString = JavaEmbedUtils.javaToRuby(runtime, scriptString.toString());
+            IRubyObject erb = (IRubyObject) JavaEmbedUtils
+                .invokeMethod(runtime, erbModule, "new", new Object[]{scriptRubyString},
IRubyObject.class);
+
+            JavaEmbedUtils.invokeMethod(runtime, erb, "set_props",
+                new Object[]{JavaEmbedUtils.javaToRuby(runtime, props)}, IRubyObject.class);
+
+            IRubyObject binding = (IRubyObject) JavaEmbedUtils
+                .invokeMethod(runtime, erb, "send", new Object[]{bindingSym}, IRubyObject.class);
+
+            String out = (String) JavaEmbedUtils.invokeMethod(runtime, erb, "result",
+                new Object[]{(Object) binding}, String.class);
+
+            stream.println(out);
+            stream.flush();
+
+        } catch(IOException ioe) {
+            throw ioe;
+        } catch(Throwable t) {
+            throw new SlingException("Failure running Ruby script ", t);
+        }
+    }
+}

Propchange: incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/scripting/engines/ruby/ErbScriptEngine.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/scripting/engines/ruby/ErbScriptEngine.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL

Added: incubator/sling/trunk/microsling/microsling-core/src/main/webapp/erb-scripts.html
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/microsling/microsling-core/src/main/webapp/erb-scripts.html?rev=591606&view=auto
==============================================================================
--- incubator/sling/trunk/microsling/microsling-core/src/main/webapp/erb-scripts.html (added)
+++ incubator/sling/trunk/microsling/microsling-core/src/main/webapp/erb-scripts.html Sat
Nov  3 04:59:55 2007
@@ -0,0 +1,39 @@
+<html>
+<head>
+  <title>microsling Ruby Erb templates</title>
+  <link rel="stylesheet" href="microsling.css"/>
+</head>
+<body>
+  <h1>microsling Ruby Erb templates</h1>
+  
+  <div class="note">
+    <a href="../">back to homepage</a>
+  </div>
+  <p> 
+  	Scripts found by the <code>SlingScriptResolver</code> using the <code>.erb</code>
extension are executed by
+    the <code>ErbScriptEngine</code>.
+  </p>
+  <p>
+    This works exactly like the <a href="velocity-scripts.html">Velocity templates</a>,
except that
+    templates use the Erb syntax.
+  </p>
+  <p>
+    Here's an example HTML template. See the <a href="http://www.ruby-doc.org/stdlib/libdoc/erb/rdoc/">Erb
docs</a> website 
+    more info about the syntax.
+    <pre>
+      &lt;html>
+        &lt;body>
+          &lt;p>&lt;span>This is an example Erb template &lt;%= Time.now
%>&lt;/span>&lt;/p>
+          &lt;p>&lt;span>&lt;%= resource.getItem().getProperty("text").getString()
%>&lt;/span>&lt;/p>
+          &lt;% unless props.nil? or props.empty? %>
+            &lt;table>
+              &lt;% for prop, val in props %>
+                &lt;tr>&lt;td>&lt;b>&lt;%= prop %>&lt;/b>:&lt;/td>&lt;td>&lt;%=
val %>&lt;/td>&lt;/tr>
+              &lt;% end %>
+            &lt;/table>
+          &lt;% end %>
+        &lt;/body>
+      &lt;/html></pre>  
+  </p>
+</body>
+</html>

Propchange: incubator/sling/trunk/microsling/microsling-core/src/main/webapp/erb-scripts.html
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: incubator/sling/trunk/microsling/microsling-core/src/main/webapp/index.html
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/microsling/microsling-core/src/main/webapp/index.html?rev=591606&r1=591605&r2=591606&view=diff
==============================================================================
--- incubator/sling/trunk/microsling/microsling-core/src/main/webapp/index.html (original)
+++ incubator/sling/trunk/microsling/microsling-core/src/main/webapp/index.html Sat Nov  3
04:59:55 2007
@@ -40,6 +40,11 @@
         FreeMarker templates
       </a>: explains how to use FreeMarker templates to render content.
     </li>
+    <li>
+      <a href="erb-scripts.html">
+        Ruby Erb templates
+      </a>: explains how to use Ruby Erb templates to render content.
+    </li>
   </ul>
   
   <h2>Show me the code</h2>
@@ -144,4 +149,4 @@
     </ul>
   </p>
 </body>
-</html>
\ No newline at end of file
+</html>

Modified: incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/NodetypeRenderingTest.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/NodetypeRenderingTest.java?rev=591606&r1=591605&r2=591606&view=diff
==============================================================================
--- incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/NodetypeRenderingTest.java
(original)
+++ incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/NodetypeRenderingTest.java
Sat Nov  3 04:59:55 2007
@@ -119,4 +119,15 @@
             testClient.delete(toDelete);
         }
     }
-}
\ No newline at end of file
+
+    public void testErbHtml() throws IOException {
+        final String toDelete = uploadTestScript("rendering-test.erb","html.erb");
+        try {
+            final String content = getContent(displayUrl + ".html", CONTENT_TYPE_HTML);
+            assertTrue("Content includes Ruby marker",content.contains("Ruby template"));
+            assertTrue("Content contains formatted test text",content.contains("<p><span>"
+ testText + "</span></p>"));
+        } finally {
+            testClient.delete(toDelete);
+        }
+    }
+}

Modified: incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/SlingResourceTypeRenderingTest.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/SlingResourceTypeRenderingTest.java?rev=591606&r1=591605&r2=591606&view=diff
==============================================================================
--- incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/SlingResourceTypeRenderingTest.java
(original)
+++ incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/SlingResourceTypeRenderingTest.java
Sat Nov  3 04:59:55 2007
@@ -123,4 +123,15 @@
             testClient.delete(toDelete);
         }
     }
+    
+    public void testErbHtml() throws IOException {
+        final String toDelete = uploadTestScript("rendering-test.erb","html.erb");
+        try {
+            final String content = getContent(displayUrl + ".html", CONTENT_TYPE_HTML);
+            assertTrue("Content includes Ruby marker",content.contains("Ruby template"));
+            assertTrue("Content contains formatted test text",content.contains("<p><span>"
+ testText + "</span></p>"));
+        } finally {
+            testClient.delete(toDelete);
+        }
+    }
 }

Added: incubator/sling/trunk/microsling/microsling-core/src/test/resources/integration-test/rendering-test.erb
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/microsling/microsling-core/src/test/resources/integration-test/rendering-test.erb?rev=591606&view=auto
==============================================================================
--- incubator/sling/trunk/microsling/microsling-core/src/test/resources/integration-test/rendering-test.erb
(added)
+++ incubator/sling/trunk/microsling/microsling-core/src/test/resources/integration-test/rendering-test.erb
Sat Nov  3 04:59:55 2007
@@ -0,0 +1,15 @@
+!-- used by ScriptedRenderingTest -->
+<html>
+  <body>
+    <p><span>Ruby template <%= Time.now %></span></p>
+		<p><span><%= resource.getRawData().getProperty("text").getString() %></span></p>
+    <% unless props.nil? or props.empty? %>
+      <table>
+        <% for prop, val in props %>
+          <tr><td><b><%= prop %></b>:</td><td><%=
val %></td></tr>
+        <% end %>
+      </table>
+    <% end %>
+  </body>
+</html>
+



Mime
View raw message