Просмотр исходного кода

Add possibility to specify custom ErlyDTL options

Krzysztof Jurewicz 9 лет назад
Родитель
Сommit
6c4a17ee75
3 измененных файлов с 44 добавлено и 2 удалено
  1. 4 0
      README.legacy.md
  2. 2 1
      plugins/erlydtl.mk
  3. 38 1
      test/plugin_erlydtl.mk

+ 4 - 0
README.legacy.md

@@ -74,6 +74,10 @@ subdirectories names in the compiled module name add
 `DTL_FULL_PATH=1` into your Makefile - `a/b/templatename.dtl`
 will be compiled into `a_b_templatename_dtl.beam`.
 
+Additional ErlyDTL options can be specified as a comma-separated list
+by defining the `DTL_OPTS` variable. Those options will be prepended
+to the options specified by the plugin itself.
+
 Escript plugin
 --------------
 

+ 2 - 1
plugins/erlydtl.mk

@@ -6,6 +6,7 @@
 DTL_FULL_PATH ?=
 DTL_PATH ?= templates/
 DTL_SUFFIX ?= _dtl
+DTL_OPTS ?=
 
 # Verbosity.
 
@@ -38,7 +39,7 @@ define erlydtl_compile.erl
 				re:replace(F2, "/",  "_",  [{return, list}, global])
 		end,
 		Module = list_to_atom(string:to_lower(Module0) ++ "$(DTL_SUFFIX)"),
-		case erlydtl:compile(F, Module, [{out_dir, "ebin/"}, return_errors, {doc_root, "templates"}]) of
+		case erlydtl:compile(F, Module, [$(DTL_OPTS)] ++ [{out_dir, "ebin/"}, return_errors, {doc_root, "templates"}]) of
 			ok -> ok;
 			{ok, _} -> ok
 		end

+ 38 - 1
test/plugin_erlydtl.mk

@@ -1,6 +1,6 @@
 # ErlyDTL plugin.
 
-ERLYDTL_CASES = compile full-path
+ERLYDTL_CASES = compile full-path opts
 ERLYDTL_TARGETS = $(addprefix erlydtl-,$(ERLYDTL_CASES))
 
 .PHONY: erlydtl $(ERLYDTL_TARGETS)
@@ -62,3 +62,40 @@ erlydtl-full-path: build clean
 		ok = application:load($(APP)), \
 		{ok, [deep_$(APP)_two_dtl, $(APP_)_one_dtl]} = application:get_key($(APP), modules), \
 		halt()"
+
+erlydtl-opts: build clean
+
+	$i "Bootstrap a new OTP library named $(APP)"
+	$t mkdir $(APP)/
+	$t cp ../erlang.mk $(APP)/
+	$t $(MAKE) -C $(APP) -f erlang.mk bootstrap-lib $v
+
+	$i "Add ErlyDTL to the list of dependencies"
+	$t perl -ni.bak -e 'print;if ($$.==1) {print "DEPS = erlydtl\n"}' $(APP)/Makefile
+
+	$i "Generate ErlyDTL template"
+	$t mkdir $(APP)/templates/
+	$t echo "{{ foo }}" > $(APP)/templates/$(APP)_foo.dtl
+
+	$i "Build the application with auto escape turned on"
+	$t $(MAKE) -C $(APP) DTL_OPTS=auto_escape $v
+
+	$i "Check that HTML is escaped"
+	$t $(ERL) -pa $(APP)/ebin/ $(APP)/deps/erlydtl/ebin/ -eval " \
+		ok = application:load($(APP)), \
+		{ok, Result} = $(APP)_foo_dtl:render([{foo, <<\"<&>\">>}]), \
+		<<\"&lt;&amp;&gt;\", _/binary>> = iolist_to_binary(Result), \
+		halt()"
+
+	$i "Clean the application"
+	$t $(MAKE) -C $(APP) clean $v
+
+	$i "Build the application with auto escape turned off"
+	$t $(MAKE) -C $(APP) "DTL_OPTS={auto_escape, false}" $v
+
+	$i "Check that HTML is not escaped"
+	$t $(ERL) -pa $(APP)/ebin/ $(APP)/deps/erlydtl/ebin/ -eval " \
+		ok = application:load($(APP)), \
+		{ok, Result} = $(APP)_foo_dtl:render([{foo, <<\"<&>\">>}]), \
+		<<\"<&>\", _/binary>> = iolist_to_binary(Result), \
+		halt()"