Permalink
Browse files

initial version

  • Loading branch information...
splix committed Sep 24, 2012
0 parents commit e880bc2a17a616654acccafec42132ce18efb374
@@ -0,0 +1,10 @@
+*.ipr
+*.ipl
+*.iml
+*.iws
+.classpath
+.project
+.idea
+.settings
+target
+*.swp
@@ -0,0 +1,13 @@
+Copyright 2012 Igor Artamonov
+
+Licensed 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.
@@ -0,0 +1,57 @@
+HAML Closures
+=============
+
+Version of Google Closure Templates with HAML syntax.
+
+How it works
+------------
+
+Converts from
+
+```haml
+!!! namespace tests.test2
+
+/
+ Greets a person using "Hello" by default.
+ @param name The name of the person.
+ @param? greetingWord Optional greeting word to use instead of "Hello".
+#x1
+ %h1
+ A Greeting
+ {if not $greetingWord}
+ .default
+ Hello {$name}!
+ {else}
+ %h2 {$greetingWord} {$name}!
+ {/if}
+```
+
+to
+
+```
+{namespace tests.test2}
+
+/**
+* Greets a person using "Hello" by default.
+* @param name The name of the person.
+* @param? greetingWord Optional greeting word to use instead of "Hello".
+*/
+{template .x1}
+<h1>
+ A Greeting
+</h1>
+{if not $greetingWord}
+ <div class='default'>
+ Hello {$name}!
+ </div>
+{else}
+ <h2>{$greetingWord} {$name}!</h2>
+{/if}
+{/template}
+```
+
+
+License
+-------
+
+Licensed under the Apache License, Version 2.0
105 pom.xml
@@ -0,0 +1,105 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>com.the6hours</groupId>
+ <artifactId>haml-closures</artifactId>
+ <packaging>jar</packaging>
+ <version>1.0-SNAPSHOT</version>
+ <name>Google Closures with HAML syntax</name>
+
+ <properties>
+ <gmaven.version>1.3</gmaven.version>
+ <gmaven.runtime>1.7</gmaven.runtime>
+ <groovy.version>1.8.6</groovy.version>
+ <spock.version>0.5-groovy-1.8</spock.version>
+ </properties>
+
+ <dependencies>
+
+ <dependency>
+ <groupId>com.cadrlife.jhaml</groupId>
+ <artifactId>jhaml</artifactId>
+ <version>0.1.4</version>
+ </dependency>
+ <!--dependency>
+ <groupId>com.google.template</groupId>
+ <artifactId>soy</artifactId>
+ <version>2011-14-10</version>
+ </dependency-->
+
+ <dependency>
+ <groupId>org.spockframework</groupId>
+ <artifactId>spock-core</artifactId>
+ <version>${spock.version}</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.codehaus.groovy</groupId>
+ <artifactId>groovy-all</artifactId>
+ <version>${groovy.version}</version>
+ <scope>test</scope>
+ </dependency>
+
+ </dependencies>
+
+ <build>
+ <plugins>
+
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <version>2.0.2</version>
+ <configuration>
+ <source>1.6</source>
+ <target>1.6</target>
+ <meminitial>512m</meminitial>
+ <maxmem>1024m</maxmem>
+ <encoding>UTF-8</encoding>
+ </configuration>
+ </plugin>
+
+ <plugin>
+ <groupId>org.codehaus.gmaven</groupId>
+ <artifactId>gmaven-plugin</artifactId>
+ <version>${gmaven.version}</version>
+ <dependencies>
+ <dependency>
+ <groupId>org.codehaus.gmaven.runtime</groupId>
+ <artifactId>gmaven-runtime-${gmaven.runtime}</artifactId>
+ <version>${gmaven.version}</version>
+ <exclusions>
+ <exclusion>
+ <groupId>org.codehaus.groovy</groupId>
+ <artifactId>groovy-all</artifactId>
+ </exclusion>
+ </exclusions>
+ </dependency>
+ <dependency>
+ <groupId>org.codehaus.groovy</groupId>
+ <artifactId>groovy-all</artifactId>
+ <version>${groovy.version}</version>
+ <scope>compile</scope>
+ </dependency>
+ </dependencies>
+ <executions>
+ <execution>
+ <goals>
+ <goal>testCompile</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
+ </plugins>
+ </build>
+
+ <repositories>
+ <repository>
+ <id>github-raymyers</id>
+ <url>https://raw.github.com/raymyers/raymyers-mvn-repo/master/releases</url>
+ <releases><enabled>true</enabled></releases>
+ <snapshots><enabled>false</enabled></snapshots>
+ </repository>
+ </repositories>
+
+</project>
@@ -0,0 +1,46 @@
+package com.the6hours.hamlclosures;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ *
+ * @author Igor Artamonov (http://igorartamonov.com)
+ * @since 15.07.12
+ */
+class HsoyDocument {
+
+ private String namespace;
+ private List<HsoyTemplate> templates = new ArrayList<HsoyTemplate>();
+
+ public String getNamespace() {
+ return namespace;
+ }
+
+ public void setNamespace(String namespace) {
+ this.namespace = namespace;
+ }
+
+ public List<HsoyTemplate> getTemplates() {
+ return templates;
+ }
+
+ public void setTemplates(List<HsoyTemplate> templates) {
+ this.templates = templates;
+ }
+
+ public String getSoy() {
+ StringBuilder buf = new StringBuilder();
+ buf.append("{namespace ").append(namespace).append("}\n\n");
+ if (!templates.isEmpty()) {
+ for (HsoyTemplate tpl: templates) {
+ buf.append(tpl.getSoyDocs()).append('\n');
+ buf.append("{template .").append(tpl.getName()).append("}\n");
+ buf.append(tpl.getSoyBody()).append('\n');
+ buf.append("{/template}\n\n");
+ }
+ buf.delete(buf.length()-2, buf.length());
+ }
+ return buf.toString();
+ }
+}
@@ -0,0 +1,17 @@
+package com.the6hours.hamlclosures;
+
+/**
+ *
+ * @author Igor Artamonov (http://igorartamonov.com)
+ * @since 15.07.12
+ */
+class HsoyFormatException extends Exception {
+
+ public HsoyFormatException(String s) {
+ super(s);
+ }
+
+ public HsoyFormatException(String s, Throwable throwable) {
+ super(s, throwable);
+ }
+}
@@ -0,0 +1,79 @@
+package com.the6hours.hamlclosures;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ * @author Igor Artamonov (http://igorartamonov.com)
+ * @since 15.07.12
+ */
+public class HsoyParser {
+
+ private static final int MODE_UNKNOWN = 0;
+ private static final int MODE_DOC = 1;
+ private static final int MODE_BODY = 2;
+
+
+ private static final Pattern namespace = Pattern.compile("^!!! namespace\\s+(.+?)\\s*$");
+
+ public HsoyDocument parse(String soy) throws HsoyFormatException {
+ HsoyDocument doc = new HsoyDocument();
+ String[] lines = soy.trim().split("\n");
+ Matcher m = namespace.matcher(lines[0]);
+ if (!m.matches()) {
+ throw new HsoyFormatException("Can't find templates namespace");
+ }
+ doc.setNamespace(m.group(1));
+
+ List<String> templateBody = new ArrayList<String>();
+ List<String> templateDoc = new ArrayList<String>();
+ HsoyTemplate currentTemplate = null;
+
+ int mode = MODE_UNKNOWN;
+
+ for (int i = 1; i < lines.length; i++) {
+ String line = lines[i];
+ if (line.startsWith("#")) {
+ //start template body
+ String name = line.substring(1).trim();
+ if (currentTemplate == null) {
+ throw new HsoyFormatException(
+ String.format("Found start of template %1s (line %2s) w/o doc",
+ name, i));
+ }
+ if (name.length() == 0) {
+ throw new HsoyFormatException(
+ String.format("Empty template name at line %1s",
+ i));
+ }
+ currentTemplate.setName(name);
+ mode = MODE_BODY;
+ } else if (line.startsWith("/")) {
+ //start new template
+ if (currentTemplate != null) {
+ doc.getTemplates().add(currentTemplate);
+ }
+ currentTemplate = new HsoyTemplate();
+ line = line.trim();
+ if (line.length() > 1) {
+ line = line.substring(1).trim();
+ currentTemplate.addDoc(line);
+ }
+ mode = MODE_DOC;
+ } else if (line.startsWith(" ")) {
+ //content of doc or body
+ if (mode == MODE_DOC) {
+ currentTemplate.addDoc(line);
+ } else if (mode == MODE_BODY) {
+ currentTemplate.addBody(line);
+ }
+ }
+ }
+ if (currentTemplate != null) {
+ doc.getTemplates().add(currentTemplate);
+ }
+ return doc;
+ }
+}
Oops, something went wrong.

0 comments on commit e880bc2

Please sign in to comment.