1%%
2%% %CopyrightBegin%
3%%
4%% Copyright Ericsson AB 2001-2016. All Rights Reserved.
5%%
6%% Licensed under the Apache License, Version 2.0 (the "License");
7%% you may not use this file except in compliance with the License.
8%% You may obtain a copy of the License at
9%%
10%%     http://www.apache.org/licenses/LICENSE-2.0
11%%
12%% Unless required by applicable law or agreed to in writing, software
13%% distributed under the License is distributed on an "AS IS" BASIS,
14%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15%% See the License for the specific language governing permissions and
16%% limitations under the License.
17%%
18%% %CopyrightEnd%
19%%
20-module(docgen_xmerl_xml_cb).
21
22%% This is the callback module for exporting XHTML to an
23%% erlref or chapter document in XML format.
24%% See docgen_edoc_xml_cb.erl for further information.
25%%
26%% The origin of this file is the xmerl module xmerl_otpsgml.erl
27%% written by Ulf Wiger and Richard Carlsson.
28
29-export(['#xml-inheritance#'/0]).
30
31-export(['#root#'/4,
32	 '#element#'/5,
33	 '#text#'/1]).
34
35-include("xmerl.hrl").
36
37'#xml-inheritance#'() ->
38    [xmerl_xml].
39
40'#root#'(Data, Attrs, [], _E) ->
41    Encoding =
42        case [E || #xmlAttribute{name = encoding, value = E} <- Attrs] of
43            [E] -> E;
44            _ -> atom_to_list(epp:default_encoding())
45        end,
46    ["<",DTD,">"] = hd(hd(Data)),
47    ["<?xml version=\"1.0\" encoding=\"",Encoding,"\" ?>\n",
48     "<!DOCTYPE "++DTD++" SYSTEM \""++DTD++".dtd\">\n",
49     Data].
50
51'#element#'(Tag, Data, Attrs, _Parents, _E) ->
52    {NewTag, NewAttrs} = convert_tag(Tag, Attrs),
53    xmerl_lib:markup(NewTag, NewAttrs, Data).
54
55'#text#'(Text) ->
56    xmerl_lib:export_text(Text).
57
58%% Utility functions
59
60convert_tag(a, [Attr]) ->
61    case Attr#xmlAttribute.name of
62	href ->
63	    Val = Attr#xmlAttribute.value,
64	    case is_url(Val) of
65		true ->
66		    {url, [Attr]};
67		false ->
68		    makesee(Val)
69	    end;
70	name ->
71	    {marker, [Attr#xmlAttribute{name=id}]}
72    end;
73convert_tag(b, Attrs)          -> {em, Attrs};
74convert_tag(blockquote, Attrs) -> {quote, Attrs};
75convert_tag(code, Attrs)       -> {c, Attrs};
76convert_tag(dd, Attrs)         -> {item, Attrs};
77convert_tag(dl, Attrs)         -> {taglist, Attrs};
78convert_tag(dt, Attrs)         -> {tag, Attrs};
79convert_tag(li, Attrs)         -> {item, Attrs};
80convert_tag(ol, Attrs)         -> {list, Attrs};
81convert_tag(strong, Attrs)     -> {em, Attrs};
82convert_tag(td, Attrs)         -> {cell, Attrs};
83convert_tag(tr, Attrs)         -> {row, Attrs};
84convert_tag(tt, Attrs)         -> {c, Attrs};
85convert_tag(ul, Attrs)         -> {list, Attrs};
86convert_tag(underline, Attrs)  -> {em, Attrs};
87convert_tag(Tag, Attrs)        -> {Tag, Attrs}.
88
89is_url("http:"++_) -> true;
90is_url("https:"++_) -> true;
91is_url("../"++_) -> true;
92is_url(FileRef) ->
93    case filename:extension(FileRef) of
94	"" -> false; % no extension = xml file
95	_Ext -> true % extension
96    end.
97
98makesee(Ref) ->
99    {Tag, Marker} = docgen_edoc_xml_cb:makesee(Ref),
100    {Tag,[#xmlAttribute{name = marker, value = Marker}]}.
101