<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:fo="http://www.w3.org/1999/XSL/Format">
  <xsl:output method="xml" encoding="UTF-8" indent="yes"/>

  <xsl:template match="/">
    <fo:root>
      <fo:layout-master-set>
        <fo:simple-page-master master-name="cover">
          <fo:region-body margin="1in"/>
        </fo:simple-page-master>
        <fo:simple-page-master master-name="front-matter">
          <fo:region-after extent="0.5in"/>
          <fo:region-body margin="1in"/>
        </fo:simple-page-master>
        <fo:simple-page-master master-name="contents">
          <fo:region-after extent="0.5in"/>
          <fo:region-body margin="1in"/>
        </fo:simple-page-master>
      </fo:layout-master-set>
      <xsl:apply-templates/>
    </fo:root>
  </xsl:template>

<!-- 
  This stylesheet handles both cookbooks and stand-alone recipes so a
  template is needed to handle either one as the root element.
-->

<!-- A stand-alone recipe is easy to define. -->

  <xsl:template match="/recipe">
    <fo:page-sequence master-reference="contents">
      <fo:flow flow-name="xsl-region-body">
        <xsl:apply-templates/>
      </fo:flow>
    </fo:page-sequence>
  </xsl:template>


<!--
   A full cookbook is much more complex because of things like the cover,
   front matter, page numbering and so on.
 -->

  <xsl:template match="/cookbook">

<!-- The cookbook cover layout is title, subtitle, artwork, author. -->

    <fo:page-sequence master-reference="cover">
      <fo:flow flow-name="xsl-region-body">
        <fo:block font-size="24pt" text-align="end">
          <xsl:value-of select="title"/>
        </fo:block>
        <fo:block font-size="16pt" font-style="italic" text-align="end">
          <xsl:value-of select="subtitle"/>
        </fo:block>
        <fo:block space-before="16pt" text-align="center">
          <fo:external-graphic src="{cookbookinfo/coverart/@url}"/>
        </fo:block>
        <fo:block font-size="16pt" space-before="16pt" text-align="end">
          <xsl:value-of select="cookbookinfo/author"/>
        </fo:block>
      </fo:flow>
    </fo:page-sequence>

<!-- Front matter (roughly follows Chicago Manual of Style) -->

    <fo:page-sequence master-reference="front-matter" 
      initial-page-number="1" format="i">

<!-- Put a lowercase Roman numeral page number in the footer. -->

      <fo:static-content flow-name="xsl-region-after"
        font-family="sans-serif" font-size="10pt">
        <fo:block text-align="center">
          <fo:page-number/>
        </fo:block>
      </fo:static-content>

<!-- The following is the actual front matter. -->
 
     <fo:flow flow-name="xsl-region-body"
        font-family="serif" font-size="12pt">

<!-- The half-title is on page i. -->

        <fo:block font-size="16pt" text-align="center"
          space-before="2in" break-after="page">
          <xsl:value-of select="title"/>
        </fo:block>

<!-- The contributors page (used for cover art credit) is page on ii. -->

        <fo:block break-after="page">
          <fo:block space-after="12pt">
            <xsl:value-of select="cookbookinfo/coverart/@credit" />
          </fo:block>
        </fo:block>

<!-- Full title (similar to cover, but without artwork) is page iii. -->

        <fo:block break-after="page">
          <fo:block font-size="24pt" text-align="end">
            <xsl:value-of select="title"/>
          </fo:block>
          <fo:block font-size="16pt" font-style="italic"
          space-after="16pt" text-align="end">
            <xsl:value-of select="subtitle"/>
          </fo:block>
          <fo:block font-size="16pt" space-before="16pt" text-align="end">
            <xsl:value-of select="cookbookinfo/author"/>
          </fo:block>
        </fo:block>

<!-- Copyright and restrictions go on page iv. -->

        <fo:block break-after="page">
          <fo:block space-after="12pt">
            <xsl:value-of select="cookbookinfo/copyright"/>
          </fo:block>
          <fo:block>
            <xsl:value-of select="cookbookinfo/license"/>
          </fo:block>
        </fo:block>

<!-- Table of contents (auto-generated) -->

        <fo:block break-after="page">
          <fo:block font-size="16pt" font-weight="bold" space-after="16pt">
            Contents
          </fo:block>
          <xsl:for-each 
            select="/cookbook/section/title | /cookbook/section/recipe/title">
            <fo:block text-align="justify">
              <xsl:value-of select="."/>
              <fo:leader leader-pattern="space"/>
              <fo:page-number-citation ref-id="{generate-id()}"/>
            </fo:block>
          </xsl:for-each>
        </fo:block>

      </fo:flow>
    </fo:page-sequence>


