35
36:- module(pldoc_colours,
37 [ colour_fragments/2 38 ]). 39:- use_module(library(prolog_xref)). 40:- use_module(library(prolog_source)). 41:- use_module(library(prolog_colour)). 42:- use_module(library(lists)).
51:- thread_local
52 fragment/3. 53
54:- create_prolog_flag(xref, false, [type(boolean)]).
62colour_fragments(Source, Hierarchy) :-
63 F = fragment(_,_,_),
64 retractall(F),
65 prolog_canonical_source(Source, SourceID),
66 xref_source(SourceID, [silent(true)]),
67 setup_call_cleanup(
68 prolog_open_source(SourceID, Stream),
69 prolog_colourise_stream(Stream, SourceID, assert_fragment),
70 prolog_close_source(Stream)),
71 findall(F, retract(F), Fragments0),
72 sort(2, >=, Fragments0, Fragments1),
73 sort(1, =<, Fragments1, Fragments2),
74 fragment_hierarchy(Fragments2, Hierarchy0),
75 include_fullstops(Hierarchy0, Hierarchy).
76
77assert_fragment(Class, Start, Length) :-
78 End is Start+Length,
79 assert(fragment(Start, End, Class)).
85include_fullstops([], []).
86include_fullstops([fragment(TS,FS,Class,Subs0),fragment(FS,FE,fullstop,[])|T0],
87 [fragment(TS,FE0,Class,Subs)|T]) :-
88 !,
89 append(Subs0, [fragment(FS,FE0,fullstop,[])], Subs),
90 FE0 is FE-1,
91 include_fullstops(T0, T).
92include_fullstops([H|T0], [H|T]) :-
93 include_fullstops(T0, T).
103fragment_hierarchy([], []).
104fragment_hierarchy([fragment(S,E,C)|Rest0], [fragment(S,E,C,Sub)|Rest]) :-
105 sub_fragments(Rest0, E, Sub, Rest1),
106 fragment_hierarchy(Rest1, Rest).
107
108sub_fragments([], _, [], []).
109sub_fragments([F|R0], End, Sub, Rest) :-
110 F = fragment(SF,EF,C),
111 ( EF =< End
112 -> Sub = [fragment(SF,EF,C,FSub)|RSub],
113 sub_fragments(R0, EF, FSub, R1),
114 sub_fragments(R1, End, RSub, Rest)
115 ; Sub = [],
116 Rest = [F|R0]
117 )
Source colouring support
Provide hierarchical colouring information on top of the
library(prolog_colour)
. We need ordered hierarchical information to create HTML fragments. */