Debian Codegraph @

examples/markdown_middleware/src/markdown_converter.erl

raw
1 %% Feel free to use, reuse and abuse the code in this file.
2
3 -module(markdown_converter).
4 -behaviour(cowboy_middleware).
5
6 -export([execute/2]).
7
8 execute(Req, Env) ->
9 [Path] = cowboy_req:path_info(Req),
10 case filename:extension(Path) of
11 <<".html">> -> maybe_generate_markdown(resource_path(Path));
12 _Ext -> ok
13 end,
14 {ok, Req, Env}.
15
16 maybe_generate_markdown(Path) ->
17 ModifiedAt = filelib:last_modified(source_path(Path)),
18 GeneratedAt = filelib:last_modified(Path),
19 case ModifiedAt > GeneratedAt of
20 true -> markdown:conv_file(source_path(Path), Path);
21 false -> ok
22 end.
23
24 resource_path(Path) ->
25 PrivDir = code:priv_dir(markdown_middleware),
26 filename:join([PrivDir, Path]).
27
28 source_path(Path) ->
29 << (filename:rootname(Path))/binary, ".md" >>.