问题描述
我正在尝试使用 SWI Prolog 的 library(http/html_write)
生成 HTML 文件。我想生成下面的 HTML 并将其写入名为“mypage.html”的文件:
<!DOCTYPE html>
<html>
<head>
<title>Hello</title>
</head>
<body>
<h1>Hello</h1>
<p id="my-id">This is a paragraph.</p>
</body>
</html>
到目前为止,我已经编写了 HTML 的 Prolog 表示:
html(head(title('Hello')),body([h1('Hello'),p(id('my-id'),'This is a paragraph.')]))
现在呢?我如何实际将此表示转换为一个字符串,然后我将写入一个名为“mypage.html”的文件?我已阅读文档 (Examples for using the HTML write library),但我无法理解如何将 HTML 表示形式转换为字符串。
我尝试使用 html_write:print_html/1
,但它所做的只是像我写的那样打印结构:
$ swipl --quiet
?- use_module(library(http/html_write)).
true.
?- print_html([html(head(title('Hello')),| body([h1('Hello'),| p(id('my-id'),'This is a paragraph.')]))]).
html(head(title(Hello)),body([h1(Hello),p(id(my-id),This is a paragraph.)]))
true.
?-
您能否提供一个最小的工作示例,将 HTML 的 Prolog 表示形式转换为字符串,然后将其写入文件?
更新:我在 SWi Prolog 的论坛上交叉发布了这个问题并收到了回复:How to use the http/html_write library to write HTML to a file
解决方法
您的代码已经写入了一个字符串。要将其写入映射到文件的流,只需使用此 print_html(+Stream,+List)。
也许您想要一个使用 html 响应 http://localhost:8080/hello_world 的网站的最小工作示例
:- use_module(library(http/thread_httpd)).
:- use_module(library(http/http_dispatch)).
:- use_module(library(http/html_write)).
:- http_handler(root(hello_world),say_hi,[]).
:- http_server(http_dispatch,[port(8080)]).
say_hi(_Request) :-
reply_html_page(title('Hello World'),[ h1('Hello World'),p(['This example demonstrates generating HTML ','messages from Prolog'
])
]).
查看此文件并将浏览器指向给定的 URI。