<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet href="/parts/xsltdoc.xsl" type="text/xsl" media="screen" ?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:data-view="http://www.w3.org/2003/g/data-view#"  data-view:transformation="/parts/grddl.xsl"
  xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  xmlns:h="http://www.w3.org/1999/xhtml"
  xmlns:foaf="http://xmlns.com/foaf/0.1/"
  xmlns:dc="http://purl.org/dc/elements/1.1/"
  xmlns:b="http://www.w3.org/2002/01/bookmark#"
  exclude-result-prefixes="h data-view"
>
<xsl:output indent="yes"/>

<xsl:template name="_doas_description">
 <rdf:RDF xmlns="http://purl.org/net/ns/doas#">
  <rdf:Description rdf:about="{$base}">
   <title>RDF Bookmark extraction stylesheet</title>
   <description>This stylesheet is designed to extract RDF Bookmark from Danny Ayers&#39; &quot;This Week&#39;s Semantic Web&quot;. This book mark can be used by RDF bookmark client such as Annotea Ubimark</description>
   <author rdf:parseType="Resource">
    <name>Masahide Kanzaki</name>
    <mbox rdf:resource="mailto:webmaster@kanzaki.com"/>
   </author>
   <created>2007-10-24</created>
   <release rdf:parseType="Resource">
    <revision>0.5.0</revision>
    <created>2008-04-22</created>
   </release>
   <rights>(c) 2007-2008 by the author, copyleft under GPL</rights>
   <license rdf:resource="http://creativecommons.org/licenses/GPL/2.0/"/>
  </rdf:Description>
 </rdf:RDF>
</xsl:template>

<!--xsl:variable name="content" select="/h:html/h:body/h:div[@id='container']/h:div[@id='center']/h:div[@class='content']"/-->
<xsl:variable name="content" select="/h:html/h:body//h:div[@class='content' or @id='content']"/>
<xsl:variable name="base">http://www.kanzaki.com/works/2007/misc/thisweek-sw</xsl:variable>
<xsl:variable name="thisyearpara">
 <xsl:choose>
  <xsl:when test="$content/h:h2[@class='date']">
   <xsl:value-of select="$content/h:h2[@class='date'][1]"/>
  </xsl:when>
  <xsl:otherwise>
   <xsl:value-of select="substring-before($content/h:div[@class='post'][1]/h:div[@class='postmetadata'][1],',')"/>
  </xsl:otherwise>
 </xsl:choose>
</xsl:variable>
<xsl:variable name="yearbase">http://www.kanzaki.com/works/<xsl:value-of select="substring-after(substring-after(normalize-space($thisyearpara),' '),' ')"/>/misc/thisweek-sw</xsl:variable>

<!--==== Main matching templates ====-->

<xsl:template match="/">
 <!--** generates wrapper rdf:RDF and select 'real' content -->
 <rdf:RDF>
  <xsl:apply-templates select="$content"/>
 </rdf:RDF>
</xsl:template>

<xsl:template match="h:div[@class='content' or @id='content']">
 <!--** main link content selection. Steps:-->
 <!--@ sets base URI -->
 <xsl:attribute name="xml:base"><xsl:value-of select="$base"/></xsl:attribute>
 <!--@ generates root topic of RDF bookmark -->
  <b:Topic rdf:about="{$base}">
   <dc:title>This Week&#39;s Semantic Web</dc:title>
  </b:Topic>
 <!--@ finds h2 that stats a day's entry -->
 <xsl:apply-templates select="h:h2[@class='date']"/>
 <xsl:apply-templates select="h:div[@class='post']/h:div[@class='entrytext']"/>
</xsl:template>


