34
35:- module(ws_browser, [browse_server/1]). 36
37:- use_module(library(apply)). 38:- use_module(library(transpose)). 39:- use_module(library(http/html_write)). 40:- use_module(library(http/http_dispatch)). 41:- use_module(library(http/http_parameters)). 42:- use_module(library(http/thread_httpd)). 43
44:- http_handler(root(.), list_files, []). 45:- http_handler(root(show_source), show_source, []). 46
47browse_server(Port) :-
48 http_server(http_dispatch, [port(Port)]).
49
50:- multifile
51 provides_method/1,
52 fetch_files_properties_hook/3,
53 show_source_hook/2. 54
55list_files(Request) :-
56 print_message(information, format('Preparing to list files', [])),
57 once(provides_method(DMethod)),
58 http_parameters(Request,
59 [ meth(Method, [default(DMethod)])
60 ]),
61 fetch_files_properties_hook(Method, Descs, FilesProps),
62 reply_html_page([ 63 title('Browse Code')
64 ],
65 [h1('Files'),
66 table([border(1)],
67 [\header(Descs),
68 \foldl(html_module_props(Method), FilesProps)
69 ])
70 ]),
71 print_message(information, format('done', [])).
72
73show_source(Request) :-
74 once(provides_method(DMethod)),
75 http_parameters(Request,
76 [meth(Method, [default(DMethod)]),
77 file(File, [])
78 ]),
79 show_source_hook(Method, File),
80 print_message(information, format('done', [])).
81
(Descs) -->
83 html(tr([td(b('File')),
84 \foldl(desc, Descs)
85 ])).
86
87desc(Desc) --> html(td(b(Desc))).
88
89html_module_props(Method, File-Props) -->
90 {transpose(Props, PropsT)},
91 html(tr([td(\html_file(Method, File)), \foldl(html_prop, PropsT)])).
92
93html_prop(PropL) -->
94 html(td(table(\foldl(prop, PropL)))).
95
96prop(Prop) --> html(tr(td(Prop))).
97
98html_file(Method, File) -->
99 {http_link_to_id(show_source, [meth=Method, file=File], HREF)},
100 html(a(href(HREF), File))