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
  43/** <module> Source colouring support
  44
  45Provide   hierarchical   colouring   information   on     top   of   the
  46library(prolog_colour). We need  ordered   hierarchical  information  to
  47create HTML fragments.
  48*/
  49
  50:- thread_local
  51    fragment/3.                     % Start, Length, Class
  52
  53:- create_prolog_flag(xref, false, [type(boolean)]).
  54
  55%!  colour_fragments(+In, -Fragments:list) is det.
  56%
  57%   Create a list of colour fragments from In.
  58%
  59%   @param Fragments        List of fragment(Start, End, Class)
  60
  61colour_fragments(Source, Fragments) :-
  62    F = fragment(_,_,_),
  63    retractall(F),
  64    prolog_canonical_source(Source, SourceID),
  65    xref_source(SourceID, [silent(true)]),
  66    setup_call_cleanup(
  67        prolog_open_source(SourceID, Stream),
  68        prolog_colourise_stream(Stream, SourceID, assert_fragment),
  69        prolog_close_source(Stream)),
  70    findall(F, retract(F), Fragments0),
  71    sort(Fragments0, Fragments1),
  72    fragment_hierarchy(Fragments1, Fragments).
  73
  74assert_fragment(Class, Start, Length) :-
  75    End is Start+Length,
  76    assert(fragment(Start, End, Class)).
  77
  78
  79%!  fragment_hierarchy(+Fragments, -Hierarchy) is det.
  80%
  81%   Translate   list   of   fragment(Start,     End,   Class)   into
  82%   fragment(Start, End, Class, SubFragments).
  83%
  84%   @tbd    Detect improper nesting.  How to handle?
  85
  86fragment_hierarchy([], []).
  87fragment_hierarchy([fragment(S,E,C)|Rest0], [fragment(S,E,C,Sub)|Rest]) :-
  88    sub_fragments(Rest0, E, Sub, Rest1),
  89    fragment_hierarchy(Rest1, Rest).
  90
  91sub_fragments([], _, [], []).
  92sub_fragments([F|R0], End, Sub, Rest) :-
  93    F = fragment(SF,EF,C),
  94    (   EF =< End
  95    ->  Sub = [fragment(SF,EF,C,FSub)|RSub],
  96        sub_fragments(R0, EF, FSub, R1),
  97        sub_fragments(R1, End, RSub, Rest)
  98    ;   Sub = [],
  99        Rest = [F|R0]
 100    ).