File: doc_files.pl

package info (click to toggle)
swi-prolog 7.2.3%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 84,180 kB
  • ctags: 45,684
  • sloc: ansic: 330,358; perl: 268,104; sh: 6,795; java: 4,904; makefile: 4,561; cpp: 4,153; ruby: 1,594; yacc: 843; xml: 82; sed: 12; sql: 6
file content (365 lines) | stat: -rw-r--r-- 10,822 bytes parent folder | download | duplicates (3)
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/*  Part of SWI-Prolog

    Author:        Jan Wielemaker
    E-mail:        J.Wielemaker@vu.nl
    WWW:           http://www.swi-prolog.org
    Copyright (C): 2007-2012, University of Amsterdam
			      VU University Amsterdam

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
    as published by the Free Software Foundation; either version 2
    of the License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
    MA 02110-1301 USA

    As a special exception, if you link this library with other files,
    compiled with a Free Software compiler, to produce an executable, this
    library does not by itself cause the resulting executable to be covered
    by the GNU General Public License. This exception does not however
    invalidate any other reasons why the executable file might be covered by
    the GNU General Public License.
*/

:- module(pldoc_files,
	  [ doc_save/2,			% +File, +Options
	    doc_pack/1			% +Pack (re-export from doc_pack)
	  ]).
:- use_module(library(pldoc), []).
:- use_module(pldoc(doc_html)).
:- use_module(pldoc(doc_index)).
:- use_module(pldoc(doc_pack)).
:- use_module(library(option)).
:- use_module(library(lists)).

/** <module> Create stand-alone documentation files

Create stand-alone documentation from a  bundle of source-files. Typical
use of the PlDoc package is to run   it as a web-server from the project
in progress, providing search and guaranteed consistency with the loaded
version. Creating stand-alone files as  provided   by  this  file can be
useful for printing or distribution.

@tbd	Generate a predicate index?
*/

:- predicate_options(doc_save/2, 2,
		     [ format(oneof([html])),
		       doc_root(atom),
		       man_server(atom),
		       index_file(atom),
		       if(oneof([loaded,true])),
		       recursive(boolean),
		       css(oneof([copy,inline])),
		       title(atom)
		     ]).


%%	doc_save(+FileOrDir, +Options)
%
%	Save documentation for FileOrDir to file(s).  Options include
%
%		* format(+Format)
%		Currently only supports =html=.
%
%		* doc_root(+Dir)
%		Save output to the given directory.  Default is to save
%		the documentation for a file in the same directory as
%		the file and for a directory in a subdirectory =doc=.
%
%		* title(+Title)
%		Title is an atom that provides the HTML title of the
%		main (index) page.  Only meaningful when generating
%		documentation for a directory.
%
%		* man_server(+RootURL)
%		Root of a manual server used for references to built-in
%		predicates. Default is
%		=|http://www.swi-prolog.org/pldoc/|=
%
%		* index_file(+Base)
%		Filename for directory indices.  Default is =index=.
%
%		* if(Condition)
%		What to do with files in a directory.  =loaded= (default)
%		only documents files loaded into the Prolog image.  =true=
%		documents all files.
%
%		* recursive(+Bool)
%		If =true=, recurse into subdirectories.
%
%		* css(+Mode)
%		If =copy=, copy the CSS file to created directories.
%		Using =inline=, include the CSS file into the created
%		files.  Currently, only the default =copy= is supported.
%
%	The typical use-case is to document the Prolog files that belong
%	to a project in the  current  directory.   To  do  this load the
%	Prolog  files  and  run  the   goal    below.   This  creates  a
%	sub-directory  =doc=  with  an  index  file  =|index.html|=.  It
%	replicates the directory structure  of   the  source  directory,
%	creating an HTML file for each Prolog file and an index file for
%	each sub-directory. A  copy  of  the   required  CSS  and  image
%	resources is copied to the =doc= directory.
%
%	  ==
%	  ?- doc_save(., [recursive(true)]).
%	  ==

