Giter Site home page Giter Site logo

gohaml's Introduction

What is it?

You can find out more about HAML at haml.info
You can find out more about GO at golang.org

I’ve tried to remain diligent about reproducing the output from the canonical Ruby-based parser, so if you feel familiar with the one, then you should feel familiar with the other.

This branch compiles with 6g/8g “go version go1.0.3.”

Is it done?

I think so. It has…

  • Tags with
    • empty content;
    • attributes of the form {:attr => "value"};
    • id moniker using “#” (#divId); and,
    • class moniker using “.” (.divClass).
  • Tag nesting
  • Scope lookup
    • Arbitrary number of keys as specified by struct (someKeyInScope.Subkey1.Subkey2)
    • Valid as tag content (%p= someKeyInScope)
    • Valid as tag attribute value (%p{:attr => someKeyInScope})
    • Valid as tag attribute name (%p{someKeyInScope => "value"})
  • Engine-level autoclose option (<br /> vs. <br>)
  • Tag-specific close option (%br/ becomes <br /> regardless of autoclose setting)
  • Whitespace removal with the < operator
  • Simple scripting
    • Declaration and assignment of strings, floats, and ints (- varname := “value”)
    • Range looping construct (- for i, v := range scopeVar)
  • Error messages for badly-formed templates

If you would like another feature added, just log an issue and I’ll review it forthright.

How can I install this?

To install the library for use in your project, you can use goinstall.

go get "github.com/realistschuckle/gohaml"

How can I install this from source?

In a Go workspace, create a directory for gohaml.

mkdir -p src/github.com/realistschuckle/gohaml

Clone the gohaml repository into that newly created directory.
git clone git://github.com/realistschuckle/gohaml.git src/github.com/realistschuckle/gohaml

Now build and install it.
go install github.com/realistschuckle/gohaml

How can I use it?

How about something like this? Save it, compile it, link it, and run it!

package main

import (
	"github.com/realistschuckle/gohaml"
	"fmt"
)

func main() {
	var scope = make(map[string]interface{})
	scope["lang"] = "HAML"
	content := "I love <\n=lang<\n!"
	engine, _ := gohaml.NewEngine(content)
	output := engine.Render(scope)
	fmt.Println(output) // Prints "I love HAML!"
}

gohaml's People

Contributors

a2800276 avatar denheck avatar realistschuckle avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

gohaml's Issues

Should support string keys on properties

Lets say we have a property called http-equiv

%meta{ 'http-equiv' => "content-type", :content => "text/html;charset=utf-8" }

Currently it doesn't liked string based property keys, but haml support this.

Should support json-like syntax for properties

I'm used to haml supporting the json-like syntax over hash rockets.

.projects{ng:{repeat:"project in projects"}}

If you are using angularjs it really condenses the code to have json-like
syntax

Needs CDATA blocks (perhaps haml filters)

Haml has special blocks for outputting javascript/css (and generic CDATA) blocks.

Bundled with these features is the ability to embed other markup styles.

Embedding other markup isn't very important, but CDATA blocks are nice in some circumstances where page-specific javascript is used. So, filter blocks might as well be implemented anyway.

I'd be willing to help implement this. I imagine an interface

type Filter interface{
    // content is indented and ends with "\n".
    // output should be indented and end with "\n".
    Filter(content, indent string) string
}

And so CDATA, css, and javascript blocks can all be implemented is simple functions

func cdataHelper(pre, post, content, indent string) string {
    return fmt.Sprintf("%s%s<![CDATA[%s\n%s\n%s%s]]>%s\n",indent, pre, post, content, indent, pre, post)
}

func cdata(content, indent string) string { return cdataHelper("", "", content, indent) }
func css(content, indent string) string {
    return fmt.Sprintf(`%s<style type="text/css">\n%s\n%s</style>\n`, indent, cdataHelper("", "", content, indent), indent)
}
func javascript(content, indent string) string {
    return fmt.Sprintf(`%s<script type="text/javascript">\n%s\n%s</script>\n`, indent, cdataHelper("", "", content, indent), indent)
}

The parsers could be usable in the parser via a map.

type FilterMap map[string]func(input, indent string) string

Note: I haven't actually compiled any of this, so there could be some small errors.

Support for Method Calls

First of all,
your gohaml project has made writing my go web app much easier(I hate HTML). Currently i've been using gohaml "statically"-- that is, only to replace html, and then html/template to plug in data/ iterate over ranges. My app has some pages with a lot of data from a DB, and i access this data on a per-page basis with a method call using html/template. For sanity/clarity's sake, i'm currently trying migrate all of my html/template code over to gohaml, but as far as i can tell, there's no support for method/function calls. Is this a feature we can expect in the future?

LICENSE

Hi Curtis, have you put any thought into how you'd like to license this? This would be interesting to know before working more on the lib...

Struggling to compile

Hi,

I'm having trouble trying to install this - if I run go install, I get a lot of undefineds.

Any ideas what's going wrong? Looking at the code it doesn't even look like it should work. But that might just be my misunderstanding!

$ go install "github.com/realistschuckle/gohaml/lib"

github.com/realistschuckle/gohaml/lib

