forked from SWI-Prolog/packages-pldoc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
doc_wiki.pl
1845 lines (1550 loc) · 47.8 KB
/
doc_wiki.pl
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Part of SWI-Prolog
Author: Jan Wielemaker
E-mail: [email protected]
WWW: http://www.swi-prolog.org
Copyright (C): 2006-2013, 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_wiki,
[ wiki_codes_to_dom/3, % +Codes, +Args, -DOM
wiki_lines_to_dom/3, % +Lines, +Map, -DOM
section_comment_header/3, % +Lines, -Header, -RestLines
summary_from_lines/2, % +Lines, -Codes
indented_lines/3, % +Text, +PrefixChars, -Lines
strip_leading_par/2, % +DOM0, -DOM
normalise_white_space/3, % -Text, //
autolink_extension/2, % ?Extension, ?Type
autolink_file/2 % +FileName, -Type
]).
:- use_module(library(lists)).
:- use_module(library(debug)).
:- use_module(library(error)).
:- use_module(library(memfile)).
:- use_module(library(pairs)).
:- use_module(library(option)).
:- use_module(library(debug)).
:- use_module(library(apply)).
/** <module> PlDoc wiki parser
This file defines the PlDoc wiki parser, which parses both comments and
wiki text files. The original version of this SWI-Prolog wiki format was
largely modeled after Twiki (http://twiki.org/). The current version is
extended to take many aspects from markdown, in particular the doxygen
refinement thereof.
@see http://www.stack.nl/~dimitri/doxygen/manual/markdown.html
*/
/*******************************
* WIKI PARSING *
*******************************/
%% wiki_lines_to_dom(+Lines:lines, +Args:list(atom), -Term) is det
%
% Translate a Wiki text into an HTML term suitable for html//1
% from the html_write library.
wiki_lines_to_dom(Lines, Args, HTML) :-
tokenize_lines(Lines, Tokens0),
normalise_indentation(Tokens0, Tokens),
wiki_structure(Tokens, -1, Pars),
wiki_faces(Pars, Args, HTML).
%% wiki_codes_to_dom(+String, +Args, -DOM) is det.
%
% Translate a plain text into a DOM term.
%
% @param String Plain text. Either a string or a list of codes.
wiki_codes_to_dom(Codes, Args, DOM) :-
indented_lines(Codes, [], Lines),
wiki_lines_to_dom(Lines, Args, DOM).
%% wiki_structure(+Lines:lines, +BaseIndent,
%% -Blocks:list(block)) is det
%
% Get the structure in terms of block-level elements: paragraphs,
% lists and tables. This processing uses a mixture of layout and
% punctuation.
wiki_structure([], _, []) :- !.
wiki_structure([_-[]|T], BI, Pars) :- !, % empty lines
wiki_structure(T, BI, Pars).
wiki_structure(Lines, _, [\tags(Tags)]) :-
tags(Lines, Tags), !.
wiki_structure(Lines, BI, [P1|PL]) :-
take_block(Lines, BI, P1, RestLines),
wiki_structure(RestLines, BI, PL).
%% take_block(+Lines, +BaseIndent, ?Block, -RestLines) is semidet.
%
% Take a block-structure from the input. Defined block elements
% are lists, table, hrule, section header and paragraph.
take_block([_-[]|Lines], BaseIndent, Block, Rest) :- !,
take_block(Lines, BaseIndent, Block, Rest).
take_block([N-_|_], BaseIndent, _, _) :-
N < BaseIndent, !,
fail. % less indented
take_block(Lines, BaseIndent, List, Rest) :-
list_item(Lines, Type, Indent, LI, LIT, Rest0), !,
Indent > BaseIndent,
rest_list(Rest0, Type, Indent, LIT, [], Rest),
List0 =.. [Type, LI],
( ul_to_dl(List0, List)
-> true
; List0 = dl(Items)
-> List = dl(class=wiki, Items)
; List = List0
).
take_block([N-['|'|RL1]|LT], _, Table, Rest) :-
phrase(row(R0), RL1),
rest_table(LT, N, RL, Rest), !,
Table = table(class=wiki, [tr(R0)|RL]).
take_block([0-[-,-|More]|LT], _, Block, LT) :- % seperation line
maplist(=(-), More), !,
Block = hr([]).
take_block([_-Line|LT], _, Block, LT) :- % seperation line
ruler(Line), !,
Block = hr([]).
take_block([_-[@|_]], _, _, _) :- !, % starts @tags section
fail.
take_block(Lines, _BaseIndent, Section, RestLines) :-
section_header(Lines, Section, RestLines), !.
take_block([_-Verb|Lines], _, Verb, Lines) :-
verbatim_term(Verb), !.
take_block([I-L1|LT], BaseIndent, Elem, Rest) :- !,
append(L1, PT, Par),
rest_par(LT, PT, I, BaseIndent, MaxI, Rest),
( MaxI >= BaseIndent+16
-> Elem = center(Par)
; MaxI >= BaseIndent+4
-> Elem = blockquote(Par)
; Elem = p(Par)
).
take_block([Verb|Lines], _, Verb, Lines).
%% ruler(+Line) is semidet.
%
% True if Line contains 3 ruler chars and otherwise spaces.
ruler([C0|Line]) :-
rule_char(C0),
phrase(ruler(C0, 1), Line).
ruler(C, N) --> [C], !, { N2 is N+1 }, ruler(C, N2).
ruler(C, N) --> [' '], !, ruler(C, N).
ruler(_, N) --> { N >= 3 }.
rule_char('-').
rule_char('_').
rule_char('*').
%% list_item(+Lines, ?Type, ?Indent, -LI0, -LIT, -RestLines) is det.
%
% Create a list-item. Naturally this should produce a single item,
% but DL lists produce two items, so we create the list of items
% as a difference list.
%
% @tbd Pass base-indent
list_item([Indent-Line|LT], Type, Indent, Items, ItemT, Rest) :- !,
list_item_prefix(Type, Line, L1),
( Type == dl
-> split_dt(L1, DT0, DD1),
append(DD1, LIT, DD),
strip_ws_tokens(DT0, DT),
Items = [dt(DT),dd(DD)|ItemT]
; append(L1, LIT, LI0),
Items = [li(LI0)|ItemT]
),
rest_list_item(LT, Type, Indent, LIT, Rest).
%% rest_list_item(+Lines, +Type, +Indent, -RestItem, -RestLines) is det
%
% Extract the remainder (after the first line) of a list item.
rest_list_item(Lines, _Type, Indent, RestItem, RestLines) :-
take_blocks_at_indent(Lines, Indent, Blocks, RestLines),
( Blocks = [p(Par)|MoreBlocks]
-> append(['\n'|Par], MoreBlocks, RestItem)
; RestItem = Blocks
).
%% take_blocks_at_indent(+Lines, +Indent, -Pars, -RestLines) is det.
%
% Process paragraphs and verbatim blocks (==..==) in bullet-lists.
take_blocks_at_indent(Lines, _, [], Lines) :-
skip_empty_lines(Lines, Lines1),
section_header(Lines1, _, _), !.
take_blocks_at_indent(Lines, N, [Block|RestBlocks], RestLines) :-
take_block(Lines, N, Block, Rest0), !,
take_blocks_at_indent(Rest0, N, RestBlocks, RestLines).
take_blocks_at_indent(Lines, _, [], Lines).
%% rest_list(+Lines, +Type, +Indent,
%% -Items, -ItemTail, -RestLines) is det.
rest_list(Lines, Type, N, Items, IT, Rest) :-
skip_empty_lines(Lines, Lines1),
list_item(Lines1, Type, N, Items, IT0, Rest0), !,
rest_list(Rest0, Type, N, IT0, IT, Rest).
rest_list(Rest, _, _, IT, IT, Rest).
%% list_item_prefix(?Type, +Line, -Rest) is det.
list_item_prefix(ul, [*, ' '|T], T) :- !.
list_item_prefix(ul, [-, ' '|T], T) :- !.
list_item_prefix(dl, [$, ' '|T], T) :-
split_dt(T, _, _), !.
list_item_prefix(ol, [w(N), '.', ' '|T], T) :-
atom_codes(N, [D]),
between(0'0, 0'9, D).
%% split_dt(+LineAfterDollar, -DT, -Rest)
%
% First see whether the entire line is the item. This allows
% creating items holding : by using $ <tokens> :\n
split_dt(In, DT, []) :-
append(DT, [':'], In), !.
split_dt(In, DT, Rest) :-
append(DT, [':'|Rest0], In),
( Rest0 == []
-> Rest = []
; Rest0 = [' '|Rest]
), !.
%% ul_to_dl(+UL, -DL) is semidet.
%
% Translate an UL list into a DL list if all entries are of the
% form "* <term> nl, <description>" and at least one <description>
% is non-empty, or all items are of the form
% [[PredicateIndicator]].
ul_to_dl(ul(Items), Description) :-
term_items(Items, DLItems, []),
( terms_to_predicate_includes(DLItems, Preds)
-> Description = dl(class(predicates), Preds)
; member(dd(DD), DLItems), DD \== []
-> Description = dl(class(termlist), DLItems)
).
term_items([], T, T).
term_items([LI|LIs], DLItems, Tail) :-
term_item(LI, DLItems, Tail1),
term_items(LIs, Tail1, Tail).
%% term_item(+LI, -DLItem, ?Tail) is semidet.
%
% If LI is of the form <Term> followed by a newline, return it as
% dt-dd tuple. The <dt> item contains a term
%
% \term(Text, Term, Bindings).
term_item(li(Tokens),
[ dt(class=term, \term(Text, Term, Bindings)),
dd(Descr)
| Tail
], Tail) :-
( ( append(TermTokens, ['\n'|Descr], Tokens)
-> true
; TermTokens = Tokens,
Descr = []
)
-> setup_call_cleanup(
( new_memory_file(MemFile),
open_memory_file(MemFile, write, Out)
),
( forall(member(T, TermTokens),
write_token(Out, T)),
write(Out, ' .\n')
),
close(Out)),
catch(setup_call_cleanup(
open_memory_file(MemFile, read, In,
[ free_on_close(true)
]),
( read_dt_term(In, Term, Bindings),
read_dt_term(In, end_of_file, []),
memory_file_to_atom(MemFile, Text)
),
close(In)),
_, fail)
).
write_token(Out, w(X)) :- !,
write(Out, X).
write_token(Out, X) :-
write(Out, X).
read_dt_term(In, Term, Bindings) :-
read_term(In, Term,
[ variable_names(Bindings),
module(pldoc_modes)
]).
terms_to_predicate_includes([], []).
terms_to_predicate_includes([dt(class=term, \term(_, [[PI]], [])), dd([])|T0],
[\include(PI, predicate, [])|T]) :-
is_pi(PI),
terms_to_predicate_includes(T0, T).
is_pi(Name/Arity) :-
atom(Name),
integer(Arity),
between(0, 20, Arity).
is_pi(Name//Arity) :-
atom(Name),
integer(Arity),
between(0, 20, Arity).
%% row(-Cells)// is det.
row([C0|CL]) -->
cell(C0), !,
row(CL).
row([]) -->
[].
cell(td(C)) -->
face_tokens(C0),
['|'], !,
{ strip_ws_tokens(C0, C)
}.
face_tokens([]) -->
[].
face_tokens(Tokens) -->
face_token(H), % Deal with embedded *|...|*, etc.
token('|'),
face_tokens(Face),
token('|'),
face_token(H), !,
{ append([[H,'|'], Face, ['|', H], Rest], Tokens) },
face_tokens(Rest).
face_tokens([H|T]) -->
token(H),
face_tokens(T).
face_token(=) --> [=].
face_token(*) --> [*].
face_token('_') --> ['_'].
rest_table([N-['|'|RL1]|LT], N, [tr(R0)|RL], Rest) :- !,
phrase(row(R0), RL1),
rest_table(LT, N, RL, Rest).
rest_table(Rest, _, [], Rest).
%% rest_par(+Lines, -Par,
%% +BaseIndent, +MaxI0, -MaxI, -RestLines) is det.
%
% Take the rest of a paragraph. Paragraphs are ended by a blank
% line or the start of a list-item. The latter is a bit dubious.
% Why not a general block-level object? The current defition
% allows for writing lists without a blank line between the items.
rest_par([], [], BI, MaxI0, MaxI, []) :- !,
MaxI is max(BI, MaxI0).
rest_par([_-[]|Rest], [], _, MaxI, MaxI, Rest) :- !.
rest_par(Lines, [], _, MaxI, MaxI, Lines) :-
Lines = [_-Verb|_],
verbatim_term(Verb), !.
rest_par([I-L|Rest], [], _, MaxI, MaxI, [I-L|Rest]) :-
list_item_prefix(_, L, _), !.
rest_par([I-L1|LT], ['\n'|Par], BI, MaxI0, MaxI, Rest) :-
append(L1, PT, Par),
MaxI1 is max(I, MaxI0),
rest_par(LT, PT, BI, MaxI1, MaxI, Rest).
%% section_header(+Lines, -Section, -RestLines) is semidet.
%
% Get a section line from the input.
section_header([_-L1|LT], Section, LT) :-
twiki_section_line(L1, Section), !.
section_header([0-L1|LT], Section, LT) :-
md_section_line(L1, Section), !.
section_header([_-L1,0-L2|LT], Section, LT) :-
md_section_line(L1, L2, Section), !.
%% twiki_section_line(+Tokens, -Section) is semidet.
%
% Extract a section using the Twiki conventions. The section may
% be preceeded by [Word], in which case we generate an anchor name
% Word for the section.
twiki_section_line([-,-,-|Rest], Section) :-
plusses(Rest, Section).
plusses([+, ' '|Rest], h1(Attrs, Content)) :-
hdr_attributes(Rest, Attrs, Content).
plusses([+, +, ' '|Rest], h2(Attrs, Content)) :-
hdr_attributes(Rest, Attrs, Content).
plusses([+, +, +, ' '|Rest], h3(Attrs, Content)) :-
hdr_attributes(Rest, Attrs, Content).
plusses([+, +, +, +, ' '|Rest], h4(Attrs, Content)) :-
hdr_attributes(Rest, Attrs, Content).
hdr_attributes(List, Attrs, Content) :-
strip_leading_ws(List, List2),
( List2 = ['[',w(Name),']'|List3]
-> strip_ws_tokens(List3, Content),
Attrs = [class(wiki), id(Name)]
; Attrs = class(wiki),
strip_ws_tokens(List, Content)
).
%% md_section_line(+Tokens, -Section) is semidet.
%
% Handle markdown section lines staring with #
md_section_line([#, ' '|Rest], h1(Attrs, Content)) :-
md_section_attributes(Rest, Attrs, Content).
md_section_line([#, #, ' '|Rest], h2(Attrs, Content)) :-
md_section_attributes(Rest, Attrs, Content).
md_section_line([#, #, #, ' '|Rest], h3(Attrs, Content)) :-
md_section_attributes(Rest, Attrs, Content).
md_section_line([#, #, #, #, ' '|Rest], h4(Attrs, Content)) :-
md_section_attributes(Rest, Attrs, Content).
md_section_attributes(List, Attrs, Content) :-
phrase((tokens(Content), [' '], section_label(Label)), List), !,
Attrs = [class(wiki), id(Label)].
md_section_attributes(Content, Attrs, Content) :-
Attrs = [class(wiki)].
section_label(Label) -->
[ '{', '#', w(Name) ],
label_conts(Cont), ['}'], !,
{ atomic_list_concat([Name|Cont], Label) }.
label_conts([H|T]) --> label_cont(H), !, label_conts(T).
label_conts([]) --> [].
label_cont(-) --> [-].
label_cont(Name) --> [w(Name)].
md_section_line(Line1, Line2, Header) :-
Line1 \== [],
section_underline(Line2, Type),
phrase(wiki_words(_), Line1), !,% Should not have structure elements
( phrase(labeled_section_line(Title, Attrs), Line1)
-> true
; Title = Line1,
Attrs = []
),
Header =.. [Type, [class(wiki)|Attrs], Title].
section_underline([=,=,=|T], h1) :-
maplist(=(=), T), !.
section_underline([-,-,-|T], h2) :-
maplist(=(-), T), !.
labeled_section_line(Title, Attrs) -->
tokens(Title), [' '], section_label(Label), !,
{ Attrs = [id(Label)] }.
%% strip_ws_tokens(+Tokens, -Stripped)
%
% Strip leading and trailing whitespace from a token list. Note
% the the whitespace is already normalised.
strip_ws_tokens([' '|T0], T) :- !,
strip_ws_tokens(T0, T).
strip_ws_tokens(L0, L) :-
append(L, [' '], L0), !.
strip_ws_tokens(L, L).
%% strip_leading_ws(+Tokens, -Stripped) is det.
%
% Strip leading whitespace from a token list.
strip_leading_ws([' '|T], T) :- !.
strip_leading_ws(T, T).
/*******************************
* TAGS *
*******************************/
%% tags(+Lines:lines, -Tags) is semidet.
%
% If the first line is a @tag, read the remainder of the lines to
% a list of \tag(Name, Value) terms.
tags(Lines, Tags) :-
collect_tags(Lines, Tags0),
keysort(Tags0, Tags1),
pairs_values(Tags1, Tags2),
combine_tags(Tags2, Tags).
%% collect_tags(+IndentedLines, -Tags) is semidet
%
% Create a list Order-tag(Tag,Tokens) for each @tag encountered.
% Order is the desired position as defined by tag_order/2.
%
% @tbd Tag content is often poorly aligned. We now find the
% alignment of subsequent lines and assume the first line is
% alligned with the remaining lines.
collect_tags([], []).
collect_tags([Indent-[@,String|L0]|Lines], [Order-tag(Tag,Value)|Tags]) :-
tag_name(String, Tag, Order), !,
strip_leading_ws(L0, L),
rest_tag(Lines, Indent, VT, RestLines),
normalise_indentation(VT, VT1),
wiki_structure([0-L|VT1], -1, Value0),
strip_leading_par(Value0, Value),
collect_tags(RestLines, Tags).
%% tag_name(+String, -Tag:atom, -Order:int) is semidet.
%
% If String denotes a know tag-name,
tag_name(w(Name), Tag, Order) :-
( renamed_tag(Name, Tag, Level),
tag_order(Tag, Order)
-> print_message(Level, pldoc(deprecated_tag(Name, Tag)))
; tag_order(Name, Order)
-> Tag = Name
; print_message(warning, pldoc(unknown_tag(Name))),
fail
).
rest_tag([], _, [], []) :- !.
rest_tag(Lines, Indent, [], Lines) :-
Lines = [Indent-[@,Word|_]|_],
tag_name(Word, _, _), !.
rest_tag([L|Lines0], Indent, [L|VT], Lines) :-
rest_tag(Lines0, Indent, VT, Lines).
%% renamed_tag(+DeprecatedTag:atom, -Tag:atom, -Warn) is semidet.
%
% Declaration for deprecated tags.
renamed_tag(exception, throws, warning).
renamed_tag(param, arg, silent).
%% tag_order(+Tag:atom, -Order:int) is semidet.
%
% Both declares the know tags and their expected order. Currently
% the tags are forced into this order without warning. Future
% versions may issue a warning if the order is inconsistent.
:- multifile
pldoc:tag_order/2.
tag_order(Tag, Order) :-
pldoc:tag_order(Tag, Order), !.
tag_order(arg, 100).
tag_order(error, 200). % same as throw
tag_order(throws, 300).
tag_order(author, 400).
tag_order(version, 500).
tag_order(see, 600).
tag_order(deprecated, 700).
tag_order(compat, 800). % PlDoc extension
tag_order(copyright, 900).
tag_order(license, 1000).
tag_order(bug, 1100).
tag_order(tbd, 1200).
%% combine_tags(+Tags:list(tag(Key, Value)), -Tags:list) is det.
%
% Creates the final tag-list. Tags is a list of
%
% * \params(list(param(Name, Descr)))
% * \tag(Name, list(Descr))
%
% Descr is a list of tokens.
combine_tags([], []).
combine_tags([tag(arg, V1)|T0], [\args([P1|PL])|Tags]) :- !,
arg_tag(V1, P1),
arg_tags(T0, PL, T1),
combine_tags(T1, Tags).
combine_tags([tag(Tag,V0)|T0], [\tag(Tag, [V0|Vs])|T]) :-
same_tag(Tag, T0, T1, Vs),
combine_tags(T1, T).
arg_tag([PT|Descr0], arg(PN, Descr)) :-
word_of(PT, PN),
strip_leading_ws(Descr0, Descr).
word_of(w(W), W) :- !. % TBD: check non-word arg
word_of(W, W).
arg_tags([tag(arg, V1)|T0], [P1|PL], T) :- !,
arg_tag(V1, P1),
arg_tags(T0, PL, T).
arg_tags(T, [], T).
same_tag(Tag, [tag(Tag, V)|T0], T, [V|Vs]) :- !,
same_tag(Tag, T0, T, Vs).
same_tag(_, L, L, []).
/*******************************
* FACES *
*******************************/
%% wiki_faces(+Structure, +ArgNames, -HTML) is det.
%
% Given the wiki structure, analyse the content of the paragraphs,
% list items and table cells and apply font faces and links.
wiki_faces([dt(Class, \term(Text, Term, Bindings)), dd(Descr0)|T0],
ArgNames,
[dt(Class, \term(Text, Term, Bindings)), dd(Descr)|T]) :- !,
varnames(Bindings, VarNames, ArgNames),
wiki_faces(Descr0, VarNames, Descr),
wiki_faces(T0, ArgNames, T).
wiki_faces(DOM0, ArgNames, DOM) :-
structure_term(DOM0, Functor, Content0), !,
wiki_faces_list(Content0, ArgNames, Content),
structure_term(DOM, Functor, Content).
wiki_faces(Verb, _, Verb) :-
verbatim_term(Verb), !.
wiki_faces(Content0, ArgNames, Content) :-
assertion(is_list(Content0)),
phrase(wiki_faces(Content, ArgNames), Content0), !.
varnames([], List, List).
varnames([Name=_|T0], [Name|T], List) :-
varnames(T0, T, List).
wiki_faces_list([], _, []).
wiki_faces_list([H0|T0], Args, [H|T]) :-
wiki_faces(H0, Args, H),
wiki_faces_list(T0, Args, T).
%% structure_term(+Term, -Functor, -Content) is semidet.
%% structure_term(-Term, +Functor, +Content) is det.
%
% (Un)pack a term describing structure, so we can process Content
% and re-pack the structure.
structure_term(\tags(Tags), tags, [Tags]) :- !.
structure_term(\args(Params), args, [Params]) :- !.
structure_term(arg(Name,Descr), arg(Name), [Descr]) :- !.
structure_term(\tag(Name,Value), tag(Name), [Value]) :- !.
structure_term(\include(What,Type,Opts), include(What,Type,Opts), []) :- !.
structure_term(dl(Att, Args), dl(Att), [Args]) :- !.
structure_term(dt(Att, Args), dt(Att), [Args]) :- !.
structure_term(table(Att, Args), table(Att), [Args]) :- !.
structure_term(h1(Att, Args), h1(Att), [Args]) :- !.
structure_term(h2(Att, Args), h2(Att), [Args]) :- !.
structure_term(h3(Att, Args), h3(Att), [Args]) :- !.
structure_term(h4(Att, Args), h4(Att), [Args]) :- !.
structure_term(hr(Att), hr(Att), []) :- !.
structure_term(p(Args), p, [Args]) :- !.
structure_term(Term, Functor, Args) :-
functor(Term, Functor, 1),
structure_tag(Functor), !,
Term =.. [Functor|Args].
structure_tag(ul).
structure_tag(ol).
structure_tag(dl).
structure_tag(li).
structure_tag(dt).
structure_tag(dd).
structure_tag(table).
structure_tag(tr).
structure_tag(td).
structure_tag(blockquote).
structure_tag(center).
%% verbatim_term(?Term) is det
%
% True if Term must be passes verbatim.
verbatim_term(pre(_,_)).
verbatim_term(\term(_,_,_)).
%% matches(:Goal, -Input, -Last)//
%
% True when Goal runs successfully on the DCG input and Input
% is the list of matched tokens.
matches(Goal, Input, Last, List, Rest) :-
call(Goal, List, Rest),
input(List, Rest, Input, Last).
input([H|T0], Rest, Input, Last) :-
( T0 == Rest
-> Input = [H],
Last = H
; Input = [H|T],
input(T0, Rest, T, Last)
).
%% wiki_faces(-WithFaces, +ArgNames)// is nondet.
%
% Apply font-changes and automatic links to running text. The
% faces are applied after discovering the structure (paragraphs,
% lists, tables, keywords).
wiki_faces([EmphTerm|T], ArgNames) -->
emphasis_start(C),
matches(wiki_faces(Emph, ArgNames), Input, Last),
emphasis_end(C),
{ emph_markdown(Last, Input),
emphasis_term(C, Emph, EmphTerm)
}, !,
wiki_faces_int(T, ArgNames).
wiki_faces(Faces, ArgNames) -->
wiki_faces_int(Faces, ArgNames).
wiki_faces_int([], _) -->
[].
wiki_faces_int([Before,EmphTerm|T], ArgNames) -->
emphasis_before(Before),
emphasis_start(C),
matches(wiki_faces(Emph, ArgNames), Input, Last),
emphasis_end(C),
{ emph_markdown(Last, Input),
emphasis_term(C, Emph, EmphTerm)
}, !,
wiki_faces_int(T, ArgNames).
wiki_faces_int([H|T], ArgNames) -->
wiki_face(H, ArgNames),
wiki_faces(T, ArgNames).
wiki_face(var(Arg), ArgNames) -->
[w(Arg)],
{ memberchk(Arg, ArgNames)
}, !.
wiki_face(b(Bold), ArgNames) -->
[*,'|'], wiki_faces_int(Bold, ArgNames), ['|',*], !.
wiki_face(i(Italic), ArgNames) -->
['_','|'], wiki_faces_int(Italic, ArgNames), ['|','_'], !.
wiki_face(code(Code), _) -->
[=], eq_code_words(Words), [=], !,
{ atomic_list_concat(Words, Code) }.
wiki_face(code(Code), _) -->
[=,'|'], wiki_words(Code), ['|',=], !.
wiki_face(Code, _) -->
['`'], code_words(Words), ['`'],
{ atomic_list_concat(Words, Text),
catch(atom_to_term(Text, Term, Vars), _, fail), !,
code_face(Text, Term, Vars, Code)
}.
wiki_face(Face, _) -->
[ w(Name) ], arg_list(List),
{ atomic_list_concat([Name|List], Text),
catch(atom_to_term(Text, Term, Vars), _, fail),
term_face(Text, Term, Vars, Face)
}, !.
wiki_face(\predref(Name/Arity), _) -->
[ w(Name), '/' ], arity(Arity),
{ functor_name(Name)
}, !.
wiki_face(\predref(Module:(Name/Arity)), _) -->
[ w(Module), ':', w(Name), '/' ], arity(Arity),
{ functor_name(Name)
}, !.
wiki_face(\predref(Name/Arity), _) -->
prolog_symbol_char(S0),
symbol_string(SRest), [ '/' ], arity(Arity), !,
{ atom_chars(Name, [S0|SRest])
}.
wiki_face(\predref(Name//Arity), _) -->
[ w(Name), '/', '/' ], arity(Arity),
{ functor_name(Name)
}, !.
wiki_face(\predref(Module:(Name//Arity)), _) -->
[ w(Module), ':', w(Name), '/', '/' ], arity(Arity),
{ functor_name(Name)
}, !.
wiki_face(\include(Name, Type, Options), _) -->
['[','['], file_name(Base, Ext), [']',']'],
{ autolink_extension(Ext, Type), !,
file_name_extension(Base, Ext, Name),
resolve_file(Name, Options, [])
}, !.
wiki_face(\include(Name, Type, [caption(Caption)|Options]), _) -->
( ['!','['], tokens(Caption), [']','(']
-> file_name(Base, Ext), [')'],
{ autolink_extension(Ext, Type), !,
file_name_extension(Base, Ext, Name),
resolve_file(Name, Options, [])
}
), !.
wiki_face(Link, _ArgNames) --> % [[Label][Link]]
( ['[','['], tokens(LabelParts), [']','[']
-> wiki_link(Link, [label(Label), relative(true), end(']')]),
[']',']'], !,
{ make_label(LabelParts, Label) }
).
wiki_face(Link, _ArgNames) --> % Markdown: [Label](Link)
( ['['], tokens(LabelParts), [']','(']
-> wiki_link(Link, [label(Label), relative(true), end(')')]),
[')'], !,
{ make_label(LabelParts, Label) }
).
wiki_face(Link, _ArgNames) -->
wiki_link(Link, []), !.
wiki_face(Word, _) -->
[ w(Word) ], !.
wiki_face(SpaceOrPunct, _) -->
[ SpaceOrPunct ],
{ atomic(SpaceOrPunct) }, !.
wiki_face(FT, ArgNames) -->
[Structure],
{ wiki_faces(Structure, ArgNames, FT)
}.
wiki_words([]) --> [].
wiki_words([Word|T]) --> [w(Word)], !, wiki_words(T).
wiki_words([Punct|T]) --> [Punct], {atomic(Punct)}, wiki_words(T).
%% code_words(-Words)//
%
% True when Words is the content as it appears in =|`code`|=,
% where =|``|= is mapped to =|`|=.
code_words([]) --> [].
code_words([Word|T]) --> [w(Word)], code_words(T).
code_words(CodeL) --> ['`','`'], {CodeL = ['`'|T]}, code_words(T).
code_words([Punct|T]) --> [Punct], {atomic(Punct)}, code_words(T).
%% eq_code_words(-Words)//
%
% Stuff that can be between single `=`. This is limited to
%
% - Start and end must be a word
% - In between may be the following punctuation chars:
% =|.-:/|=, notably dealing with file names and
% identifiers in various external languages.
eq_code_words([Word]) -->
[ w(Word) ].
eq_code_words([Word|T]) -->
[ w(Word) ], eq_code_internals(T, [End]), [w(End)].
eq_code_internals(T, T) --> [].
eq_code_internals([H|T], Tail) -->
eq_code_internal(H),
eq_code_internals(T, Tail).
eq_code_internal(Word) -->
[w(Word)].
eq_code_internal(Punct) -->
[Punct],
{ eq_code_internal_punct(Punct) }.
eq_code_internal_punct('.').
eq_code_internal_punct('-').
eq_code_internal_punct(':').
eq_code_internal_punct('/').
%% code_face(+Text, +Term, +Vars, -Code) is det.
%
% Deal with =|`... code ...`|= sequences. Text is the matched
% text, Term is the parsed Prolog term and Code is the resulting
% intermediate code.
code_face(Text, Var, _, Code) :-
var(Var), !,
Code = var(Text).
code_face(Text, _, _, code(Text)).
%% emphasis_term(+Emphasis, +Tokens, -Term) is det.
%% emphasis_before(-Before)// is semidet.
%% emphasis_start(-Emphasis)// is semidet.
%% emphasis_end(+Emphasis)// is semidet.
%
% Primitives for Doxygen emphasis handling.
emphasis_term('_', Term, i(Term)).
emphasis_term('*', Term, b(Term)).
emphasis_term('__', Term, strong(Term)).
emphasis_term('**', Term, strong(Term)).
emph_markdown(_, [w(_)]) :- !.
emph_markdown(Last, Tokens) :-
\+ emphasis_after_sep(Last),
( catch(b_getval(pldoc_object, Obj), _, fail)
-> true
; Obj = '??'
),
debug(markdown(emphasis), '~q: additionally emphasis: ~p',
[Obj, Tokens]).
emphasis_before(Before) -->
[Before],
{ emphasis_start_sep(Before) }.
emphasis_start_sep(' ').
emphasis_start_sep('<').
emphasis_start_sep('{').
emphasis_start_sep('(').
emphasis_start_sep('[').
emphasis_start_sep(',').
emphasis_start_sep(':').
emphasis_start_sep(';').
emphasis_start(Which), [w(Word)] -->
emphasis(Which),
[w(Word)].
emphasis(**) --> [*, *].
emphasis(*) --> [*].
emphasis('__') --> ['_', '_'].
emphasis('_') --> ['_'].
emphasis_end(Which), [After] -->
emphasis(Which),
[ After ], !,
{ After \= w(_) }.
emphasis_end(Which) -->
emphasis(Which).
% these characters should not be before a closing * or _.
emphasis_after_sep(' ').
emphasis_after_sep('(').
emphasis_after_sep('[').
emphasis_after_sep('<').
emphasis_after_sep('=').
emphasis_after_sep('+').
emphasis_after_sep('\\').
emphasis_after_sep('@').
%% arg_list(-Atoms) is nondet.
%
% Atoms is a token-list for a Prolog argument list. An
% argument-list is a sequence of tokens '(' ... ')'.
%
% @bug the current implementation does not deal correctly with
% brackets that are embedded in quoted strings.
arg_list(['('|T]) -->
['('], arg_list_close(T, 1).
arg_list_close(Tokens, Depth) -->
[')'], !,
( { Depth == 1 }
-> { Tokens = [')'] }
; { Depth > 1 }
-> { Tokens = [')'|More],
NewDepth is Depth - 1
},
arg_list_close(More, NewDepth)
).
arg_list_close(['('|T], Depth) -->
['('], { NewDepth is Depth+1 },
arg_list_close(T, NewDepth).
arg_list_close([H|T], Depth) -->
[w(H)], !,
arg_list_close(T, Depth).
arg_list_close([H|T], Depth) -->
[H],
arg_list_close(T, Depth).
%% term_face(+Text, +Term, +Vars, -Face) is semidet.
%
% Process embedded Prolog-terms. Currently processes Alias(Arg)
% terms that refer to files. Future versions will also provide
% pretty-printing of Prolog terms.
term_face(_Text, Term, _Vars, \file(Name, FileOptions)) :-
ground(Term),