2013-01-18 25 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來包含它。