C:\Go\src\pkg\github.com\realistschuckle\gohaml\lib\parser.go:319: undefined: yyParse
C:\Go\src\pkg\github.com\realistschuckle\gohaml\lib\parser.go:323: undefined: Output
C:\Go\src\pkg\github.com\realistschuckle\gohaml\lib\parser.go:339: undefined: yySymType
C:\Go\src\pkg\github.com\realistschuckle\gohaml\lib\parser.go:345: undefined: FOR
C:\Go\src\pkg\github.com\realistschuckle\gohaml\lib\parser.go:347: undefined: RANGE
C:\Go\src\pkg\github.com\realistschuckle\gohaml\lib\parser.go:349: undefined: IDENT
C:\Go\src\pkg\github.com\realistschuckle\gohaml\lib\parser.go:353: undefined: ATOM
C:\Go\src\pkg\github.com\realistschuckle\gohaml\lib\parser.go:357: undefined: ATOM
C:\Go\src\pkg\github.com\realistschuckle\gohaml\lib\parser.go:360: undefined: ATOM

make compile with current go releases

This patch makes gohaml compile on a current go release:

diff --git a/lib/parser.go b/lib/parser.go
index dd59dd7..af6f7eb 100644
--- a/lib/parser.go
+++ b/lib/parser.go
@@ -336,7 +336,7 @@ func scan(v *yystype) (output int) {
                v.i, _ = strconv.Atoi(s.TokenText())
        case scanner.Float:
                output = atom
-               v.i, _ = strconv.Atof(s.TokenText())
+               v.i, _ = strconv.Atof64(s.TokenText())
        case scanner.EOF:
                output = eof
        default:

Can't seem to be able to output struct field values

The following test fails when trying to evaluate %span= v.Val.

type testObject struct{ Val int }

func TestForArrayObjectRangeConstruct(t *testing.T) {
    scope := make(map[string]interface{})
    scope["looper"] = [5]testObject{
        testObject{4},
        testObject{-128},
        testObject{38},
        testObject{99},
        testObject{1},
    }

    expected := "<p>\n" +
        "   <span>0</span><span>4</span>\n" +
        "   <span>1</span><span>-128</span>\n" +
        "   <span>2</span><span>38</span>\n" +
        "   <span>3</span><span>99</span>\n" +
        "   <span>4</span><span>1</span>\n" +
        "</p>"
    input := "%p\n  - for i, v := range looper\n    %span= i<\n    %span= v.Val"
    engine, _ := NewEngine(input)
    output := engine.Render(scope)

    if output != expected {
        t.Errorf("Expected\n%s\nbut got\n%s\n", expected, output)
    }
}
--- FAIL: TestForArrayObjectRangeConstruct (0.00 seconds)
    lang.go:1507: Expected
        <p>
            <span>0</span><span>4</span>
            <span>1</span><span>-128</span>
            <span>2</span><span>38</span>
            <span>3</span><span>99</span>
            <span>4</span><span>1</span>
        </p>
        but got
        <p>
            <span>0</span><span><invalid Value></span>
            <span>1</span><span><invalid Value></span>
            <span>2</span><span><invalid Value></span>
            <span>3</span><span><invalid Value></span>
            <span>4</span><span><invalid Value></span>
        </p>

Error Installing w/Go "6g version weekly.2011-04-13 8085"

Looks like the worse problem is in the reflection used in tree.go, but I'm too new to Go to tell you why.

parser.go:339: undefined: strconv.Atof
tree.go:71: undefined: reflect.StringValue
tree.go:72: t.Get undefined (type interface { } has no field or method Get)
tree.go:73: undefined: reflect.IntValue
tree.go:74: t.Get undefined (type interface { } has no field or method Get)
tree.go:75: undefined: reflect.FloatValue
tree.go:76: t.Get undefined (type interface { } has no field or method Get)
tree.go:77: undefined: reflect.PtrValue
tree.go:78: t.IsNil undefined (type interface { } has no field or method IsNil)
tree.go:79: t.Elem undefined (type interface { } has no field or method Elem)
tree.go:79: too many errors

Issue with the <script> tag

Hi,

I'm trying to use your awesome work in combination with Revel, and I face the following issue:
when I try to include a script tag, the generated HTML use the autoclose syntax <script src="foo" />, which is invalid. The only workaround I found so far is to put some dummy content in the tag, to enforce the closing tag. Is there any way I can tell the parser to use the "2 tags" syntax ?

Support for if / else construct

Hello,

Playing with your nice library to port some code from sinatra to go. I would like to know if implementing the support for if / else would be too much asking :)

Thank you for this library !
Pierre.

Should support commas in attrs/properties

I'll give you an example.

%link{ :href => "https://fonts.googleapis.com/css?family=Open+Sans:300italic,300,400,600,700,800", :rel =>"stylesheet", :type => "text/css" }

This will error out as "runtime error: index out of range"

Features not up to par with "templates"

I like this project. The haml-to-html translation seems pretty solid. And, I really want to use haml to write web-apps with Go and Google App Engine.

But, the template capabilities of gohaml are not good enough, imho. If you look at the "template" godoc page, there are obviously a number of features provided by it which are not by gohaml.

Bringing gohaml up to par with "template" is going to take a lot of work. And I don't want to just dump it all on you, demanding an amazing haml template library.

However, it sounds like more work than I really want to do. So, I forked this project because I have a plan and I want to hear your thoughts.

The Go authors have done a lot of good work with their template package. And you've done a lot of good work here with you're haml parser. I'm going to chain them together.

I'm making the haml parser output a template which i'll feed into a template parser. I'm sure it will work. And, I think the output as well as the new code will be pretty clean. I'm concerned about the time it would take to parse twice, because they can't be done concurrently.

If you don't want any part of this, I might break my fork off into a new repository, and I'd encourage you to keep working on adding things like embedding other templates using a Set type structure. Things like function/method calls would be really nice to.

Thanks for all the hard work you put in writing this great parser. =)

Cheers,

Bryan

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.