HTML in RDF/Notation3

Based on a little scribbling of DanC's in #rdfig:-

   <DanC> timbl did some sketching a while ago of what HTML
     should look like in n3: [ :p ("this is a paragraph with" [ :em
     ("emphasized")] "text in it")]. or something like that.
 - http://ilrt.org/discovery/chatlogs/rdfig/2001-12-13.html#T15-58-22

I wrote up a fuller HTML document in N3:-

[[[
@prefix h: <http://infomesh.net/2002/html#> .
@prefix : <http://infomesh.net/2002/html-attrs#> .

[:lang "en"; h:html (
   [h:head (
      [h:title ("HTML in Notation3 Test Document")]
      [:rel "stylesheet"; :type "text/css"; :href <style.css>; h:link ()]
   )]
   [h:body (
      [h:h1 ("HTML in N3")]
      [h:p ("This is a test document.")]
      [h:address (
        [:href <http://purl.org/net/sbp/>;
         :title "A Homepage Of Sean B. Palmer"; h:a ("Sean B. Palmer")]
      )]
   )]
)] .
]]] - <<html.n3>>

and then wrote a little filter to transform it into XHTML:-

[[[
@prefix h: <http://infomesh.net/2002/html#> .
@prefix : <http://infomesh.net/2002/html-attrs#> .
@prefix daml: <http://www.daml.org/2001/03/daml+oil#> .
@prefix log: <http://www.w3.org/2000/10/swap/log#> .
@prefix string: <http://www.w3.org/2000/10/swap/string#> .
@prefix b: <blargh-hack#> .

this log:forAll :x, :y, :z, :f, :r , :p, :u .

{ :x h:html [] } log:implies { :x :xmlns "http://www.w3.org/1999/xhtml" } .

{ :x daml:first [ log:rawType log:Literal ]; log:uri :u }
   log:implies { :x b:genID :u; a b:NoChild } .

{ :x [ log:uri [ string:startsWith "http://infomesh.net/2002/html" ] ] [];
     log:uri :u } log:implies { :x b:genID :u } .

{ :x b:genID :u . (:u "_g(\\d)$") string:scrape :y .
  :z string:concat ("00" :y) } log:implies { :x b:id :z } .

{ :x b:genID :u . (:u "_g(\\d\\d)$") string:scrape :y .
  :z string:concat ("0" :y) } log:implies { :x b:id :z } .

{ :x b:genID :u . (:u "_g(\\d\\d\\d)$") string:scrape :y }
   log:implies { :x b:id :y } .

{ [] [ log:uri :p ] []; b:id :u .
  :p string:startsWith "http://infomesh.net/2002/html#" . # quicker
  (:p "^http://infomesh.net/2002/html#(.+)$") string:scrape :r .
  :f string:concat ("<" :r) } # " id=\"" :u "\""
   log:implies { :u log:outputString :f } .

{ [] [ log:uri :p ] :z; b:id :u . :x string:concat (:u "a") .
  :p string:startsWith "http://infomesh.net/2002/html-attrs#" .
  (:p "^http://infomesh.net/2002/html-attrs#(.+)$") string:scrape :r .
  :f string:concat (" " :r "=\"" :z "\"") }
   log:implies { :x log:outputString :f } .

{ [] [ log:uri :p ] [ log:rawType log:Other; log:uri :z ]; b:id :u .
  :x string:concat (:u "a") . :f string:concat (" " :r "=\"" :z "\"") .
  :p string:startsWith "http://infomesh.net/2002/html-attrs#" .
  (:p "^http://infomesh.net/2002/html-attrs#(.+)$") string:scrape :r }
   log:implies { :x log:outputString :f } .

{ [] [ log:uri [ string:startsWith "http://infomesh.net/2002/html#" ] ]
   [ log:notEqualTo daml:nil ]; b:id :u .
  :x string:concat (:u "b") } log:implies { :x log:outputString ">" } .

{ :x [ log:uri [ string:startsWith "http://infomesh.net/2002/html#" ] ] ();
  b:id :u . :y string:concat (:u "b") }
   log:implies { :x a b:NoChild . :y log:outputString " />\n" } .

{ [] daml:first :x; b:id :y . :x log:rawType log:Literal }
   log:implies { :y log:outputString :x } .

{ :x [ log:uri [string:startsWith "http://infomesh.net/2002/html#"] ] :y .
  :y log:notEqualTo () } log:implies { :y a b:List; b:sourceElem :x } .

{ [] a b:List; b:sourceElem :x; daml:first :f; daml:rest :r .
  :r log:notEqualTo () } log:implies { :r a b:List; b:sourceElem :x } .

{ [] a b:List, b:NoChild; b:id :y; b:sourceElem :x; daml:first :f;
  daml:rest () } log:implies { :x b:lastChildElementID :y } .

{ [] a b:List; b:sourceElem :x; daml:first [ a b:NoChild; b:id :y ];
  daml:rest () } log:implies { :x b:lastChildElementID :y } .

{ [] a b:List; b:sourceElem :x; daml:first [ b:id :y;
         b:lastChildElementID :z ]; daml:rest () .
   :p string:concat (:z "e") } log:implies { :x b:lastChildElementID :p } .

{ [] [log:uri :p] []; b:lastChildElementID :u . :x string:concat (:u "d") .
  :p string:startsWith "http://infomesh.net/2002/html#" .
  (:p "^http://infomesh.net/2002/html#(.+)$") string:scrape :r .
  :f string:concat ("</" :r ">\n") }
   log:implies { :x log:outputString :f } .
]]] - <<n3tohtml.n3>>

there are some interesting things going on in there, capitalizing on some
of CWM's quirks. I ran it using:-

   $ cwm html.n3 n3tohtml.n3 --think --strings > result.html

and got the following output after two and a half minutes of processing
time:-

[[[
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"><head><title>HTML in
Notation3 Test Document</title>
<link href="file:/home/style.css" rel="stylesheet" type="text/css" />
</head>
<body><h1>HTML in N3</h1>
<p>This is a test document.</p>
<address><a href="http://purl.org/net/sbp/" title="A Homepage Of Sean B.
Palmer">Sean B. Palmer</a>
</address>
</body>
</html>
]]] - <<result.html>>

All of the files are attached for slurpability.

--
Kindest Regards,
Sean B. Palmer
@prefix : <http://purl.org/net/swn#> .
:Sean :homepage <http://purl.org/net/sbp/> .

Received on Sunday, 21 July 2002 15:39:38 UTC