Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
out(Arg) ->
% change directory to the document root of the webserver
% 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
"<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"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.