rss_converter_twitter.com.xsl: use direct URLs for links, when possible
[tweeper.git] / rss_converter_twitter.com.xsl
1 <!--
2   Stylesheet to convert Twitter user timelines to RSS.
3
4   Copyright (C) 2013-2014  Antonio Ospite <ao2@ao2.it>
5
6   This file is part of tweeper.
7
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.
12
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.
17
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/>.
20 -->
21 <xsl:stylesheet version="1.0"
22     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
23     xmlns:php="http://php.net/xsl"
24     xsl:extension-element-prefixes="php"
25     exclude-result-prefixes="php">
26
27     <xsl:param name="generate-enclosure"/>
28
29     <xsl:output method="xml" indent="yes"/>
30
31     <xsl:variable name="BaseURL">
32         <xsl:text>https://twitter.com</xsl:text>
33     </xsl:variable>
34
35     <!-- Identity transform -->
36     <xsl:template match="@*|node()">
37         <xsl:copy>
38             <xsl:apply-templates select="@*|node()"/>
39         </xsl:copy>
40     </xsl:template>
41
42     <!--
43          Anchors to external links provide the direct URL in the
44          data-expanded-url attribute, so use this in the href attribute too
45          instead of the default short URL which uses the t.co redirection
46          service.
47
48          NOTE: when creating an element, attributes must be processed _before_
49          adding the contents (either children or a value):
50          http://stackoverflow.com/questions/21984867/
51     -->
52     <xsl:template match="a[@data-expanded-url]">
53         <!-- Prepend and append a white space for aestethic reasons -->
54         <xsl:text> </xsl:text>
55         <a>
56             <xsl:attribute name="href">
57                 <xsl:value-of select="@data-expanded-url"/>
58             </xsl:attribute>
59             <!-- Also strip &nbsp; and &hellip; -->
60             <xsl:value-of select="translate(., '&#xA0;&#x2026;', '')"/>
61         </a>
62         <xsl:text> </xsl:text>
63     </xsl:template>
64
65     <!--
66          These are links to pic.twitter.com, use the direct link for those
67          too instead of the t.co redirections.
68     -->
69     <xsl:template match="a[@data-pre-embedded='true']">
70         <!-- Prepend and append a white space for aestethic reasons -->
71         <xsl:text> </xsl:text>
72         <a>
73             <xsl:attribute name="href">
74                 <xsl:value-of select="concat('https://', .)"/>
75             </xsl:attribute>
76             <xsl:value-of select="concat('https://', .)"/>
77         </a>
78         <xsl:text> </xsl:text>
79     </xsl:template>
80
81     <xsl:template match="a[@data-expanded-url]" mode="enclosure">
82         <xsl:copy-of select="php:function('Tweeper::generateEnclosure', string(./@data-expanded-url))"/>
83     </xsl:template>
84
85     <xsl:variable name="screen-name" select="//div[@class='user-actions btn-group not-following ']/@data-screen-name"/>
86
87     <xsl:template match="//li[@data-item-id and @data-item-type='tweet']">
88         <xsl:variable name="user-name" select=".//div[contains(@class, 'js-stream-tweet')]/@data-screen-name"/>
89         <xsl:variable name="item-content" select=".//p[contains(@class, 'js-tweet-text')]"/>
90         <xsl:variable name="item-permalink" select="concat($BaseURL, .//div[@data-permalink-path]/@data-permalink-path)"/>
91         <item>
92             <title>
93                 <xsl:value-of select="concat($user-name, ': ')"/>
94                 <!--
95                      Prepend a space in front of the URLs which are not
96                      preceded by an open parenthesis, for aestethic reasons.
97                      Also, regex, I know: http://xkcd.com/1171/
98                 -->
99                 <xsl:variable
100                     name="processed-title"
101                     select="php:function('preg_replace', '@((?&lt;!\()(?:http[s]?://|pic.twitter.com))@', ' \1', string($item-content))"/>
102                 <!-- Also strip &nbsp; and &hellip; -->
103                 <xsl:value-of select="normalize-space(translate($processed-title, '&#xA0;&#x2026;', ''))"/>
104             </title>
105             <link>
106                 <xsl:value-of select="$item-permalink"/>
107             </link>
108             <guid>
109                 <xsl:value-of select="$item-permalink"/>
110             </guid>
111             <pubDate>
112                 <xsl:variable name="timestamp" select=".//span[contains(@class, 'js-short-timestamp')]/@data-time"/>
113                 <xsl:value-of select="php:functionString('Tweeper::epochToRssDate', number($timestamp))"/>
114             </pubDate>
115             <description>
116                 <xsl:value-of select="concat($user-name, ': ')"/>
117                 <xsl:text disable-output-escaping="yes">&lt;![CDATA[</xsl:text>
118                 <xsl:apply-templates select="$item-content/node()"/>
119                 <xsl:text disable-output-escaping="yes">]]&gt;</xsl:text>
120             </description>
121             <xsl:if test="$generate-enclosure = 1">
122                 <xsl:apply-templates select="$item-content//a[@data-expanded-url]" mode="enclosure"/>
123             </xsl:if>
124         </item>
125     </xsl:template>
126
127     <xsl:template match="/">
128         <xsl:variable name="channel-title">
129             <xsl:choose>
130                 <xsl:when test="$screen-name != ''">
131                     <xsl:value-of select="concat('Twitter / ', $screen-name)"/>
132                 </xsl:when>
133                 <xsl:otherwise>
134                     <xsl:value-of select="concat('Twitter / ', normalize-space(//h1[1]))"/>
135                 </xsl:otherwise>
136             </xsl:choose>
137         </xsl:variable>
138         <xsl:variable name="channel-link" select="//link[@rel='canonical']/@href"/>
139
140         <rss version="2.0">
141             <xsl:attribute name="xml:base"><xsl:value-of select="$BaseURL" /></xsl:attribute>
142             <channel>
143                 <generator>Tweeper</generator>
144                 <title>
145                     <xsl:value-of select="$channel-title"/>
146                 </title>
147                 <link>
148                     <xsl:value-of select="$channel-link"/>
149                 </link>
150                 <description>
151                     <xsl:value-of select="normalize-space(//div[@class='ProfileHeaderCard'])"/>
152                 </description>
153                 <image>
154                     <title>
155                         <xsl:value-of select="$channel-title"/>
156                     </title>
157                     <link>
158                         <xsl:value-of select="$channel-link"/>
159                     </link>
160                     <url>
161                         <xsl:value-of select="//a[contains(@class, 'profile-picture')]/@href"/>
162                     </url>
163                 </image>
164                 <xsl:apply-templates select="//ol[@id='stream-items-id']/li[@data-item-id and @data-item-type='tweet']"/>
165             </channel>
166         </rss>
167     </xsl:template>
168 </xsl:stylesheet>