adding all of botlist, initial add
[botlist.git] / botlistprojects / botsocial / lib / erlang / www_tools / html_expand.erl
blob4fde6aab00f25f44dad71a8eb1bb04f471e08e6c
1 -module(html_expand).
3 -export([dir/0, file/1]).
5 -import(lists, [foreach/2, reverse/1]).
7 %% try the command html
9 dir() ->
10 foreach(fun file/1, find:out_of_date(".", ".mhtml", ".html")).
12 file(File) ->
13 io:format("expanding:~s.mhtml~n", [File]),
14 Toks = html_tokenise:file2toks(File ++ ".mhtml"),
15 {Mod, Toks1} = get_macro_name(Toks),
16 Toks2 = expand(Toks1, Mod),
17 %% io:format("Toks2:~p~n", [Toks2]),
18 html_tokenise:toks2file(Toks2, File).
20 expand([{tagStart,[$$|Start]}|Toks], Mod) ->
21 {Args, Toks1} = collect_args([$$|Start], Toks, Mod),
22 Toks2 = apply_macro(Mod, Start, Args),
23 expand(Toks2 ++ Toks1, Mod);
24 expand([H|T], Mod) ->
25 [H|expand(T, Mod)];
26 expand([], _) ->
27 [].
29 collect_args(Stop, Toks, Mod) ->
30 collect_args(Stop, Toks, Mod, [], []).
32 collect_args(Stop, [{tagEnd, Stop}|Toks], Mod, C, L) ->
33 {reverse([reverse(C)|L]), Toks};
34 collect_args(Stop, [{tagStart, [$$|Start]}|Toks], Mod, C, L) ->
35 {Args, Toks1} = collect_args([$$|Start], Toks, Mod),
36 Toks2 = apply_macro(Mod, Start, Args),
37 collect_args(Stop, Toks2, Mod, C, L);
38 collect_args(Stop, [{tagStart, "<>"}|Toks], Mod, C, L) ->
39 collect_args(Stop, Toks, Mod, [], [reverse(C)|L]);
40 collect_args(Stop, [H|T], Mod, C, L) ->
41 collect_args(Stop, T, Mod, [H|C], L);
42 collect_args(Stop, [], Mod, C, L) ->
43 {reverse([reverse(C)|L]), []}.
45 get_macro_name([{tagStart,"$macro"},{raw,Macro},{tagEnd,"$macro"}|Toks]) ->
46 {list_to_atom(Macro), Toks}.
48 apply_macro(Mod, Name, Args) ->
49 %% io:format("Here: ~p ~p :: ~p~n", [Mod, Name, Args]),
50 Func = list_to_atom(Name),
51 String = case catch apply(Mod, Func, Args) of
52 {'EXIT', Why} ->
53 io:format("Bad macro call:~p~n", [{Mod,Func,Args}]),
54 "** error **";
55 Str ->
56 Str
57 end,
58 html_tokenise:string2toks(String).