-module(wiki_to_html).
%% File : wiki_to_html.erl
%% Author : Joe Armstrong (joe@bluetail.com)
%% : Johan Bevemyr, minor modifications (jb@bevemyr.com)
%% : Mickael Remond (mickael.remond@erlang-fr.org)
%% Purpose : Convert wiki page tree to HTML
%%
%% $Id: wiki_to_html.erl 838 2005-02-17 12:56:32Z jbevemyr $
-export([format_wiki/3,format_wiki/4, format_link/2, format_wiki_files/4,
format_wiki_files/5, format_menu_link/3]).
-include_lib("kernel/include/file.hrl").
-import(lists, [member/2, map/2]).
format_wiki_files(Page, FileDir, [], Root) -> [];
format_wiki_files(Page, FileDir, Files, Root) ->
format_wiki_files(Page, FileDir, Files, Root, "Attached files:").
format_wiki_files(Page, FileDir, Files, Root, Heading) ->
LinkFun = fun(I) -> format_link(I, FileDir, Page, Root, show) end,
("
" ++ Heading ++ "
\n"
"
\n"
++ lists:map(LinkFun, lists:keysort(2,Files)) ++
"
\n").
format_wiki(Page, Wik, Root) ->
LinkFun = fun(I) -> format_link(I, Page, Root, show) end,
pp(Wik, LinkFun, Page, Root).
format_wiki(Page, Wik, Root, preview) ->
LinkFun = fun(I) -> format_link(I, Page, Root, preview) end,
pp(Wik, LinkFun, Page, Root).
format_link(Page, Root) ->
format_link({wikiLink, Page}, [], Root, show).
%% TODO: Refactor that: The use of the page is ugly:
%% Most of the time the Page is in the second parameter,
%% but in the wikiLink it is the second element of the first parameter tuple
format_link({wikiLink, Page}, _, Root, _Mode) ->
%% MR: I first need to extract the code here into a separate function:
wiki_link(Page, Page, Root);
format_link({editTag, Tag}, Page, Root, show) ->
["",
"
"];
format_link({editTag, Tag}, _Page, _Root, preview) ->
["
"].
format_link({file, FileName, C}, FileDir, Page, Root, Mode) ->
format_link({file, FileName, "", C}, FileDir, Page, Root, Mode);
format_link({file, FileName, Description, _}, FileDir, Page, Root,_) ->
Size = get_filesize(filename:join([Root,FileDir,FileName])),
["| ",
FileName,
" | ",
Description, " |
\n"].
wiki_link(LinkName, Page, Root) ->
FullName = Root ++ "/" ++ Page ++ ".wob",
case is_file(FullName) of
true ->
["",LinkName," "];
false ->
[" ",Page,"???"]
end.
%% Same as format_link, but drop the prefix
%% This is used to create the Wiki menu
format_menu_link(Prefix, Page, Root) ->
Prefix_length = length(Prefix),
LinkName = case Prefix_length < length(Page) of
true -> string:substr(Page, Prefix_length + 1);
false -> Page
end,
wiki_link(LinkName, Page, Root).
get_filesize(File) ->
case file:read_file_info(File) of
{ok, FileInfo} ->
Size = FileInfo#file_info.size/1024,
io_lib:format("~.1fKB",[Size]);
_ -> "unknown"
end.
i2s(X) ->
integer_to_list(X).
pp({wik,L}, F, Node, Root) ->
map(fun(I) -> pp(I, F, Node, Root) end, L);
pp({txt,_,Str}, F, Node, Root) ->
wiki_format_txt:format(Str, F, Node);
pp({open,Tag,Str}, F, Node, Root) ->
open("#CCFFCC",Tag,F,pp({txt,9999,Str}, F, Node, Root));
pp({write_append,Tag,Str}, F, Node, Root) ->
open("#99FFFF",Tag,F,pp({txt,8888,Str}, F, Node, Root));
pp(Other, F, Node, Root) ->
wiki:show({cannot,format,Other}, Root).
open(Color, Tag, F, Stuff) ->
["\n\n| \n", Stuff,
" ",F({editTag,Tag})," |
\n"].
is_file(File) ->
case file:read_file_info(File) of
{ok, _} ->
true;
_ ->
false
end.