<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:atom="http://www.w3.org/2005/Atom"
  exclude-result-prefixes="atom">
  <xsl:output method="html" encoding="UTF-8" indent="yes"/>

  <!-- trueTKactual live palette: #16181a / #181b15 / #1f2320 background, #c9a646 gold, #e9e4d6 text. -->
  <xsl:template name="page">
    <xsl:param name="page-title"/>
    <xsl:param name="feed-url"/>
    <xsl:param name="feed-kind"/>
    <xsl:param name="entries"/>
    <html lang="en">
      <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <meta name="viewport" content="width=device-width, initial-scale=1"/>
        <title><xsl:value-of select="$page-title"/></title>
        <style type="text/css">
          * { box-sizing: border-box; }
          body { margin: 0; font: 17px/1.65 Georgia, "Iowan Old Style", "Palatino Linotype", serif; background: #16181a; color: #e9e4d6; }
          a { color: #c9a646; }
          a:hover { color: #e0c56a; }
          header { border-bottom: 1px solid #343a31; background: #121410; padding: 0.85rem 1.2rem; }
          .brand { font-family: "Trebuchet MS", "Segoe UI", system-ui, sans-serif; font-weight: 800; letter-spacing: 0.04em; text-decoration: none; color: #e9e4d6; font-size: 1.1rem; }
          .brand span { color: #c9a646; }
          main { max-width: 860px; margin: 0 auto; padding: 2rem 1.2rem 3.5rem; }
          .kicker { font-family: "Trebuchet MS", "Segoe UI", system-ui, sans-serif; text-transform: uppercase; letter-spacing: 0.25em; font-size: 0.8rem; font-weight: 700; color: #c9a646; margin: 0 0 0.6rem; }
          h1 { font-family: "Trebuchet MS", "Segoe UI", system-ui, sans-serif; font-size: clamp(1.7rem, 4vw, 2.4rem); margin: 0 0 1rem; }
          h1:after { content: ""; display: block; width: 56px; height: 3px; background: #c9a646; margin-top: 0.6rem; }
          .lede { margin: 0 0 1.2rem; }
          .btn { display: inline-block; margin: 0.2rem 0.5rem 0.2rem 0; padding: 0.75rem 1.4rem; border-radius: 4px; text-decoration: none; font-family: system-ui, sans-serif; font-weight: 700; background: #c9a646; color: #16181a; }
          .btn:hover { color: #16181a; filter: brightness(1.08); }
          .note { margin: 1.3rem 0 0; padding: 1rem 1.15rem; background: #181b15; border: 1px solid #343a31; border-radius: 8px; font-family: system-ui, sans-serif; font-size: 0.95rem; }
          .note code { font-family: ui-monospace, "Cascadia Mono", "Segoe UI Mono", monospace; font-size: 0.9em; color: #c9a646; word-break: break-all; }
          h2 { font-family: "Trebuchet MS", "Segoe UI", system-ui, sans-serif; font-size: 1.25rem; margin: 2.2rem 0 0.9rem; }
          ul { list-style: none; margin: 0; padding: 0; }
          li { background: #1f2320; border: 1px solid #343a31; border-radius: 8px; padding: 1.15rem 1.25rem; margin: 0 0 0.9rem; }
          .date { display: block; font-family: system-ui, sans-serif; font-size: 0.82rem; letter-spacing: 0.04em; text-transform: uppercase; color: #c9a646; margin: 0 0 0.35rem; }
          h3 { font-family: "Trebuchet MS", "Segoe UI", system-ui, sans-serif; font-size: 1.15rem; margin: 0 0 0.45rem; }
          h3 a { color: #e9e4d6; text-decoration: none; }
          h3 a:hover { color: #c9a646; }
          .summary { margin: 0; }
          footer { text-align: center; padding: 2rem 1rem; font-family: system-ui, sans-serif; font-size: 0.9rem; background: #0f1011; border-top: 1px solid #343a31; }
          footer a { color: #c9a646; }
        </style>
      </head>
      <body>
        <header>
          <a class="brand" href="https://truetkactual.net/">true<span>TK</span>actual</a>
        </header>
        <main>
          <p class="kicker">Syndication feed</p>
          <h1>trueTKactual</h1>
          <p class="lede">
            This is the <xsl:value-of select="$feed-kind"/> feed of dated public writings from Thomas K. Morgan, II.
            Open any title to read it on the site.
          </p>
          <p>
            <a class="btn" href="https://truetkactual.net/">Visit trueTKactual.net</a>
          </p>
          <p class="note">
            To follow this feed in an RSS reader, copy this address and paste it into your reader:
            <code><xsl:value-of select="$feed-url"/></code>
          </p>
          <h2>Latest writings</h2>
          <ul>
            <xsl:for-each select="$entries">
              <xsl:variable name="is-atom" select="boolean(atom:title or atom:link or atom:id)"/>
              <xsl:variable name="href">
                <xsl:choose>
                  <xsl:when test="$is-atom and atom:link[@rel='alternate']/@href"><xsl:value-of select="atom:link[@rel='alternate']/@href"/></xsl:when>
                  <xsl:when test="$is-atom and atom:link/@href"><xsl:value-of select="atom:link/@href"/></xsl:when>
                  <xsl:when test="$is-atom"><xsl:value-of select="atom:id"/></xsl:when>
                  <xsl:when test="link"><xsl:value-of select="link"/></xsl:when>
                  <xsl:otherwise><xsl:value-of select="guid"/></xsl:otherwise>
                </xsl:choose>
              </xsl:variable>
              <xsl:variable name="title">
                <xsl:choose>
                  <xsl:when test="$is-atom"><xsl:value-of select="atom:title"/></xsl:when>
                  <xsl:otherwise><xsl:value-of select="title"/></xsl:otherwise>
                </xsl:choose>
              </xsl:variable>
              <xsl:variable name="summary">
                <xsl:choose>
                  <xsl:when test="$is-atom"><xsl:value-of select="atom:summary"/></xsl:when>
                  <xsl:otherwise><xsl:value-of select="description"/></xsl:otherwise>
                </xsl:choose>
              </xsl:variable>
              <xsl:variable name="date-label">
                <xsl:choose>
                  <xsl:when test="$is-atom and atom:published">
                    <xsl:call-template name="format-iso-date"><xsl:with-param name="iso" select="atom:published"/></xsl:call-template>
                  </xsl:when>
                  <xsl:when test="$is-atom and atom:updated">
                    <xsl:call-template name="format-iso-date"><xsl:with-param name="iso" select="atom:updated"/></xsl:call-template>
                  </xsl:when>
                  <xsl:when test="pubDate">
                    <xsl:call-template name="format-rfc822"><xsl:with-param name="rfc" select="pubDate"/></xsl:call-template>
                  </xsl:when>
                </xsl:choose>
              </xsl:variable>
              <li>
                <xsl:if test="$date-label != ''"><span class="date"><xsl:value-of select="$date-label"/></span></xsl:if>
                <h3><a href="{$href}"><xsl:value-of select="$title"/></a></h3>
                <xsl:if test="$summary != ''"><p class="summary"><xsl:value-of select="$summary"/></p></xsl:if>
              </li>
            </xsl:for-each>
          </ul>
        </main>
        <footer>
          <a href="https://truetkactual.net/">trueTKactual.net</a>
        </footer>
      </body>
    </html>
  </xsl:template>

  <xsl:template name="month-name">
    <xsl:param name="m"/>
    <xsl:choose>
      <xsl:when test="$m='01' or $m='1' or $m='Jan'">January</xsl:when>
      <xsl:when test="$m='02' or $m='2' or $m='Feb'">February</xsl:when>
      <xsl:when test="$m='03' or $m='3' or $m='Mar'">March</xsl:when>
      <xsl:when test="$m='04' or $m='4' or $m='Apr'">April</xsl:when>
      <xsl:when test="$m='05' or $m='5' or $m='May'">May</xsl:when>
      <xsl:when test="$m='06' or $m='6' or $m='Jun'">June</xsl:when>
      <xsl:when test="$m='07' or $m='7' or $m='Jul'">July</xsl:when>
      <xsl:when test="$m='08' or $m='8' or $m='Aug'">August</xsl:when>
      <xsl:when test="$m='09' or $m='9' or $m='Sep'">September</xsl:when>
      <xsl:when test="$m='10' or $m='Oct'">October</xsl:when>
      <xsl:when test="$m='11' or $m='Nov'">November</xsl:when>
      <xsl:when test="$m='12' or $m='Dec'">December</xsl:when>
    </xsl:choose>
  </xsl:template>

  <xsl:template name="format-iso-date">
    <xsl:param name="iso"/>
    <xsl:variable name="ymd" select="substring($iso,1,10)"/>
    <xsl:call-template name="month-name"><xsl:with-param name="m" select="substring($ymd,6,2)"/></xsl:call-template>
    <xsl:text> </xsl:text>
    <xsl:value-of select="number(substring($ymd,9,2))"/>
    <xsl:text>, </xsl:text>
    <xsl:value-of select="substring($ymd,1,4)"/>
  </xsl:template>

  <xsl:template name="format-rfc822">
    <xsl:param name="rfc"/>
    <xsl:variable name="rest" select="substring-after($rfc, ', ')"/>
    <xsl:variable name="day" select="number(substring-before($rest, ' '))"/>
    <xsl:variable name="afterday" select="substring-after($rest, ' ')"/>
    <xsl:variable name="mon" select="substring-before($afterday, ' ')"/>
    <xsl:variable name="year" select="substring(substring-after($afterday, ' '), 1, 4)"/>
    <xsl:call-template name="month-name"><xsl:with-param name="m" select="$mon"/></xsl:call-template>
    <xsl:text> </xsl:text>
    <xsl:value-of select="$day"/>
    <xsl:text>, </xsl:text>
    <xsl:value-of select="$year"/>
  </xsl:template>

  <xsl:template match="/atom:feed">
    <xsl:call-template name="page">
      <xsl:with-param name="page-title">trueTKactual (Atom)</xsl:with-param>
      <xsl:with-param name="feed-url">https://truetkactual.net/atom.xml</xsl:with-param>
      <xsl:with-param name="feed-kind">Atom</xsl:with-param>
      <xsl:with-param name="entries" select="atom:entry"/>
    </xsl:call-template>
  </xsl:template>

  <xsl:template match="/rss">
    <xsl:call-template name="page">
      <xsl:with-param name="page-title">trueTKactual (RSS)</xsl:with-param>
      <xsl:with-param name="feed-url">https://truetkactual.net/rss.xml</xsl:with-param>
      <xsl:with-param name="feed-kind">RSS</xsl:with-param>
      <xsl:with-param name="entries" select="channel/item"/>
    </xsl:call-template>
  </xsl:template>
</xsl:stylesheet>
