View source with formatted comments or as raw
    1/*  Part of SWI-Prolog
    2
    3    Author:        Jan Wielemaker
    4    E-mail:        J.Wielemaker@vu.nl
    5    WWW:           http://www.swi-prolog.org
    6    Copyright (c)  2006-2013, University of Amsterdam
    7                              VU University Amsterdam
    8    All rights reserved.
    9
   10    Redistribution and use in source and binary forms, with or without
   11    modification, are permitted provided that the following conditions
   12    are met:
   13
   14    1. Redistributions of source code must retain the above copyright
   15       notice, this list of conditions and the following disclaimer.
   16
   17    2. Redistributions in binary form must reproduce the above copyright
   18       notice, this list of conditions and the following disclaimer in
   19       the documentation and/or other materials provided with the
   20       distribution.
   21
   22    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   23    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   24    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   25    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   26    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   27    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   28    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   29    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   30    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   31    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   32    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   33    POSSIBILITY OF SUCH DAMAGE.
   34*/
   35
   36:- module(pldoc_colours,
   37          [ colour_fragments/2          % +Source, -Fragments
   38          ]).   39:- use_module(library(prolog_xref)).   40:- use_module(library(prolog_source)).   41:- use_module(library(prolog_colour)).   42:- use_module(library(lists)).   43
   44/** <module> Source colouring support
   45
   46Provide   hierarchical   colouring   information   on     top   of   the
   47library(prolog_colour). We need  ordered   hierarchical  information  to
   48create HTML fragments.
   49*/
   50
   51:- thread_local
   52    fragment/3.                     % Start, Length, Class
   53
   54:- create_prolog_flag(xref, false, [type(boolean)]).   55
   56%!  colour_fragments(+In, -Fragments:list) is det.
   57%
   58%   Create a list of colour fragments from In.
   59%
   60%   @param Fragments List of fragment(Start, End, Class, Subs)
   61
   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)).
   80
   81%!  include_fullstops(+FragmentsIn, -FragmentsOut) is det.
   82%
   83%   Include fullstops into the term that preceeds them.
   84
   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).
   94
   95
   96%!  fragment_hierarchy(+Fragments, -Hierarchy) is det.
   97%
   98%   Translate   list   of   fragment(Start,     End,   Class)   into
   99%   fragment(Start, End, Class, SubFragments).
  100%
  101%   @tbd    Detect improper nesting.  How to handle?
  102
  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    )