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

<!--
/**
 * Rendering stylesheet for Liferea (item view: item rendering)
 *
 * Copyright (C) 2009-2019 Mikel Olasagasti Uranga <mikel@olasagasti.info>
 * Copyright (C) 2006-2024 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"/>

<!-- the local pixmap directory (e.g. /usr/share/liferea/pixmaps/) -->
<xsl:param name="pixmapsDir"/>

<xsl:param name="baseUrl"/> <!-- item specific content base URL -->
<xsl:param name="showFeedName"/>
<xsl:param name="txtDirection"/>	<!-- text direction, either "ltr" or "rtl" -->
<xsl:param name="appDirection"/>	<!-- text direction, either "ltr" or "rtl" -->

<xsl:template match="/itemset">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta http-equiv="Content-Type" content="application/xhtml+xml; charset=UTF-8" />
  <base href="{$baseUrl}"/>
</head>
<xsl:element name="body">

<xsl:for-each select="item">
  <xsl:call-template name="item"/>
</xsl:for-each>

</xsl:element>
</html>

</xsl:template>

<!-- ==================================================================== -->

<xsl:template name="item">

<div class='item'>

<!-- header table -->
<table class="itemhead" cellspacing="0" cellpadding="0" dir="{$txtDirection}">
<tbody>
<tr>
  <td valign="middle" class="head_favicon">
    <!-- Feed link as favicon -->

    <xsl:variable name="favicon">
      <xsl:choose>
        <xsl:when test="not(sourceFavicon)">
          <xsl:value-of select="/itemset/feed/favicon"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="sourceFavicon"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>

    <a class="favicon" href="{/itemset/feed/attributes/attribute[ @name = 'homepage' ]}">
      <img src="{$favicon}"/>
    </a>
  </td>
  <td class="head_title">
    <!-- Item title as link -->
    <a class="itemhead" href="{source}">
      <xsl:value-of select="title"/>
      <!-- the title added above might be empty, in this
           case we add the date of the post -->
      <xsl:if test="title = ''">
         <xsl:value-of select="timestr"/>
      </xsl:if>
    </a>
  </td>
</tr>
</tbody>
</table>

<!-- header metadata (author + categories + date + source feed) -->
<table class='headmeta' cellspacing="0" cellpadding="0" dir="{$appDirection}">
<tbody>
<xsl:if test="attributes/attribute[ @name = 'slash' ]">
<tr>
 <td valign="top" class='slash'>
   <xsl:apply-templates select="attributes/attribute[ @name = 'slash']"/>
 </td>
</tr>
</xsl:if>

<xsl:if test="attributes/attribute[ @name = 'realSourceUrl' ]">
<tr>
 <td valign="top" class='source'>
     <_span>Source</_span>
     <b><span class='source'>
       <a href="{attributes/attribute[ @name = 'realSourceUrl' ]}">
         <xsl:value-of select="attributes/attribute[ @name = 'realSourceTitle' ]"/>
       </a>
     </span></b>
 </td>
</tr>
</xsl:if>

<!-- When presenting multiple feeds we display the source feed title for the item. -->
<xsl:if test="$showFeedName = '1'">
<tr>
 <td valign="top" class='source'>
     <_span>Feed</_span>
     <b><span class='source'>
       <a href="{/itemset/feed/attributes/attribute[ @name = 'homepage' ]}">
         <xsl:value-of select="/itemset/feed/feedTitle"/>
       </a>
     </span></b>
 </td>
</tr>
</xsl:if>

<xsl:if test="attributes/attribute[ @name = 'category' ]">
<tr>
 <td valign="top" class='categories'>
     <_span>Filed under</_span>
     <b><span class='categories'>
       <xsl:for-each select="attributes/attribute[ @name = 'category' ]">
         <xsl:if test="position() != 1">, </xsl:if>
         <xsl:value-of select="." disable-output-escaping='yes'/>
       </xsl:for-each>
     </span></b>
 </td>
</tr>
</xsl:if>

<xsl:if test="attributes/attribute[ @name = 'author' ]">
 <tr>
   <td valign="top" class='author'>
     <_span>Author</_span>
     <b><span class='author'>
       <xsl:value-of select="attributes/attribute[ @name = 'author' ]" disable-output-escaping='yes'/>
     </span></b>
   </td>
 </tr>
</xsl:if>

<xsl:if test="attributes/attribute[ @name = 'sharedby' ]">
 <tr>
   <td valign="top" class='sharedby'>
     <_span>Shared by</_span>
     <b><span class='sharedby'>
       <xsl:value-of select="attributes/attribute[ @name = 'sharedby' ]" disable-output-escaping='yes'/>
     </span></b>
   </td>
 </tr>
</xsl:if>

<!-- Indicate Atom "via" and "related" links -->
<xsl:for-each select="attributes/attribute[ @name = 'via' ]">
<tr>
 <td valign="top" class='source'>
     <_span>Via</_span>
     <b><span class='source'>
       <a href="{.}">
         <xsl:value-of select="."/>
       </a>
     </span></b>
 </td>
</tr>
</xsl:for-each>
<xsl:for-each select="attributes/attribute[ @name = 'related' ]">
<tr>
 <td valign="top" class='source'>
     <_span>Related</_span>
     <b><span class='source'>
       <a href="{.}">
         <xsl:value-of select="."/>
       </a>
     </span></b>
 </td>
</tr>
</xsl:for-each>

<!-- Indicate all duplicates -->
<xsl:for-each select="duplicates/duplicateNode">
<tr>
 <td valign="top" class='source'>
     <_span>Also posted in</_span>
     <b><span class='source'>
         <xsl:value-of select="."/>
     </span></b>
 </td>
</tr>
</xsl:for-each>

<xsl:if test="attributes/attribute[ @name = 'creator' ]">
 <tr>
   <td valign="top" class='creator'>
     <_span>Creator</_span>
     <b><span class='creator'>
       <xsl:value-of select="attributes/attribute[ @name = 'creator' ]" disable-output-escaping='yes'/>
     </span></b>
   </td>
 </tr>
</xsl:if>

<xsl:if test="attributes/attribute[ @name = 'point' ]">
 <xsl:variable name="lat" select="substring-before(attributes/attribute[ @name = 'point' ],' ')"/>
 <xsl:variable name="lng" select="substring-after(attributes/attribute[ @name = 'point' ],' ')"/>
 <tr>
   <td valign="top" class='point'>
     <_span>Coordinates</_span>
     <b><span class='point'><xsl:value-of select="$lat"/>, <xsl:value-of select="$lng"/></span></b>
   </td>
 </tr>
  <tr>
   <td valign="top" class='point'>
     <_span>Map</_span>
     <b><span class='point'>
     <a href="https://www.openstreetmap.org/?mlat={$lat}&amp;mlon={$lng}#map=12/{$lat}/{$lng}">OpenStreeMap</a>
     </span></b>
   </td>
 </tr>
</xsl:if>

<xsl:if test="attributes/attribute[ @name = 'mediaviews' ]">
 <tr>
   <td valign="top" class='mediaviews'>
     <_span>View count </_span>
     <b><span class='mediaviews'>
       <xsl:value-of select="attributes/attribute[ @name = 'mediaviews' ]"/>
     </span></b>
   </td>
 </tr>
</xsl:if>

<xsl:if test="(attributes/attribute[ @name = 'mediastarRatingmax' ]) and (attributes/attribute[ @name = 'mediastarRatingavg' ])">
 <tr>
   <td valign="top" class='mediastarRating'>
     <_span>Rating </_span>
     <b><span class='mediastarRating'>
       <xsl:value-of select="attributes/attribute[ @name = 'mediastarRatingavg' ]"/> / <xsl:value-of select="attributes/attribute[ @name = 'mediastarRatingmax' ]" />
       <xsl:if test="attributes/attribute[ @name = 'mediastarRatingcount' ]">
         (<xsl:value-of select="attributes/attribute[ @name = 'mediastarRatingcount' ]" /> votes)
       </xsl:if>
     </span></b>
   </td>
 </tr>
</xsl:if>

</tbody>
</table> <!-- end of header metadata -->

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

  <!-- the item's text content -->
  <p>
    <!-- optional gravatar -->
    <xsl:if test="attributes/attribute[ @name = 'gravatar' ]">
      <img align='left' class='gravatar' src="{attributes/attribute[ @name = 'gravatar' ]}"/>
    </xsl:if>
    <!-- add optional MediaRSS thumbnail (only if no image is in content, as it is often duplicated, e.g. on HTML5 fetch) -->
    <xsl:if test="not(contains(description,'&lt;img') or contains(description,'&lt;IMG'))">
      <xsl:if test="attributes/attribute[ @name = 'mediathumbnail' ]">
        <img align='left' class='thumbnail' src="{attributes/attribute[ @name = 'mediathumbnail' ]}"/>
      </xsl:if>
    </xsl:if>
    <!-- optional MediaRSS description -->
    <xsl:if test="attributes/attribute[ @name = 'mediadescription' ]">
      <p>
        <xsl:call-template name="add-newline">
          <xsl:with-param name="input"  select="attributes/attribute[ @name = 'mediadescription' ]/text()"/>
        </xsl:call-template>
      </p>
    </xsl:if>
    <!-- the real text content -->
    <xsl:value-of select="description" disable-output-escaping='yes'/>
  </p>
</div> <!-- end of text content -->

<!-- all content below is not subject to Readability.js, this is intentional and prevents
     unintended stripping -->

<div class='content'>

<!-- embed suitable enclosures -->

<div id='enclosureAudio'>
  <xsl:if test="count(enclosures/enclosure[contains(@mime, 'audio/')]) > 1">
    <select>
      <xsl:for-each select="enclosures/enclosure[contains(@mime, 'audio/')]">
        <xsl:element name="option">
          <xsl:value-of select="@url"/>
        </xsl:element>
      </xsl:for-each>
    </select>
  </xsl:if>
  <xsl:for-each select="enclosures/enclosure[contains(@mime, 'audio/')]">
    <xsl:if test="position() = 1">
      <xsl:element name="audio">
        <xsl:attribute name="class">enclosure</xsl:attribute>
        <xsl:attribute name="controls">controls</xsl:attribute>
        <xsl:attribute name="preload">none</xsl:attribute>
        <xsl:attribute name="src"><xsl:value-of select="@url"/></xsl:attribute>
      </xsl:element>
    </xsl:if>
  </xsl:for-each>
</div>

<div id='enclosureVideo'>
  <xsl:if test="count(enclosures/enclosure[contains(@mime, 'video/')]) > 1">
    <select>
      <xsl:for-each select="enclosures/enclosure[contains(@mime, 'video/')]">
        <xsl:element name="option">
          <xsl:value-of select="@url"/>
        </xsl:element>
      </xsl:for-each>
    </select>
  </xsl:if>
  <xsl:for-each select="enclosures/enclosure[contains(@mime, 'video/')]">
    <xsl:if test="position() = 1">
      <xsl:element name="video">
        <xsl:attribute name="class">enclosure</xsl:attribute>
        <xsl:attribute name="controls">controls</xsl:attribute>
        <xsl:attribute name="preload">none</xsl:attribute>
        <xsl:attribute name="src"><xsl:value-of select="@url"/></xsl:attribute>
      </xsl:element>
    </xsl:if>
  </xsl:for-each>
</div>

<!-- optional YouTube embed -->
<xsl:variable name="url"><xsl:value-of select="source"/></xsl:variable>
<xsl:if test="contains($url,'https://www.youtube.com/watch?v=')">
  <xsl:variable name="youtubeid" select="substring-after($url,'https://www.youtube.com/watch?v=')"/>

  <div class='content'>
    <div class='video-embed' id="{$youtubeid}">
      <xsl:element name='a'>
        <xsl:attribute name='href'>javascript:youtube_embed('<xsl:value-of select="$youtubeid"/>');</xsl:attribute>
        <!-- FIXME: translate  me -->
        <xsl:attribute name='title'>Click to embed video</xsl:attribute>
        <div class="play-btn">
        <!-- button SVG from https://douglasgreen.com/demo/responsive-video-play-button/svg-play-button.html -->
          <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 311.69 311.69">
            <path d="M155.84,0A155.85,155.85,0,1,0,311.69,155.84,155.84,155.84,0,0,0,155.84,0Zm0,296.42A140.58,140.58,0,1,1,296.42,155.84,140.58,140.58,0,0,1,155.84,296.42Z"/><polygon points="218.79 155.84 119.22 94.34 119.22 217.34 218.79 155.84"/>
          </svg>
        </div>
      </xsl:element>
      <xsl:element name='img'>
        <xsl:attribute name='class'>thumb</xsl:attribute>
        <xsl:attribute name='src'>https://i.ytimg.com/vi/<xsl:value-of select="$youtubeid"/>/hqdefault.jpg</xsl:attribute>
        <!-- FIXME: translate  me -->
        <xsl:attribute name='alt'>Youtube video image</xsl:attribute>
      </xsl:element>
      <!---->
    </div>
  </div>
</xsl:if>

<!-- comment feed display handling -->
<div id='item_comments' class='item_comments'>
  <xsl:if test="attributes/attribute[ @name = 'commentFeedUri' ] and not(./commentsSuppressed)">
     <p>
        <xsl:if test="count(comments/item) != 0">
	   <b><_span>Comments</_span></b>
	</xsl:if>

     <!-- Display "comment feed updating" hint -->
        <xsl:if test="comments/updateState = 'updating'">
           (<span>
	      <_span>Updating...</_span>
	    </span>)
	</xsl:if>
     </p>

     <!-- Display error box if necessary -->
     <xsl:if test="comments/updateError">
        <p>
	   <div id="errors">
	      <div id="updateError">
	         <xsl:value-of select="comments/updateError"/>
	      </div>
	   </div>
	</p>
     </xsl:if>

     <p>
        <xsl:apply-templates select="comments/item">
           <xsl:sort select="time"/>
        </xsl:apply-templates>
     </p>
  </xsl:if>
</div> <!-- end of comments -->
</div> <!-- end of content-->
</div> <!-- end of txtdirection div -->
</div> <!-- end of item -->

</xsl:template>

<!-- ==================================================================== -->

<!-- slashdot namespace -->
<xsl:template match="attributes/attribute[ @name = 'slash']">
  <span class="slashSection"><_span>Section</_span></span>
  <span class="slashValue"><xsl:value-of select="substring-before(., ',')"/></span>
  <span class="slashDepartment"><_span>Department</_span></span>
  <span class="slashValue"><xsl:value-of select="substring-after(., ',')"/></span>
</xsl:template>

<!-- ==================================================================== -->

<!-- comment rendering -->
<xsl:template match="comments/item">
   <div class="comment" dir="{$txtDirection}">
      <div class="comment_title"><xsl:value-of select="title"/></div>
      <div class="comment_body"><xsl:value-of select="description" disable-output-escaping='yes'/></div>
   </div>
</xsl:template>

<!-- ==================================================================== -->

<!-- newline to br based in https://stackoverflow.com/questions/3309746/how-to-convert-newline-into-br-with-xslt -->
<xsl:template name="add-newline">
  <xsl:param name="input" />

  <xsl:choose>
    <xsl:when test="not(contains($input, '&#xA;'))">
      <xsl:copy-of select="$input"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="substring-before($input, '&#xA;')" disable-output-escaping='yes'/>
      <br />
      <xsl:call-template name="add-newline">
        <xsl:with-param name="input" select="substring-after($input, '&#xA;')"/>
      </xsl:call-template>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

</xsl:stylesheet>
