%% A sample Erlang program to obtain a edoc.dtd - compliant
%% XML from a given file containing a single Erlang module.
%% This code was suggested by Joseph Wayne Norton:
%%
%% http://www.erlang.org/pipermail/erlang-questions/2008-July/037056.html
%%
%% The original code was simplified so as no framed HTML pieces of documentation
%% is created. Also the "module" callback function attempts to retrieve
%% the actual module name from the options proplist. This module name is
%% whatever comes from edoc:get_doc.

-module (mkxml).

-export ([main/0, module/2, package/2, overview/2]).

-include_lib("xmerl/include/xmerl.hrl").

%% ---------------------------------------------------------------------
module(#xmlElement{name = module, content = Es}, Options) ->
   Prolog = ["<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"],
   Modn = proplists:get_value (force_module, Options, module_name_undefined),
   Content = [#xmlElement{
     name=package
     , attributes=[#xmlAttribute{name=name,value=[]}]
     , content=[#xmlElement{
         name=modules
         , attributes=[]
         , content=[#xmlElement{
             name=module
             , attributes=[#xmlAttribute{name=name,value=atom_to_list (Modn)}]
             , content=Es
            }]}]}],
   xmerl:export_simple(Content,xmerl_xml,[{prolog,Prolog}]).

package(#xmlElement{name = package, content = Es}, _Options) ->
   Prolog = ["<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"],
   xmerl:export_simple(Es,xmerl_xml, [{prolog,Prolog}]).

overview(#xmlElement{name = overview, content = Es}, _Options) ->
   Prolog = ["<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"],
   xmerl:export_simple(Es,xmerl_xml, [{prolog,Prolog}]).

%% ---------------------------------------------------------------------
%main() -> make_xml ([undefined], "/home2/dima/OTP/erlcouch-0.2/src/").

main() ->
  File = "/home2/dima/OTP/erlcouch-0.2/src/couch.erl",
  {Mod, Doc} = edoc:get_doc (File),
  Options = [{layout, ?MODULE}, {force_module, Mod}],
  Str = lists:flatten (edoc:layout (Doc, Options)),
  {_, Out} = file:open ("out.xml", [write]),
  io:fwrite (Out, Str ++ "~n", []),
  file:close (Out).

