/usr/share/doc/lua-cosmo/index.html is in lua-cosmo 13.01.30-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 | <p><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta name="description" content="Cosmo: Safe Templates in Lua">
<meta name="keywords" content="Lua, Cosmo, safe templates">
<title>Cosmo</title>
<style type="text/css">
body { color:#000; background:#fff; }
#header { width:100%;
text-align:center;<br/>
border-top:solid #aaa 1px;
border-bottom:solid #aaa 1px;
}
#header p { margin-left:0; }
p { margin-left:3px; }
pre { background-color:#ffe; white-space:pre; padding-left:3ex; border-left: 1px solid gray; margin-left: 10px}
</style>
</head>
<body>
<div id="header">
<img border=0 alt="Cosmo Logo" src="cosmo.png"/>
<p>Safe Templates in Lua</p>
<p>
<a href="#Overview">Overview</a> ·
<a href="#Installation">Installation</a> ·
<a href="#Using">Using Cosmo</a> ·
<a href="#Reference">API Reference</a> ·
<a href="#Contact">Contact Us</a> ·
<a href="#License">License</a></p>
<p> </p>
</div></p>
<h1><a name="Overview"></a> Overview</h1>
<p>Cosmo is a “safe templates” engine. It allows you to fill nested
templates, providing many of the advantages of Turing-complete
template engines, without without the downside of allowing arbitrary
code in the templates.</p>
<h1><a name="Overview"></a>Installation</h1>
<p>The current version of Cosmo is 13.01.30. This release adds Lua 5.2 support.</p>
<p>The previous version, 10.04.06, added, expressions to selectors $(<exp>),
allowed nested [[ ]] in templates, made commas between subtemplates
optional, and added a second parameter to cosmo.yield that tells Cosmo
the first parameter is a literal to be included in the expansion instead
of an environment.</p>
<p>Cosmo is installed as a rock. To install the most recent release
do <code>luarocks install cosmo</code>. The Cosmo rock is in the standard
repository. Installation on UNIX-based systems need the gcc toolchain.</p>
<h1><a name="Using"></a>Using Cosmo</h1>
<h2>Simple Form Filling</h2>
<p>Let’s start with a simple example of filling a set of scalar values
into a template: Here are a few examples of Cosmo in use:</p>
<pre><code>> values = { rank="Ace", suit="Spades" }
> template = "$rank of $suit"
> require("cosmo")
> = cosmo.fill(template, values)
Ace of Spades
</code></pre>
<p>Note that the template is a string that marks where values should
go. We call a template variable like <code>$rank</code> a <em>selector</em>, and <code>rank</code>
is the selector’s name. The table passed to <code>cosmo.fill</code> is the
<em>environment</em>, and it provides the
values. <code>$rank</code> will get replaced by <code>value.rank</code> (“Ace”) and <code>$suit</code>
will get replaced by <code>value.suit</code> (“Spades”).</p>
<p><code>cosmo.fill</code> takes two parameters at once. Cosmo also provides a
“shortcut” method <code>f()</code> which takes only one parameter - the template
- and returns a function that then takes the second parameter. This
allows for a more compact notation:</p>
<pre><code>> = cosmo.f(template){ rank="Ace", suit="Spades" }
Ace of Spades
</code></pre>
<p>A selector can be either a string or a Lua expression in parenthesis,
like <code>$("foo" .. "bar")</code> is replaced by <code>foobar</code> in the template. Any
variables in the expression are looked-up in the current template
environment, so <code>$(foo)</code> is the same as <code>$foo</code>, and <code>$(rank .. suit)</code>
would be replaced by <code>AceSpades</code> in the previous example.</p>
<h2>Nested Values</h2>
<p>You aren’t restricted to scalar values; your values can be Lua tables that
you can destructure using a <em>$val|key1|key2|…|keyn</em> syntax. For example:</p>
<pre><code>> values = { cards = { { rank = "Ace" , suit = "Spades" } } }
> template = "$cards|1|rank of $cards|1|suit"
> = cosmo.fill(template, values)
Ace of Spades
</code></pre>
<p>As you can see above, you can either use numbers or strings as keys.</p>
<h2>Arguments</h2>
<p>You can also pass arguments to a selector using the syntax <em>$selector{ args }</em>. The syntax
for the argument list is the same as a Lua table constructor, but function definitions are
not allowed, and you can use template selectors, which are looked up in the template environment.</p>
<p>If you pass an argument list and the selector maps to a function then Cosmo calls
this function with the argument list as a table, and the selector expands to what
the function returns. For example:</p>
<pre><code>> values = { message = function (arg) return arg.rank .. " of "
.. arg.suit end }
> template = "$message{ rank = 'Ace', suit = 'Spades' }"
> = cosmo.fill(template, values)
Ace of Spades
</code></pre>
<h2>Subtemplates</h2>
<p>Now, suppose we have not just one card, but several. Cosmo allows us
to handle this case with “subtemplates”</p>
<pre><code>> mycards = { {rank="Ace", suit="Spades"}, {rank="Queen", suit="Diamonds"}, {rank="10", suit="Hearts"} }
> template = "$do_cards[[$rank of $suit, ]]"
> = cosmo.fill(template, {do_cards = mycards})
Ace of Spades, Queen of Diamonds, 10 of Hearts,
</code></pre>
<p>The subtemplate “$rank or $suit” could be enclosed in <code>[[...]]</code>,
<code>[=[...]=]</code>, <code>[==[...]==]</code>, etc. - just like Lua’s long-quoted
strings. Again, we can use the shortcut <code>f()</code>:</p>
<pre><code>> = cosmo.f(template){do_cards = mycards}
Ace of Spades, Queen of Diamonds, 10 of Hearts,
</code></pre>
<h2>Subtemplates with Functions</h2>
<p>If we don’t have a ready table that would match the template, we can
set the value of <code>do_cards</code> to a function, which will yield a set of
values for the subtemplate each time it’s called:</p>
<pre><code>> mycards = { {"Ace", "Spades"}, {"Queen", "Diamonds"}, {"10", "Hearts"} }
> = cosmo.f(template){
do_cards = function()
for i,v in ipairs(mycards) do
cosmo.yield{rank=v[1], suit=v[2]}
end
end
}
Ace of Spades, Queen of Diamonds, 10 of Hearts,
</code></pre>
<p>You can also pass a list of arguments to this function:</p>
<pre><code>> template = "$do_cards{ true, false, true }[[$rank of $suit, ]]"
> mycards = { {"Ace", "Spades"}, {"Queen", "Diamonds"}, {"10", "Hearts"} }
> = cosmo.f(template){
do_cards = function(arg)
for i,v in ipairs(mycards) do
if arg[i] then cosmo.yield{rank=v[1], suit=v[2]} end
end
end
}
Ace of Spades, 10 of Hearts,
</code></pre>
<p>Finally, you can pass a literal to be included in the expansion
instead of an environment. An example:</p>
<pre><code>> template = "$do_cards{ true, false, true }[[$rank of $suit]]"
> mycards = { {"Ace", "Spades"}, {"Queen", "Diamonds"}, {"10", "Hearts"} }
> = cosmo.f(template){
do_cards = function(arg)
local n = #mycards
for i,v in ipairs(mycards) do
cosmo.yield{rank=v[1], suit=v[2]}
if i < n then cosmo.yield(", ", true) end
end
end
}
Ace of Spades, Queen of Diamonds, 10 of Hearts
</code></pre>
<h2>Alternative Subtemplates</h2>
<p>In some cases we may want to use differente templates for different
items in the list. For example, we might want to use a different
template for the first and/or last item, or to use different templates
for odd and even numbers. We can do this by specifying several
templates, separated by a comma. In that case, cosmo will use the
first template in the sequence, unless the table of values for the
item contains a special field <code>_template</code>, in which case this field
will be used as an index into the list of alternative templates. For
instance, setting <code>_template</code> to 2 would tell cosmo to use the 2nd
template for this item.</p>
<pre><code>> table.insert(mycards, {"2", "Clubs"})
> template = "You have: $do_cards[[$rank of $suit]],[[, $rank of $suit]],[[, and $rank of $suit]]"
> = cosmo.f(template){
do_cards = function()
for i,v in ipairs(mycards) do
local t
if i == #mycards then -- for the last item use the third template
t = 3
elseif i~=1 then -- use the second template for items 2...n-1
t = 2
end
cosmo.yield{rank=v[1], suit=v[2], _template=t}
end
end
}
You have: Ace of Spades, Queen of Diamonds, 10 of Heards, and 2 of Clubs
</code></pre>
<p>Note that the first item is formatted without preceeding “, ”, while
the last item is preceeded by an extra “and”.</p>
<h2>Deeper Nesting</h2>
<p>Templates and subtemplates can be nested to arbitrary depth. For
instance, instead of formatting a set of cards, we can format a list
of sets of cards:</p>
<pre><code>> players = {"John", "João"}
> cards = {}
> cards["John"] = mycards
> cards["João"] = { {"Ace", "Diamonds"} }
> template = "$do_players[[$player has $do_cards[[$rank of $suit]],
[[, $rank of $suit]],[[, and $rank of $suit]]\n]]"
> = cosmo.f(template){
do_players = function()
for i,p in ipairs(players) do
cosmo.yield {
player = p,
do_cards = function()
for i,v in ipairs(cards[p]) do
local t
if i == #mycards then
t = 3
elseif i~=1 then -- use the second template for items 2...n-1
t = 2
end
cosmo.yield{rank=v[1], suit=v[2], _template=t}
end
end
}
end
end
}
John has Ace of Spades, Queen of Diamonds, 10 of Hearts, and 2 of Clubs
João has Ace of Diamonds
</code></pre>
<h2>Scope</h2>
<p>Subtemplates can see values that were set in the higher scope:</p>
<pre><code>> template = "$do_players[[$do_cards[[$rank of $suit ($player), ]]]]"
> = cosmo.f(template){
do_players = function()
for i,p in ipairs(players) do
cosmo.yield {
player = p,
do_cards = function()
for i,v in ipairs(cards[p]) do
cosmo.yield{rank=v[1], suit=v[2]}
end
end,
}
end
end
}
Ace of Spades (John), Queen of Diamonds (John), 10 of Hearts (John), 2 of Clubs (John), Ace of Diamonds (João),
</code></pre>
<p>Note that in this case the field “player” is set in the table of
values that is passed to <code>do_players</code>, but is used one level deeper -
in <code>do_cards</code>.</p>
<p>The scoping behavior can be overriden by setting a metatable on the environment you
pass to the subtemplates.</p>
<h2>If</h2>
<p>Subtemplates and arguments let you implement a more generic conditional:</p>
<pre><code>> template = "$do_players[=[$player: $n card$if{ $plural }[[s]]
$if{ $more, $n_more }[[(needs $2 more)]],[[(no more needed)]]\n]=]"
> = cosmo.f(template){
do_players = function()
for i,p in ipairs(players) do
cosmo.yield {
player = p,
n = #cards[p],
["if"] = function (arg)
if arg[1] then arg._template = 1 else arg._template = 2 end
cosmo.yield(arg)
end,
plural = #cards[p] > 1,
more = #cards[p] < 3,
n_more = 3 - #cards[p]
}
end
end
}
John: 4 cards (no more needed)
João: 1 card (needs 2 more)
</code></pre>
<p>The conditional above is already present in Cosmo as <code>cosmo.cif</code>. Expressions in arguments
make it more useful:</p>
<pre><code>> template = "$if{ math.fmod(x, 4) == 0, target = 'World' }[[ Hello $target! ]],
[[ Hi $target! ]]"
> result = cosmo.fill(template, { math = math, x = 2, ["if"] = cosmo.cif })
> assert(result == " Hi World! ")
</code></pre>
<h2>Other conditionals</h2>
<p>In some cases we want to format an set of values if some condition
applies, and <code>cosmo.if</code> is not enough.
This can be done with a function and a subtemplate by just
replacing a for-loop with an if-block. However, since this is a
common case, cosmo provides a function for it:</p>
<pre><code>> template = "$do_players[[$player: $n card$if_plural[[s]] $if_needs_more[[(needs $n more)]]\n]]"
> = cosmo.f(template){
do_players = function()
for i,p in ipairs(players) do
cosmo.yield {
player = p,
n = #cards[p],
if_plural = cosmo.cond(#cards[p] > 1, {}),
if_needs_more = cosmo.cond(#cards[p] < 3, { n = 3 - #cards[p] })
}
end
end
}
John: 4 cards
João: 1 card (needs 2 more)
</code></pre>
<p>Like <code>fill()</code>, <code>cond()</code> has a “shortcut” equivalent which takes only
one parameter (the template) and returns a function:</p>
<pre><code>> = cosmo.f(template){
do_players = function()
for i,p in ipairs(players) do
cosmo.yield {
player = p,
n = #cards[p],
if_plural = cosmo.c(#cards[p] > 1){},
if_needs_more = cosmo.c(#cards[p] < 3){ n = 3-#cards[p] }
}
end
end
}
John: 4 cards
João: 1 card (needs 2 more)
</code></pre>
<h2>Map and Inject</h2>
<p>Cosmo provides two convenience functions for writing simple templates, <code>cosmo.map</code>
and <code>cosmo.inject</code>. Both functions have to be passed in a template’s environment.
The <code>cosmo.map</code> function yields each of its arguments in sequence, and inject yields
its whole argument table. A simple example:</p>
<pre><code>> template = "<ol>\n$map{ 'Spades', 'Hearts', 'Clubs', 'Diamonds'}[[<li>$it</li>\n]]</ol>"
> = cosmo.fill(template, { map = cosmo.map })
<ol>
<li>Spades</li>
<li>Hearts</li>
<li>Clubs</li>
<li>Diamonds</li>
</ol>
> template = "$inject{ suit = 'Spades' }[[Ace of <b>$suit</b>]]"
> = cosmo.fill(template, { inject = cosmo.inject })
Ace of <b>Spades</b>
</code></pre>
<h2>Concat</h2>
<p>Putting a delimiter between each expansion of a subtemplate is so
common that Cosmo provides also provides a convenience function for
it, <code>cosmo.concat</code>. This is an example:</p>
<pre><code>> template = "$concat{ cards, ', ' }[[$1 of $2]]"
> mycards = { {"Ace", "Spades"}, {"Queen", "Diamonds"}, {"10", "Hearts"} }
> = cosmo.f(template){
cards = mycards,
concat = cosmo.concat
}
Ace of Spades, Queen of Diamonds, 10 of Hearts
</code></pre>
<h1><a name="Reference"></a> API Reference</h1>
<p><strong>cosmo.compile(<em>template</em>, <em>chunkname</em>)</strong> - compiles <em>template</em> into
a function that takes an environment and returns the filled
template. Assigns <em>chunkname</em> as the name of this function</p>
<p><strong>cosmo.fill(<em>template</em>, <em>env</em>)</strong> - same as <strong>cosmo.compile(<em>template</em>)(<em>env</em>)</strong></p>
<p><strong>cosmo.yield(<em>env</em>, <em>is_literal</em>)</strong> - fills the current subtemplate
with <em>env</em> if <em>is_literal</em> is <code>nil</code> or <code>false</code> and adds it to the
output stream; otherwise adds the the string <em>env</em> to the output
stream</p>
<p><strong>cosmo.cond(<em>bool</em>, <em>tab</em>)</strong> - returns a function that yields an
empty environment if <em>bool</em> is <code>nil</code> or <code>false</code> and <em>tab</em> otherwise</p>
<p><strong>cosmo.c(<em>bool</em>)</strong> - returns a function that takes a table <em>tab</em> and does the same thing as
<strong>cosmo.cond(<em>bool</em>, <em>tab</em>)</strong></p>
<p><strong>cosmo.map{ … }</strong> - has to be used inside a template; yields each
element of its argument in turn</p>
<p><strong>cosmo.inject(env)</strong> - has to be used inside a template; yields its
argument</p>
<p><strong>cosmo.cif{ <em>exp</em>, … }</strong> - has to be used inside a template; yields
its argument to subtemplate 2 is <em>exp</em> is <code>nil</code> or <code>false</code> and to
subtemplate 1 otherwise</p>
<p><strong>cosmo.concat{ <em>list</em>, [<em>delim</em>] }</strong> - has to be used inside a
template; for each element of <em>list</em> yields it and, if it is not the
last, yields the literal <em>delim</em> or “, ” is <em>delim</em> is <code>nil</code></p>
<h1><a name="Contact"></a> Contact Us</h1>
<p>For more information please contact one of the authors,
<a href="mailto:mascarenhas_no_spam@acm.org">Fabio Mascarenhas</a> and <a href="http://takhteyev.org/contact/">Yuri
Takhteyev</a>, or write to the
<a href="http://sputnik.freewisdom.org/en/Mailing_List">Sputnik Mailing List</a>.</p>
<p>Comments are welcome!</p>
<h1><a name="License"></a> License</h1>
<p>Cosmo is free software: it can be used for both academic and
commercial purposes at absolutely no cost. There are no royalties or
GNU-like “copyleft” restrictions. Cosmo qualifies as Open Source
software. Its licenses are compatible with GPL. The legal details are
below.</p>
<p>The spirit of the license is that you are free to use Cosmo for any
purpose at no cost without having to ask us. The only requirement is
that if you do use Cosmo, then you should give us credit by including
the appropriate copyright notice somewhere in your product or its
documentation.</p>
<p>The original Cosmo library is designed and implemented by Yuri Takhteyev, with
much feedback and inspiration by André Carregal. This version is a reimplementation
by Fabio Mascarenhas, with aditional features. The implementations
are not derived from licensed software.</p>
<p>Copyright © 2008-2010 Fabio Mascarenhas.
Copyright © 2007-2008 Yuri Takhteyev.</p>
<hr />
<p>Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
“Software”), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:</p>
<p>The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.</p>
<p>THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.</p>
<p></body>
</html></p>
|