【问题标题】:How to use SWI Prolog's http/html_write library to write HTML to a file如何使用 SWI Prolog 的 http/html_write 库将 HTML 写入文件
【发布时间】:2021-02-19 21:11:26
【问题描述】:

我正在尝试使用 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

【问题讨论】:

    标签: html prolog swi-prolog


    【解决方案1】:

    您的代码已经写入了一个字符串。要将其写入映射到文件的流,只需使用此 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。

    【讨论】:

    • 我按照您的建议尝试了print_html/2open('mypage.html', write, Stream), print_html(Stream, [html(head(title('Hello')), body([h1('Hello'), p(id('my-id'), 'This is a paragraph.')]))]).,但 Prolog 结构写入mypage.html 而不是 HTML。
    • 我已经知道如何在 localhost 上提供 HTML 页面。那是不是的问题。问题是要求一个将 HTML 的 Prolog 表示形式转换为字符串然后写入文件的最小示例。
    猜你喜欢
    • 2014-05-09
    • 2011-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多