Browse Source

Add spec's for compile options (fixes #133)

Andreas Stenius 11 years ago
parent
commit
3e8ee1be91
3 changed files with 177 additions and 101 deletions
  1. 130 85
      README.markdown
  2. 46 15
      src/erlydtl.erl
  3. 1 1
      src/erlydtl_compiler.erl

+ 130 - 85
README.markdown

@@ -29,47 +29,79 @@ Template compilation
 
 
 Usage:
 Usage:
 
 
-    erlydtl:compile_file("/path/to/template.dtl", my_module_name)
+```erlang
+erlydtl:compile_file("/path/to/template.dtl", my_module_name)
 
 
-    erlydtl:compile_file("/path/to/template.dtl", my_module_name, Options)
+erlydtl:compile_file("/path/to/template.dtl", my_module_name, Options)
 
 
-    erlydtl:compile_template("<html>{{ foo }}</html>", my_module_name)
+erlydtl:compile_template("<html>{{ foo }}</html>", my_module_name)
 
 
-    erlydtl:compile_template("<html>{{ foo }}</html>", my_module_name, Options)
+erlydtl:compile_template("<html>{{ foo }}</html>", my_module_name, Options)
+```
 
 
 Result:
 Result:
 
 
-    ok %% existing compiled template is up to date.
+```erlang
+ok %% existing compiled template is up to date.
 
 
-    {ok, Module}
-    {ok, Module, Warnings}
-    {ok, Module, Binary}
-    {ok, Module, Binary, Warnings}
+{ok, Module}
+{ok, Module, Warnings}
+{ok, Module, Binary}
+{ok, Module, Binary, Warnings}
 
 
-    error
-    {error, Errors, Warnings}
+error
+{error, Errors, Warnings}
+```
 
 
 Options is a proplist possibly containing:
 Options is a proplist possibly containing:
 
 
-* `out_dir` - Directory to store generated .beam files. If not
-  specified, no .beam files will be created and a warning is
-  emitted. To silence the warning, use `{out_dir, false}`.
+* `auto_escape` - Control automatic HTML escaping of template
+  values. Enabled by default.
 
 
-* `doc_root` - Included template paths will be relative to this
-  directory; defaults to the compiled template's directory.
+* `binary` - Include the compiled template binary code in the result
+  tuple (between the module name and any warning/error lists). Note,
+  this option is named the same as for the Erlang compiler, with
+  similar use, except that this option does NOT affect whether or not
+  a .beam file is saved.
 
 
-* `libraries` - A list of `{Name, Module}` libraries implementing
-  custom tags and filters. `Module` should implement the
-  `erlydtl_library` behaviour.
+* `binary_strings` - Whether to compile strings as binary terms
+  (rather than lists). Defaults to `true`.
 
 
-* `default_libraries` - A list of libraries that should be loaded by
-  default when compiling a template. Libraries can be specified either
-  by name (when there is a name to module mapping also provided in the
-  `libraries` option) or by module.
+* `blocktrans_fun` - A two-argument fun to use for translating
+  `blocktrans` blocks, `trans` tags and `_(..)` expressions. This will
+  be called once for each pair of translated element and locale
+  specified in `blocktrans_locales`. The fun should take the form:
+
+  ```erlang
+  Fun(Block::string(), Locale::string()) -> <<"ErlyDTL code">>::binary() | default
+  ```
+
+* `blocktrans_locales` - A list of locales to be passed to
+  `blocktrans_fun`.  Defaults to [].
+
+* `compiler_options` - Proplist with extra options passed directly to
+  `compiler:forms/2`. This can prove useful when using extensions to
+  add extra defines etc when compiling the generated code.
+
+* `custom_filters_modules` **deprecated** - A list of modules to be
+  used for handling custom filters. The modules will be searched in
+  order and take precedence over the built-in filters. Each custom
+  filter should correspond to an exported filter, e.g.
+
+  ```erlang
+  some_filter(Value) -> iolist()
+  ```
+
+  If the filter takes any arguments (e.g. "foo:2"), those will be
+  added to the call:
+
+  ```erlang
+  some_filter(Value, Arg) -> iolist()
+  ```
 
 
 * `custom_tags_dir` - Directory of DTL files (no extension) includable
 * `custom_tags_dir` - Directory of DTL files (no extension) includable
