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(_, Attrs, CSS) :-
   58    memberchk(style=Style, Attrs),
   59    style_css_attrs(Style, CSS),
   60    text_style(CSS).
   61applicable_style(El, Attrs, CSS) :-
   62    html_text:style(El, Cond, CSS),
   63    (   eval(Cond, Attrs)
   64    ->  true
   65    ).
   66
   67eval(true, _).
   68eval(class(Class), Attrs) :-
   69    attrs_classes(Attrs, Classes),
   70    memberchk(Class, Classes).
   71
   72attrs_classes(Attrs, Classes) :-
   73    memberchk(class=Spec, Attrs),
   74    split_string(Spec, " \t\r\n", " \t\r\n", ClassStrings),
   75    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.
   83style_css_attrs(Style, CSS) :-
   84    split_string(Style, ";", " \t\r\n", Parts),
   85    convlist(style_css_attr, Parts, CSS).
   86
   87style_css_attr(Style, CSS) :-
   88    split_string(Style, ":", " \t\r\n", [NameS,ValueS]),
   89    atom_string(Name, NameS),
   90    atom_string(Value, ValueS),
   91    CSS =.. [Name,Value].
   92
   93text_style(float(right)).
 css_block_options(+CSS, +Margins0, -Margins, -ParOptions, -Style)
   99css_block_options(CSS, Top0-Bottom0, Top-Bottom, ParOptions, Style) :-
  100    option(margin_top(Top), CSS, Top0),
  101    option(margin_bottom(Bottom), CSS, Bottom0),
  102    convlist(par_option, CSS, ParOptions),
  103    convlist(font_style, CSS, Style).
  104
  105par_option(text_align(Align),   text_align(Align)).
  106par_option(margin_left(Align),  margin_left(Align)).
  107par_option(margin_right(Align), margin_right(Align)).
  108
  109font_style(font_weight(bold),     bold).
  110font_style(font_weight(normal),   normal).
  111font_style(color(BC),             hfg(C)) :- atom_concat(bright_, C, BC).
  112font_style(color(C),              fg(C)).
  113font_style(background(BC),        hbg(C)) :- atom_concat(bright_, C, BC).
  114font_style(background(C),         bg(C)).
  115font_style(text_decoration(none), underline(false)).
 css_inline_options(+CSS, -Margins, -Style)
  121css_inline_options(CSS, Left-Right, Style) :-
  122    option(margin_left(Left), CSS, 0),
  123    option(margin_right(Right), CSS, 0),
  124    convlist(inline_style, CSS, Style).
  125
  126inline_style(CSS, Style) :-
  127    font_style(CSS, Style),
  128    !.
  129inline_style(float(right),          float(right))