浏览代码

add encoding support for template.

pull/2233/head
Takuya Shiozaki 5 年前
父节点
当前提交
025a96b143
共有 3 个文件被更改,包括 18 次插入6 次删除
  1. +1
    -1
      src/rebar_prv_shell.erl
  2. +11
    -3
      src/rebar_string.erl
  3. +6
    -2
      src/rebar_templater.erl

+ 1
- 1
src/rebar_prv_shell.erl 查看文件

@ -571,7 +571,7 @@ consult_env_config(State, Filename) ->
{ok, Bin} -> unicode:characters_to_list(Bin)
end,
ReplacedStr = replace_env_vars(RawString),
case rebar_string:consult(unicode:characters_to_list(ReplacedStr)) of
case rebar_string:consult(ReplacedStr, utf8) of
{error, Reason} ->
throw(?PRV_ERROR({bad_term_file, Filename, Reason}));
[Terms] ->

+ 11
- 3
src/rebar_string.erl 查看文件

@ -6,7 +6,7 @@
%% Compatibility exports
-export([join/2, split/2, lexemes/2, trim/1, trim/3, uppercase/1, lowercase/1, chr/2]).
%% Util exports
-export([consult/1]).
-export([consult/1, consult/2]).
-ifdef(unicode_str).
@ -55,10 +55,18 @@ chr(Str, Char) -> string:chr(Str, Char).
%% @doc
%% Given a string or binary, parse it into a list of terms, ala file:consult/1
-spec consult(unicode:chardata()) -> {error, term()} | [term()].
consult(Str) ->
-spec consult(unicode:chardata(), epp:source_encoding() | none) -> {error, term()} | [term()].
consult(Str, none) ->
consult(Str, epp:default_encoding());
consult(Bin, latin1) when is_binary(Bin) ->
consult([], binary_to_list(Bin), []);
consult(Str, _) ->
consult([], unicode:characters_to_list(Str), []).
%% Backward compatibility for plugins.
-spec consult(unicode:chardata()) -> {error, term()} | [term()].
consult(Str) -> consult(Str, utf8).
consult(Cont, Str, Acc) ->
case erl_scan:tokens(Cont, Str, 0) of
{done, Result, Remaining} ->

+ 6
- 2
src/rebar_templater.erl 查看文件

@ -59,7 +59,7 @@ list_templates(State) ->
%% Expand a single template's value
list_template(Files, {Name, Type, File}, State) ->
case rebar_string:consult(binary_to_list(load_file(Files, Type, File))) of
case consult_template(Files, Type, File) of
{error, Reason} ->
{error, {consult, File, Reason}};
TemplateTerms ->
@ -158,7 +158,7 @@ drop_var_docs([{K,V}|Rest]) -> [{K,V} | drop_var_docs(Rest)].
%% Load the template index, resolve all variables, and then execute
%% the template.
create({Template, Type, File}, Files, UserVars, Force, State) ->
TemplateTerms = rebar_string:consult(binary_to_list(load_file(Files, Type, File))),
TemplateTerms = consult_template(Files, Type, File),
Vars = drop_var_docs(override_vars(UserVars, get_template_vars(TemplateTerms, State))),
maybe_warn_about_name(Vars),
TemplateCwd = filename:dirname(File),
@ -439,3 +439,7 @@ render(Bin, Context) ->
[{key_type, atom},
{escape_fun, fun(X) -> X end}] % disable HTML-style escaping
).
consult_template(Files, Type, File) ->
TemplateBin = load_file(Files, Type, File),
rebar_string:consult(TemplateBin, epp:read_encoding_from_binary(TemplateBin)).

正在加载...
取消
保存