-  as tags.  E.g. if `$custom_tags_dir/foo` contains `<b>{{ bar }}</b>`,
-  then `{% foo bar=100 %}` will evaluate to `<b>100</b>`.
+  as tags.  E.g. if `$custom_tags_dir/foo` contains `<b>{{ bar
+  }}</b>`, then `{% foo bar=100 %}` will evaluate to `<b>100</b>`.
 
 
 * `custom_tags_modules` **deprecated** - A list of modules to be used
 * `custom_tags_modules` **deprecated** - A list of modules to be used
   for handling custom tags. The modules will be searched in order and
   for handling custom tags. The modules will be searched in order and
@@ -77,8 +109,10 @@ Options is a proplist possibly containing:
   correspond to an exported function with one of the following
   correspond to an exported function with one of the following
   signatures:
   signatures:
 
 
-      some_tag(TagVars)          -> iolist()
-      some_tag(TagVars, Options) -> iolist()
+  ```erlang
+  some_tag(TagVars)          -> iolist()
+  some_tag(TagVars, Options) -> iolist()
+  ```
 
 
   The `TagVars` are variables provided to a custom tag in the
   The `TagVars` are variables provided to a custom tag in the
   template's body (e.g. `{% foo bar=100 %}` results in `TagVars =
   template's body (e.g. `{% foo bar=100 %}` results in `TagVars =
@@ -86,62 +120,51 @@ Options is a proplist possibly containing:
   argument to the `render/2` call at render-time. (These may include
   argument to the `render/2` call at render-time. (These may include
   any options, not just `locale` and `translation_fun`.)
   any options, not just `locale` and `translation_fun`.)
 
 
