Make stylesheet file name parametric
authorAntonio Ospite <ospite@studenti.unina.it>
Sat, 27 Jul 2013 11:24:44 +0000 (13:24 +0200)
committerAntonio Ospite <ospite@studenti.unina.it>
Sat, 27 Jul 2013 11:24:44 +0000 (13:24 +0200)
The host is encoded in the file name, this is in order to support more
sites with no changes to the code, all that is needed is just new
stylesheets with the host in their filename following the scheme will:

  rss_converter_HOST.xsl

Where HOST has the meaning of the "host" field in the return value of
the PHP parse_url() function.

rss_converter_twitter.com.xsl [new file with mode: 0644]
tweeper.php
twitter_user_timeline2rss.xsl [deleted file]

diff --git a/rss_converter_twitter.com.xsl b/rss_converter_twitter.com.xsl
new file mode 100644 (file)
index 0000000..6d72ceb
--- /dev/null
@@ -0,0 +1,79 @@
+<!--
+  Stylesheet to convert Twitter user timelines to RSS.
+
+  Copyright (C) 2013  Antonio Ospite <ospite@studenti.unina.it>
+
+  This file is part of tweeper.
+
+  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 3 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, see <http://www.gnu.org/licenses/>.
+-->
+<xsl:stylesheet version="1.0"
+    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
+    xmlns:php="http://php.net/xsl"
+    xsl:extension-element-prefixes="php">
+
+    <xsl:output method="xml" indent="yes"/>
+
+    <xsl:variable name="twitterBaseURL">
+        <xsl:text>https://twitter.com</xsl:text>
+    </xsl:variable>
+
+    <xsl:variable name="screen-name" select="//div[@class='profile-card-inner']/@data-screen-name"/>
+
+    <xsl:template match="//div[@id='timeline']//ol[@id='stream-items-id']//li[@data-item-type='tweet']">
+        <xsl:variable name="tweet-text" select=".//p[@class='js-tweet-text tweet-text']"/>
+        <item>
+            <title>
+                <xsl:value-of select="concat($screen-name, ': ')"/>
+                <xsl:value-of select="$tweet-text"/>
+            </title>
+            <link>
+                <xsl:value-of select="$twitterBaseURL"/><xsl:value-of select=".//a[@class='details with-icn js-details']/@href"/>
+            </link>
+            <pubDate>
+                <xsl:value-of select="php:functionString('formatDate', .//small[@class='time']//span/@data-time)"/>
+            </pubDate>
+            <description>
+                <xsl:value-of select="concat($screen-name, ': ')"/>
+                <xsl:text disable-output-escaping="yes">&lt;![CDATA[</xsl:text>
+                <xsl:copy-of select="$tweet-text/node()"/>
+                <xsl:text disable-output-escaping="yes">]]&gt;</xsl:text>
+            </description>
+        </item>
+    </xsl:template>
+
+    <xsl:template match="/">
+
+        <rss version="2.0">
+            <channel>
+                <generator>Tweeper</generator>
+                <title>
+                    <xsl:text>Twitter / </xsl:text><xsl:value-of select="$screen-name"/>
+                </title>
+                <link>
+                    <xsl:value-of select="//link[@rel='canonical']/@href"/>
+                </link>
+                <description>
+                    <xsl:value-of select="//meta[@name='description']/@content"/>
+                </description>
+                <image>
+                    <url>
+                        <xsl:value-of select="//a[@class='profile-picture media-thumbnail']/@href"/>
+                    </url>
+                </image>
+                <xsl:apply-templates select="//div[@id='timeline']//ol[@id='stream-items-id']//li[@data-item-type='tweet']"/>
+            </channel>
+        </rss>
+    </xsl:template>
+</xsl:stylesheet>
index a275a64..4050100 100755 (executable)
@@ -95,14 +95,9 @@ $url = parse_url($src_url);
 if (FALSE === $url || empty($url["host"]))
   die("Invalid url\n");
 
 if (FALSE === $url || empty($url["host"]))
   die("Invalid url\n");
 
-$stylesheets = array(
-  "twitter.com" => "twitter_user_timeline2rss.xsl",
-);
-
-if (FALSE === array_key_exists($url["host"], $stylesheets))
+$stylesheet = "rss_converter_" . $url["host"] . ".xsl";
+if (FALSE === file_exists($stylesheet))
   die("Unsupported host\n");
 
   die("Unsupported host\n");
 
-$stylesheet = $stylesheets[$url["host"]];
-
 $tweeper = new Tweeper($stylesheet);
 echo $tweeper->tweep($src_url);
 $tweeper = new Tweeper($stylesheet);
 echo $tweeper->tweep($src_url);
diff --git a/twitter_user_timeline2rss.xsl b/twitter_user_timeline2rss.xsl
deleted file mode 100644 (file)
index 6d72ceb..0000000
+++ /dev/null
@@ -1,79 +0,0 @@
-<!--
-  Stylesheet to convert Twitter user timelines to RSS.
-
-  Copyright (C) 2013  Antonio Ospite <ospite@studenti.unina.it>
-
-  This file is part of tweeper.
-
-  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 3 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, see <http://www.gnu.org/licenses/>.
--->
-<xsl:stylesheet version="1.0"
-    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
-    xmlns:php="http://php.net/xsl"
-    xsl:extension-element-prefixes="php">
-
-    <xsl:output method="xml" indent="yes"/>
-
-    <xsl:variable name="twitterBaseURL">
-        <xsl:text>https://twitter.com</xsl:text>
-    </xsl:variable>
-
-    <xsl:variable name="screen-name" select="//div[@class='profile-card-inner']/@data-screen-name"/>
-
-    <xsl:template match="//div[@id='timeline']//ol[@id='stream-items-id']//li[@data-item-type='tweet']">
-        <xsl:variable name="tweet-text" select=".//p[@class='js-tweet-text tweet-text']"/>
-        <item>
-            <title>
-                <xsl:value-of select="concat($screen-name, ': ')"/>
-                <xsl:value-of select="$tweet-text"/>
-            </title>
-            <link>
-                <xsl:value-of select="$twitterBaseURL"/><xsl:value-of select=".//a[@class='details with-icn js-details']/@href"/>
-            </link>
-            <pubDate>
-                <xsl:value-of select="php:functionString('formatDate', .//small[@class='time']//span/@data-time)"/>
-            </pubDate>
-            <description>
-                <xsl:value-of select="concat($screen-name, ': ')"/>
-                <xsl:text disable-output-escaping="yes">&lt;![CDATA[</xsl:text>
-                <xsl:copy-of select="$tweet-text/node()"/>
-                <xsl:text disable-output-escaping="yes">]]&gt;</xsl:text>
-            </description>
-        </item>
-    </xsl:template>
-
-    <xsl:template match="/">
-
-        <rss version="2.0">
-            <channel>
-                <generator>Tweeper</generator>
-                <title>
-                    <xsl:text>Twitter / </xsl:text><xsl:value-of select="$screen-name"/>
-                </title>
-                <link>
-                    <xsl:value-of select="//link[@rel='canonical']/@href"/>
-                </link>
-                <description>
-                    <xsl:value-of select="//meta[@name='description']/@content"/>
-                </description>
-                <image>
-                    <url>
-                        <xsl:value-of select="//a[@class='profile-picture media-thumbnail']/@href"/>
-                    </url>
-                </image>
-                <xsl:apply-templates select="//div[@id='timeline']//ol[@id='stream-items-id']//li[@data-item-type='tweet']"/>
-            </channel>
-        </rss>
-    </xsl:template>
-</xsl:stylesheet>