From 5a377a73ec2c750393b81fcf66b25878f946dac6 Mon Sep 17 00:00:00 2001 From: Takuya Shiozaki Date: Tue, 18 Feb 2020 15:05:48 +0900 Subject: [PATCH] add encoding support for template. --- src/rebar_templater.erl | 16 +++++++-- test/rebar_templater_SUITE.erl | 63 ++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 test/rebar_templater_SUITE.erl diff --git a/src/rebar_templater.erl b/src/rebar_templater.erl index 7266f44e..15bc6b09 100644 --- a/src/rebar_templater.erl +++ b/src/rebar_templater.erl @@ -29,6 +29,9 @@ -export([new/4, list_templates/1, render/2]). +-ifdef(TEST). +-export([consult_template/3]). +-endif. -include("rebar.hrl"). @@ -59,7 +62,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 +161,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 +442,12 @@ 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), + Encoding = + case epp:read_encoding_from_binary(TemplateBin) of + none -> epp:default_encoding(); + X -> X + end, + rebar_string:consult(unicode:characters_to_list(TemplateBin, Encoding)). diff --git a/test/rebar_templater_SUITE.erl b/test/rebar_templater_SUITE.erl new file mode 100644 index 00000000..be78e899 --- /dev/null +++ b/test/rebar_templater_SUITE.erl @@ -0,0 +1,63 @@ +%% coding:utf-8 + +-module(rebar_templater_SUITE). +-compile(export_all). + +-include_lib("common_test/include/ct.hrl"). +-include_lib("eunit/include/eunit.hrl"). + +all() -> + [ + consult_template_latin1_test, + consult_template_utf8_test + ]. + +init_per_suite(Config) -> Config. +end_per_suite(_Config) -> ok. + +init_per_testcase(Case, Config) + when Case =:= consult_template_latin1_test; + Case =:= consult_template_utf8_test -> + %% Generate UCS string containing all printable characters of latin-1 area. + Description = lists:seq(16#A1, 16#AC) ++ lists:seq(16#AE, 16#FE), + Expected = [{description, Description}], + SampleTemplate = "{description, \"" ++ Description ++ "\"}.\n", + Path = generate_sample_template_file(Case, SampleTemplate, Config), + [{template_file_path, Path}, {expected, Expected} | Config]; +init_per_testcase(_Case, Config) -> Config. + +end_per_testcase(_Case, _Config) -> ok. + +generate_sample_template_file(Case, Content, Config) -> + CaseName = atom_to_list(Case), + {Encoding, EncodingName} = + case string:str(CaseName, "latin1") of + 0 -> {utf8, "utf-8"}; + _ -> {latin1, "latin-1"} + end, + PrivDir = ?config(priv_dir, Config), + Path = filename:join([PrivDir, CaseName ++ ".template"]), + {ok, FH} = file:open(Path, [write, {encoding, Encoding}]), + try + io:format(FH, "%% coding:~s~n~s", [EncodingName, Content]) + after + file:close(FH) + end, + Path. + +consult_template_test_common(Config) -> + Expected = ?config(expected, Config), + Path = ?config(template_file_path, Config), + Result = rebar_templater:consult_template([], file, Path), + ?assertEqual(Expected, Result), + ok. + +consult_template_latin1_test() -> + [{doc, "parse test for latin1 template file"}]. +consult_template_latin1_test(Config) -> + consult_template_test_common(Config). + +consult_template_utf8_test() -> + [{doc, "parse test for utf8 template file"}]. +consult_template_utf8_test(Config) -> + consult_template_test_common(Config).