erlydtl.erl 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. | {record_info, [{Name::atom(), [Field::atom()]}]}
  83. | {scanner_module, Module::atom()}
  84. | {vars, [{atom(), iodata()}]}.
  85. %% --------------------------------------------------------------------
  86. %% Compile file
  87. %% --------------------------------------------------------------------
  88. -spec compile_file(filename(), atom()) -> {ok, Module::atom()} | error.
  89. compile_file(File, Module) ->
  90. erlydtl_compiler:compile_file(File, Module, erlydtl_compiler:default_options()).
  91. -spec compile_file(filename(), atom(), compile_options()) -> ok_ret() | err_ret().
  92. compile_file(File, Module, Options) ->
  93. erlydtl_compiler:compile_file(File, Module, Options).
  94. %% --------------------------------------------------------------------
  95. %% Compile template
  96. %% --------------------------------------------------------------------
  97. -spec compile_template(iodata(), atom()) -> {ok, Module::atom()} | error.
  98. compile_template(Template, Module) ->
  99. erlydtl_compiler:compile_template(Template, Module, erlydtl_compiler:default_options()).
  100. -spec compile_template(iodata(), atom(), compile_options()) -> ok_ret() | err_ret().
  101. compile_template(Template, Module, Options) ->
  102. erlydtl_compiler:compile_template(Template, Module, Options).
  103. %% --------------------------------------------------------------------
  104. %% Compile directory
  105. %% --------------------------------------------------------------------
  106. -spec compile_dir(filename(), atom()) -> {ok, Module::atom()} | error.
  107. compile_dir(DirectoryPath, Module) ->
  108. erlydtl_compiler:compile_dir(DirectoryPath, Module, erlydtl_compiler:default_options()).
  109. -spec compile_dir(filename(), atom(), compile_options()) -> ok_ret() | err_ret().
  110. compile_dir(DirectoryPath, Module, Options) ->
  111. erlydtl_compiler:compile_dir(DirectoryPath, Module, Options).
  112. %% --------------------------------------------------------------------
  113. %% Legacy API
  114. %% --------------------------------------------------------------------
  115. %% keep for backwards compatibility, with a tuple-twist to ease migration / offer alternative path..
  116. -spec compile(FileOrTemplate, atom()) -> {ok, Module::atom()} | error
  117. when FileOrTemplate :: string() | binary()
  118. | {file, filename()}
  119. | {template, iodata()}
  120. | {dir, filename()}.
  121. compile({file, File}, Module) ->
  122. compile_file(File, Module);
  123. compile({template, Template}, Module) ->
  124. compile_template(Template, Module);
  125. compile({dir, Directory}, Module) ->
  126. compile_dir(Directory, Module);
  127. compile(FileOrTemplate, Module) when is_binary(FileOrTemplate) ->
  128. compile_template(FileOrTemplate, Module);
  129. compile(FileOrTemplate, Module) ->
  130. compile_file(FileOrTemplate, Module).
  131. -spec compile(FileOrTemplate, atom(), compile_options() ) -> ok_ret() | err_ret()
  132. when FileOrTemplate :: string() | binary()
  133. | {file, filename()}
  134. | {template, iodata()}
  135. | {dir, filename()}.
  136. compile({file, File}, Module, Options) ->
  137. compile_file(File, Module, Options);
  138. compile({template, Template}, Module, Options) ->
  139. compile_template(Template, Module, Options);
  140. compile({dir, Directory}, Module, Options) ->
  141. compile_dir(Directory, Module, Options);
  142. compile(FileOrTemplate, Module, Options) when is_binary(FileOrTemplate) ->
  143. compile_template(FileOrTemplate, Module, Options);
  144. compile(FileOrTemplate, Module, Options) ->
  145. compile_file(FileOrTemplate, Module, Options).