erlydtl.erl 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. %%%-------------------------------------------------------------------
  2. %%% File: erlydtl.erl
  3. %%% @author Roberto Saccon <rsaccon@gmail.com> [http://rsaccon.com]
  4. %%% @author Evan Miller <emmiller@gmail.com>
  5. %%% @author Andreas Stenius <kaos@astekk.se>
  6. %%% @copyright 2008 Roberto Saccon, Evan Miller
  7. %%% @copyright 2014 Andreas Stenius
  8. %%% @doc
  9. %%% Public interface for ErlyDTL
  10. %%% @end
  11. %%%
  12. %%% The MIT License
  13. %%%
  14. %%% Copyright (c) 2008 Roberto Saccon, Evan Miller
  15. %%% Copyright (c) 2014 Andreas Stenius
  16. %%%
  17. %%% Permission is hereby granted, free of charge, to any person obtaining a copy
  18. %%% of this software and associated documentation files (the "Software"), to deal
  19. %%% in the Software without restriction, including without limitation the rights
  20. %%% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  21. %%% copies of the Software, and to permit persons to whom the Software is
  22. %%% furnished to do so, subject to the following conditions:
  23. %%%
  24. %%% The above copyright notice and this permission notice shall be included in
  25. %%% all copies or substantial portions of the Software.
  26. %%%
  27. %%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  28. %%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  29. %%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  30. %%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  31. %%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  32. %%% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  33. %%% THE SOFTWARE.
  34. %%%
  35. %%% @since 2007-11-11 by Roberto Saccon, Evan Miller
  36. %%% @since 2014 by Andreas Stenius
  37. %%%-------------------------------------------------------------------
  38. -module(erlydtl).
  39. -author('rsaccon@gmail.com').
  40. -author('emmiller@gmail.com').
  41. -author('Andreas Stenius <kaos@astekk.se>').
  42. %% --------------------------------------------------------------------
  43. %% API
  44. %% --------------------------------------------------------------------
  45. -export([compile_file/2, compile_file/3]).
  46. -export([compile_template/2, compile_template/3]).
  47. -export([compile/2, compile/3]).
  48. -export([compile_dir/2, compile_dir/3]).
  49. -type position() :: non_neg_integer().
  50. -type location() :: none | position() | {Line::position(), Column::position()}.
  51. -type error_info() :: {File::list(),
  52. [{location(),
  53. Module::atom(),
  54. ErrorDesc::term()}]}.
  55. -type errors() :: list(error_info()).
  56. -type warnings() :: list(error_info()).
  57. -type ok_ret() :: {ok, Module::atom()} | {ok, Module::atom(), warnings()}.
  58. -type err_ret() :: error | {error, errors(), warnings()}.
  59. -type filename() :: file:name_all().
  60. -type compiler_options() :: compiler_option() | compile:option().
  61. -type compiler_option() :: return | return_warnings | return_errors
  62. | report | report_warnings | report_errors
  63. | warnings_as_errors | debug_info | verbose.
  64. -type compile_options() :: [compile_option() | {atom(), term()}].
  65. -type compile_option() :: compiler_option()
  66. | auto_escape | binary | binary_strings
  67. | force_recompile | no_env | no_load
  68. | {blocktrans_fun, Trans::fun((Block::iodata(), Locale::string()) ->
  69. iodata() | default)}
  70. | {blocktrans_locales, [string()]}
  71. | {compiler_options, [compiler_options()]}
  72. | {custom_filters_modules, [Module::atom]}
  73. | {custom_tags_dirs, [filename()]}
  74. | {custom_tags_modules, [Module::atom]}
  75. | {default_libraries, [Name::atom()]}
  76. | {doc_root, filename()}
  77. | {extension_module, Module::atom()}
  78. | {libraries, [{Name::atom(), Module::atom()}]}
  79. | {locale, string()}
  80. | {out_dir, false | filename()}
  81. | {reader, {Module::atom(), Function::atom}}
  82. | {reader_options, [{Name::atom(), iodata()}]}
  83. | {record_info, [{Name::atom(), [Field::atom()]}]}
  84. | {scanner_module, Module::atom()}
  85. | {vars, [{atom(), iodata()}]}.
  86. %% --------------------------------------------------------------------
  87. %% Compile file
  88. %% --------------------------------------------------------------------
  89. -spec compile_file(filename(), atom()) -> {ok, Module::atom()} | error.
  90. compile_file(File, Module) ->
  91. erlydtl_compiler:compile_file(File, Module, erlydtl_compiler:default_options()).
  92. -spec compile_file(filename(), atom(), compile_options()) -> ok_ret() | err_ret().
  93. compile_file(File, Module, Options) ->
  94. erlydtl_compiler:compile_file(File, Module, Options).
  95. %% --------------------------------------------------------------------
  96. %% Compile template
  97. %% --------------------------------------------------------------------
  98. -spec compile_template(iodata(), atom()) -> {ok, Module::atom()} | error.
  99. compile_template(Template, Module) ->
  100. erlydtl_compiler:compile_template(Template, Module, erlydtl_compiler:default_options()).
  101. -spec compile_template(iodata(), atom(), compile_options()) -> ok_ret() | err_ret().
  102. compile_template(Template, Module, Options) ->
  103. erlydtl_compiler:compile_template(Template, Module, Options).
  104. %% --------------------------------------------------------------------
  105. %% Compile directory
  106. %% --------------------------------------------------------------------
  107. -spec compile_dir(filename(), atom()) -> {ok, Module::atom()} | error.
  108. compile_dir(DirectoryPath, Module) ->
  109. erlydtl_compiler:compile_dir(DirectoryPath, Module, erlydtl_compiler:default_options()).
  110. -spec compile_dir(filename(), atom(), compile_options()) -> ok_ret() | err_ret().
  111. compile_dir(DirectoryPath, Module, Options) ->
  112. erlydtl_compiler:compile_dir(DirectoryPath, Module, Options).
  113. %% --------------------------------------------------------------------
  114. %% Legacy API
  115. %% --------------------------------------------------------------------
  116. %% keep for backwards compatibility, with a tuple-twist to ease migration / offer alternative path..
  117. -spec compile(FileOrTemplate, atom()) -> {ok, Module::atom()} | error
  118. when FileOrTemplate :: string() | binary()
  119. | {file, filename()}
  120. | {template, iodata()}
  121. | {dir, filename()}.
  122. compile({file, File}, Module) ->
  123. compile_file(File, Module);
  124. compile({template, Template}, Module) ->
  125. compile_template(Template, Module);
  126. compile({dir, Directory}, Module) ->
  127. compile_dir(Directory, Module);
  128. compile(FileOrTemplate, Module) when is_binary(FileOrTemplate) ->
  129. compile_template(FileOrTemplate, Module);
  130. compile(FileOrTemplate, Module) ->
  131. compile_file(FileOrTemplate, Module).
  132. -spec compile(FileOrTemplate, atom(), compile_options() ) -> ok_ret() | err_ret()
  133. when FileOrTemplate :: string() | binary()
  134. | {file, filename()}
  135. | {template, iodata()}
  136. | {dir, filename()}.
  137. compile({file, File}, Module, Options) ->
  138. compile_file(File, Module, Options);
  139. compile({template, Template}, Module, Options) ->
  140. compile_template(Template, Module, Options);
  141. compile({dir, Directory}, Module, Options) ->
  142. compile_dir(Directory, Module, Options);
  143. compile(FileOrTemplate, Module, Options) when is_binary(FileOrTemplate) ->
  144. compile_template(FileOrTemplate, Module, Options);
  145. compile(FileOrTemplate, Module, Options) ->
  146. compile_file(FileOrTemplate, Module, Options).