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)  2007-2011, University of Amsterdam
   7    All rights reserved.
   8
   9    Redistribution and use in source and binary forms, with or without
  10    modification, are permitted provided that the following conditions
  11    are met:
  12
  13    1. Redistributions of source code must retain the above copyright
  14       notice, this list of conditions and the following disclaimer.
  15
  16    2. Redistributions in binary form must reproduce the above copyright
  17       notice, this list of conditions and the following disclaimer in
  18       the documentation and/or other materials provided with the
  19       distribution.
  20
  21    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  28    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  29    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32    POSSIBILITY OF SUCH DAMAGE.
  33*/
  34
  35:- module(doc_util,
  36          [ insert_alias/2,             % +Path, -Aliased
  37            expand_alias/2,             % +Aliased, -Path
  38            ensure_slash_end/2,         % +Dir, -DirSlash
  39            atom_pi/2                   % +Atom, -PI
  40          ]).
  41
  42/** <module> PlDoc utilities
  43
  44@author Jan Wielemaker
  45*/
  46
  47                 /*******************************
  48                 *     PATH ALIAS PROCESSING    *
  49                 *******************************/
  50
  51%!  insert_alias(+Path0, -Path) is det.
  52%
  53%   Translate a native path to  an   aliased  path. Path aliases are
  54%   defined by path_alias/2. Aliased paths   are  re-translated into
  55%   native form using expand_alias/2.
  56
  57insert_alias(Path0, Path) :-
  58    path_alias(Alias, Prefix),
  59    atom_concat(Prefix, PostFix, Path0),
  60    !,
  61    atom_concat(Alias, PostFix, Path).
  62insert_alias(Path, Path).
  63
  64
  65%!  expand_alias(+Path0, -Path) is det.
  66%
  67%   Translate an aliased path to a native path.
  68
  69expand_alias(Path0, Path) :-
  70    path_alias(Alias, Prefix),
  71    atom_concat(Alias, Postfix, Path0),
  72    !,
  73    atom_concat(Prefix, Postfix, Path).
  74expand_alias(Path, Path).
  75
  76
  77%!  path_alias(?Alias, ?Path) is nondet.
  78%
  79%   True if Alias: is an alias  for   Path.  This is used to rewrite
  80%   paths below the SWI-Prolog home to   give them shorter and fixed
  81%   names.
  82
  83path_alias('/swi/', Dir) :-
  84    current_prolog_flag(home, Dir0),
  85    ensure_slash_end(Dir0, Dir).
  86
  87
  88                 /*******************************
  89                 *      MISC PATH OPERATIONS    *
  90                 *******************************/
  91
  92%!  ensure_slash_end(+Dir, -DirSlash) is det.
  93%
  94%   Ensure Dir ends with a /.
  95
  96ensure_slash_end(Dir, Dir) :-
  97    sub_atom(Dir, _, _, 0, /),
  98    !.
  99ensure_slash_end(Dir0, Dir) :-
 100    atom_concat(Dir0, /, Dir).
 101
 102                 /*******************************
 103                 *          PREDICATES          *
 104                 *******************************/
 105
 106%!  atom_pi(+Atom, -PI) is det.
 107%
 108%   Translate an external predicate indicator   representated  as an
 109%   atom  into  a  predicate  indicator    term.  If  Atom  contains
 110%   <module>:, PI is qialified. If no arity is provided it is a term
 111%   Name/_, i.e., with unbound arity.
 112
 113
 114atom_pi(Atom, Module:PI) :-
 115    atomic_list_concat([Module, PIAtom], :, Atom),
 116    Module \== '',
 117    forall(sub_atom(Module, _,1,_,C), char_type(C,alnum)),
 118    !,
 119    atom_pi2(PIAtom, PI).
 120atom_pi(Atom, PI) :-
 121    atom_pi2(Atom, PI).
 122
 123atom_pi2(Atom, Name//Arity) :-
 124    sub_atom(Atom, B, _, A, //),
 125    sub_atom(Atom, _, A, 0, ArityA),
 126    atom_number(ArityA, Arity),
 127    !,
 128    sub_atom(Atom, 0, B, _, Name).
 129atom_pi2(Atom, Name/Arity) :-
 130    sub_atom(Atom, B, _, A, /),
 131    sub_atom(Atom, _, A, 0, ArityA),
 132    atom_number(ArityA, Arity),
 133    !,
 134    sub_atom(Atom, 0, B, _, Name).
 135atom_pi2(Name, Name/_).