<xsl:template match="h:h2">
 <!--** processes one entry that starts with h2. Steps:-->
 <!--@ finds date string by calling find-date template -->
 <xsl:variable name="ymd">
  <xsl:call-template name="find-date">
   <xsl:with-param name="next" select="following-sibling::h:p[1]"/>
  </xsl:call-template>
 </xsl:variable>
 <!--@ if date found -->
 <xsl:if test="$ymd != ''">
  <xsl:variable name="md" select="translate(substring($ymd,6),'-','/')"/>
  <xsl:variable name="d" select="translate($md,'/','')"/>
  <!--@ _ generates a b:Topic for this day's bookmark set it as subTopicOf root -->
  <b:Topic rdf:about="{$yearbase}#s-{$d}">
   <dc:title>Bookmarks in week ending <xsl:value-of select="$ymd"/></dc:title>
   <dc:date><xsl:value-of select="$ymd"/></dc:date>
   <b:subTopicOf rdf:resource="{$base}"/>
  </b:Topic>
  <!--@ _ then, call proc-h3 with the first h3 element -->
  <xsl:call-template name="proc-h3">
   <xsl:with-param name="h3" select="following-sibling::h:h3[1]"/>
   <xsl:with-param name="d" select="$d"/>
   <xsl:with-param name="md" select="$md"/>
  </xsl:call-template>
 </xsl:if>
</xsl:template>


<xsl:template match="h:div[@class='entrytext']">
 <!--** processes post in div. Steps:-->
 <!--@ finds date string by calling find-date template -->
 <xsl:variable name="ymd">
  <xsl:call-template name="find-date">
   <xsl:with-param name="next" select="h:p[1]"/>
  </xsl:call-template>
 </xsl:variable>
 <!--@ if date found -->
 <xsl:if test="$ymd != ''">
  <xsl:variable name="md" select="translate(substring($ymd,6),'-','/')"/>
  <xsl:variable name="d" select="translate($md,'/','')"/>
  <!--@ _ generates a b:Topic for this day's bookmark set it as subTopicOf root -->
  <b:Topic rdf:about="{$yearbase}#s-{$d}">
   <dc:title>Bookmarks in week ending <xsl:value-of select="$ymd"/></dc:title>
   <dc:date><xsl:value-of select="$ymd"/></dc:date>
   <b:subTopicOf rdf:resource="{$base}"/>
  </b:Topic>
  <!--@ _ then, call proc-h3 with the first h3 element -->
  <xsl:call-template name="proc-h3">
   <xsl:with-param name="h3" select="h:h3[1]"/>
   <xsl:with-param name="d" select="$d"/>
   <xsl:with-param name="md" select="$md"/>
  </xsl:call-template>
 </xsl:if>
</xsl:template>



<!--==== Named templates ====-->

<xsl:template name="proc-h3">
 <!--** processes one subsection starts with h3, recursively. Steps:-->
 <xsl:param name="h3"/><!--** current h3 element -->
 <xsl:param name="d"/><!--** date of current entry (h2) -->
 <xsl:param name="md"/><!--** mm/dd style date -->
 <xsl:param name="g"/><!--** genre of previous h3, if any -->
 <xsl:variable name="next" select="local-name($h3/following-sibling::*[1])"/><!--** an element next to this h3 -->
 <!--@ Switch according to $next: -->
 <xsl:choose>
  <!--@ _ case h2: return -->
  <xsl:when test="$next='h2'"><!--return--></xsl:when>
  <!--@ _ case p: continue to next h3 by calling this template recursively -->
  <xsl:when test="$next='p'">
   <xsl:call-template name="proc-h3">
    <xsl:with-param name="h3" select="$h3/following-sibling::h:h3[1]"/>
    <xsl:with-param name="d" select="$d"/>
    <xsl:with-param name="md" select="$md"/>
   </xsl:call-template>
  </xsl:when>
  <!--@ _ case ul: this is the link. call proc-ul, and check next h3, if any -->
  <xsl:when test="$next='ul'">
   <xsl:call-template name="proc-ul">
    <xsl:with-param name="ul" select="$h3/following-sibling::h:ul[1]"/>
    <xsl:with-param name="d" select="$d"/>
    <xsl:with-param name="md" select="$md"/>
    <xsl:with-param name="g" select="$h3"/>
   </xsl:call-template>
   <xsl:call-template name="proc-h3">
    <xsl:with-param name="h3" select="$h3/following-sibling::h:h3[1]"/>
    <xsl:with-param name="d" select="$d"/>
    <xsl:with-param name="md" select="$md"/>
   </xsl:call-template>
  </xsl:when>
 </xsl:choose>
</xsl:template>

