File: README.md

package info (click to toggle)
ruby-mab 0.0.3-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, stretch
  • size: 268 kB
  • ctags: 98
  • sloc: ruby: 740; makefile: 2
file content (224 lines) | stat: -rw-r--r-- 3,440 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
Mab (Markup as Ruby)
====================

Mab let's you write HTML in plain Ruby:

```ruby
doctype!
html do
  head do
    link :rel => 'stylesheet', :href => 'style.css'
    script :src => 'jquery.js'
  end
  body :id => :frontpage do
    h1 'Hello World', :class => :main
  end
end
```


Syntax
------

### 1. Tags and Attributes

There are four basic forms:

```ruby
tagname(content)

tagname(content, attributes)

tagname do
  content
end

tagname(attributes) do
  content
end
```

Example:

```ruby
doctype!
html do
  head do
    link :rel => 'stylesheet', :href => 'style.css'
    script :src => 'jquery.js'
  end
  body :id => :frontpage do
    h1 'Hello World', :class => :main
  end
end
```

Which results in:

```html
<!DOCTYPE html>
<html>
  <head>
    <link rel="stylesheet" href="style.css">
    <script src="jquery"></script>
  </head>
  <body id="frontpage">
    <h1 class="main">Hello World</h1>
  </body>
</html>
```

Notice how Mab knows that script tag must have content, so although you didn't
specify anything it closed the tag for you.

### 2. Element Classes and IDs

You can easily add classes and IDs by hooking methods onto the container:

```ruby
body.frontpage! do
  h1.main 'Hello World'
end
```

Which results in:

```html
<body id="frontpage">
  <h1 class="main">Hello World</h1>
</body>
```

You can mix and match as you'd like (`div.klass.klass1.id!`), but you can only
provide content and attributes on the *last* call:

```ruby
# This is not valid:
form(:action => :post).world do
  input
end

# But this is:
form.world(:action => :post) do
  input
end
```

### 3. Escape or Not Escape

Mab uses a very simple convention for escaping: Strings as *arguments* gets
escaped, strings in *blocks* don't:

```ruby
div.comment "<script>alert(1)</script>"
# <div class="comment">&lt;script&gt;alert(1)&lt;/script&gt;</div>

div.comment { "I <strong>love</strong> you" }
# <div class="comment">I <strong>love</strong> you</div>
```

Be aware that Mab ignores the string in a block if there's other tags there:

```ruby
div.comment do
  div.author "BitPuffin"
  "<p>Silence!</p>"
end
```

The p tag above won't appear in the output.

### 4. Text

Sometimes you need to insert plain text:

```ruby
p.author do
  text 'Written by '
  a 'Bluebie', :href => 'http://creativepony.com/'
end
```

Which results in:

```html
<p class="author">
  Written by
  <a href="http://creativepony.com/">Bluebie</a>
</p>
```

There's also `text!` which doesn't escape:

```ruby
p.author do
  text! '<strong>Written</strong> by'
  a 'Bluebie', :href => 'http://creativepony.com/'
```


Invoking Mab
------------

Using #mab:

```ruby
require 'mab/kernel_method'

str = mab do
  doctype!
  html do
    # ...
  end
end
```

Using Mab::Builder (or Mab::PrettyBuilder if you want indentation):

```ruby
class Person
  attr_reader :name

  def initialize(name)
    @name = name
  end

  def awesome?; true end
end

# Assign instance variables:
Mab::Builder.new(:person => Person.new('BitBuffin')) do
  if @person.awesome?
    h1 @person.name
  else
    p @person.name
  end
end.to_s

# Use helper (methods and instance variables will be available):
Mab::Builder.new({}, Person.new('BitPuffin')) do
  if awesome?
    h1 @name
  else
    p @name
  end
end.to_s
```

Extending an object (*advanced usage*):

```ruby
r = Object.new
r.extend Mab::Mixin::HTML5
r.extend Mab::Indentation

r.mab do
  doctype!
  html do
    # ...
  end
end

```