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
|
/* $Id$
Part of SWI-Prolog
Author: Jan Wielemaker
E-mail: wielemak@science.uva.nl
WWW: http://www.swi-prolog.org
Copyright (C): 1985-2007, University of 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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
]).
:- use_module(doc_html).
:- use_module(doc_index).
:- 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 Handle CSS files
@tbd Suppress search header
@tbd Fix filenames (.pl --> .html)
@tbd Fix predicate references
*/
%% 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 files in the same directory as the
% sources.
%
% * man_server(+RootURL)
% Root of a manual server used for references to built-in
% predicates. Default is
% =|http://gollem.science.uva.nl/SWI-Prolog/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.
%
% @tbd Copy CSS files, inline CSS files
doc_save(Spec, Options) :-
doc_target(Spec, Target, Options),
phrase(file_map(Target), FileMap), % Assoc?
Options1 = [files(FileMap)|Options],
nb_setval(pldoc_options, Options1),
call_cleanup(generate(Target, Options1),
nb_delete(pldoc_options)).
%% 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) :-
open(DocFile, write, Out, [encoding(utf8)]),
call_cleanup(with_output_to(Out, doc_for_file(PlFile, Options)),
close(Out)).
generate(directory(Dir, IndexFile, Members), Options) :-
open(IndexFile, write, Out, [encoding(utf8)]),
call_cleanup(with_output_to(Out, doc_for_dir(Dir, Options)),
close(Out)),
generate(Members, Options).
%% doc_target(+Spec, -Target) 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)
% Document Dir in IndexFile. Memmbers 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)
]), !,
document_file(File, DocFile, Options).
doc_target(FileOrDir, directory(Dir, Index, Members), Options) :-
absolute_file_name(FileOrDir, Dir,
[ file_type(directory),
file_errors(fail),
access(read)
]), !,
document_file(Dir, Index, Options),
findall(Member,
( prolog_file_in_dir(Dir, File, Options),
doc_target(File, Member, Options)
),
Members).
%% 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)) -->
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)
-> working_directory(PWD, PWD),
atom_concat(PWD, 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),
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),
atom_concat(Dir, '/*', Pattern),
expand_file_name(Pattern, Matches),
member(SubDir, Matches),
exists_directory(SubDir).
%% blocked(+File) is semidet.
%
% True if File is blocked from documentation.
blocked('.plrc').
blocked('INDEX.pl').
|