1-module(wiki_to_html).
2
3%% File    : wiki_to_html.erl
4%% Author  : Joe Armstrong (joe@bluetail.com)
5%%         : Johan Bevemyr, minor modifications (jb@bevemyr.com)
6%%         : Mickael Remond (mickael.remond@erlang-fr.org)
7%% Purpose : Convert wiki page tree to HTML
8%%
9%% $Id$
10
11-export([format_wiki/3,format_wiki/4, format_link/2, format_wiki_files/4,
12        format_wiki_files/5, format_menu_link/3]).
13
14-include_lib("kernel/include/file.hrl").
15
16-import(lists, [member/2, map/2]).
17
18format_wiki_files(_Page, _FileDir, [], _Root) -> [];
19format_wiki_files(Page, FileDir, Files, Root) ->
20    format_wiki_files(Page, FileDir, Files, Root, "Attached files:").
21
22format_wiki_files(Page, FileDir, Files, Root, Heading) ->
23    LinkFun = fun(I) -> format_link(I, FileDir, Page, Root, show) end,
24    ("<hr><b><p>" ++ yaws_api:htmlize(Heading) ++ "</b><br>\n"
25     "<table cellspacing=10 width = \"100%\">\n"
26     ++ lists:map(LinkFun, lists:keysort(2,Files)) ++
27     "</table></p>\n").
28
29format_wiki(Page, Wik, Root) ->
30    LinkFun = fun(I) -> format_link(I, Page, Root, show) end,
31    pp(Wik, LinkFun, Page, Root).
32
33format_wiki(Page, Wik, Root, preview) ->
34    LinkFun = fun(I) -> format_link(I, Page, Root, preview) end,
35    pp(Wik, LinkFun, Page, Root).
36
37format_link(Page, Root) ->
38    format_link({wikiLink, Page}, [], Root, show).
39
40
41%% TODO: Refactor that: The use of the page is ugly:
42%% Most of the time the Page is in the second parameter,
43%% but in the wikiLink it is the second element of the first parameter tuple
44format_link({wikiLink, Page}, _, Root, _Mode) ->
45    %% MR: I first need to extract the code here into a separate function:
46    wiki_link(Page, Page, Root);
47format_link({editTag, Tag}, Page, _Root, show) ->
48    ["<a href=\"editTag.yaws?node=",wiki:str2urlencoded(Page),
49     "&tag=",i2s(Tag),"\">",
50     "<img border=0 src='WikiPreferences.files/edit.gif'></a> "];
51format_link({editTag, _Tag}, _Page, _Root, preview) ->
52    ["<img border=0 src='WikiPreferences.files/edit.gif'>"].
53
54format_link({file, FileName, C}, FileDir, Page, Root, Mode) ->
55    format_link({file, FileName, "", C}, FileDir, Page, Root, Mode);
56
57format_link({file, FileName, Description, _}, FileDir, _Page, Root,_) ->
58    Size = get_filesize(filename:join([Root,FileDir,FileName])),
59    ["<tr><td valign=top align=left><a href=\"",
60     wiki:str2urlencoded(FileDir),
61     "/", wiki:str2urlencoded(FileName),"\" title='",Size,"'>",
62     yaws_api:htmlize(FileName),
63     "</a></td><td align=left valign=top>",
64     yaws_api:htmlize(Description), "</td></tr>\n"].
65
66wiki_link(LinkName, Page, Root) ->
67    FullName = Root ++ "/" ++ Page ++ ".wob",
68    case is_file(FullName) of
69        true ->
70            ["<a href=\"showPage.yaws?node=",
71             wiki:str2urlencoded(Page),"\">",yaws_api:htmlize(LinkName),"</a> "];
72        false ->
73            [" ",yaws_api:htmlize(Page),"<a href=\"createNewPage.yaws?node=",
74             wiki:str2urlencoded(Page),"\">???</a>"]
75    end.
76
77%% Same as format_link, but drop the prefix
78%% This is used to create the Wiki menu
79format_menu_link(Prefix, Page, Root) ->
80    Prefix_length = length(Prefix),
81    LinkName = case Prefix_length < length(Page) of
82                   true  -> string:substr(Page, Prefix_length + 1);
83                   false -> Page
84               end,
85    wiki_link(LinkName, Page, Root).
86
87
88get_filesize(File) ->
89    case file:read_file_info(File) of
90        {ok, FileInfo} ->
91            Size = FileInfo#file_info.size/1024,
92            io_lib:format("~.1fKB",[Size]);
93        _ -> "unknown"
94    end.
95
96
97i2s(X) ->
98    integer_to_list(X).
99
100pp({wik,L}, F, Node, Root) ->
101    map(fun(I) -> pp(I, F, Node, Root) end, L);
102pp({txt,_,Str}, F, Node, _Root) ->
103    wiki_format_txt:format(Str, F, Node);
104pp({open,Tag,Str}, F, Node, Root) ->
105    open("#CCFFCC",Tag,F,pp({txt,9999,Str}, F, Node, Root));
106pp({write_append,Tag,Str}, F, Node, Root) ->
107    open("#99FFFF",Tag,F,pp({txt,8888,Str}, F, Node, Root));
108pp(Other, _F, _Node, Root) ->
109    wiki:show({cannot,format,Other}, Root).
110
111open(Color, Tag, F, Stuff) ->
112    ["\n<table width=\"90%\" cellpadding=20>\n<tr><td bgcolor=\"",
113     Color, "\">\n", Stuff,
114     "<p>",F({editTag,Tag}),"</td></tr></table><p>\n"].
115
116is_file(File) ->
117    case file:read_file_info(File) of
118        {ok, _} ->
119            true;
120        _ ->
121            false
122    end.
123