Skip to content

Commit

Permalink
Added DOCTYPE support which closes #13. Sorry for the delay to get th…
Browse files Browse the repository at this point in the history
…is done.
  • Loading branch information
realistschuckle committed Dec 8, 2014
1 parent 5cf1e7f commit f11c3cb
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 51 deletions.
92 changes: 46 additions & 46 deletions haml_interpret_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,55 +55,55 @@ func TestForArrayRangeConstruct(t *testing.T) {
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)
}
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)
}
}

func TestForSliceObjectRangeConstruct(t *testing.T) {
scope := make(map[string]interface{})
scope["looper"] = []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)
}
scope := make(map[string]interface{})
scope["looper"] = []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)
}
}

func TestForMapRangeConstruct(t *testing.T) {
Expand Down
8 changes: 8 additions & 0 deletions haml_io_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ var autoCloseTests = []testcase{
testcase{"%input{:type => \"checkbox\", cd => outputTrue}", "<input type=\"checkbox\" checked=\"checked\" />"},
testcase{"%one\n %two\n %three\n", "<one>\n\t<two>\n\t\t<three />\n\t</two>\n</one>"},
testcase{"%one\n %two\n %three\n ", "<one>\n\t<two>\n\t\t<three />\n\t</two>\n</one>"},
testcase{"!!!", "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">"},
testcase{"!!! Strict", "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">"},
testcase{"!!! Frameset", "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Frameset//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd\">"},
testcase{"!!! 5", "<!DOCTYPE html>"},
testcase{"!!! 1.1", "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">"},
testcase{"!!! Basic", "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML Basic 1.1//EN\" \"http://www.w3.org/TR/xhtml-basic/xhtml-basic11.dtd\">"},
testcase{"!!! Mobile", "<!DOCTYPE html PUBLIC \"-//WAPFORUM//DTD XHTML Mobile 1.2//EN\" \"http://www.openmobilealliance.org/tech/DTD/xhtml-mobile12.dtd\">"},
testcase{"!!! RDFa", "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML+RDFa 1.0//EN\" \"http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd\">"},
}

func TestAutoCloseIO(t *testing.T) {
Expand Down
17 changes: 15 additions & 2 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ func parseLeadingSpace(input string, lastSpaceChar rune, line int) (output inode
node := new(node)
for i, r := range input {
switch {
case r == '!' && len(input) >= i+2 && input[i+1] == '!' && input[i+2] == '!':
if len(input) > i+2 {
output = parseDoctype(input[i+3:], node, line)
} else {
output = parseDoctype("", node, line)
}
case r == '-':
output = parseCode(input[i+1:], node, line)
case r == '%':
Expand All @@ -93,7 +99,6 @@ func parseLeadingSpace(input string, lastSpaceChar rune, line int) (output inode
to = "space"
}
msg := fmt.Sprintf("Syntax error on line %d: Inconsistent spacing in document changed from %s to %s characters.\n", line, from, to)
//err = os.NewError(msg)
err = errors.New(msg)
} else {
lastSpaceChar = r
Expand All @@ -111,6 +116,15 @@ func parseLeadingSpace(input string, lastSpaceChar rune, line int) (output inode
return
}

func parseDoctype(input string, n *node, line int) (output inode) {
output = n
n._name = "doctype"
if len(input) > 0 {
output = parseRemainder(input, n, line)
}
return
}

func parseKey(input string, n *node, line int) (output inode) {
if input[len(input)-1] == '<' {
n = parseNoNewline("", n, line)
Expand All @@ -125,7 +139,6 @@ func parseKey(input string, n *node, line int) (output inode) {

func parseTag(input string, node *node, newTag bool, line int) (output inode, err error) {
if 0 == len(input) && newTag {
//err = os.NewError(fmt.Sprintf("Syntax error on line %d: Invalid tag: %s.\n", line, input))
err = errors.New(fmt.Sprintf("Syntax error on line %d: Invalid tag: %s.\n", line, input))
return
}
Expand Down
23 changes: 20 additions & 3 deletions tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package gohaml

import (
"bytes"
//"container/vector"
"fmt"
"reflect"
"strings"
Expand Down Expand Up @@ -126,8 +125,26 @@ func (self tree) resolve(scope map[string]interface{}, indent string, autoclose

func (self node) resolve(scope map[string]interface{}, buf *bytes.Buffer, curIndent string, indent string, autoclose bool) {
remainder := self._remainder.resolve(scope)
//if self._attrs.Len() > 0 && len(remainder) > 0 {
if len(self._attrs) > 0 && len(remainder) > 0 {
if self._name == "doctype" {
buf.WriteString("<!DOCTYPE html")
switch strings.TrimSpace(self._remainder.value) {
case "":
buf.WriteString(" PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"")
case "Strict":
buf.WriteString(" PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\"")
case "Frameset":
buf.WriteString(" PUBLIC \"-//W3C//DTD XHTML 1.0 Frameset//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd\"")
case "1.1":
buf.WriteString(" PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\"")
case "Basic":
buf.WriteString(" PUBLIC \"-//W3C//DTD XHTML Basic 1.1//EN\" \"http://www.w3.org/TR/xhtml-basic/xhtml-basic11.dtd\"")
case "Mobile":
buf.WriteString(" PUBLIC \"-//WAPFORUM//DTD XHTML Mobile 1.2//EN\" \"http://www.openmobilealliance.org/tech/DTD/xhtml-mobile12.dtd\"")
case "RDFa":
buf.WriteString(" PUBLIC \"-//W3C//DTD XHTML+RDFa 1.0//EN\" \"http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd\"")
}
buf.WriteString(">")
} else if len(self._attrs) > 0 && len(remainder) > 0 {
if len(self._name) == 0 {
self._name = "div"
}
Expand Down

0 comments on commit f11c3cb

Please sign in to comment.