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 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
|
= Amrita tour
== modify attributes of HTML element
=== code and output
code:
:include: sample/tour/makeurl.rb
output:
<table border="1">
<tr>
<th>name</th>
<th>author</th>
<th>webpage</th>
</tr>
<tr>
<td>Ruby</td>
<td>matz</td>
<td><a href="http://www.ruby-lang.org/">Ruby Home Page</a></td>
</tr>
<tr>
<td>perl</td>
<td>Larry Wall</td>
<td><a href="http://www.perl.com/">Perl.com</a></td>
</tr>
<tr>
<td>python</td>
<td>Guido van Rossum</td>
<td><a href="http://www.python.org/">Python Language Website</a></td>
</tr>
</table>
=== description
The Amrita#a() method produce a Amrita::AttrArray object.
a(:href=>"http://www.ruby-lang.org/") { "Ruby Home Page" },
When this special object is used for a model data, it modifies HTML
element's attributes and set text. So if template for this data is ...
<td><a id="webpage"></a></td>
The output will be....
<td><a href="http://www.ruby-lang.org/">Ruby Home Page</a></td>
filelist.rb described in docs/XML uses AttrArray object too.
There is another way to do this, see expand attribute expand in docs/Tour2
---
== proc
You can give a +proc+ as model data to edit element manualy.
=== code and output
code:
:include: sample/tour/proc.rb
output:
<ul>
<li><font color="black">java</font> </li>
<li><em><font color="red" size="big">I love Ruby!</font></em> </li>
<li><font color="blue">perl</font> </li>
...
</ul>
=== description
If model data is a Proc object, Amrita calls it with Amrita::Element
object that represents the HTML element.Amrita will replace the
element by result of +proc+.
In that proc, you can edit Element freely.
setting an attribute
elem[:color] = "red"
setting the text of element
elem.set_text("I love Ruby!")
generate a new Element with Amrita#e method
e(:em) { elem }
In this case +elem+ is <tt><font color...>I love Ruby!</font></tt>.
The output is wrapped by <tt>\<em>....\</em></tt> by <tt>e(:em) { .... }</tt>.
---
== use custom classes for model data
=== code and output
code:
:include: sample/tour/time.rb
output:
2002/7/17
=== description
If the model data is +kind_of+ Amrita::ExpandByMember, amrita uses
+id+ value as a method and call method of that name.
In this example, the data for +:time+ is a Ruby's standard Time object
but it +extend+ ExpandByMember. So +id+'s value +year+ is treated as
a method name and amrita calls that method of +t+.
So output for tempalte <tt><span id="year"></span></tt> get result of
method call <tt>t.yera</tt>: "2002" . Thus the produces the output...
<span><span>2002</span>/<span>7</span>/<span>17</span></span>
Amrita deletes <tt><span></tt> element if there is no attributes after
deleteing +id+ attribute.So last output is
2002/7/17
---
== precompile
Amrita can compile HTML template to Ruby code before +expand+.
=== code and output
code(the added code to table.rb) :
tmpl = TemplateText.new(TEMPLATE)
tmpl.use_compiler = true
tmpl.set_hint_by_sample_data(data) # optional: optimization to that data
tmpl.expand(STDOUT, data) # with compiled code
puts "----code generated by Amrita -----------"
puts tmpl.src
puts "----code generated by Amrita end -------"
The output is same as table.rb with the benchmark report added.
Here's my data on a Crusoe TM5600.
43.068354 seconds for 1000 times without compiling
5.078764 seconds for 1000 times with pre-compiled code
=== description
You only add one line for compiling
tmpl.use_compiler = true
After this, +expand+ method will be executed by compiled code that
produce (almost) same output.
And optionally give a sample data to amrita.
tmpl.set_hint_by_sample_data(data)
Amrita::HTMLCompiler uses this sample data for optimizing the output code.
So, if data structure changes after it, you must call
+set_hint_by_sample_data+ again.
Amrita::HTMLCompiler can produce a code that include interpreter mode
partially. If you need to compile and some part of model may change
dynamically, you can give +nil+ for data that may change.
Amrita::Compiler call Element::expand method in compiled code at that
point.
You can take trade off of speed and flexibility at any point you like.
---
== Sanitizing -- anti XSS attack
Amrita has a built in Amrita::Sanitizer to protect against XSS(cross
site scripting) attacks. Amrita::Formatter uses this module
automaticaly.
=== code and output
:include: sample/tour/sanitizer.rb
=== description
==== text
The dangerous characters for xhtml/html text (<>&) are escaped.
"<abc>" => "<abc>"
==== attribute value
The dangerous characters for attribute value (<>&"') are escaped.
==== special attribute value for URL
These attribute should be treated in another way because they would
have a URL value
* +href+ attribute of <a> element
* +src+ attribute of <img> element
* +action+ attribute of <form> element
for detail see tag.rb.
The value for them will be checked in more strict rule.
* They can't have any characters that is not allowd
* They can't have any schemes that is not allowd
The values that dose not match to these rules are replaced with nil
and printed like <tt><a href="">....</a></tt>
You can confiture which attribute should be treated as URL by
defineing +setup_taginfo+ method like this.
t = TemplateFile.new ...
def t.setup_taginfo
ret = TagInfo.new
ret[:aaa].set_url_attr(:bbb)
ret
end
Then +bbb+ attribute of +aaa+ element (<aaa bbb='...'>) is sanitized
as url.
==== turn sanitizing off
You can turn this feature off by providing a Amrita::SanitizedString
object as model data.
t = TemplateText.new '<p id="a">sample_text</p>'
t.expand(STDOUT, { :a=>"<xxx>" }) # => <p><xxx></p>
t.expand(result, { :a=>SanitizedString["<xxx>"] }) # => <p><xxx></p>
You should be careful to sanitize it in your own way when you pass it
to amrita as SanitizedString.
There is another way to disable this feature. If you wrapped model
data by <tt>escape {...} </tt>, text will be keeped with no change.
*USE THIS AT YOUR OWN RISK!!!*
---
|