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-2017, 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).
   86path_alias('/_CWD_/', Dir) :-
   87    working_directory(Dir0, Dir0),
   88    ensure_slash_end(Dir0, Dir).
   89
   90
   91                 /*******************************
   92                 *      MISC PATH OPERATIONS    *
   93                 *******************************/
   94
   95%!  ensure_slash_end(+Dir, -DirSlash) is det.
   96%
   97%   Ensure Dir ends with a /.
   98
   99ensure_slash_end(Dir, Dir) :-
  100    sub_atom(Dir, _, _, 0, /),
  101    !.
  102ensure_slash_end(Dir0, Dir) :-
  103    atom_concat(Dir0, /, Dir).
  104
  105                 /*******************************
  106                 *          PREDICATES          *
  107                 *******************************/
  108
  109%!  atom_pi(+Atom, -PI) is det.
  110%
  111%   Translate an external predicate indicator   representated  as an
  112%   atom  into  a  predicate  indicator    term.  If  Atom  contains
  113%   <module>:, PI is qialified. If no arity is provided it is a term
  114%   Name/_, i.e., with unbound arity.
  115
  116
  117atom_pi(Atom, Module:PI) :-
  118    atomic_list_concat([Module, PIAtom], :, Atom),
  119    Module \== '',
  120    forall(sub_atom(Module, _,1,_,C), char_type(C,alnum)),
  121    !,
  122    atom_pi2(PIAtom, PI).
  123atom_pi(Atom, PI) :-
  124    atom_pi2(Atom, PI).
  125
  126atom_pi2(Atom, Name//Arity) :-
  127    sub_atom(Atom, B, _, A, //),
  128    sub_atom(Atom, _, A, 0, ArityA),
  129    atom_number(ArityA, Arity),
  130    !,
  131    sub_atom(Atom, 0, B, _, Name).
  132atom_pi2(Atom, Name/Arity) :-
  133    sub_atom(Atom, B, _, A, /),
  134    sub_atom(Atom, _, A, 0, ArityA),
  135    atom_number(ArityA, Arity),
  136    !,
  137    sub_atom(Atom, 0, B, _, Name).
  138atom_pi2(Name, Name/_)