我正在尝试做一些非常简单的事情...发出一些 HTML 内容,其中包含指向我的样式表的链接,如此处所述,在 SWI-Prolog 上页面:重新定位 CSS 和 JavaScript 链接的 HTML:
http://www.swi-prolog.org/pldoc/man?section=html-post
唯一包含的库是:
:- use_module(library(http/html_write)).
我有用于输出 CSS 内容的通用 dcg 规则,完全按照示例:
css(URL) -->
html_post(css,
link([ type('text/css'),
rel('stylesheet'),
href(URL)
])).
然后该示例显示“接下来我们插入唯一的 CSS 链接...”,但给出的示例未提供要插入的唯一 URL 的参数。所以我猜测:
reply_html_page([title('mytitle'), \html_receive(css('/my.css'))],
[
div([id='mydivid'], 'divstuff..'
)
]).
但是运行它不会按预期输出 CSS 内容...标题已处理,但 CSS 链接丢失。
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>
查看跟踪,CSS dcg 规则从未被执行(版本 6.6.4)。
也许解决方案需要包含 html_receive/2(+Id, :Handler),而不是/1,但没有简单的复制/粘贴/运行示例来解释其用法。 html_receive/2 的代码片段包含 html_post。
最佳答案
我在 html_post//1
和 html_receive//1
中使用了简单/原子标识符,既使示例更容易,又更接近 SWI- Prolog 文档。该标识符也可以是一个化合物,但我没有在您的示例中使用它。
以下代码将打开包含生成的 HTML 页面的 Web 浏览器。标准 HTML 检查工具显示 link
元素现已存在。
:- 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)])
).
编辑:根据@magus的评论,我在标题中包含了生成带有CSS链接的HTML页面的“正常”方式,即不使用html_post//1
和 html_receive//1
。这对于与其他代码片段进行比较可能很有用。
:- 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.')).
子句 a_css_link//1
可以根据请求重用于其他 URI。
希望这有帮助!
关于css - 无法使用 html_receive 和 html_post 发出 CSS 链接 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22905530/