huml 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c226fb793dde84c7b77e8471bf67da803940ed78
4
- data.tar.gz: a4c0c772b4f03f9f6a34d35cf28c037553314c82
3
+ metadata.gz: 60f2da52abfd05442289f852c80db11b7153e8df
4
+ data.tar.gz: 1a2ab5f644d1da8f20f48e4713c8a5ff1e1c6b15
5
5
  SHA512:
6
- metadata.gz: d50a2ecc4b9cd32dd2d0f11fa4e38fe7e13b07d69a23a39c43fddfe613e21adb11cd1571a504d72d9366095cea28aace224c3723c143a50684099851bcde7f5c
7
- data.tar.gz: 4b5e1c96db2ae6ea90917a797900ed1b451c719c083e5e832360c10e004c6b4522669cd9d01b23099fa04654a15ac046edcb738c7dc33fc0ed0230629326a395
6
+ metadata.gz: d4ee88977c23a2e7e01261c4deb31724edd5b904d4974ab616ae519adca9bdf5d79123a9421f7e555013a66b67447d3c90597dd6b0721fcddc69795db7bb023d
7
+ data.tar.gz: cee05c180b9e031e86074df920dbe079c9d70ff77a486e46d364dbb111f8b79805aed0a5388fff4053cbb25d15e4e0b4d54c04ed813f578ba1bb0b8fd67a8101
data/.gitignore CHANGED
@@ -3,3 +3,4 @@ doc
3
3
  *.swo
4
4
  .rspec
5
5
  *.huml
6
+ *.gem
data/Changes ADDED
@@ -0,0 +1,6 @@
1
+
2
+
3
+ 0.0.2 Mon Jul 14 11:26:46 CST 2014
4
+ - Parser can recognize embed code
5
+ - Parser can distinguish double quote string which allows interpolation
6
+ - Parser can distinguish single quote string which doesn't allow interpolation
data/README.md CHANGED
@@ -1,23 +1,85 @@
1
1
  # Huml
2
2
 
