2013-02-09 04:04:49 -06:00
|
|
|
%% Feel free to use, reuse and abuse the code in this file.
|
|
|
|
|
|
|
|
-module(markdown_converter).
|
|
|
|
-behaviour(cowboy_middleware).
|
|
|
|
|
|
|
|
-export([execute/2]).
|
|
|
|
|
|
|
|
execute(Req, Env) ->
|
2014-09-23 16:43:29 +03:00
|
|
|
[Path] = cowboy_req:path_info(Req),
|
2013-02-09 04:04:49 -06:00
|
|
|
case filename:extension(Path) of
|
|
|
|
<<".html">> -> maybe_generate_markdown(resource_path(Path));
|
|
|
|
_Ext -> ok
|
|
|
|
end,
|
2014-09-23 16:43:29 +03:00
|
|
|
{ok, Req, Env}.
|
2013-02-09 04:04:49 -06:00
|
|
|
|
|
|
|
maybe_generate_markdown(Path) ->
|
|
|
|
ModifiedAt = filelib:last_modified(source_path(Path)),
|
|
|
|
GeneratedAt = filelib:last_modified(Path),
|
|
|
|
case ModifiedAt > GeneratedAt of
|
2024-11-07 13:28:26 +01:00
|
|
|
true -> markdown:conv_file(source_path(Path), Path);
|
2013-02-09 04:04:49 -06:00
|
|
|
false -> ok
|
|
|
|
end.
|
|
|
|
|
|
|
|
resource_path(Path) ->
|
2013-10-22 09:10:24 +02:00
|
|
|
PrivDir = code:priv_dir(markdown_middleware),
|
|
|
|
filename:join([PrivDir, Path]).
|
2013-02-09 04:04:49 -06:00
|
|
|
|
|
|
|
source_path(Path) ->
|
|
|
|
<< (filename:rootname(Path))/binary, ".md" >>.
|