-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreport.py
More file actions
186 lines (160 loc) · 5.91 KB
/
report.py
File metadata and controls
186 lines (160 loc) · 5.91 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# import datetime
import re
import os
import sys
from cachier import cachier
import pywikibot
from pyzotero import zotero
from bs4 import BeautifulSoup
import json
from datetime import datetime
REPORT_TEMPLATE = """<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="report.css">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>{title}</title>
<meta name="title" content="{title}"/>
<meta name="date" content="{date}"/>
<meta name="description" content="{abstract}"/>
<meta name="series" content="{series}"/>
<meta name="index" content="{number}"/>
<meta name="publisher" content="{institution}"/>
<meta name="author" content="{authors}"/>
</head>
<body class="mw-content content" style="background:white; max-width:100%">
<section id="frontmatter">
<p>© Copyright {date} {institution}</p>
<p>{license}</p>
<p>{address}</p>
<p>{acknowledgements}</p>
</section>
<section id="abstract" style="page-break-before:always">
<h3>Abstract</h3>
<p class="abstract">{abstract}</p>
</section>
<section id="content">
<article>
{source}
</article>
</section>
</body>
</html>
"""
def fetch_bibliography(zotero_library, refs):
q = 'https://api.zotero.org/groups/{}/items/{}?format=json&include=bib&style=chicago-note-bibliography&linkwrap=1'
zot = zotero.Zotero(zotero_library, 'group')
# zot.add_parameters(content='bib', style='mla')
zot.add_parameters(format='json', content='bib',
style='chicago-note-bibliography', linkwrap=1,
itemKey=','.join(refs))
items = zot.everything(zot.items())
bib = '\r\n'.join(sorted(items))
return bib
def process_metadata(wikitext):
m = re.search('{{Report metadata(.*?)}}', wikitext,
flags=re.MULTILINE|re.DOTALL)
if not m:
sys.exit("No metadata template found in the source!")
tpl = m.group(1)
meta = {}
for line in tpl.strip().split('|'):
if not '=' in line:
continue
k,sep,v = line.partition('=')
meta[k.strip()] = v.strip()
return meta
def wiki_page(pagename):
site = pywikibot.Site()
page = pywikibot.Page(site, pagename)
req = site._simple_request(action='parse', page=page,
prop="text|images",
disableeditsection="1", disabletoc="1")
data = req.submit()
html = data['parse']['text']['*']
images = data['parse']['images']
return (html, page.text, images)
# return (page.get_parsed_page(), page.text)
def unlink_notes(report):
soup = BeautifulSoup(report, 'html.parser')
for n in soup.find_all(class_='noprint'):
n.decompose()
for n in soup.find_all(class_='reference-text'):
for link in n.find_all('a'):
link.unwrap()
for ref in soup.find_all('div', 'magnify'):
ref.decompose()
# calibre doesn't recognize page-break-after:avoid but it does recognize
# page-break-inside:avoid
for h in ('h3', 'h2'):
for header in soup.find_all(h):
n1 = header.next_sibling # should be whitespace
n2 = None
if n1:
n2 = n1.next_sibling
new_tag = soup.new_tag('div')
new_tag['style'] = 'page-break-inside:avoid'
header.wrap(new_tag)
if n1:
new_tag.append(n1)
if n2:
new_tag.append(n2)
for ref in soup.find_all('div', 'csl-entry'):
ref.name = 'p' # change citation container from "div" to "p"
del ref['style'] # remove hard-coded hanging indent
for thumb in soup.find_all('div', 'thumb'):
name = thumb.a['href'].partition('File:')[2]
if int(thumb.img['width']) > 528: # 5.5in @ 96 dpi
thumb.div['class'] = 'fullwidth'
del thumb.div['style']
del thumb.img['height']
del thumb.img['width']
del thumb.a['href']
thumb.img['src'] = name
return str(soup)
def download_images(images, outdir):
site = pywikibot.Site()
# retrieve all the full-resolution images (i.e., not the thumbs)
for image in images:
filename = image.replace('File:', '')
local_path = os.path.join(outdir, filename)
if not os.path.exists(local_path):
allimages = site.allimages(start=image, total=1)
for i in allimages:
print('downloading {}...'.format(local_path))
i.download(filename=local_path)
def run(*args):
options = {}
local_args = pywikibot.handle_args(args)
required = ['title', 'outdir', 'license', 'address',
'acknowledgements']
for arg in local_args:
option, sep, value = arg.partition(':')
options[option.strip('-')] = value
for option in required:
if not options.get(option, None):
value = pywikibot.input('Please enter a value for ' + option)
options[option] = value
html, wikitext, images = wiki_page(options['title'])
metadata = process_metadata(wikitext)
metadata['license'] = options['license']
metadata['date'] = datetime.now().strftime('%Y')
metadata['title'] = options['title']
metadata['address'] = options['address']
metadata['acknowledgements'] = options ['acknowledgements']
download_images(images, options['outdir'])
if options.get('zotero_library', None):
pagerefs = re.findall('{{Zotero.*?id=([A-Z0-9]+)', wikitext)
refs = []
refs.extend([ref for ref in pagerefs if not ref in refs])
if len(refs) > 0:
html = '\r\n\r\n'.join([html,
'<article class="bibliography"><h2>Bibliography</h2></article>',
fetch_bibliography(options['zotero_library'], refs)])
html = REPORT_TEMPLATE.format( source=html, **metadata )
html = unlink_notes(html)
with open(os.path.join(options['outdir'], 'index.html'), 'w') as out:
out.write(html)
if __name__ == '__main__':
run()