3
- Huml is a template engine derived from [Haml](github.com/haml/haml).
3
+ Huml is to [Haml](github.com/haml/haml) what [Scss](http://sass-lang.com/) is to [Sass](http://sass-lang.com/).
4
4
  Like the relationship between Sass and Scss, Huml uses braces for nesting structure
5
5
  when Haml uses indentations.
6
6
 
7
+ ## Basic Usage
8
+
9
+ Huml can be used from the command line or as part of a Ruby web framework. The first step is to install the gem:
10
+
11
+ gem install huml
12
+
13
+ After writing some Huml, you can run
14
+
15
+ huml document.huml
16
+
17
+ to compile it to HTML.
18
+
7
19
  ## Formatting
8
20
 
21
+ The most basic element of Huml is a similar to Haml.
22
+
23
+ %tagname(attr1='value1' attr2='value2') = 'Content'
24
+
25
+ Adding `class` and `id` attributes uses the same syntax as the CSS too.
26
+
27
+ %tagname#id.class
28
+
29
+ If the element contains inner elements, you can use curly brace to wrap it
30
+
31
+ %ul {
32
+ %li = 'Salt'
33
+ %li = 'Pepper'
34
+ }
35
+
36
+ becomes
37
+
38
+ <ul>
39
+ <li>Salt</li>
40
+ <li>Pepper</li>
41
+ </ul>
42
+
43
+ You can also put plain text as a child of an element:
44
+
45
+ %p {
46
+ 'Hello,'
47
+ 'World!'
48
+ }
49
+
50
+ It's also possible to embed Ruby code into Haml documents. A hyphen, `-`, will run the code but not output the result.
51
+ You can even use control statements like if and while:
52
+
53
+ %p {
54
+ 'Date/Time:'
55
+ - now = DateTime.now
56
+ %strong = "#{now}"
57
+
58
+ - if now > DateTime.parse("December 31, 2006")
59
+ 'Happy new year!'
60
+ - end
61
+ }
62
+
63
+ ## An Example
64
+
9
65
  doctype 5
10
66
  %html {
11
67
 
12
68
  %head {
13
69
  %script(type="text/javascript" src="app.js")
14
- %link(rel="stylesheet" type="text/css" media="all")
70
+ %link(href="app.css" rel="stylesheet" type="text/css" media="all")
15
71
  }
16
72
 
17
73
  %body {
18
74
  %div.container {
19
75
  %h3.header = "page header"
20
76
  %p(style="background-color: #fff;") = "Lorem Ipsum is simply dummy text of the printing and typesetting industry."
77
+
78
+ - for item in %w(hello world) do
79
+ %div = "interpolate #{item} here"
80
+ - end
81
+
82
+ "plain text is eligible"
21
83
  }
22
84
  }
23
85
 
@@ -87,9 +87,21 @@ module Huml
87
87
  end
88
88
  end
89
89
 
90
- class String < Treetop::Runtime::SyntaxNode
90
+ class Dynamic < Treetop::Runtime::SyntaxNode
91
+ def tokenize
92
+ [:dynamic, "\"#{literal.text_value}\""]
93
+ end
94
+ end
95
+
96
+ class Static < Treetop::Runtime::SyntaxNode
91
97
  def tokenize
92
98
  [:static, literal.text_value]
93
99
  end
94
100
  end
101
+
102
+ class Code < Treetop::Runtime::SyntaxNode
103
+ def tokenize
104
+ [:code, code.text_value]
105
+ end
106
+ end
95
107
  end
@@ -33,13 +33,17 @@ grammar Huml
33
33
  end
34
34
 
35
35
  rule html
36
- block* <HTML>
36
+ ( block / string / code )* <HTML>
37
37
  end
38
38
 
39
39
  rule string
40
- '"' literal:([a-zA-Z0-9_!@#$%:;/\-\s\.]*) '"' <String>
40
+ '"' literal:([^"]*) '"' space <Dynamic>
41
41
  /
42
- "'" literal:([a-zA-Z0-9_!@#$%:;/\-\s\.]*) "'" <String>
42
+ "'" literal:([^']*) "'" space <Static>
43
+ end
44
+
45
+ rule code
46
+ space "-" space code:([^\n]*) [\r\n] space <Code>
43
47
  end
44
48
 
45
49
  end
@@ -1,3 +1,3 @@
1
1
  module Huml
2
- VERSION = '0.0.1'
2
+ VERSION = '0.0.2'
3
3
  end
@@ -10,4 +10,13 @@ describe Huml::Engine do
10
10
  it "outputs a divider with its content" do
11
11
  expect(eval(subject.call("%div = 'content'"))).to eq("<div>\n content\n</div>")
12
12
  end
13
+
14
+ it "interpolates for dynamic strings" do
15
+ expect(eval(Huml::Engine.new.call("'\#{var}'"))).to eq("\#{var}")
16
+ end
17
+
18
+ it "doesn't interpolate for static strings" do
19
+ var = "hello"
20
+ expect(eval(Huml::Engine.new.call("\"\#{var}\""))).to eq("hello")
21
+ end
13
22
  end
@@ -64,49 +64,53 @@ describe Huml::Parser do
64
64
  end
65
65
 
66
66
  it "recognizes a literal string" do
67
- expect(subject.parse('"hello world"', root: :string).tokenize).to eq([:static, "hello world"])
67
+ expect(subject.parse('"hello world"', root: :string).tokenize).to eq([:dynamic, '"hello world"'])
68
68
  expect(subject.parse("'hello world'", root: :string).tokenize).to eq([:static, "hello world"])
69
69
  end
70
70
 
71
71
  it "recognizes string composed of characters with hyphen" do
72
- expect(subject.parse('"col-xs-6"', root: :string).tokenize).to eq([:static, "col-xs-6"])
72
+ expect(subject.parse("'col-xs-6'", root: :string).tokenize).to eq([:static, "col-xs-6"])
73
73
  end
74
74
 
75
75
  it "recognizes string composed of characters with underscore" do
76
- expect(subject.parse('"foo_bar"', root: :string).tokenize).to eq([:static, "foo_bar"])
76
+ expect(subject.parse("'foo_bar'", root: :string).tokenize).to eq([:static, "foo_bar"])
77
+ end
78
+
79
+ it "recognizes UTF-8 characters" do
80
+ expect(subject.parse("'中文嘛也通'", root: :string).tokenize).to eq([:static, "中文嘛也通"])
77
81
  end
78
82
 
79
83
  it "recognizes a fragment of css" do
80
- expect(subject.parse('"background-color: #fff;"'))
84
+ expect(subject.parse("'background-color: #fff;'", root: :string).tokenize).to eq([:static, "background-color: #fff;"])
81
85
  end
82
86
 
83
87
  it "recognizes a block containing a string" do
84
- expect(subject.parse('%div = "string here"', root: :block).tokenize).to eq([:html, :tag, :div, [:html, :attrs], [:static, "string here"]])
88
+ expect(subject.parse("%div = 'string here'", root: :block).tokenize).to eq([:html, :tag, :div, [:html, :attrs], [:static, "string here"]])
85
89
  end
86
90
 
87
91
  it "recognizes a pair" do
88
- expect(subject.parse('src = "foo.png"', root: :pair).tokenize).to eq([:html, :attr, :src, [:static, "foo.png"]])
92
+ expect(subject.parse("src = 'foo.png'", root: :pair).tokenize).to eq([:html, :attr, :src, [:static, "foo.png"]])
89
93
  end
90
94
 
91
95
  it "recognizes attributes" do
92
- expect(subject.parse('( src = "foo.png" alt = "foo image" )', root: :attributes).tokenize).to eq([[:html, :attr, :src, [:static, "foo.png"]], [:html, :attr, :alt, [:static, "foo image"]]])
96
+ expect(subject.parse("( src = 'foo.png' alt = 'foo image' )", root: :attributes).tokenize).to eq([[:html, :attr, :src, [:static, "foo.png"]], [:html, :attr, :alt, [:static, "foo image"]]])
93
97
  end
94
98
 
95
99
  it "recognizes a block with attributes" do
96
- expect(subject.parse('%div(class="foo") {}', root: :block).tokenize).to eq([:html, :tag, :div, [:html, :attrs, [:html, :attr, :class, [:static, "foo"]]], [:multi]])
100
+ expect(subject.parse("%div(class='foo') {}", root: :block).tokenize).to eq([:html, :tag, :div, [:html, :attrs, [:html, :attr, :class, [:static, "foo"]]], [:multi]])
97
101
  end
98
102
 
99
103
  it "recognizes a assignment with attributes" do
100
- expect(subject.parse('%div(class="foo") = "string here"', root: :block).tokenize).to eq([:html, :tag, :div, [:html, :attrs, [:html, :attr, :class, [:static, "foo"]]], [:static, "string here"]])
104
+ expect(subject.parse("%div(class='foo') = 'string here'", root: :block).tokenize).to eq([:html, :tag, :div, [:html, :attrs, [:html, :attr, :class, [:static, "foo"]]], [:static, "string here"]])
101
105
  end
102
106
 
103
107
  it "recognizes a atomic element" do
104
108
  expect(subject.parse('%div', root: :block).tokenize).to eq([:html, :tag, :div, [:html, :attrs]])
105
109
  expect(subject.parse('%div()', root: :block).tokenize).to eq([:html, :tag, :div, [:html, :attrs]])
106
- expect(subject.parse('%div(class="foo")', root: :block).tokenize).to eq([:html, :tag, :div,
110
+ expect(subject.parse("%div(class='foo')", root: :block).tokenize).to eq([:html, :tag, :div,
107
111
  [:html, :attrs,
108
112
  [:html, :attr, :class, [:static, "foo"]]]])
109
- expect(subject.parse('%div.foo#bar(role="sidebar")', root: :block).tokenize).to eq([:html, :tag, :div,
113
+ expect(subject.parse("%div.foo#bar(role='sidebar')", root: :block).tokenize).to eq([:html, :tag, :div,
110
114
  [:html, :attrs,
111
115
  [:html, :attr, :class, [:static, "foo"]],
112
116
  [:html, :attr, :id, [:static, "bar"]],
@@ -116,4 +120,17 @@ describe Huml::Parser do
116
120
  it "recognizes multiple atomic elements" do
117
121
  expect(subject.parse("%div()\n%div()", root: :html).tokenize).to eq([[:html, :tag, :div, [:html, :attrs]], [:html, :tag, :div, [:html, :attrs]]])
118
122
  end
123
+
124
+ it "recognizes code" do
125
+ expect(subject.parse(" - @var.each do |item| \n", root: :code).tokenize).to eq([:code, "@var.each do |item| "])
126
+ end
127
+
128
+ it "recognizes embeded code" do
129
+ template = <<TEMPLATE
130
+ - @var.each do |item|
131
+ "variable #\{item\} here.\n"
132
+ - end
133
+ TEMPLATE
134
+ expect(subject.parse(template, root: :html).tokenize).to eq([[:code, '@var.each do |item|'], [:dynamic, "\"variable \#{item} here.\n\""], [:code, 'end']])
135
+ end
119
136
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: huml
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - shelling
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-07-10 00:00:00.000000000 Z
11
+ date: 2014-07-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: temple
@@ -75,12 +75,14 @@ extensions: []
75
75
  extra_rdoc_files: []
76
76
  files:
77
77
  - .gitignore
78
+ - Changes
78
79
  - Gemfile
79
80
  - Gemfile.lock
80
81
  - README.md
81
82
  - Rakefile
82
83
  - bin/huml
83
84
  - examples/simple.huml
85
+ - examples/string.huml
84
86
  - huml.gemspec
85
87
  - lib/huml.rb
86
88
  - lib/huml/engine.rb