1
%%% The contents of this file are subject to the Erlang Public License,
2
%%% Version 1.0, (the "License"); you may not use this file except in
3
%%% compliance with the License. You may obtain a copy of the License at
4
%%% http://www.erlang.org/license/EPL1_0.txt
6
%%% Software distributed under the License is distributed on an "AS IS"
7
%%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
8
%%% the License for the specific language governing rights and limitations
11
%%% The Original Code is xmerl-0.7
13
%%% The Initial Developer of the Original Code is Ericsson Telecom
14
%%% AB. Portions created by Ericsson are Copyright (C), 1998, Ericsson
15
%%% Telecom AB. All Rights Reserved.
17
%%% Contributor(s): ______________________________________.
19
%%%----------------------------------------------------------------------
20
%%% #0. BASIC INFORMATION
21
%%%----------------------------------------------------------------------
22
%%% File: test_html.erl
23
%%% Author : Ulf Wiger <ulf.wiger@ericsson.com>
25
%%% Description : Callback module for exporting XML to HTML with support
26
%%% for special Erlang-related tags. (Experimental)
28
%%% Modules used : lists, io_lib
30
%%%----------------------------------------------------------------------
33
-author('ulf.wiger@ericsson.com').
36
-export(['#xml-inheritance#'/0]).
38
%%% special Erlang forms
53
-include("xmerl.hrl").
56
'#xml-inheritance#'() -> [xmerl_xml].
60
%% The '#root#' tag is called when the entire structure has been exported.
61
%% It does not appear in the structure itself.
62
'#root#'(Data, Attrs, [], E) ->
64
case find_attribute(title, Attrs) of
66
["<title>", T, "</title>"];
70
["<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML//EN\">\n"
72
"<head>\n", Title, "</head>\n"
73
"<body>\n", Data, "</body>\n"].
78
%%% Special token: EXIT
79
'EXIT'(Reason, Attrs = [], Parents = [], E) ->
80
%% This happens e.g. if a request function crashes completely.
81
["<pre>\n", mk_string({'EXIT', Reason}), "</pre>"].
84
title(Str, Attrs, Parents, E) ->
85
["<h1>", Str, "</h1>\n"].
88
%%% section/3 is to be used instead of headings.
89
section(Data, Attrs, [{section,_}, {section,_}, {section,_} | _], E) ->
90
opt_heading(Attrs, "<h4>", "</h4>", Data);
91
section(Data, Attrs, [{section,_}, {section,_} | _], E) ->
92
opt_heading(Attrs, "<h3>", "</h3>", Data);
93
section(Data, Attrs, [{section,_} | _], E) ->
94
opt_heading(Attrs, "<h2>", "</h2>", Data);
95
section(Data, Attrs, Parents, E) ->
96
opt_heading(Attrs, "<h1>", "</h1>", Data).
98
opt_heading(Attrs, StartTag, EndTag, Data) ->
99
case find_attribute(heading, Attrs) of
101
[StartTag, Text, EndTag, "\n" | Data];
108
%% e.g. {table, [{heading, [{col, H1}, {col, H2}]},
109
%% {row, [{col, C11}, {col, C12}]},
110
%% {row, [{col, C21}, {col, C22}]}]}.
112
table(Data, Attrs, Parents, E) ->
113
Border = case find_attribute(border, Attrs) of
117
[" border=", mk_string(N)]
119
["<table", Border, ">\n", Data, "\n</table>\n"].
121
row(Data, Attrs, [{table,_}|_], E) ->
122
["<tr>", Data, "</tr>\n"].
124
heading(Data, Attrs, [{table,_}|_], E) ->
125
["<tr>", Data, "</tr>\n"].
128
%% Context-sensitive columns (heading- or row columns)
129
col(Data, Attrs, [{heading,_}, {table,_} | _], E) ->
130
["<th>", nbsp_if_empty(Data), "</th>\n"];
131
col(Data, Attrs, [{row,_}, {table,_} | _], E) ->
132
["<td>", nbsp_if_empty(Data), "</td>\n"].
135
tuple_list(List, Attrs, Parents, E) ->
136
Elems = case find_attribute(elements, Attrs) of
142
lists:seq(1,size(H));
147
TableData = [{row, [{col, {element(P, Rec)}} || P <- Elems]} ||
149
Table = case find_attribute(heading, Attrs) of
151
Head = {heading, [{col, C} || C <- Cols]},
152
{table, [Head | TableData]};
156
{'#xml-redefine#', Table}.
159
data(Data, Pos, Attrs, Parents) ->
164
p(Data, Pos, Attrs, Parents) ->
165
{'#xml-alias#', 'P'}.
167
para(Data, Pos, Attrs, Parents) ->
168
{'#xml-alias#', 'P'}.
170
'P'(Data, Pos, Attrs, Parents) ->
171
["<p>", mk_string(Data), "</p>\n"].
174
emphasis(Str, Pos, Attrs, Parents) ->
175
["<strong>", Str, "</strong>"].
178
nbsp_if_empty(Data) when binary(Data), size(Data) == 0 ->
180
nbsp_if_empty(Data) when list(Data) ->
181
case catch list_to_binary(Data) of
183
nbsp_if_empty_term(Data);
184
B when size(B) == 0 ->
189
nbsp_if_empty(Data) ->
190
nbsp_if_empty_term(Data).
192
nbsp_if_empty_term(Data) ->
193
Str = io_lib:format("~p", [Data]),
194
case list_to_binary(Str) of
195
B when size(B) == 0 ->
202
mk_string(I) when integer(I) ->
204
mk_string(A) when atom(A) ->
206
mk_string(L) when list(L) ->
207
%% again, we can't regognize a string without "parsing" it
208
case catch list_to_binary(L) of
210
io_lib:format("~p", [L]);
215
io_lib:format("~p", [Term]).
219
find_attribute(Name, Attrs) ->
220
case lists:keysearch(Name, #xmlAttribute.name, Attrs) of
221
{value, #xmlAttribute{value = V}} ->