aboutsummaryrefslogtreecommitdiffstats
path: root/lib/docbuilder/src/docb_gen.erl
blob: 75494314f1415904e0c38783c50ec4f27bdb5458 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
%% ``The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
%% compliance with the License. You should have received a copy of the
%% Erlang Public License along with this software. If not, it can be
%% retrieved via the world wide web at http://www.erlang.org/.
%% 
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either expressed or implied. See
%% the License for the specific language governing rights and limitations
%% under the License.
%% 
%% The Initial Developer of the Original Code is Ericsson Utvecklings AB.
%% Portions created by Ericsson are Copyright 1999-2000, Ericsson 
%% Utvecklings AB. All Rights Reserved.''
%% 
%%     $Id$
%%
-module(docb_gen).

-export([module/1, module/2, users_guide/1, users_guide/2]).
-deprecated([{module,1,next_major_release},
	     {module,2,next_major_release},
	     {users_guide,1,next_major_release},
	     {users_guide,2,next_major_release}]).

-record(args, {suffix=".xml",
	       layout=docb_edoc_xml_cb,
	       def=[],
	       includes=[],
	       preprocess=false,
	       sort_functions=true}).

%% module(File) -> ok | {error, Reason}
%% module(File, Opts) -> ok | {error, Reason}
%%   File = string(), file name with or without ".erl" extension
%%   Opts -- see code
%%   Reason = badfile | {badopt, Term}
module(File0) ->
    module(File0, []).
module(File0, RawOpts) ->
    File = case filename:extension(File0) of
	       ".erl" -> File0;
	       _ -> File0++".erl"
	   end,
    case filelib:is_regular(File) of
	true ->
	    case parse(RawOpts, #args{}) of
		{ok, Args} ->
		    Opts = [{def,         Args#args.def},
			    {includes,    Args#args.includes},
			    {preprocess,  Args#args.preprocess},
			    {sort_functions, Args#args.sort_functions},

			    {app_default, "OTPROOT"},
			    {file_suffix, Args#args.suffix},
			    {dir,         "."},
			    {layout,      Args#args.layout}],
		    edoc:file(File, Opts);
		Error ->
		    Error
	    end;
	false ->
	    {error, badfile}
    end.

%% users_guide(File) -> ok | {error, Reason}
%% users_guide(File, Opts) -> ok | {error, Reason}
%%   File = string()
%%   Opts -- see code
%%   Reason = badfile | {badopt, Opt}
users_guide(File) ->
    users_guide(File, []).
users_guide(File, RawOpts) ->
    case filelib:is_regular(File) of
	true ->
	    case parse(RawOpts, #args{}) of
		{ok, Args} ->
		    Opts = [{def,         Args#args.def},
			    {app_default, "OTPROOT"},
			    {file_suffix, Args#args.suffix},
			    {layout,      Args#args.layout}],

		    Env = edoc_lib:get_doc_env(Opts),

		    {ok, Tags} =
			edoc_extract:file(File, overview, Env, Opts),
		    Data =
			edoc_data:overview("Overview", Tags, Env, Opts),
		    F = fun(M) -> M:overview(Data, Opts) end,
		    Text = edoc_lib:run_layout(F, Opts),

		    OutFile = "chapter" ++ Args#args.suffix,
		    edoc_lib:write_file(Text, ".", OutFile);
		Error ->
		    Error
	    end;
	false ->
	    {error, badfile}
    end.

parse([{output,xml} | RawOpts], Args) ->
    parse(RawOpts, Args); % default, no update of record necessary
parse([{output,sgml} | RawOpts], Args) ->
    parse(RawOpts, Args#args{suffix=".sgml", layout=docb_edoc_sgml_cb});
parse([{def,Defs} | RawOpts], Args) ->
    case parse_defs(Defs) of
	true ->
	    Args2 = Args#args{def=Args#args.def++Defs},
	    parse(RawOpts, Args2);
	false ->
	    {error, {badopt, {def,Defs}}}
    end;
parse([{includes,Dirs} | RawOpts], Args) ->
    case parse_includes(Dirs) of
	true ->
	    Args2 = Args#args{includes=Args#args.includes++Dirs},
	    parse(RawOpts, Args2);
	false ->
	    {error, {badopt, {includes,Dirs}}}
    end;
parse([{preprocess,Bool} | RawOpts], Args) when Bool==true;
						Bool==false ->
    parse(RawOpts, Args#args{preprocess=Bool});
parse([{sort_functions,Bool} | RawOpts], Args) when Bool==true;
						    Bool==false ->
    parse(RawOpts, Args#args{sort_functions=Bool});
parse([], Args) ->
    {ok, Args};
parse([Opt | _RawOpts], _Args) ->
    {error, {badopt, Opt}}.

parse_defs(Defs) ->
    lists:all(fun({Key,Val}) when is_atom(Key), is_list(Val) -> true;
		 (_) -> false
	      end,
	      Defs).

parse_includes(Dirs) ->
    lists:all(fun(Dir) when is_list(Dir) -> true;
		 (_) -> false
	      end,
	      Dirs).