View source with raw 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)  2018, CWI 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(format_style,
   36          [ element_css/3,             % +El, +Attrs, -CSS
   37            css_block_options/5,       % +CSS, +M0, -Margins, -ParOptions, -Style
   38            css_inline_options/3,      % +CSS, -Margins, -Style
   39            attrs_classes/2,           % +Attrs, -Classes
   40            style_css_attrs/2          % +Style, -Properties
   41          ]).   42:- autoload(library(apply),[maplist/3,convlist/3]).   43:- autoload(library(lists),[append/2,list_to_set/2]).   44:- autoload(library(option),[option/3]).   45
   46:- multifile
   47    html_text:style/3.
 element_css(+El, +Attrs, -CSS) is semidet
   51element_css(El, Attrs, CSS) :-
   52    findall(CSSs, applicable_style(El, Attrs, CSSs), CssList),
   53    CssList \== [],
   54    append(CssList, CSS0),
   55    list_to_set(CSS0, CSS).
   56
   57applicable_style(a, Attrs, [href(HREF)]) :- % href(HREF) is a pseudo style.
   58    memberchk(href=HREF, Attrs).
   59applicable_style(_, Attrs, CSS) :-
   60    memberchk(style=Style, Attrs),
   61    style_css_attrs(Style, CSS0),
   62    include(text_style, CSS0, CSS).
   63applicable_style(El, Attrs, CSS) :-
   64    html_text:style(El, Cond, CSS),
   65    (   eval(Cond, Attrs)
   66    ->  true
   67    ).
   68
   69eval(true, _).
   70eval(class(Class), Attrs) :-
   71    attrs_classes(Attrs, Classes),
   72    memberchk(Class, Classes).
   73
   74attrs_classes(Attrs, Classes) :-
   75    memberchk(class=Spec, Attrs),
   76    split_string(Spec, " \t\r\n", " \t\r\n", ClassStrings),
   77    maplist(atom_string, Classes, ClassStrings).
 style_css_attrs(+Style, -CSS:list) is det
Convert a style description into a list Property(Value).
bug
- : far too simple.
   85style_css_attrs(Style, CSS) :-
   86    split_string(Style, ";", " \t\r\n", Parts),
   87    convlist(style_css_attr, Parts, CSS).
   88
   89style_css_attr(Style, CSS) :-
   90    split_string(Style, ":", " \t\r\n", [NameS,ValueS]),
   91    atom_string(Name, NameS),
   92    atom_string(Value, ValueS),
   93    CSS =.. [Name,Value].
   94
   95text_style(float(right)).
 css_block_options(+CSS, +Margins0, -Margins, -ParOptions, -Style)
  101css_block_options(CSS, Top0-Bottom0, Top-Bottom, ParOptions, Style) :-
  102    option(margin_top(Top), CSS, Top0),
  103    option(margin_bottom(Bottom), CSS, Bottom0),
  104    convlist(par_option, CSS, ParOptions),
  105    convlist(font_style, CSS, Style).
  106
  107par_option(text_align(Align),   text_align(Align)).
  108par_option(margin_left(Align),  margin_left(Align)).
  109par_option(margin_right(Align), margin_right(Align)).
  110
  111font_style(font_weight(bold),     bold).
  112font_style(font_weight(normal),   normal).
  113font_style(color(BC),             hfg(C)) :- atom_concat(bright_, C, BC).
  114font_style(color(C),              fg(C)).
  115font_style(background(BC),        hbg(C)) :- atom_concat(bright_, C, BC).
  116font_style(background(C),         bg(C)).
  117font_style(text_decoration(none), underline(false)).
 css_inline_options(+CSS, -Margins, -Style)
  123css_inline_options(CSS, Left-Right, Style) :-
  124    option(margin_left(Left), CSS, 0),
  125    option(margin_right(Right), CSS, 0),
  126    convlist(inline_style, CSS, Style).
  127
  128inline_style(CSS, Style) :-
  129    font_style(CSS, Style),
  130    !.
  131inline_style(float(right), float(right)).
  132inline_style(href(HREF),   href(HREF))