2013-01-18 27 views
6

如果我已将端口9000上的Prolog HTTP服务器绑定到localhost,我该如何让Prolog为我的映像生成正确的路径?例如,如果我的.pl文件和.jpg.png文件所在的桌面,如何使服务器上生成这样的代码:如何在Prolog中动态提供的HTML页面中包含图像?

<img src="C:\Users\Luka\\Desktop\ImageName.ext"/>,其中“转”代表扩展。我看了一下SWI-Prolog和this tutorial的文档,但是我发现所有这些抽象路径非常混乱。我在Web服务器方面有很多经验,但是这有很大的不同,并且我对理解它有很多困难。

这里是我的尝试,我已经学到了什么(或者至少我觉得我有)由整个SWI-Prolog的文件和上面提到的教程:

:- use_module(library(http/thread_httpd)). 
:- use_module(library(http/http_dispatch)). 
:- use_module(library(http/http_parameters)). 
:- use_module(library(http/html_write)). 
file_search_path('*', 'C:\\Users\\Luka\\Desktop\\'). 

server(Port) :- 
    http_server(http_dispatch, [port(Port)]). 

:- http_handler(root(.), render_base, []). 
:- http_handler('/form', process_form, []). 

process_form(Request) :- 
    http_parameters(Request, 
      [name(Name,[atom])]), 
      reply_html_page('Posted data: ',['',Name]). 

render_base(_Request) :- 
    reply_html_page(
     title('Naslov'), 
     img([src='/image.png', alt='Incident']) 
    ). 

再次感谢提前你的巨大耐心。 :-)

回答

7

确实,解决问题并不简单。请仔细阅读this'如何'页面,部分Serving many 'server support' files

这里是我测试的代码:

http:location(images, root(images), []). 
user:file_search_path(icons, '/home/carlo/prolog'). 
:- http_handler(images(.), serve_files_in_directory(icons), [prefix]). 

和使用的HTML资源

intro --> 
    html([p(ol([li('select a path'), 
      li('scan resources from it'), 
      li('RDF-ize them'), 
      li('browse with foldable SVG') 
      ])), 
      \sep, 
      'png before', img(src='images/swipl.png'), 'png after', 
      \sep, 
      'jpeg before', img(src='/images/swipl.jpeg'), 'jpeg after' 
     ]). 

我注意到,这两个规格img(src='images/swipl.png')img(src='/images/swipl.jpeg')工作,而这个 '功能' 有助于模糊界面行为。

这里输出

enter image description here

HTH

+0

它适用于css和js文件吗? –

1

记住序言服务器确实是服务器,而不是一个插件到Apache。 这意味着您需要有prolog为图像提供服务。当然,让Web服务器发送静态文件非常方便,所以Carlo的例子确实既服务于图像目录,也使用html来包含它。