<!-- Contents -->

    <fo:page-sequence master-reference="contents"
      initial-page-number="1" format="1">
      <fo:static-content flow-name="xsl-region-after"
        font-family="sans-serif" font-size="10pt">
        <fo:block text-align="center">
          <fo:page-number/>
        </fo:block>
      </fo:static-content>
      <fo:flow flow-name="xsl-region-body">
        <xsl:apply-templates/>
      </fo:flow>
    </fo:page-sequence>
  </xsl:template>


<!--
  The remaining templates apply to elements in both stand-alone recipes
  and full-featured cookbooks.
-->


<!-- Hyperlinks can be used almost anywhere in a document. -->

  <xsl:template match="hyperlink">
    <fo:basic-link external-destination="{@url}" color="blue">
      <xsl:value-of select="."/>
    </fo:basic-link> 
  </xsl:template>


<!-- Create dividers for each section using the section title. -->

  <xsl:template match="/cookbook/section/title">
    <fo:block id="{generate-id()}"
      break-before="page" break-after="page"
      font-family="sans-serif" font-size="24pt"
      space-before="3in" text-align="end">
      <xsl:value-of select="."/>
    </fo:block>
      <xsl:apply-templates/>
  </xsl:template>


<!-- Recipes always start on the top of a page. -->

  <xsl:template match="recipe">
    <fo:block break-before="page"
      font-family="serif" font-size="12pt">
      <xsl:apply-templates/>
    </fo:block>
  </xsl:template>


<!-- Recipe title, a required element, is displayed in bold. -->

  <xsl:template match="recipe/title">
      <fo:block font-weight="bold" id="{generate-id()}">
        <xsl:value-of select="."/>
      </fo:block>
  </xsl:template>


<!-- Recipe subtitles are italicized. -->

  <xsl:template match="recipe/subtitle">
      <fo:block font-style="italic">
        <xsl:value-of select="."/>
      </fo:block>
  </xsl:template>


<!-- Recipe meta-information is displayed in a fixed order. -->

  <xsl:template match="recipe/recipeinfo">
    <fo:block margin-left="12pt" space-before="12pt">
      <xsl:if test="blurb">
        <fo:block><xsl:value-of select="blurb"/></fo:block>
      </xsl:if>
      <xsl:if test="genre">
        <fo:block>File under: <xsl:value-of select="genre"/></fo:block>
      </xsl:if>
      <xsl:if test="author">
        <fo:block>Recipe by: <xsl:value-of select="author"/></fo:block>
      </xsl:if>
      <xsl:if test="source">
        <fo:block>Source: <xsl:apply-templates select="source"/></fo:block>
      </xsl:if>
      <xsl:if test="yield">
        <fo:block>Yield: <xsl:value-of select="yield"/></fo:block>
      </xsl:if>
      <xsl:if test="preptime">
        <fo:block>Preptime: <xsl:value-of select="preptime"/></fo:block>
      </xsl:if>
    </fo:block>
  </xsl:template>


<!-- Recipe ingredientlist is displayed with the heading 'Ingredients'. -->

  <xsl:template match="recipe/ingredientlist">
    <fo:block
      font-size="12pt"
      font-weight="bold"
      space-before="12pt"
      space-after="12pt"
      keep-with-next="always">Ingredients</fo:block>
    <fo:block margin-left="12pt"><xsl:apply-templates/></fo:block>
  </xsl:template>


<!-- Individual ingredients are displayed one per line. -->

  <xsl:template match="ingredient">
    <fo:block><xsl:apply-templates/></fo:block>
  </xsl:template>


<!-- Recipe preparation instructions are prepended with a heading. -->

  <xsl:template match="recipe/preparation">
    <fo:block
      font-size="12pt"
      font-weight="bold"
      space-before="12pt"
      space-after="12pt"
      keep-with-next="always">Preparation Instructions</fo:block>
    <fo:block margin-left="12pt"><xsl:apply-templates/></fo:block>
  </xsl:template>


<!-- Recipe serving suggestion gets a heading as well. -->

  <xsl:template match="recipe/serving">
    <fo:block
      font-size="12pt"
      font-weight="bold"
      space-before="12pt"
      space-after="12pt"
      keep-with-next="always">Serving Suggestion</fo:block>
    <fo:block margin-left="12pt"><xsl:apply-templates/></fo:block>
  </xsl:template>


<!-- Recipe notes also have a heading. -->

  <xsl:template match="recipe/notes">
    <fo:block
      font-size="12pt"
      font-weight="bold"
      space-before="12pt"
      space-after="12pt"
      keep-with-next="always">Notes</fo:block>
    <fo:block margin-left="12pt"><xsl:apply-templates/></fo:block>
  </xsl:template>

</xsl:stylesheet>
