File: doc_pack.pl

package info (click to toggle)
swi-prolog 9.0.4%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 82,408 kB
  • sloc: ansic: 387,503; perl: 359,326; cpp: 6,613; lisp: 6,247; java: 5,540; sh: 3,147; javascript: 2,668; python: 1,900; ruby: 1,594; yacc: 845; makefile: 428; xml: 317; sed: 12; sql: 6
file content (200 lines) | stat: -rw-r--r-- 6,364 bytes parent folder | download | duplicates (2)
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
/*  Part of SWI-Prolog

    Author:        Jan Wielemaker
    E-mail:        J.Wielemaker@vu.nl
    WWW:           http://www.swi-prolog.org
    Copyright (c)  2012, VU University Amsterdam
    All rights reserved.

    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions
    are met:

    1. Redistributions of source code must retain the above copyright
       notice, this list of conditions and the following disclaimer.

    2. Redistributions in binary form must reproduce the above copyright
       notice, this list of conditions and the following disclaimer in
       the documentation and/or other materials provided with the
       distribution.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    POSSIBILITY OF SUCH DAMAGE.
*/

:- module(pldoc_pack,
	  [ doc_pack/1                  % +Pack
	  ]).
:- if(exists_source(library(http/http_dispatch))).
:- use_module(library(http/http_dispatch)).
:- endif.
:- use_module(library(prolog_pack)).
:- use_module(library(http/html_write)).
:- use_module(library(http/html_head)).
:- use_module(doc_html).
:- use_module(doc_index).

/** <module> PlDoc for Prolog extension packs

This module profiles PlDoc support specific   to Prolog extension packs.
It extends the PlDoc web-browser with the ability to lists the installed
packs and provide an overview of  a   pack,  whether  loaded or not. The
predicate  doc_pack/1  can  be  used    to   generate  stand-alone  HTML
documentation for a pack.
*/

:- if(current_predicate(http_handler/3)).
:- http_handler(pldoc(pack),     http_redirect(moved, pldoc('pack/')), []).
:- http_handler(pldoc('pack/'),  pldoc_pack, [prefix]).

%!  pldoc_pack(+Request)
%
%   HTTP handler that handles /pack/ in the PlDoc server. Without an
%   additional  path,  it  lists  the    installed  packs.  With  an
%   additional package name, it lists  the   content  of  a pack and
%   finally, /pack/<pack>/<file> can be used to get documentation or
%   the source of a pack file.

pldoc_pack(Request) :-
    memberchk(path_info(PackPath), Request),
    PackPath \== '',
    !,
    (   pack_path(Pack, PackFile, PackPath)
    ->  list_pack(Pack, PackFile, Request)
    ;   http_404([], Request)
    ).
pldoc_pack(_Request) :-
    reply_html_page(
	pldoc(packs),
	title('Installed extension packs'),
	\pack_page([])).

pack_path(Pack, PackFile, PackPath) :-
    sub_atom(PackPath, B, _, A, /),
    !,
    sub_atom(PackPath, 0, B, _, Pack),
    sub_atom(PackPath, _, A, 0, PackFile).

pack_page(Options) -->
    html_requires(pldoc),
    object_page_header(-, Options),
    html([ h1('Installed extension packs'),
	   p([ 'The following extension packages are installed in ',
	       'the this Prolog system.  Other packages can be found at ',
	       a(href('http://www.swi-prolog.org/pack/list'),
		 'the SWI-Prolog website')
	     ]),
	   \pack_table(Options)
	 ]).


%!  pack_table(+Options)// is det.
%
%   Generate a table with installed packages

pack_table(_Options) -->
    { findall(Pack, pack_property(Pack, directory(_)), Packs0),
      sort(Packs0, Packs)
    },
    html(table(class(packs),
	       [ tr([th('Pack'), th('Version'), th('Title')])
	       | \packs(Packs)
	       ])).

packs([]) --> [].
packs([H|T]) --> pack(H), packs(T).

pack(Pack) -->
    { uri_encoded(path, Pack, HREF),
      pack_property(Pack, version(Version)),
      (   pack_property(Pack, title(Title))
      ->  true
      ;   Title = '<no title>'
      )
    },
    html(tr([ td(class(pack_name),    a(href(HREF+'/'), Pack)),
	      td(class(pack_version), Version),
	      td(class(pack_title),   Title)
	    ])).


%!  list_pack(+Pack, +PackFile, +Request)
%
%   List a directory or file in a pack

list_pack(Pack, '', _) :-
    !,
    reply_html_page(
	pldoc(pack),
	title('Documentation for pack ~w'-[Pack]),
	\pack_doc(Pack)).
list_pack(Pack, File, Request) :-
    pack_property(Pack, directory(PackDir)),
    directory_file_path(PackDir, File, Path0),
    absolute_file_name(Path0, Path),        % Canonical
    sub_atom(Path, 0, _, _, PackDir),
    pldoc_http:doc_reply_file(Path, Request).

pack_doc(Pack) -->
    { pack_property(Pack, directory(PackDir)),
      pack_title(Pack, Title),
      findall(O, pack_option(Pack, O), Options)
    },
    dir_index(PackDir,
	      [ if(true),
		recursive(true),
		title(Title)
	      | Options
	      ]).
:- endif.			% server facilities


		 /*******************************
		 *        STAND ALONE DOCS      *
		 *******************************/

%!  doc_pack(+Pack)
%
%   Generate stand-alone documentation for  the   package  Pack. The
%   documentation is generated in a directory =doc= inside the pack.
%   The  index  page  consists  of  the    content  of  =readme=  or
%   =|readme.txt|= in the main directory of the pack and an index of
%   all files and their public predicates.

doc_pack(Pack) :-
    pack_property(Pack, directory(PackDir)),
    pack_title(Pack, PackTitle),
    findall(O, pack_option(Pack, O), Options),
    directory_file_path(PackDir, prolog, SourceDir),
    directory_file_path(PackDir, doc, DocDir),
    doc_save(SourceDir,
	     [ title(PackTitle),
	       doc_root(DocDir),
	       if(true),
	       recursive(true)
	     | Options
	     ]).

pack_title(Pack, PackTitle) :-
    pack_property(Pack, title(Title)),
    !,
    format(atom(PackTitle), 'Pack ~w -- ~w', [Pack, Title]).
pack_title(Pack, PackTitle) :-
    format(atom(PackTitle), 'Pack ~w', [Pack]).

pack_option(Pack, Option) :-
    pack_option(Option),
    pack_property(Pack, Option).

pack_option(readme(_)).
pack_option(todo(_)).