Daniel@0: /* Part of DML (Digital Music Laboratory) Daniel@0: Copyright 2014-2015 Samer Abdallah, University of London Daniel@0: Daniel@0: This program is free software; you can redistribute it and/or Daniel@0: modify it under the terms of the GNU General Public License Daniel@0: as published by the Free Software Foundation; either version 2 Daniel@0: of the License, or (at your option) any later version. Daniel@0: Daniel@0: This program is distributed in the hope that it will be useful, Daniel@0: but WITHOUT ANY WARRANTY; without even the implied warranty of Daniel@0: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Daniel@0: GNU General Public License for more details. Daniel@0: Daniel@0: You should have received a copy of the GNU General Public Daniel@0: License along with this library; if not, write to the Free Software Daniel@0: Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA Daniel@0: */ Daniel@0: Daniel@0: :- module(htmlutils, Daniel@0: [ link//2 Daniel@0: , style//1 Daniel@0: , script//1 Daniel@0: , use_font//2 Daniel@0: , use_font//3 Daniel@0: , paginator//3 Daniel@0: , element//2 Daniel@0: ]). Daniel@0: Daniel@0: :- use_module(library(http/html_write)). Daniel@0: :- use_module(library(http/html_head)). Daniel@0: :- use_module(library(http/http_path)). Daniel@0: :- use_module(library(http/http_dispatch)). Daniel@0: :- use_module(library(http/http_server_files)). Daniel@0: Daniel@0: :- multifile user:body//1, user:body//2. Daniel@0: :- multifile user:head//1, user:head//2. Daniel@0: % :- multifile user:style/2. Daniel@0: Daniel@0: :- set_prolog_flag(double_quotes,string). Daniel@0: Daniel@0: % :- setting(appname, string, "anApp", "Application name"). Daniel@0: Daniel@0: :- meta_predicate element(+,:,?,?). Daniel@0: element(Element,Content) --> { Item=..[Element, Content] }, html(Item). Daniel@0: Daniel@0: link(Id,Text) --> html(a(href(location_by_id(Id)),Text)). Daniel@0: style(Loc) --> {http_absolute_location(Loc,Ref,[])}, html(link([rel(stylesheet), href(Ref)],[])). Daniel@0: script(Loc) --> {http_absolute_location(Loc,Ref,[])}, html(script(src(Ref),[])). Daniel@0: Daniel@0: http:location(googlefonts,"//fonts.googleapis.com",[]). Daniel@0: Daniel@0: user:term_expansion((:- googlefont(Name,Family)), Decls) :- Daniel@0: Decls = [ (:- html_resource(Name,[virtual(true),requires(googlefonts(Query))])) Daniel@0: , (:- html_resource(googlefonts(Query),[mime_type(text/css)])) Daniel@0: ], Daniel@0: string_concat("css?family=",Family,Query). Daniel@0: Daniel@0: Daniel@0: use_font(Family,Resource) --> Daniel@0: html_requires(Resource), Daniel@0: html_post(head, style(type="text/css","html,body,h1,h2,h3,h4,h5,h6,select,input,.btn {font-family: '~s';}"-[Family])). Daniel@0: Daniel@0: use_font(Elements,Family,Resource) --> Daniel@0: html_requires(Resource), Daniel@0: html_post(head, style(type="text/css","~w {font-family: '~s';}"-[Elements,Family])). Daniel@0: Daniel@0: paginator(_,_,1) --> []. Daniel@0: paginator(Handler,Page,Pages) --> Daniel@0: html(div(class('text-centered'), Daniel@0: ul(class(pagination), Daniel@0: [ li(\prev(Page,Pages,Handler)) Daniel@0: , li(\page_link(Handler,1,1)) Daniel@0: , li(span("~d of ~d"-[Page,Pages])) Daniel@0: , li(\page_link(Handler,Pages,Pages)) Daniel@0: , li(\next(Page,Pages,Handler)) Daniel@0: ]))). Daniel@0: Daniel@0: prev(1,_,_) --> !,html(span(class(inactive),&(larr))). Daniel@0: prev(N,_,Handler) --> { succ(M,N) }, page_link(Handler,M,&(larr)). Daniel@0: Daniel@0: next(N,N,_) --> !,html(span(class(inactive),&(rarr))). Daniel@0: next(N,_,Handler) --> { succ(N,M) }, page_link(Handler,M,&(rarr)). Daniel@0: Daniel@0: page_link(ID-Params,N,Content) --> Daniel@0: { http_link_to_id(ID,[page(N)|Params],Link) }, Daniel@0: html(a(href(Link),Content)). Daniel@0: Daniel@0: