From 28bbe71988c692d99e146dee166e78dc6eaf54e0 Mon Sep 17 00:00:00 2001 From: fperrad Date: Sun, 18 Jul 2010 07:14:11 -0700 Subject: [PATCH 01/21] github generated gh-pages branch --- index.html | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 index.html diff --git a/index.html b/index.html new file mode 100644 index 0000000..d3ab387 --- /dev/null +++ b/index.html @@ -0,0 +1,80 @@ + + + + + + + fperrad/lua-CodeGen @ GitHub + + + + + + + Fork me on GitHub + +
+ +
+ + + + +
+ +

lua-CodeGen + by fperrad

+ +
+ a template engine +
+ +

Authors

+

Francois Perrad (francois.perrad@gadz.org)

+

Contact

+

François Perrad (francois.perrad@gadz.org)

+ + +

Download

+

+ You can download this project in either + zip or + tar formats. +

+

You can also clone the project with Git + by running: +

$ git clone git://github.com/fperrad/lua-CodeGen
+

+ + + +
+ + + + From 1b543761dfece55dad4bc6c192ae9179e22f8ce5 Mon Sep 17 00:00:00 2001 From: Francois Perrad Date: Sun, 18 Jul 2010 16:29:06 +0200 Subject: [PATCH 02/21] copy from doc/ (branch master) --- codegen.html | 74 +++++++++++++++++++++ index.html | 180 +++++++++++++++++++++++++++++++++------------------ 2 files changed, 192 insertions(+), 62 deletions(-) create mode 100644 codegen.html diff --git a/codegen.html b/codegen.html new file mode 100644 index 0000000..8839236 --- /dev/null +++ b/codegen.html @@ -0,0 +1,74 @@ + + + + Module CodeGen + + + + + + + + + + +
+ +
+ +
lua-CodeGen
+
a template engine
+
+ +
+ + + +
+ +

Manual

+ +

TODO

+ +
+ +
+ +
+
+ +
+ + + diff --git a/index.html b/index.html index d3ab387..de236bf 100644 --- a/index.html +++ b/index.html @@ -1,80 +1,136 @@ - + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> - - - fperrad/lua-CodeGen @ GitHub - - - + lua-CodeGen - Homepage + + + + + + - Fork me on GitHub -
+
-
- - - - -
+
+ +
lua-CodeGen
+
a template engine
+
-

lua-CodeGen - by fperrad

+
-
- a template engine -
+ -

Authors

-

Francois Perrad (francois.perrad@gadz.org)

-

Contact

-

François Perrad (francois.perrad@gadz.org)

+
+

Overview

-

Download

-

- You can download this project in either - zip or - tar formats. -

-

You can also clone the project with Git - by running: -

$ git clone git://github.com/fperrad/lua-CodeGen
-

+

lua-CodeGen is a "safe" template engine.

- +

References

-
+

the Terence Parr's papers :

+ + +

Status

+ +

lua-CodeGen is in alpha stage.

+ +

It's developed for Lua 5.1.

+ +

Download

+ +

lua-CodeGen source can be downloaded from +GitHub or +Lua Forge.

+ +

Installation

+ +

lua-CodeGen is available via LuaRocks:

+ +
+luarocks install lua-codegen
+
+ +

or manually, with:

+ +
+make install
+
+ +

Test

+ +

The test suite requires the module +lua-TestMore.

+ +
+make test
+
+ +

Copyright and License

+ +

Copyright © 2010 François Perrad + + ohloh profile for François Perrad + + + View François Perrad's profile on LinkedIn + +

+ +

This library is licensed under the terms of the MIT/X11 license, like Lua itself.