-* `custom_filters_modules` **deprecated** - A list of modules to be
-  used for handling custom filters. The modules will be searched in
-  order and take precedence over the built-in filters. Each custom
-  filter should correspond to an exported filter, e.g.
-
-      some_filter(Value) -> iolist()
+* `debug_info` - This option is passed to `compile:forms` to include
+  debug information in the compiled module. It will also print all
+  options passed to `compile:forms` if verbosity is >= 2 (e.g. with
+  two or more `verbose` options) and save the compiled template in
+  source form to a .erl file.
 
 
-  If the filter takes any arguments (e.g. "foo:2"), those will be
-  added to the call:
+* `default_libraries` - A list of libraries that should be loaded by
+  default when compiling a template. Libraries can be specified either
+  by name (when there is a name to module mapping also provided in the
+  `libraries` option) or by module.
 
 
-      some_filter(Value, Arg) -> iolist()
+* `doc_root` - Included template paths will be relative to this
+  directory; defaults to the compiled template's directory.
 
 
-* `vars` - Variables (and their values) to evaluate at compile-time
-  rather than render-time. (Currently not strictly true, see
-  [#61](https://github.com/erlydtl/erlydtl/issues/61))
-
-* `reader` - {module, function} tuple that takes a path to a template
-  and returns a binary with the file contents. Defaults to `{file,
-  read_file}`. Useful for reading templates from a network resource.
+* `extension_module` **experimental** - This is work in progress to
+  make erlydtl extensible.
 
 
 * `force_recompile` - Recompile the module even if the source's
 * `force_recompile` - Recompile the module even if the source's
   checksum has not changed. Useful for debugging.
   checksum has not changed. Useful for debugging.
 
 
-* `locale` - DEPRECATED. The same as {blocktrans_locales, [Val]}.
+* `libraries` - A list of `{Name, Module}` libraries implementing
+  custom tags and filters. `Module` should implement the
+  `erlydtl_library` behaviour.
 
 
-* `blocktrans_fun` - A two-argument fun to use for translating
-  `blocktrans` blocks, `trans` tags and `_(..)` expressions. This will
-  be called once for each pair of translated element and locale
-  specified in `blocktrans_locales`. The fun should take the form:
+* `locale` **deprecated** - The same as {blocktrans_locales, [Val]}.
 
 
-      Fun(Block::string(), Locale::string()) -> <<"ErlyDTL code">>::binary() | default
+* `no_env` - Do not read additional options from the OS environment
+  variable `ERLYDTL_COMPILER_OPTIONS`.
 
 
-* `blocktrans_locales` - A list of locales to be passed to
-  `blocktrans_fun`.  Defaults to [].
+* `no_load` - Do not load the compiled template.
 
 
-* `binary_strings` - Whether to compile strings as binary terms
-  (rather than lists). Defaults to `true`.
+* `out_dir` - Directory to store generated .beam files. If not
+  specified, no .beam files will be created and a warning is
+  emitted. To silence the warning, use `{out_dir, false}`.
+
+* `reader` - {module, function} tuple that takes a path to a template
+  and returns a binary with the file contents. Defaults to `{file,
+  read_file}`. Useful for reading templates from a network resource.
 
 
 * `record_info` - List of records to look for when rendering the
 * `record_info` - List of records to look for when rendering the
   template. Each record info is a tuple with the fields of the record:
   template. Each record info is a tuple with the fields of the record:
 
 
-      {my_record, record_info(fields, my_record)}
-
-* `no_env` - Do not read additional options from the OS environment
-  variable `ERLYDTL_COMPILER_OPTIONS`.
-
-* `auto_escape` - Control automatic HTML escaping of template
-  values. Enabled by default.
-
-* `no_load` - Do not load the compiled template.
-
-* `binary` - Include the compiled template binary code in the result
-  tuple (between the module name and any warning/error lists). Note,
-  this option is named the same as for the Erlang compiler, with
-  similar use, except that this option does NOT affect whether or not
-  a .beam file is saved.
+  ```erlang
+  {my_record, record_info(fields, my_record)}
+  ```
 
 
 * `return` - Short form for both `return_warnings` and `return_errors`.
 * `return` - Short form for both `return_warnings` and `return_errors`.
 
 
@@ -158,13 +181,14 @@ Options is a proplist possibly containing:
 
 
 * `report_errors` - Print errors as they occur.
 * `report_errors` - Print errors as they occur.
 
 
-* `warnings_as_errors` - Treat warnings as errors.
+* `vars` - Variables (and their values) to evaluate at compile-time
+  rather than render-time. (Currently not strictly true, see
+  [#61](https://github.com/erlydtl/erlydtl/issues/61))
 
 
-* `verbose` - Enable verbose printing of compilation results.
+* `verbose` - Enable verbose printing of compilation progress. Add
+  several for even more verbose (e.g. debug) output.
 
 
-* `compiler_options` - Proplist with extra options passed directly to
-  `compiler:forms/2`. This can prove useful when using extensions to
-  add extra defines etc when compiling the generated code.
+* `warnings_as_errors` - Treat warnings as errors.
 
 
 
 
 Helper compilation
 Helper compilation
@@ -174,9 +198,11 @@ Helpers provide additional templating functionality and can be used in
 conjunction with the `custom_tags_module` option above. They can be
 conjunction with the `custom_tags_module` option above. They can be
 created from a directory of templates thusly:
 created from a directory of templates thusly:
 
 
-    erlydtl:compile_dir("/path/to/dir", my_helper_module_name)
+```erlang
+erlydtl:compile_dir("/path/to/dir", my_helper_module_name)
 
 
-    erlydtl:compile_dir("/path/to/dir", my_helper_module_name, Options)
+erlydtl:compile_dir("/path/to/dir", my_helper_module_name, Options)
+```
 
 
 The resulting module will export a function for each template
 The resulting module will export a function for each template
 appearing in the specified directory. Options is the same as for
 appearing in the specified directory. Options is the same as for
@@ -190,7 +216,10 @@ once (rather than once per template).
 Usage (of a compiled template)
 Usage (of a compiled template)
 ------------------------------
 ------------------------------
 
 
-    my_compiled_template:render(Variables) -> {ok, IOList} | {error, Err}
+
+```erlang
+my_compiled_template:render(Variables) -> {ok, IOList} | {error, Err}
+```
 
 
 Variables is a proplist, dict, gb_tree, or a parameterized module
 Variables is a proplist, dict, gb_tree, or a parameterized module
 (whose method names correspond to variable names). The variable values
 (whose method names correspond to variable names). The variable values
@@ -198,26 +227,33 @@ can be atoms, strings, binaries, or (nested) variables.
 
 
 IOList is the rendered template.
 IOList is the rendered template.
 
 
-    my_compiled_template:render(Variables, Options) ->
-            {ok, IOList} | {error, Err}
+
+```erlang
+my_compiled_template:render(Variables, Options) -> {ok, IOList} | {error, Err}
+```
 
 
 Same as `render/1`, but with the following options:
 Same as `render/1`, but with the following options:
 
 
 * `translation_fun` - A fun/1 that will be used to translate strings
 * `translation_fun` - A fun/1 that will be used to translate strings
-  appearing inside `{% trans %}` and `{% blocktrans %}` tags. The simplest
-  TranslationFun would be `fun(Val) -> Val end`. Placeholders for
-  blocktrans variable interpolation should be wrapped to `{{` and `}}`.
+  appearing inside `{% trans %}` and `{% blocktrans %}` tags. The
+  simplest TranslationFun would be `fun(Val) -> Val end`. Placeholders
+  for blocktrans variable interpolation should be wrapped to `{{` and
+  `}}`.
 
 
 * `locale` - A string specifying the current locale, for use with the
 * `locale` - A string specifying the current locale, for use with the
   `blocktrans_fun` compile-time option.
   `blocktrans_fun` compile-time option.
 
 
+
 ```erlang
 ```erlang
 my_compiled_template:translatable_strings() -> [String]
 my_compiled_template:translatable_strings() -> [String]
 ```
 ```
 
 
 List of strings appearing in `{% trans %}` and `_(..)` tags.
 List of strings appearing in `{% trans %}` and `_(..)` tags.
 
 
-    my_compiled_template:translated_blocks() -> [String]
+
+```erlang
+my_compiled_template:translated_blocks() -> [String]
+```
 
 
 List of strings appearing in `{% blocktrans %}...{% endblocktrans %}`
 List of strings appearing in `{% blocktrans %}...{% endblocktrans %}`
 blocks; the translations (which can contain ErlyDTL code) are
 blocks; the translations (which can contain ErlyDTL code) are
@@ -225,17 +261,26 @@ hard-coded into the module and appear at render-time. To get a list of
 translatable blocks before compile-time, use the provided
 translatable blocks before compile-time, use the provided
 `blocktrans_extractor` module.
 `blocktrans_extractor` module.
 
 
-    my_compiled_template:source() -> {FileName, CheckSum}
+
+```erlang
+my_compiled_template:source() -> {FileName, CheckSum}
+```
 
 
 Name and checksum of the original template file.
 Name and checksum of the original template file.
 
 
-    my_compiled_template:dependencies() -> [{FileName, CheckSum}]
+
+```erlang
+my_compiled_template:dependencies() -> [{FileName, CheckSum}]
+```
 
 
 List of names/checksums of templates included by the original template
 List of names/checksums of templates included by the original template
 file. Useful for frameworks that recompile a template only when the
 file. Useful for frameworks that recompile a template only when the
 template's dependencies change.
 template's dependencies change.
 
 
-    my_compiled_template:variables() -> [Variable::atom()]
+
+```erlang
+my_compiled_template:variables() -> [Variable::atom()]
+```
 
 
 Sorted list of unique variables used in the template's body. The list
 Sorted list of unique variables used in the template's body. The list
 can be used for determining which variable bindings need to be passed
 can be used for determining which variable bindings need to be passed

+ 46 - 15
src/erlydtl.erl

@@ -50,8 +50,10 @@
 -export([compile/2, compile/3]).
 -export([compile/2, compile/3]).
 -export([compile_dir/2, compile_dir/3]).
 -export([compile_dir/2, compile_dir/3]).
 
 
+-type position() :: non_neg_integer().
+-type location() :: none | position() | {Line::position(), Column::position()}.
 -type error_info() :: {File::list(),
 -type error_info() :: {File::list(),
-                       [{Line::integer() | none,
+                       [{location(),
                          Module::atom(),
                          Module::atom(),
                          ErrorDesc::term()}]}.
                          ErrorDesc::term()}]}.
 -type errors() :: list(error_info()).
 -type errors() :: list(error_info()).
@@ -59,16 +61,45 @@
 -type ok_ret() :: {ok, Module::atom()} | {ok, Module::atom(), warnings()}.
 -type ok_ret() :: {ok, Module::atom()} | {ok, Module::atom(), warnings()}.
 -type err_ret() :: error | {error, errors(), warnings()}.
 -type err_ret() :: error | {error, errors(), warnings()}.
 
 
+-type filename() :: file:name_all().
+
+-type compiler_options() :: compiler_option() | compile:option().
+-type compiler_option() :: return | return_warnings | return_errors
+                        | report | report_warnings | report_errors
+                        | warnings_as_errors | debug_info | verbose.
+
+-type compile_options() :: [compile_option() | {atom(), term()}].
+-type compile_option() :: compiler_option()
+                        | auto_escape | binary | binary_strings
+                        | force_recompile | no_env | no_load
+                        | {blocktrans_fun, Trans::fun((Block::iodata(), Locale::string()) ->
+                                                             iodata() | default)}
+                        | {blocktrans_locales, [string()]}
+                        | {compiler_options, [compiler_options()]}
+                        | {custom_filters_modules, [Module::atom]}
+                        | {custom_tags_dirs, [filename()]}
+                        | {custom_tags_modules, [Module::atom]}
+                        | {default_libraries, [Name::atom()]}
+                        | {doc_root, filename()}
+                        | {extension_module, Module::atom()}
+                        | {libraries, [{Name::atom(), Module::atom()}]}
+                        | {locale, string()}
+                        | {out_dir, false | filename()}
+                        | {reader, {Module::atom(), Function::atom}}
+                        | {record_info, [{Name::atom(), [Field::atom()]}]}
+                        | {scanner_module, Module::atom()}
+                        | {vars, [{atom(), iodata()}]}.
+
 
 
 %% --------------------------------------------------------------------
 %% --------------------------------------------------------------------
 %% Compile file
 %% Compile file
 %% --------------------------------------------------------------------
 %% --------------------------------------------------------------------
 
 
--spec compile_file( list() | binary(), atom() ) -> {ok, Module::atom()} | error.
+-spec compile_file(filename(), atom()) -> {ok, Module::atom()} | error.
 compile_file(File, Module) ->
 compile_file(File, Module) ->
     erlydtl_compiler:compile_file(File, Module, erlydtl_compiler:default_options()).
     erlydtl_compiler:compile_file(File, Module, erlydtl_compiler:default_options()).
 
 
--spec compile_file( list() | binary(), atom(), list() ) -> ok_ret() | err_ret().
+-spec compile_file(filename(), atom(), compile_options()) -> ok_ret() | err_ret().
 compile_file(File, Module, Options) ->
 compile_file(File, Module, Options) ->
     erlydtl_compiler:compile_file(File, Module, Options).
     erlydtl_compiler:compile_file(File, Module, Options).
 
 
@@ -77,11 +108,11 @@ compile_file(File, Module, Options) ->
 %% Compile template
 %% Compile template
 %% --------------------------------------------------------------------
 %% --------------------------------------------------------------------
 
 
--spec compile_template( list() | binary(), atom() ) -> {ok, Module::atom()} | error.
+-spec compile_template(iodata(), atom()) -> {ok, Module::atom()} | error.
 compile_template(Template, Module) ->
 compile_template(Template, Module) ->
     erlydtl_compiler:compile_template(Template, Module, erlydtl_compiler:default_options()).
     erlydtl_compiler:compile_template(Template, Module, erlydtl_compiler:default_options()).
 
 
--spec compile_template( list() | binary(), atom(), list() ) -> ok_ret() | err_ret().
+-spec compile_template(iodata(), atom(), compile_options()) -> ok_ret() | err_ret().
 compile_template(Template, Module, Options) ->
 compile_template(Template, Module, Options) ->
     erlydtl_compiler:compile_template(Template, Module, Options).
     erlydtl_compiler:compile_template(Template, Module, Options).
 
 
@@ -90,11 +121,11 @@ compile_template(Template, Module, Options) ->
 %% Compile directory
 %% Compile directory
 %% --------------------------------------------------------------------
 %% --------------------------------------------------------------------
 
 
--spec compile_dir(list() | binary(), atom()) -> {ok, Module::atom()} | error.
+-spec compile_dir(filename(), atom()) -> {ok, Module::atom()} | error.
 compile_dir(DirectoryPath, Module) ->
 compile_dir(DirectoryPath, Module) ->
     erlydtl_compiler:compile_dir(DirectoryPath, Module, erlydtl_compiler:default_options()).
     erlydtl_compiler:compile_dir(DirectoryPath, Module, erlydtl_compiler:default_options()).
 
 
--spec compile_dir(list() | binary(), atom(), list()) -> ok_ret() | err_ret().
+-spec compile_dir(filename(), atom(), compile_options()) -> ok_ret() | err_ret().
 compile_dir(DirectoryPath, Module, Options) ->
 compile_dir(DirectoryPath, Module, Options) ->
     erlydtl_compiler:compile_dir(DirectoryPath, Module, Options).
     erlydtl_compiler:compile_dir(DirectoryPath, Module, Options).
 
 
@@ -104,10 +135,10 @@ compile_dir(DirectoryPath, Module, Options) ->
 %% --------------------------------------------------------------------
 %% --------------------------------------------------------------------
 
 
 %% keep for backwards compatibility, with a tuple-twist to ease migration / offer alternative path..
 %% keep for backwards compatibility, with a tuple-twist to ease migration / offer alternative path..
--spec compile(FileOrBinary, atom() ) -> {ok, Module::atom()} | error
-                                            when FileOrBinary :: list() | binary() 
-                                                               | {file, list() | binary()}
-                                                               | {template, list() | binary()}.
+-spec compile(FileOrBinary, atom()) -> {ok, Module::atom()} | error
+                                           when FileOrBinary :: string() | binary()
+                                                              | {file, filename()}
+                                                              | {template, iodata()}.
 compile({file, File}, Module) ->
 compile({file, File}, Module) ->
     compile_file(File, Module);
     compile_file(File, Module);
 compile({template, Template}, Module) ->
 compile({template, Template}, Module) ->
@@ -117,10 +148,10 @@ compile(FileOrBinary, Module) when is_binary(FileOrBinary) ->
 compile(FileOrBinary, Module) ->
 compile(FileOrBinary, Module) ->
     compile_file(FileOrBinary, Module).
     compile_file(FileOrBinary, Module).
 
 
--spec compile( FileOrBinary, atom(), list() ) -> ok_ret() | err_ret()
-                                                     when FileOrBinary :: list() | binary() 
-                                                                        | {file, list() | binary()}
-                                                                        | {template, list() | binary()}.
+-spec compile(FileOrBinary, atom(), compile_options() ) -> ok_ret() | err_ret()
+                                                               when FileOrBinary :: string() | binary()
+                                                                                  | {file, filename()}
+                                                                                  | {template, iodata()}.
 compile({file, File}, Module, Options) ->
 compile({file, File}, Module, Options) ->
     compile_file(File, Module, Options);
     compile_file(File, Module, Options);
 compile({template, Template}, Module, Options) ->
 compile({template, Template}, Module, Options) ->

+ 1 - 1
src/erlydtl_compiler.erl

@@ -65,7 +65,7 @@ default_options() -> [verbose, report].
 
 
 compile_template(Template, Module, Options) ->
 compile_template(Template, Module, Options) ->
     Context = process_opts(undefined, Module, Options),
     Context = process_opts(undefined, Module, Options),
-    compile(Context#dtl_context{ bin = Template }).
+    compile(Context#dtl_context{ bin = iolist_to_binary(Template) }).
 
 
 compile_file(File, Module, Options) ->
 compile_file(File, Module, Options) ->
     Context = process_opts(File, Module, Options),
     Context = process_opts(File, Module, Options),