Skip to content

Instantly share code, notes, and snippets.

@TimP69
Last active December 19, 2015 04:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TimP69/5894919 to your computer and use it in GitHub Desktop.
Save TimP69/5894919 to your computer and use it in GitHub Desktop.
<erl>
out(Arg) ->
% change directory to the document root of the webserver
c:cd(Arg#arg.docroot),
% determining YAWS' current working directory
% usually where you were when you typed sudo yaws
% not normally the same as the document root
CurrentDir = element(2,file:get_cwd()),
% pull up a directory listing
DirList = element(2,file:list_dir(CurrentDir)),
% define a function for our filter
IsDir = fun(F) -> filelib:is_dir(F) end,
% filter out all the directories
AllDirs = lists:filter(IsDir,DirList),
% make directories into links using a mapping function
ConvertToLink = fun(D) ->
"<a href=\"" ++ D ++ "\">" ++ D ++ "</a>" end,
LinkedDirs = lists:map(ConvertToLink,AllDirs),
% define a filter function and use it to
% filter out all the regular files
IsReg = fun(F) -> filelib:is_regular(F) end,
AllRegs = lists:filter(IsReg,DirList),
% HTML-ify the two lists ready for output
DirBlock = string:join(LinkedDirs,"</li>\n<li>"),
RegBlock = string:join(AllRegs,"</li>\n<li>"),
% send output back to the browser
{html,
"<h1>" ++ CurrentDir ++ "</h1>\n" ++
"<h2>Directories</h2>\n" ++
"<ul>\n<li>" ++ DirBlock ++ "</li>\n</ul>\n" ++
"<h2>Regular Files</h2>\n" ++
"<ul>\n<li>" ++ RegBlock ++ "</li>\n</ul>\n"
}.
</erl>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment