Repository: getpelican/pelican

Issue:
Atom feeds don't validate with SITEURL containing HTTPS scheme and/or a specific service port.
When SITEURL = 'https://example.com' or 'http://example.com:8080', `writers.py` generates `unique_id` producing wrong 'TAG:' IDs.

A possible fix could be to switch **line 45** from :

``` python
unique_id='tag:%s,%s:%s' % (self.site_url.replace('http://', ''),
                            item.date.date(), item.url),
```

to :

``` python
unique_id='tag:%s,%s:%s' % (re.sub('^https?://(?P<host>.*?)(:\d+)?$','\g<host>',self.site_url),
                            item.date.date(), item.url),
```

Atom feeds don't validate with SITEURL containing HTTPS scheme and/or a specific service port.
When SITEURL = 'https://example.com' or 'http://example.com:8080', `writers.py` generates `unique_id` producing wrong 'TAG:' IDs.

A possible fix could be to switch **line 45** from :

``` python
unique_id='tag:%s,%s:%s' % (self.site_url.replace('http://', ''),
                            item.date.date(), item.url),
```

to :

``` python
unique_id='tag:%s,%s:%s' % (re.sub('^https?://(?P<host>.*?)(:\d+)?$','\g<host>',self.site_url),
                            item.date.date(), item.url),
```



Discussion:
Also when the siteurl contains a path (eg `http://example.com/blog`)

I think the proper solution is:

``` python
    from urlparse import urlparse

    ...

    def _add_item_to_the_feed(self, feed, item):
        title = Markup(item.title).striptags()
        link='%s/%s' % (self.site_url, item.url)
        feed.add_item(
            title=title,
            link=link,
            unique_id='tag:%s,%s:%s' % (urlparse(link).netloc,
                                        item.date.date(), urlparse(link).path),
            description=item.get_content(self.site_url),
            categories=item.tags if hasattr(item, 'tags') else None,
            author_name=getattr(item, 'author', ''),
            pubdate=set_date_tzinfo(item.date,
                self.settings.get('TIMEZONE', None)))

```

That works in every case and the feed created is valid.

Also when the siteurl contains a path (eg `http://example.com/blog`)

I think the proper solution is:

``` python
    from urlparse import urlparse

    ...

    def _add_item_to_the_feed(self, feed, item):
        title = Markup(item.title).striptags()
        link='%s/%s' % (self.site_url, item.url)
        feed.add_item(
            title=title,
            link=link,
            unique_id='tag:%s,%s:%s' % (urlparse(link).netloc,
                                        item.date.date(), urlparse(link).path),
            description=item.get_content(self.site_url),
            categories=item.tags if hasattr(item, 'tags') else None,
            author_name=getattr(item, 'author', ''),
            pubdate=set_date_tzinfo(item.date,
                self.settings.get('TIMEZONE', None)))

```

That works in every case and the feed created is valid.


Resolving patch:
diff --git a/pelican/writers.py b/pelican/writers.py
--- a/pelican/writers.py
+++ b/pelican/writers.py
@@ -8,6 +8,9 @@
 
 if not six.PY3:
     from codecs import open
+    from urlparse import urlparse
+else:
+    from urllib.parse import urlparse
 
 from feedgenerator import Atom1Feed, Rss201rev2Feed
 from jinja2 import Markup
@@ -41,11 +44,13 @@ def _create_new_feed(self, feed_type, context):
     def _add_item_to_the_feed(self, feed, item):
 
         title = Markup(item.title).striptags()
+        link = '%s/%s' % (self.site_url, item.url)
         feed.add_item(
             title=title,
-            link='%s/%s' % (self.site_url, item.url),
-            unique_id='tag:%s,%s:%s' % (self.site_url.replace('http://', ''),
-                                        item.date.date(), item.url),
+            link=link,
+            unique_id='tag:%s,%s:%s' % (urlparse(link).netloc,
+                                        item.date.date(),
+                                        urlparse(link).path.lstrip('/')),
             description=item.get_content(self.site_url),
             categories=item.tags if hasattr(item, 'tags') else None,
             author_name=getattr(item, 'author', ''),


Test patch: