<?xml version="1.0" encoding="UTF-8"?>

<!--
/**
 * Rendering stylesheet for Liferea (feed description view)
 *
 * Copyright (C) 2006-2021 Lars Windolf <lars.windolf@gmx.de>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
-->

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
                xmlns="http://www.w3.org/1999/xhtml">

<xsl:output method="xml" version="1.0" indent="yes"/>

<xsl:param name="appDirection"/>	<!-- application direction, either "ltr" or "rtl" -->
<xsl:param name="txtDirection"/>	<!-- text direction, either "ltr" or "rtl" -->

<xsl:preserve-space elements="//*"/>

<xsl:template match="/feed" >
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=UTF-8" />
</head>
<body>

<!-- header table -->
<table class="feedhead" cellspacing="0" dir="{$appDirection}">
<tr>
  <td valign="middle" class="headleft">
    <b><_span>Feed:</_span></b>
  </td>
  <td width="100%" valign="middle" class="headright">
     <a href="{attributes/attribute[ @name = 'homepage' ]}">
        <xsl:value-of select="feedTitle"/>
     </a>
  </td>
</tr>
<tr>
  <td valign="middle" class="headleft">
    <b><_span>Source:</_span></b>
  </td>
  <td width="100%" valign="middle" class="headright">
     <xsl:variable name="href">
        <xsl:call-template name="strippwd">
           <xsl:with-param name="string" select="feedSource"/>
        </xsl:call-template>
     </xsl:variable>
     <a href="{$href}">
        <xsl:call-template name="strippwd">
           <xsl:with-param name="string" select="feedSource"/>
        </xsl:call-template>
     </a>
  </td>
</tr>
</table>

<!-- header metadata (author, copyright, language) -->
<table class='headmeta' cellspacing="0" cellpadding="0">
<xsl:if test="attributes/attribute[ @name = 'publisher' ]">
<tr>
 <td valign="top" class='publisher'>
     <b><_span>Publisher</_span></b>
     <span class='publisher'>
       <xsl:value-of select="attributes/attribute[ @name = 'publisher' ]" disable-output-escaping='yes'/>
     </span>
 </td>
</tr>
</xsl:if>
<xsl:if test="attributes/attribute[ @name = 'copyright' ]">
<tr>
 <td valign="top" class='copyright'>
     <b><_span>Copyright</_span></b>
     <span class='copyright'>
       <xsl:value-of select="attributes/attribute[ @name = 'copyright' ]" disable-output-escaping='yes'/>
     </span>
 </td>
</tr>
</xsl:if>
</table> <!-- end of header metadata -->

<!-- error header tables -->
<xsl:if test="/feed/error > 0">
  <div id="errors" dir="{$appDirection}">

    <_span>There was a problem when fetching this subscription!</_span>
    <ul>
      <li>
        <xsl:call-template name="parseStatus">
          <xsl:with-param name="parsePhase" select="1"/>
        </xsl:call-template>
        <_span>1. Authentication</_span>
      </li>
      <li>
        <xsl:call-template name="parseStatus">
          <xsl:with-param name="parsePhase" select="2"/>
        </xsl:call-template>
        <_span>2. Download</_span>
      </li>
      <li>
        <xsl:call-template name="parseStatus">
          <xsl:with-param name="parsePhase" select="4"/>
        </xsl:call-template>
        <_span>3. Feed Discovery</_span>
      </li>
      <li>
        <xsl:call-template name="parseStatus">
          <xsl:with-param name="parsePhase" select="8"/>
        </xsl:call-template>
        <_span>4. Parsing</_span>
      </li>
    </ul>

    <span class="details">
      <b><_span>Details:</_span></b>

      <xsl:choose>
        <xsl:when test="/feed/error = 1">
          <p><_span>Authentication failed. Please check the credentials and try again!</_span></p>
        </xsl:when>

        <xsl:when test="/feed/error = 2">
          <xsl:if test="httpError">
            <p>
              <xsl:if test="httpErrorCode >= 100">
                HTTP <xsl:value-of select="httpErrorCode"/>:
              </xsl:if>
              <xsl:value-of select="httpError" disable-output-escaping='yes'/>
            </p>
          </xsl:if>

          <xsl:if test="updateError">
            <p>
              <_span>There was an error when downloading the feed source:</_span>
              <pre class="errorOutput"><xsl:value-of select="updateError" disable-output-escaping='yes'/></pre>
            </p>
          </xsl:if>

          <xsl:if test="filterError">
            <p>
              <_span>There was an error when running the feed filter command:</_span>
              <pre class="errorOutput"><xsl:value-of select="filterError" disable-output-escaping='yes'/></pre>
            </p>
          </xsl:if>
        </xsl:when>

        <xsl:when test="/feed/error = 4">
          <p><_span>The source does not point directly to a feed or a webpage with a link to a feed!</_span></p>
        </xsl:when>

        <xsl:when test="/feed/error = 8">
          <p><_span>Sorry, the feed could not be parsed!</_span></p>
          <xsl:if test="parseError">
            <pre class="errorOutput"><xsl:value-of select="parseError" disable-output-escaping='yes'/></pre>
            <p><_span>You may want to contact the author/webmaster of the feed about this!</_span></p>
          </xsl:if>
        </xsl:when>
      </xsl:choose>
    </span>

  </div>
</xsl:if>

<div class='feed content' dir="{$txtDirection}" id='content'>

<xsl:if test="attributes/attribute[ @name = 'imageUrl' ]">
<p>
<img class="feed" src="{attributes/attribute[ @name = 'imageUrl' ]}"/>
</p>
</xsl:if>

   <xsl:value-of select="attributes/attribute[@name = 'description']" disable-output-escaping='yes'/>

   <xsl:value-of select="attributes/attribute[@name = 'textInput']" disable-output-escaping='yes'/>
</div>

</body>
</html>
</xsl:template>

<xsl:template name="parseStatus">
  <!-- match logic of feedFetchError enum (src/feed.h) -->
  <xsl:param name="parsePhase"/>
  <xsl:variable name="errorCode" select="/feed/error"/>
  <xsl:if test="$errorCode = 0 or $errorCode > $parsePhase">✅</xsl:if>
  <xsl:if test="$errorCode = $parsePhase">⛔</xsl:if>
  <xsl:if test="$parsePhase > $errorCode">⬜</xsl:if>
  <xsl:text> </xsl:text>
</xsl:template>

<xsl:template name="strippwd">
   <xsl:param name="string"/>
   <xsl:choose>
       <xsl:when test="contains($string, '@')">
       <xsl:choose>
             <!-- between "://" and "@" only user:pass is allowed, so check if it has "/" to remove or not confidential data -->
             <xsl:when test="contains(substring-after(substring-before($string, '@'), '://'), '/')">
                <xsl:value-of select="$string"/>
             </xsl:when>
             <xsl:otherwise><xsl:value-of select="substring-before($string, '://')"/>://<xsl:value-of select="substring-after($string, '@')"/></xsl:otherwise>
       </xsl:choose>
       </xsl:when>
       <xsl:otherwise><xsl:value-of select="$string"/></xsl:otherwise>
   </xsl:choose>
</xsl:template>
</xsl:stylesheet>