doc_save(Spec, Options) :-
	doc_target(Spec, Target, Options),
	target_directory(Target, Dir),
	phrase(file_map(Target), FileMap),
	merge_options([ html_resources(pldoc_files),
			source_link(false),
			resource_directory(Dir)
		      ], Options, Options1),
	Options2 = [files(FileMap)|Options1],
	setup_call_cleanup(
	    nb_setval(pldoc_options, Options2),
	    generate(Target, Options2),
	    nb_delete(pldoc_options)),
	copy_resources(Dir, Options2).


%%	generate(+Spec, +Options) is det.
%
%	Generate  documentation  for  the    specification   created  by
%	doc_target/2.

generate([], _).
generate([H|T], Options) :-
	\+ \+ generate(H, Options),
	generate(T, Options).
generate(file(PlFile, DocFile), Options) :-
	b_setval(pldoc_output, DocFile),
	setup_call_cleanup(
	    open(DocFile, write, Out, [encoding(utf8)]),
	    with_output_to(Out, doc_for_file(PlFile, Options)),
	    close(Out)).
generate(directory(Dir, IndexFile, Members, DirOptions), Options) :-
	append(DirOptions, Options, AllOptions),
	b_setval(pldoc_output, IndexFile),
	setup_call_cleanup(
	    open(IndexFile, write, Out, [encoding(utf8)]),
	    with_output_to(
		Out,
		doc_for_dir(Dir,
			    [ members(Members)
			    | AllOptions
			    ])),
	    close(Out)),
	generate(Members, Options).


%%	doc_target(+Spec, -Target, +Options) is semidet.
%
%	Generate a structure describing what to document in what files.
%	This structure is a term:
%
%		* file(PlFile, DocFile)
%		Document PlFile in DocFile
%
%		* directory(Dir, IndexFile, Members, Options)
%		Document Dir in IndexFile.  Members is a list of
%		documentation structures.

doc_target(FileOrDir, file(File, DocFile), Options) :-
	absolute_file_name(FileOrDir, File,
			   [ file_type(prolog),
			     file_errors(fail),
			     access(read)
			   ]), !,
	(   option(source_root(_), Options)
	->  Options1 = Options
	;   file_directory_name(File, FileDir),
	    Options1 = [source_root(FileDir)|Options]
	),
	document_file(File, DocFile, Options1).
doc_target(FileOrDir, directory(Dir, Index, Members, DirOptions), Options0) :-
	absolute_file_name(FileOrDir, Dir,
			   [ file_type(directory),
			     file_errors(fail),
			     access(read)
			   ]), !,
	(   option(source_root(_), Options0)		% recursive
	->  Options = Options0
	;   Options1 = [source_root(Dir)|Options0],	% top
	    exclude(main_option, Options1, Options2),
	    set_doc_root(Dir, Options2, Options)
	),
	DirOptions = Options,
	document_file(Dir, Index, Options),
	findall(Member,
		(   prolog_file_in_dir(Dir, File, Options),
		    doc_target(File, Member, Options)
		),
		Members).

%%	main_option(?Option)
%
%	Options that apply only to the main directory.

main_option(title(_)).
main_option(readme(_)).
main_option(todo(_)).

target_directory(directory(_, Index, _, _), Dir) :-
	file_directory_name(Index, Dir).
target_directory(file(_, DocFile), Dir) :-
	file_directory_name(DocFile, Dir).

set_doc_root(_Dir, Options0, Options) :-
	option(doc_root(_), Options0), !,
	Options = Options0.
set_doc_root(Dir, Options0, Options) :-
	directory_file_path(Dir, doc, DocRoot),
	Options = [doc_root(DocRoot)|Options0].

%%	file_map(+DocStruct, -List)
%
%	Create a list of file(PlFile, DocFile) for files that need to
%	be documented.

file_map([]) -->
	[].
file_map([H|T]) -->
	file_map(H),
	file_map(T).
file_map(file(Src, Doc)) -->
	[ file(Src, Doc) ].
file_map(directory(_Dir, _Doc, Members, _Options)) -->
	file_map(Members).