+ +
+ +
+ +
+
+ +
- From 7ba49c72065a86b2d321b97acbed90130afa694d Mon Sep 17 00:00:00 2001 From: Francois Perrad Date: Sun, 18 Jul 2010 16:29:17 +0200 Subject: [PATCH 03/21] google site verification --- googlee25fe224e5cc09a6.html | 1 + 1 file changed, 1 insertion(+) create mode 100644 googlee25fe224e5cc09a6.html diff --git a/googlee25fe224e5cc09a6.html b/googlee25fe224e5cc09a6.html new file mode 100644 index 0000000..0b39cba --- /dev/null +++ b/googlee25fe224e5cc09a6.html @@ -0,0 +1 @@ +google-site-verification: googlee25fe224e5cc09a6.html \ No newline at end of file From 260414952110ae945a8e83ba8613fc2f708caa7b Mon Sep 17 00:00:00 2001 From: Francois Perrad Date: Sun, 18 Jul 2010 16:29:47 +0200 Subject: [PATCH 04/21] add a Makefile for xmllint --- Makefile | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 Makefile diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..f0bc6fa --- /dev/null +++ b/Makefile @@ -0,0 +1,5 @@ + +check: + xmllint --noout --valid index.html + xmllint --noout --valid codegen.html + From e401b78164e944b6170b4e93373b7cdd8bfee5f5 Mon Sep 17 00:00:00 2001 From: Francois Perrad Date: Fri, 20 Aug 2010 00:20:16 +0200 Subject: [PATCH 05/21] start manual --- codegen.html | 104 ++++++++++++++++++++++++++++++++++++++++++++++++++- index.html | 1 + 2 files changed, 103 insertions(+), 2 deletions(-) diff --git a/codegen.html b/codegen.html index 8839236..0c755b3 100644 --- a/codegen.html +++ b/codegen.html @@ -44,6 +44,7 @@