<xsl:template name="proc-ul">
 <!--** actual bookmark processor. Steps:-->
 <xsl:param name="ul"/><!--** current ul element -->
 <xsl:param name="d"/><!--** compact date of current entry (h2) -->
 <xsl:param name="md"/><!--** mm/dd style date -->
 <xsl:param name="g"/><!--** genre of current h3 -->
 <xsl:variable name="genre" select="substring(translate($g,'ABCDEFGHIJKLMNOPQRSTUVWXYZ ._','abcdefghijklmnopqrstuvwxyz'),1,3)"/><!--** compact genre string for ids -->
 <xsl:variable name="idbase" select="concat($genre,'-',translate($d,'-',''),'-')"/><!--** id base for each Bookmark -->
 
 <!--@ generates a b:Topic of genre and set it as subTopicOf root Topic -->
 <b:Topic rdf:about="{$base}#g-{$genre}">
  <dc:title><xsl:value-of select="concat('Genre:',$g)"/></dc:title>
  <b:subTopicOf rdf:resource="{$base}"/>
 </b:Topic>

 <!--@ also generates a b:Topic of genre in week, and link (subTopicOf) to both week and genre -->
 <b:Topic rdf:about="{$yearbase}#s-{$d}-{$genre}">
  <dc:title><xsl:value-of select="concat($g,' (',$md,')')"/></dc:title>
  <b:subTopicOf rdf:resource="{$yearbase}#s-{$d}"/>
  <b:subTopicOf rdf:resource="{$base}#g-{$genre}"/>
 </b:Topic>

 <xsl:variable name="li" select="$ul/h:li"/>
 <!--@ processes each li element: -->
 <xsl:for-each select="$li">
  <!--@ _ generates an Bookmark instance which hasTopic of the genre in this week, with DC properties -->
  <xsl:if test="h:a">
   <b:Bookmark rdf:ID="{concat($idbase,position())}">
    <dc:title><xsl:value-of select="normalize-space(descendant-or-self::h:a[1])"/></dc:title>
    <xsl:choose>
     <xsl:when test="descendant-or-self::h:a/@href != ''"><xsl:call-template name="proc-href"/></xsl:when>
     <xsl:otherwise><xsl:comment>empty href attribute</xsl:comment></xsl:otherwise>
    </xsl:choose>
    <!--@ _ puts b:hasTopic link to genre in the week -->
    <b:hasTopic rdf:resource="{$yearbase}#s-{$d}-{$genre}"/>
    <dc:description><xsl:value-of select="normalize-space(.)"/></dc:description>
    <foaf:maker rdf:resource="http://www.kanzaki.com/ns/psi/en/Daniel_John_Ayers#sid"/>
   </b:Bookmark>
  </xsl:if>
 </xsl:for-each>

</xsl:template>

<xsl:template name="proc-href">
 <!--** processes hyper links. first a@href will be b:recalls, and rest will be dc:relation (rdfs:seeAlso would be more appropriate, but it kicks Tabulator to retrieve all unneccesary HTML links...) -->
 <xsl:for-each select="descendant-or-self::h:a[@href]">
  <xsl:choose>
   <xsl:when test="position()=1"><b:recalls rdf:resource="{@href}" dc:title="{normalize-space(.)}"/></xsl:when>
   <xsl:otherwise><dc:relation rdf:resource="{@href}" dc:title="{normalize-space(.)}"/></xsl:otherwise>
  </xsl:choose>
 </xsl:for-each>
</xsl:template>

<xsl:template name="find-date">
 <!--** find W3C-DTF date string of an entry-->
 <xsl:param name="next"/>
 <xsl:choose>
  <xsl:when test="$next/h:em">
   <xsl:value-of select="substring(substring-after($next,'ending '),1,10)"/>
  </xsl:when>
  <xsl:when test="$next/following-sibling::h:p[1]/h:em">
   <xsl:value-of select="substring(substring-after($next/following-sibling::h:p[1],'ending '),1,10)"/>
  </xsl:when>
  <!--
  <xsl:otherwise>
   <xsl:value-of select="translate(.,' ','_')"/>
  </xsl:otherwise>
  -->
 </xsl:choose>

</xsl:template>

<xsl:template match="*"/>

</xsl:stylesheet>
