123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- -module(mad_dtl).
- %%-copyright('Sina Samavati').
- -compile([export_all, nowarn_export_all]).
- compile(Dir,Config) ->
- case mad_utils:get_value(erlydtl_opts, Config, []) of
- [] -> false;
- X -> compile_erlydtl_files(validate_erlydtl_opts(Dir,X)) end.
- get_kv(K, Opts, Default) ->
- V = mad_utils:get_value(K, Opts, Default),
- KV = {K, V},
- {KV, Opts -- [KV]}.
- file_to_beam(Bin, Filename) -> filename:join(Bin, filename:basename(Filename) ++ ".beam").
- validate_erlydtl_opts(Cwd, Opts) ->
- DefaultDocRoot = filename:join("priv", "templates"),
- {DocRoot, Opts1} = get_kv(doc_root, Opts, DefaultDocRoot),
- {OutDir, Opts2} = get_kv(out_dir, Opts1, "ebin"),
- {CompilerOpts, Opts3} = get_kv(compiler_options, Opts2, []),
- {SourceExt, Opts4} = get_kv(source_ext, Opts3, ".dtl"),
- {ModuleExt, Opts5} = get_kv(module_ext, Opts4, ""),
- {_, DocRootDir} = DocRoot,
- DocRoot1 = {doc_root, filename:join(Cwd, DocRootDir)},
- {_, OutDir1} = OutDir,
- OutDir2 = {out_dir, filename:join(Cwd, OutDir1)},
- Auto_Escape = case application:get_env(mad, compile_dtl_escape, false) of
- true -> {auto_escape, true};
- _ -> {auto_escape, false}
- end,
- [DocRoot1, OutDir2, CompilerOpts, Auto_Escape, SourceExt, ModuleExt|Opts5].
- module_name(File, Ext, NewExt) ->
- list_to_atom(filename:basename(File, Ext) ++ NewExt).
- compile_erlydtl_files(Opts) ->
- {{_, DocRoot}, Opts1} = get_kv(doc_root, Opts, ""),
- {{_, SourceExt}, Opts2} = get_kv(source_ext, Opts1, ""),
- {{_, ModuleExt}, Opts3} = get_kv(module_ext, Opts2, ""),
- {{_, OutDir}, _} = get_kv(out_dir, Opts3, ""),
- Files = filelib:fold_files(DocRoot, SourceExt, true,
- fun(F, Acc) -> [F|Acc] end, []),
- Compile = fun(F) ->
- ModuleName = module_name(F, SourceExt, ModuleExt),
- BeamFile = file_to_beam(OutDir, atom_to_list(ModuleName)),
- Compiled = mad_compile:is_compiled(BeamFile, F),
- case Compiled of false ->
- mad:info("DTL Compiling ~s~n", [F -- mad_utils:cwd()]),
- Res = erlydtl:compile(F, ModuleName, Opts3),
- file:change_time(BeamFile, calendar:local_time()),
- case Res of {error,Error} -> mad:info("Error: ~p~n",[Error]);
- OK -> OK end;
- true -> ok end
- end,
- lists:any(fun({error,_}) -> true;
- ({error,_,_}) -> true;
- ({ok, _}) -> false;
- (ok) -> false
- end, [Compile(F) || F <- Files]).
|