Question

I am trying to do something very straightforward... emit some HTML content with a link to my style sheet, as explained here, on the SWI-Prolog page: Repositioning HTML for CSS and JavaScript links:

http://www.swi-prolog.org/pldoc/man?section=html-post

The only included library is:

:- use_module(library(http/html_write)).

I have my generic dcg rule for outputting the CSS content, exactly as per the example:

css(URL) -->
    html_post(css,
      link([ type('text/css'),
             rel('stylesheet'),
             href(URL)
           ])).

Then the example says 'Next we insert the unique CSS links...', but the example given doesn't supply a parameter for the unique URL to be inserted. So I'm guessing at this:

reply_html_page([title('mytitle'), \html_receive(css('/my.css'))],
[
  div([id='mydivid'], 'divstuff..'
  )
]).

But running it does not output the CSS content as expected... The title gets processed, but the CSS link is missing.

Content-type: text/html; charset=UTF-8

<!DOCTYPE html>
<html>
    <head>
        <title>mytitle</title>

        <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    </head>

    <body>
        <div id="mydivid">divstuff..</div>
    </body>
</html>

Looking at the trace, the CSS dcg rule never gets executed (version 6.6.4).

Perhaps the solution needs to include html_receive/2(+Id, :Handler), rather than /1, but there are no simple copy/paste/run examples which would explain its usage. The code snippet for html_receive/2 contains html_post.

Was it helpful?

Solution

I have used a simple/atomic identifier in html_post//1 and html_receive//1, both to make the example easier and to stay closer to the SWI-Prolog documentation. This identifier can also be a compound, but I did not get the use for that in your example.

The following code opens a Web browser with the generated HTML page. Standard HTML inspection tools show that the link element is there now.

:- module(css_include, []).

:- use_module(library(http/html_write)).
:- use_module(library(http/http_dispatch)).
:- use_module(library(http/thread_httpd)).
:- use_module(library(www_browser)).

:- http_server(http_dispatch, [port(5000)]).
:- http_handler(root(test), test, []).

test(_):-
  reply_html_page(
    [title('mytitle'),\html_receive(my_css_link)],
    \html_body_stuff
  ),
  www_open_url('http://localhost:5000/test').

html_body_stuff -->
  {Url = 'http://www.swi-prolog.org'},
  html_post(
    my_css_link,
    link([type('text/css'),rel('stylesheet'),href(Url)])
  ).

Edit: Based on a comment by @magus I have included the 'normal' way of generating an HTML page with a CSS link in the header, i.e. without using the html_post//1 and html_receive//1. This may be useful for comparison with the other code snippet.

:- module(css_include, []).

:- use_module(library(http/html_write)).
:- use_module(library(http/http_dispatch)).
:- use_module(library(http/thread_httpd)).
:- use_module(library(www_browser)).

:- http_server(http_dispatch, [port(5000)]).
:- http_handler(root(test), test, []).

test(_):-
  reply_html_page(
    [title('mytitle'),\a_css_link('http://www.swi-prolog.org')],
    \html_body_stuff
  ).

a_css_link(Url) -->
  html(link([type('text/css'),rel('stylesheet'),href(Url)])).

html_body_stuff -->
  html(h1('HTML body over here.')).

The clause a_css_link//1 can be reused for other URIs as requested.

Hope this helps!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top