2   Stylesheet to convert Twitter user timelines to RSS.
 
   4   Copyright (C) 2013-2020  Antonio Ospite <ao2@ao2.it>
 
   6   This file is part of tweeper.
 
   8   This program is free software: you can redistribute it and/or modify
 
   9   it under the terms of the GNU General Public License as published by
 
  10   the Free Software Foundation, either version 3 of the License, or
 
  11   (at your option) any later version.
 
  13   This program is distributed in the hope that it will be useful,
 
  14   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
  15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
  16   GNU General Public License for more details.
 
  18   You should have received a copy of the GNU General Public License
 
  19   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
  21 <xsl:stylesheet version="1.0"
 
  22     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 
  23     xmlns:php="http://php.net/xsl"
 
  24     exclude-result-prefixes="php">
 
  26     <xsl:param name="generate-enclosure"/>
 
  27     <xsl:param name="show-usernames"/>
 
  28     <xsl:param name="show-multimedia"/>
 
  30     <xsl:output method="xml" indent="yes"/>
 
  32     <xsl:variable name="BaseURL">
 
  33         <xsl:text>https://twitter.com</xsl:text>
 
  36     <!-- Identity transform -->
 
  37     <xsl:template match="@*|node()">
 
  40                 Strip the style attribute while copying elements because it may be
 
  42                 https://validator.w3.org/feed/docs/warning/DangerousStyleAttr.html
 
  44             <xsl:apply-templates select="@*[not(name() = 'style')]|node()"/>
 
  49          Anchors to external links provide the direct URL in the
 
  50          data-expanded-url attribute, so use this in the href attribute too
 
  51          instead of the default short URL which uses the t.co redirection
 
  54          NOTE: when creating an element, attributes must be processed _before_
 
  55          adding the contents (either children or a value):
 
  56          http://stackoverflow.com/questions/21984867/
 
  58     <xsl:template match="a[@data-expanded-url]">
 
  59         <!-- Prepend and append a white space for aestethic reasons -->
 
  60         <xsl:text> </xsl:text>
 
  62             <xsl:attribute name="href">
 
  63                 <xsl:value-of select="@data-expanded-url"/>
 
  65             <!-- Also strip   and … -->
 
  66             <xsl:value-of select="translate(., ' …', '')"/>
 
  68         <xsl:text> </xsl:text>
 
  72          These are links to pic.twitter.com, use the direct link for those
 
  73          too instead of the t.co redirections.
 
  75     <xsl:template match="a[@data-pre-embedded='true']">
 
  76         <xsl:if test="$show-multimedia = 1">
 
  77             <!-- Prepend and append a white space for aestethic reasons -->
 
  78             <xsl:text> </xsl:text>
 
  80                 <xsl:attribute name="href">
 
  81                     <xsl:value-of select="concat('https://', .)"/>
 
  83                 <xsl:value-of select="concat('https://', .)"/>
 
  85             <xsl:text> </xsl:text>
 
  89     <!-- Present images in a more convenient way -->
 
  90     <xsl:template match="div[@data-image-url]">
 
  92             <xsl:attribute name="href">
 
  93                 <xsl:value-of select="concat(@data-image-url, ':orig')"/>
 
  95             <img style="max-width: 100%">
 
  96                 <xsl:attribute name="src">
 
  97                     <xsl:value-of select="@data-image-url"/>
 
 103     <!-- Don't repeat background in embedded media content -->
 
 104     <xsl:template match="div[contains(@class, 'PlayableMedia-player')]">
 
 106             <xsl:apply-templates select="@*"/>
 
 107             <xsl:attribute name="style">
 
 108                 <xsl:value-of select="concat(@style, '; background-repeat: no-repeat; background-size: 100% auto')"/>
 
 110             <xsl:apply-templates select="node()"/>
 
 114     <xsl:template match="a[@data-expanded-url]" mode="enclosure">
 
 115         <xsl:copy-of select="php:functionString('Tweeper\Tweeper::generateEnclosure', ./@data-expanded-url)"/>
 
 118     <xsl:template match="div[@data-image-url]" mode="enclosure">
 
 119         <xsl:copy-of select="php:functionString('Tweeper\Tweeper::generateEnclosure', concat(./@data-image-url, ':orig'))"/>
 
 122     <xsl:variable name="screen-name" select="//div[@class='user-actions btn-group not-following ']/@data-screen-name"/>
 
 124     <xsl:template match="//div[@class='permalink-inner permalink-tweet-container'] | //li[@data-item-id and @data-item-type='tweet']">
 
 125         <xsl:variable name="user-name" select=".//div[@data-tweet-id]/@data-screen-name"/>
 
 126         <xsl:variable name="item-content" select=".//p[contains(@class, 'js-tweet-text')]"/>
 
 127         <xsl:variable name="item-media" select=".//div[contains(@class, 'AdaptiveMedia-container')]"/>
 
 128         <xsl:variable name="item-permalink" select="concat($BaseURL, .//div[@data-permalink-path]/@data-permalink-path)"/>
 
 130         <xsl:variable name="item-has-video" select="$item-media//*[contains(@class, 'PlayableMedia--video')]"/>
 
 131         <xsl:variable name="item-has-gif" select="$item-media//*[contains(@class, 'PlayableMedia--gif')]"/>
 
 134                 <xsl:if test="($show-usernames = 1) or ($screen-name != $user-name)">
 
 135                     <xsl:value-of select="concat($user-name, ': ')"/>
 
 137                 <xsl:if test="$item-has-video">
 
 138                     <xsl:text>(Video) </xsl:text>
 
 141                      Prepend a space in front of the URLs which are not
 
 142                      preceded by an open parenthesis, for aestethic reasons.
 
 143                      Also, regex, I know: http://xkcd.com/1171/
 
 146                     name="processed-title"
 
 147                     select="php:functionString('preg_replace', '@((?<!\()(?:http[s]?://|pic.twitter.com))@', ' \1', $item-content)"/>
 
 148                 <!-- Also strip   and … -->
 
 149                 <xsl:value-of select="normalize-space(translate($processed-title, ' …', ''))"/>
 
 152                 <xsl:value-of select="$item-permalink"/>
 
 155                 <xsl:value-of select="$item-permalink"/>
 
 158                 <xsl:variable name="timestamp" select=".//span[contains(@class, 'js-short-timestamp')]/@data-time"/>
 
 159                 <xsl:value-of select="php:functionString('Tweeper\Tweeper::epochToRssDate', number($timestamp))"/>
 
 162                 <xsl:text disable-output-escaping="yes"><![CDATA[</xsl:text>
 
 163                 <xsl:if test="($show-usernames = 1) or ($screen-name != $user-name)">
 
 164                     <xsl:value-of select="concat($user-name, ':')"/>
 
 165                     <xsl:element name="br"/>
 
 167                 <xsl:if test="$item-has-video">
 
 168                     <xsl:text> (Video)</xsl:text>
 
 169                     <xsl:element name="br"/>
 
 171                 <xsl:if test="$item-has-gif">
 
 172                     <xsl:text> (GIF)</xsl:text>
 
 173                     <xsl:element name="br"/>
 
 175                 <xsl:element name="span">
 
 176                     <xsl:attribute name="style">white-space: pre-wrap;</xsl:attribute>
 
 177                     <xsl:apply-templates select="$item-content/node()"/>
 
 179                 <xsl:if test="$show-multimedia = 1">
 
 180                     <xsl:apply-templates select="$item-media/node()"/>
 
 182                 <xsl:text disable-output-escaping="yes">]]></xsl:text>
 
 184             <xsl:if test="$generate-enclosure = 1">
 
 185                 <xsl:apply-templates select="$item-content//a[@data-expanded-url]" mode="enclosure"/>
 
 186                 <xsl:apply-templates select="$item-media//div[@data-image-url]" mode="enclosure"/>
 
 191     <xsl:template match="/">
 
 192         <xsl:variable name="channel-title">
 
 194                 <xsl:when test="$screen-name != ''">
 
 195                     <xsl:value-of select="concat('Twitter / ', $screen-name)"/>
 
 198                     <xsl:value-of select="concat('Twitter / ', normalize-space(//h1[1]))"/>
 
 202         <xsl:variable name="channel-link" select="//link[@rel='canonical']/@href"/>
 
 203         <xsl:variable name="channel-image" select="//a[contains(@class, 'profile-picture')]/@href"/>
 
 206             <xsl:attribute name="xml:base"><xsl:value-of select="$BaseURL" /></xsl:attribute>
 
 208                 <generator>Tweeper</generator>
 
 210                     <xsl:value-of select="$channel-title"/>
 
 213                     <xsl:value-of select="$channel-link"/>
 
 216                     <xsl:value-of select="normalize-space(//div[@class='ProfileHeaderCard'])"/>
 
 217                     <!-- The following rule should only match on hashtag URLs -->
 
 218                     <xsl:value-of select="normalize-space(//div[@class='SearchNavigation-textContainer'])"/>
 
 220                 <xsl:if test="$channel-image != ''">
 
 223                             <xsl:value-of select="$channel-title"/>
 
 226                             <xsl:value-of select="$channel-link"/>
 
 229                             <xsl:value-of select="$channel-image"/>
 
 233                 <xsl:apply-templates select="//ol[@id='stream-items-id']/li[@data-item-id and @data-item-type='tweet' and not(contains(@class, 'has-profile-promoted-tweet'))]"/>
 
 235                 <!-- These rules will only match on permalink URLs -->
 
 236                 <xsl:apply-templates select="//div[@class='permalink-inner permalink-tweet-container']"/>
 
 237                 <xsl:apply-templates select="//div[@data-component-context='replies']//li[@data-item-id and @data-item-type='tweet' and not(contains(@class, 'has-profile-promoted-tweet'))]"/>