Test.More

  • The module CodeGen
  • Project @@ -57,9 +58,108 @@

    Test.More

    -

    Manual

    +

    Manual

    + +

    For the impatient

    + +
    +require 'CodeGen'
    +
    +tmpl = CodeGen {    -- instanciation
    +    tarball = "${name}-${version}.tar.gz",
    +    name = 'lua',
    +}
    +tmpl.version = 5.1
    +output = tmpl 'tarball'     -- interpolation
    +print(output) -- > lua-5.1.tar.gz
    +
    + +

    The instanciation

    + +

    The instanciation of a template is done by the call of CodeGen +with an optional parameter. This parameter is a table. The hash part of this +table uses only string as key and could contains 3 kinds of value : +

    +
      +
    • chunk of template which is a string and could contains primitives ie. ${...}
    • +
    • data which gives access to the data model
    • +
    • formatter which is a function which accepts a string as parameter and + returns it after a transformation. The typical usage is for escape sequence.
    • +
    +

    +The array part is used for inheritance from other templates. +

    + +

    Setting data and other alteration

    + +

    After the instanciation and before the interpolation, all member of the template +are accessible and modifiable like in a table. +

    + +

    Typically, data from the model are added after the instanciation. +

    + +

    The interpolation

    + +

    The interpolation is done by calling the template with one string parameter +which is the keyname of the entry point template. +

    + +

    The interpolation returns a string and an array which could contains some error messages. +

    + +

    The 4 primitives

    + +

    The data could be in the form of foo.bar.baz. +

    + +

    Attribute reference

    + +

    The syntax is ${data[; separator='sep'][; format=name]}. +

    + +

    An undefined data produces an empty string. +

    + +

    When data is a table, the option separator is used as +parameter of table.concat(data, sep). +

    + +

    The option format allows to specify a formatter function. +The default is the standard Lua function tostring. +

    + +

    Template include

    + +

    The syntax is ${name()} where name is the keyname of a chunk template. +

    + +

    If name is not the keyname of a valid chunk, there are no substitution +and an error is reported. +

    + +

    Conditional include

    + +

    The if syntax is ${data?name1()} +and the if/else syntax is ${data?name1():name2()} +where name1 and name2 are the keyname of a chunk template +and data is evaluated as a boolean. +

    + +

    Template application

    + +

    The syntax is ${data:name()} +where data must be a table. +The template name is called for each item of the array data, +and the result is concatened. +

    + +

    Examples

    + +
    +TODO
    +
    -

    TODO

    diff --git a/index.html b/index.html index de236bf..6b6f801 100644 --- a/index.html +++ b/index.html @@ -44,6 +44,7 @@

    lua-CodeGen

  • The module CodeGen
  • Project From e1441b29200e8c16eb995d1b05929eaa1577f70f Mon Sep 17 00:00:00 2001 From: Francois Perrad Date: Fri, 20 Aug 2010 01:10:03 +0200 Subject: [PATCH 06/21] error --- codegen.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/codegen.html b/codegen.html index 0c755b3..682e700 100644 --- a/codegen.html +++ b/codegen.html @@ -105,7 +105,7 @@

    The interpolation

    which is the keyname of the entry point template.

    -

    The interpolation returns a string and an array which could contains some error messages. +

    The interpolation returns a string and an optional string which contains some error messages.

    The 4 primitives

    From 6acb0be5671cdddcf30b39dded22580c7c0a0161 Mon Sep 17 00:00:00 2001 From: Francois Perrad Date: Fri, 20 Aug 2010 23:18:39 +0200 Subject: [PATCH 07/21] add examples --- codegen.html | 133 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 128 insertions(+), 5 deletions(-) diff --git a/codegen.html b/codegen.html index 682e700..d636991 100644 --- a/codegen.html +++ b/codegen.html @@ -71,7 +71,7 @@

    For the impatient

    } tmpl.version = 5.1 output = tmpl 'tarball' -- interpolation -print(output) -- > lua-5.1.tar.gz +print(output) --> lua-5.1.tar.gz

    The instanciation

    @@ -90,6 +90,20 @@

    The instanciation

    The array part is used for inheritance from other templates.

    +

    A common pattern is to put this part in an external file. +

    + +
    +-- file: tarball.tmpl
    +return CodeGen {
    +    tarball = "${name}-${version}.tar.gz",
    +}
    +
    + +
    +tmpl = dofile 'tarball.tmpl'
    +
    +

    Setting data and other alteration

    After the instanciation and before the interpolation, all member of the template @@ -121,13 +135,25 @@

    Attribute reference

    An undefined data produces an empty string.

    +

    The option format allows to specify a formatter function. +The default is the standard Lua function tostring. +

    +

    When data is a table, the option separator is used as parameter of table.concat(data, sep).

    -

    The option format allows to specify a formatter function. -The default is the standard Lua function tostring. -

    +
    +require 'CodeGen'
    +
    +tmpl = CodeGen {
    +    call = "${name}(${parameters; separator=', '});",
    +}
    +tmpl.name = 'print'
    +tmpl.parameters = { 1, 2, 3 }
    +output = tmpl 'call'
    +print(output) --> print(1, 2, 3);
    +

    Template include

    @@ -156,10 +182,107 @@

    Template application

    Examples

    +

    A generic template for rockspec.

    + +
    +-- file: rockspec.tmpl
    +return CodeGen {
    +    rockspec = [[
    +package = '${name}'
    +version = '${version}-${revision}'
    +${_source()}
    +${_description()}
    +${_dependencies()}
    +]],
    +    _source = [[
    +source = {
    +    url = ${_url()},
    +    md5 = '${md5}',
    +    dir = '${name}-${version}',
    +},
    +]],
    +    _description = [[
    +description = {
    +    ${desc.summary?_summary()}
    +    ${desc.homepage?_homepage()}
    +    ${desc.maintainer?_maintainer()}
    +    ${desc.license?_license()}
    +},
    +]],
    +    _summary = 'summary = "${desc.summary}",',
    +    _homepage = 'homepage = "${desc.homepage}",',
    +    _maintainer = 'maintainer = "${desc.maintainer}",',
    +    _license = 'license = "${desc.license}",',
    +    _dependencies = [[
    +dependencies = {
    +${dependencies:_depend()}
    +}
    +]],
    +    _depend = [[
    +    '${name} >= ${version}',
    +]],
    +}
    +
    + +

    A specialization for all my projects.

    + +
    +-- file: my_rockspec.tmpl
    +local rs = dofile 'rockspec.tmpl'
    +
    +return CodeGen {
    +    rs;
    +    lower = string.lower,
    +    _tarball = "${name; format=lower}-${version}.tar.gz",
    +    _url = "'http://cloud.github.com/downloads/fperrad/${name}/${_tarball()}'",
    +    _homepage = 'homepage = "http://fperrad.github.com/${name}",',
    +    desc = {
    +        homepage = true,
    +        maintainer = "Francois Perrad",
    +        license = "MIT/X11",
    +    },
    +}
    +
    + +

    And now for this project.

    +
    -TODO
    +require 'CodeGen'
    +
    +rs = dofile 'my_rockspec.tmpl'
    +rs.name = 'lua-CodeGen'
    +rs.version = '0.1.0'
    +rs.revision = 1
    +rs.md5 = 'XxX'
    +rs.desc.summary = "a template engine"
    +rs.dependencies = {
    +    { name = 'lua', version = 5.1 },
    +    { name = 'lua-testmore', version = '0.2.1' },
    +}
    +print(rs 'rockspec')
     
    +

    The output is :

    + +
    +package = 'lua-CodeGen'
    +version = '0.1.0-1'
    +source = {
    +    url = 'http://cloud.github.com/downloads/fperrad/lua-CodeGen/lua-codegen-0.1.0.tar.gz',
    +    md5 = 'XxX',
    +    dir = 'lua-CodeGen-0.1.0',
    +},
    +description = {
    +    summary = "a template engine",
    +    homepage = "http://fperrad.github.com/lua-CodeGen",
    +    maintainer = "Francois Perrad",
    +    license = "MIT/X11",
    +},
    +dependencies = {
    +    'lua >= 5.1',
    +    'lua-testmore >= 0.2.1',
    +}
    +
    From 288e8ccf92f0bc1278c2da7a22157459d68b9f00 Mon Sep 17 00:00:00 2001 From: Francois Perrad Date: Sat, 21 Aug 2010 19:59:18 +0200 Subject: [PATCH 08/21] add overview --- index.html | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/index.html b/index.html index 6b6f801..44e5f73 100644 --- a/index.html +++ b/index.html @@ -8,7 +8,7 @@ - + @@ -62,6 +62,24 @@

    Overview

    lua-CodeGen is a "safe" template engine.

    +

    lua-CodeGen enforces a strict Model-View separation. +Only 4 primitives are supplied : attribute reference, +template include, conditional include and +template application (i.e., map operation). +

    + +

    lua-CodeGen allows to split template in small chunk, +and encourages the reuse of them by inheritance. +

    + +

    Each chunk of template is like a rule of a grammar +for an unparser generator. +

    + +

    lua-CodeGen is not dedicated to HTML, +it could generate any kind of textual code. +

    +

    References

    the Terence Parr's papers :

    @@ -122,7 +140,8 @@

    Copyright and License

    -

    This library is licensed under the terms of the MIT/X11 license, like Lua itself.

    +

    This library is licensed under the terms of the MIT/X11 license, +like Lua itself.

    From 8fa02f57b7d3c59f556f6af1acd788d3914084ba Mon Sep 17 00:00:00 2001 From: Francois Perrad Date: Sat, 21 Aug 2010 19:59:38 +0200 Subject: [PATCH 09/21] refactor inheritance --- codegen.html | 55 +++++++++++++++++++++++++++++----------------------- 1 file changed, 31 insertions(+), 24 deletions(-) diff --git a/codegen.html b/codegen.html index d636991..b16303d 100644 --- a/codegen.html +++ b/codegen.html @@ -8,7 +8,7 @@ - + @@ -77,20 +77,21 @@

    For the impatient

    The instanciation

    The instanciation of a template is done by the call of CodeGen -with an optional parameter. This parameter is a table. The hash part of this -table uses only string as key and could contains 3 kinds of value : +with optional parameters. This first parameter is a table. This table uses +only string as key and could contains 3 kinds of value :

      -
    • chunk of template which is a string and could contains primitives ie. ${...}
    • +
    • chunk of template which is a string and could contains primitives i.e. ${...}
    • data which gives access to the data model
    • formatter which is a function which accepts a string as parameter and - returns it after a transformation. The typical usage is for escape sequence.
    • + returns it after a transformation. + The typical usage is for escape sequence.

    -The array part is used for inheritance from other templates. +The other parameters allow inheritance (ie. access to field) from other templates or simple tables.

    -

    A common pattern is to put this part in an external file. +

    A common pattern is to put this step in an external file.

    @@ -119,15 +120,16 @@ 

    The interpolation

    which is the keyname of the entry point template.

    -

    The interpolation returns a string and an optional string which contains some error messages. +

    The interpolation returns a string as result and an optional string which +contains some error messages.

    -

    The 4 primitives

    +

    The 4 primitives in template

    The data could be in the form of foo.bar.baz.

    -

    Attribute reference

    +

    1. Attribute reference

    The syntax is ${data[; separator='sep'][; format=name]}.

    @@ -139,8 +141,8 @@

    Attribute reference

    The default is the standard Lua function tostring.

    -

    When data is a table, the option separator is used as -parameter of table.concat(data, sep). +

    When data is a table, the option separator +is used as parameter of table.concat(data, sep).

    @@ -155,16 +157,17 @@ 

    Attribute reference

    print(output) --> print(1, 2, 3);
    -

    Template include

    +

    2. Template include

    -

    The syntax is ${name()} where name is the keyname of a chunk template. +

    The syntax is ${name()} where name +is the keyname of a chunk template.

    -

    If name is not the keyname of a valid chunk, there are no substitution -and an error is reported. +

    If name is not the keyname of a valid chunk, +there are no substitution and an error is reported.

    -

    Conditional include

    +

    3. Conditional include

    The if syntax is ${data?name1()} and the if/else syntax is ${data?name1():name2()} @@ -172,13 +175,17 @@

    Conditional include

    and data is evaluated as a boolean.

    -

    Template application

    +

    4. Template application

    The syntax is ${data:name()} where data must be a table. The template name is called for each item of the array data, and the result is concatened.

    +

    The template has a direct access in the item, +and inherits access from the caller. +If the item is not a table, it is accessible via the key it. +

    Examples

    @@ -228,10 +235,10 @@

    Examples

     -- file: my_rockspec.tmpl
    -local rs = dofile 'rockspec.tmpl'
    +local parent = dofile 'rockspec.tmpl'
     
    -return CodeGen {
    -    rs;
    +return CodeGen({
    +    parent;
         lower = string.lower,
         _tarball = "${name; format=lower}-${version}.tar.gz",
         _url = "'http://cloud.github.com/downloads/fperrad/${name}/${_tarball()}'",
    @@ -241,15 +248,15 @@ 

    Examples

    maintainer = "Francois Perrad", license = "MIT/X11", }, -} +}, parent)
    -

    And now for this project.

    +

    And finally, an use for this project.

     require 'CodeGen'
     
    -rs = dofile 'my_rockspec.tmpl'
    +local rs = dofile 'my_rockspec.tmpl'
     rs.name = 'lua-CodeGen'
     rs.version = '0.1.0'
     rs.revision = 1
    
    From fef101b3395fa336913cc73c39144b5479f024d4 Mon Sep 17 00:00:00 2001
    From: Francois Perrad 
    Date: Sat, 21 Aug 2010 20:31:38 +0200
    Subject: [PATCH 10/21] list item
    
    ---
     index.html | 10 +++++++---
     1 file changed, 7 insertions(+), 3 deletions(-)
    
    diff --git a/index.html b/index.html
    index 44e5f73..9147715 100644
    --- a/index.html
    +++ b/index.html
    @@ -63,10 +63,14 @@ 

    Overview

    lua-CodeGen is a "safe" template engine.

    lua-CodeGen enforces a strict Model-View separation. -Only 4 primitives are supplied : attribute reference, -template include, conditional include and -template application (i.e., map operation). +Only 4 primitives are supplied :

    +
      +
    • attribute reference,
    • +
    • template include,
    • +
    • conditional include,
    • +
    • and template application (i.e., map operation).
    • +

    lua-CodeGen allows to split template in small chunk, and encourages the reuse of them by inheritance. From 3e99019ed9d5ced5ec3ab9ea1c45e3819ba58aa8 Mon Sep 17 00:00:00 2001 From: Francois Perrad Date: Tue, 24 Aug 2010 11:04:18 +0200 Subject: [PATCH 11/21] update --- codegen.html | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/codegen.html b/codegen.html index b16303d..1c20f04 100644 --- a/codegen.html +++ b/codegen.html @@ -170,17 +170,17 @@

    2. Template include

    3. Conditional include

    The if syntax is ${data?name1()} -and the if/else syntax is ${data?name1():name2()} +and the if/else syntax is ${data?name1()!name2()} where name1 and name2 are the keyname of a chunk template and data is evaluated as a boolean.

    4. Template application

    -

    The syntax is ${data:name()} +

    The syntax is ${data:name()[; separator='sep']} where data must be a table. The template name is called for each item of the array data, -and the result is concatened. +and the result is concatened with an optional separator.

    The template has a direct access in the item, and inherits access from the caller. From 2679e804d6d39b4f322edceb4ffeef4171d91bc9 Mon Sep 17 00:00:00 2001 From: Francois Perrad Date: Thu, 26 Aug 2010 09:29:18 +0200 Subject: [PATCH 12/21] add CodeGen.Graph --- .gitignore | 2 + Makefile | 7 ++++ codegen.html | 10 ++++- graph.html | 106 +++++++++++++++++++++++++++++++++++++++++++++++++++ graph.png | Bin 0 -> 8968 bytes index.html | 8 +++- 6 files changed, 130 insertions(+), 3 deletions(-) create mode 100644 .gitignore create mode 100644 graph.html create mode 100644 graph.png diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f4fd33f --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ + +*.dot diff --git a/Makefile b/Makefile index f0bc6fa..c9887c2 100644 --- a/Makefile +++ b/Makefile @@ -2,4 +2,11 @@ check: xmllint --noout --valid index.html xmllint --noout --valid codegen.html + xmllint --noout --valid graph.html +graph: + lua -l CodeGen.Graph -e "print(CodeGen.Graph.to_dot(CodeGen.Graph.template))" > graph.dot + dot -T png -o graph.png graph.dot + +clean: + rm -f *.dot *.png diff --git a/codegen.html b/codegen.html index 1c20f04..ab9186a 100644 --- a/codegen.html +++ b/codegen.html @@ -28,7 +28,7 @@