%%	document_file(+File, -DocFile, +Options) is semidet.
%
%	DocFile is the file into which to write the documentation for
%	File.  File must be a canonical Prolog source-file.

document_file(File, DocFile, Options) :-
	(   option(if(loaded), Options, loaded)
	->  (   source_file(File)
	    ->	true
	    ;	exists_directory(File),
		source_file(SrcFile),
		sub_atom(SrcFile, 0, _, _, File)
	    ->	true
	    )
	;   true
	),
	option(format(Format), Options, html),
	doc_extension(Format, Ext),
	(   exists_directory(File)
	->  option(index_file(Index), Options, index),
	    atomic_list_concat([File, /, Index, '.', Ext], DocFile0)
	;   file_name_extension(Base, _, File),
	    file_name_extension(Base, Ext, DocFile0)
	),
	(   option(doc_root(Dir0), Options),
	    ensure_slash(Dir0, Dir)
	->  (   option(source_root(SrcTop), Options)
	    ->	true
	    ;	working_directory(SrcTop, SrcTop)
	    ),
	    atom_concat(SrcTop, Local, DocFile0),
	    atom_concat(Dir, Local, DocFile),
	    file_directory_name(DocFile, DocDir),
	    ensure_dir(DocDir, Options)
	;   DocFile = DocFile0
	).


%%	doc_extension(+Format, -Extension) is det.

doc_extension(html, html).
doc_extension(latex, tex).


%%	ensure_slash(+DirName, -WithSlash) is det.
%
%	Ensure WithSlash ends with a /.

ensure_slash(DirName, WithSlash) :-
	(   sub_atom(DirName, _, _, 0, /)
	->  WithSlash = DirName
	;   atom_concat(DirName, /, WithSlash)
	).


%%	ensure_dir(+Directory, +Options) is det.
%
%	Create Directory as mkdir -p.  May generate file errors.

ensure_dir(Directory, _Options) :-
	exists_directory(Directory), !.
ensure_dir(Directory, Options) :-
	file_directory_name(Directory, Parent),
	Parent \== Directory,
	ensure_dir(Parent, Options),
	make_directory(Directory).


%%	prolog_file_in_dir(+Dir, -File, +Options) is nondet.
%
%	File is a file in Dir that must be documented.  Options:
%
%		* recursive(+Bool)
%		If =true=, also generate subdirectories

prolog_file_in_dir(Dir, File, Options) :-
	(   option(if(loaded), Options, loaded)
	->  source_file(File),
	    file_directory_name(File, Dir)
	;   user:prolog_file_type(Ext, prolog),
	    \+ user:prolog_file_type(Ext, qlf),
	    atomic_list_concat([Dir, '/*.', Ext], Pattern),
	    expand_file_name(Pattern, Files),
	    member(File, Files)
	),
	file_base_name(File, Base),
	\+ blocked(Base).
prolog_file_in_dir(Dir, SubDir, Options) :-
	option(recursive(true), Options, false),
	option(doc_root(DocRoot), Options),
	atom_concat(Dir, '/*', Pattern),
	expand_file_name(Pattern, Matches),
	member(SubDir, Matches),
	SubDir \== DocRoot,
	exists_directory(SubDir).

%%	blocked(+File) is semidet.
%
%	True if File is blocked from documentation.

blocked('.plrc').
blocked('INDEX.pl').


		 /*******************************
		 *	     RESOURCES		*
		 *******************************/

%%	copy_resources(+Dir, +Options)

copy_resources(Dir, Options) :-
	option(format(Format), Options, html),
	forall(doc_resource(Format, Res),
	       ( absolute_file_name(pldoc(Res), File, [access(read)]),
		 copy_file(File, Dir))).

doc_resource(html, 'pldoc.css').
doc_resource(html, 'h1-bg.png').
doc_resource(html, 'h2-bg.png').
doc_resource(html, 'multi-bg.png').
doc_resource(html, 'priv-bg.png').
doc_resource(html, 'pub-bg.png').