Permalink
Browse files

more Haml doodle. This implementation is not ready for production

  • Loading branch information...
florin.patrascu@gmail.com
florin.patrascu@gmail.com committed Sep 27, 2010
1 parent 9267b04 commit bd9246c421ccf2d8105c0d1ccc6074c75faaa51c
@@ -29,6 +29,7 @@
import org.jruby.embed.EvalFailedException;
import org.jruby.embed.LocalContextScope;
import org.jruby.embed.ScriptingContainer;
+import org.jruby.javasupport.JavaEmbedUtils;
import java.io.*;
import java.util.ArrayList;
@@ -47,16 +48,23 @@
protected static final Log log = LogFactory.getLog(HamlViewRenderer.class);
private ScriptingContainer container;
+ private Configuration scriptingContainerConfiguration;
+
private String jrubyhome = ""; // will be addressed before the final version
private String haml_rb = "require 'java'\n" +
"require 'rubygems'\n" +
"require 'date'\n" +
"require 'haml'\n" +
"Haml::Engine.new( haml_template).render.to_s";
+ private JavaEmbedUtils.EvalUnit haml_rb_unit;
+
private SiteContext siteContext;
public static final String UTF_8 = "utf-8";
- public static final String HAML_TEMPLATE_KEY = "haml_template";
+ public static final String $HAML_TEMPLATE_KEY = "$haml_template";
+ public static final String $ = "$";
+ public static final String $CONTEXT = $ + "context";
+ private static final String JRUBY_CONTEXT_INSTANCE = "$jruby_context_instance";
/**
* Set the SiteContext.
@@ -92,22 +100,22 @@ public void render(JPublishContext context, String path, Reader in,
Writer out) throws IOException, ViewRenderException {
String template = FileCopyUtils.copyToString(in); //faster than processing the Reader inside the script
-
// thread safe, right?? Promise??
Object keys[] = context.getKeys(); // the keys must be Strings only
// transfer the JPublish context to JRuby
for (Object key : keys) {
- container.put((String) key, context.get((String) key));
+ container.put($ + key, context.get((String) key));
}
- container.put("context", context); // pass our jpublish context too
- container.put(HAML_TEMPLATE_KEY, template); // put our template too
+ container.put($CONTEXT, context); // pass our jpublish context too
+ container.put($HAML_TEMPLATE_KEY, template); // put our template too
try {
- FileCopyUtils.copy(container.runScriptlet(haml_rb).toString(), out); //send it
+ FileCopyUtils.copy(haml_rb_unit.run().asJavaString(), out); //send it
+ container.clear();
} catch (EvalFailedException e) {
- FileCopyUtils.copy(String.format("[Haml::SyntaxError?] %s", e.getMessage()), out);
+ FileCopyUtils.copy(String.format("[EvalFailedException] %s", e.getMessage()), out);
e.printStackTrace(); //will be disabled in the final version
}
}
@@ -138,24 +146,29 @@ public void loadConfiguration(Configuration configuration)
throws ConfigurationException {
List<String> loadPaths = new ArrayList<String>();
+ // SINGLETHREAD is not threadsafe: http://www.ruby-forum.com/topic/206056#new
container = new ScriptingContainer(LocalContextScope.THREADSAFE);
+ scriptingContainerConfiguration = configuration;
+
RubyInstanceConfig config = container.getProvider().getRubyInstanceConfig();
- if (configuration != null) {
+ if (scriptingContainerConfiguration != null) {
- jrubyhome = configuration.getChildValue("jruby_home");
+ jrubyhome = scriptingContainerConfiguration.getChildValue("jruby_home");
if (jrubyhome != null) {
config.setJRubyHome(jrubyhome);
loadPaths.add(jrubyhome);
}
- String userHamlScript = configuration.getChildValue("haml");
+ String userHamlScript = scriptingContainerConfiguration.getChildValue("haml");
if (userHamlScript != null) {
haml_rb = userHamlScript; //just a bit of inversion of control
}
}
+ haml_rb_unit = container.parse(haml_rb);
+
log.info("haml enabled ... have fun!");
}
@@ -0,0 +1,10 @@
+%p
+ Date/Time:
+ - now = DateTime.now
+ %strong= now
+ - if now > DateTime.parse("December 31, 2006")
+ %p
+ = "Happy new " + "year!"
+ %p
+ :textile
+ And I like *cake*!
@@ -20,7 +20,7 @@
require 'date'
require 'haml'
- Haml::Engine.new( haml_template).render.to_s
+ Haml::Engine.new( $haml_template).render
]]></haml>
</view-renderer>
@@ -45,7 +45,7 @@
</cache-manager>
<mime-mapping ext="txt" mimetype="text/html"/>
-
+
<character-encoding-map path="*">
<page-encoding>UTF-8</page-encoding>
<template-encoding>UTF-8</template-encoding>
@@ -27,10 +27,10 @@
:font-size 13px
:font-family Monaco, "DejaVu Sans Mono", "Bitstream Vera Mono", "Courier New", monospace
%body
+ #header
#container
+ = $fs_repository.get( $page.path)
+ #footer
%p
- Date/Time:
- - now = DateTime.now
- %strong= now
- - if now > DateTime.parse("December 31, 2006")
- = "Happy new " + "year!"
+ Footer
+

0 comments on commit bd9246c

Please sign in to comment.