34
35:- module(swish_markdown,
36 [ wiki_file_codes_to_dom/3, 37 wiki_html//1 38 ]). 39:- use_module(library(http/http_dispatch)). 40:- use_module(library(http/http_parameters)). 41:- use_module(library(http/http_client)). 42:- use_module(library(http/html_write)). 43:- use_module(library(http/html_head)). 44:- use_module(library(pldoc/doc_html),
45 except([ file//2
46 ])). 47:- use_module(library(pldoc/doc_wiki)). 48:- use_module(library(option)). 49:- use_module(library(filesex)). 50
51:- use_module(storage). 52:- use_module(config). 53
54:- html_meta
55 wiki_html(html,?,?). 56
57:- multifile
58 dom_expansion/2. 59
60
65
66:- http_handler(swish(markdown), markdown, [id(markdown)]). 67
72
73markdown(Request) :-
74 option(method(get), Request), !,
75 http_parameters(Request,
76 [ text(Data, [optional(true), default('')])
77 ]),
78 atom_codes(Data, Codes),
79 wiki_file_codes_to_dom(Codes, '/', DOM), 80 phrase(html(DOM), Tokens),
81 format('Content-type: text/html; charset=UTF-8\n\n'),
82 print_html(Tokens).
83markdown(Request) :-
84 option(method(post), Request), !,
85 http_read_data(Request, Codes, [to(codes)]),
86 wiki_file_codes_to_dom(Codes, '/', DOM),
87 phrase(html(DOM), Tokens),
88 format('Content-type: text/html; charset=UTF-8\n\n'),
89 print_html(Tokens).
90
95
96wiki_file_codes_to_dom(String, File, DOM) :-
97 ( nb_current(pldoc_file, OrgFile)
98 -> setup_call_cleanup(
99 b_setval(pldoc_file, File),
100 wiki_codes_to_dom(String, [], DOM0),
101 b_setval(pldoc_file, OrgFile))
102 ; setup_call_cleanup(
103 b_setval(pldoc_file, File),
104 wiki_codes_to_dom(String, [], DOM0),
105 nb_delete(pldoc_file))
106 ),
107 expand_dom(DOM0, DOM).
108
109expand_dom(DOM0, DOM) :-
110 dom_expansion(DOM0, DOM), !.
111expand_dom(DOM, DOM).
112
113
114 117
118
119wiki_html(_:HTML) -->
120 html(swish_markdown:HTML).
121
122
123:- multifile
124 prolog:doc_autolink_extension/2. 125
126prolog:doc_autolink_extension(swinb, notebook).
127prolog:doc_autolink_extension(lnk, permalink).
128
129:- public
130 file//2. 131
142
143:- multifile
144 swish_config:source_alias/2. 145
146file(File, Options) -->
147 { once(sub_atom(File, Pre, _, _Post, /)),
148 sub_atom(File, 0, Pre, _, Alias),
149 swish_config:source_alias(Alias, _Options),
150 option(label(Label), Options),
151 http_location_by_id(swish, Swish),
152 directory_file_path(Swish, File, HREF)
153 }, !,
154 html(a([class([alias,file]), href(HREF)], Label)).
155file(File, Options) -->
156 { storage_file(File),
157 option(label(Label), Options, File),
158 http_location_by_id(swish, Swish),
159 directory_file_path(Swish, p, StoreDir),
160 directory_file_path(StoreDir, File, HREF)
161 }, !,
162 html(a([class(store), href(HREF)], Label)).
163file(File, Options) -->
164 pldoc_html:file(File, Options)