diff --git a/pinboardzine.py b/pinboardzine.py index d16fae6..21343ee 100644 --- a/pinboardzine.py +++ b/pinboardzine.py @@ -64,6 +64,18 @@ """ +ARTICLE_HTML = """ + + + + +

+

+
+ + """ + + def contents_ncx_for_articles(articles, uid, title): root = ElementTree.fromstring(CONTENTS_NCX_XML) # Add head/meta name=dtb:uid @@ -173,6 +185,39 @@ def contents_html_for_articles(articles, uid, title): return html +def html_for_readable_article(article, readable): + root = ElementTree.fromstring(ARTICLE_HTML.strip()) + + title_node = root.find('./head/title') + title_node.text = article['title'] + + head_node = root.find('./head') + if article['author']: + ElementTree.SubElement(head_node, 'meta', { + 'name': 'author', + 'content': article['author'], + }) + if article['description']: + ElementTree.SubElement(head_node, 'meta', { + 'name': 'description', + 'content': article['description'], + }) + + title_node = root.find('./body/h3') + title_node.text = article['title'] + + link_node = root.find('./body/h4/a') + link_node.attrib['href'] = article['u'] + link_node.text = readable['domain'] + if article['author']: + link_node.tail = ' by ' + article['author'] + + html = ElementTree.tostring(root, encoding='unicode') + html = html[:-len('')] + html = ''.join((html, readable['content'], '')) + return html + + def zine(username: 'Pinboard username to find articles for', outputfile: 'filename for the output mobi file', items: 'number of items to put in the zine' =20, @@ -237,25 +282,8 @@ def zine(username: 'Pinboard username to find articles for', article['title'] = '{} article'.format(readable['domain']) article['description'] = article['n'] or readable['dek'] or readable['excerpt'] article['author'] = readable['author'] - article['content'] = readable['content'] - article['domain'] = readable['domain'] - article['url'] = url - read_html = """ - - - {title} - - -
-

{title}

-

{domain} • by {author}

-
- {content} -
- - """.format(**article) + read_html = html_for_readable_article(article, readable) # Write it to the zine directory. filename = article['filename'] = re.sub(r'[\W_]+', '-', url) + '.html'