This repository was archived by the owner on May 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspitouthtml.py
More file actions
119 lines (88 loc) · 3.59 KB
/
spitouthtml.py
File metadata and controls
119 lines (88 loc) · 3.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# This script takes a Python dictionary of newspaper stories
# and turns them into HTML files, including an index.html page.
import codecs
import parsearticles
import pystache
from os import path
from dateutil.parser import parse
# An individual "article" or "story" is a dictionary with
# headline
# date
# body
class HtmlOutput(object):
def __init__(self, story, article_index):
self.story = story
self.article_index = article_index
self.dateindex = [x["date"] for x in self.article_index]
self.datemarker = self.dateindex.index(self.story["date"])
def headline(self):
return self.story["headline"]
def pubdate(self):
self.dateobject = parse(self.story["date"])
return english_from_date(self.dateobject)
def verbiage(self):
return self.story["body"]
def nextstoryhed(self):
if self.datemarker == len(self.article_index) - 1:
return "Last story"
else:
return "Next: " + self.article_index[self.datemarker+1]["headline"]
def nextstorydateslug(self):
if self.datemarker == len(self.article_index) - 1:
return ""
else:
self.nextdate = self.article_index[self.datemarker+1]["date"]
self.dateobject = parse(self.nextdate)
return filename_from_date(self.dateobject)
def prevstoryhed(self):
if self.datemarker == 0:
return "First story"
else:
return "Previous: " + self.article_index[self.datemarker-1]["headline"]
def prevstorydateslug(self):
if self.datemarker == 0:
return ""
else:
self.prevdate = self.article_index[self.datemarker-1]["date"]
self.dateobject = parse(self.prevdate)
return filename_from_date(self.dateobject)
class IndexOutput(object):
def __init__(self, article_index):
self.article_index = article_index
def stories(self):
# This would be better as a Pystache partial or
# iteration if I could figure that out.
toc = ""
for storydesc in self.article_index:
dateobject = parse(storydesc["date"])
dateURL = filename_from_date(dateobject)
datestring = english_from_date(dateobject)
toc += '<li><a href="' + dateURL + '">' + storydesc["headline"] + "</a>, " + datestring + "</li>"
return toc
def filename_from_date(dateobject):
return dateobject.strftime("%d-%B-%Y") + ".html"
def english_from_date(dateobject):
return dateobject.strftime("%A, %d %B %Y")
def htmlize_story(story, article_index):
'''use pystache to turn each storydict into HTML to output,
take HTML string and write it to a file'''
storyoutput = HtmlOutput(story, article_index)
dateobject = parse(story["date"])
dateslug = filename_from_date(dateobject)
renderer = pystache.Renderer()
with codecs.open(dateslug, encoding='utf-8', mode='w') as f:
f.write(renderer.render(storyoutput))
def htmlize_article_index(article_index):
'''use pystache to turn the list into HTML to output,
take HTML string and write it to a file '''
indexoutput = IndexOutput(article_index)
renderer = pystache.Renderer()
with codecs.open("index.html", encoding='utf-8', mode='w') as f:
f.write(renderer.render(indexoutput))
if __name__ == "__main__":
(article_list, article_index) = parsearticles.parse_files(parsearticles.ARCHIVEFILES, [])
htmlize_article_index(article_index)
for article in article_list:
htmlize_story(article, article_index)