3 # Generate an HTML page with the result of one or more notmuch
4 # searches, (with links to gmane views of each email if available).
6 # Copyright (c) 2011-2012 David Bremner <david@tethera.net>
7 # Copyright (c) 2014 Carl Worth <cworth@cworth.org
10 # - python 2.6 for json
11 # - argparse; either python 2.7, or install separately
13 # This program is free software: you can redistribute it and/or modify
14 # it under the terms of the GNU General Public License as published by
15 # the Free Software Foundation, either version 3 of the License, or
16 # (at your option) any later version.
18 # This program is distributed in the hope that it will be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 # GNU General Public License for more details.
23 # You should have received a copy of the GNU General Public License
24 # along with this program. If not, see http://www.gnu.org/licenses/ .
26 from __future__ import print_function
27 from __future__ import unicode_literals
34 from urllib.parse import quote
35 except ImportError: # Python 2
36 from urllib import quote
43 import xml.sax.saxutils
52 "title": "Page title",
53 "blurb": "Page description"
58 "title": "View title",
59 "comment": "View description",
60 "query": [ "{query}" ]
66 if not hasattr(collections, 'OrderedDict'): # Python 2.6 or earlier
67 class _OrderedDict (dict):
68 "Just enough of a stub to get through Page._get_threads"
69 def __init__(self, *args, **kwargs):
70 super(_OrderedDict, self).__init__(*args, **kwargs)
71 self._keys = [] # record key order
73 def __setitem__(self, key, value):
74 super(_OrderedDict, self).__setitem__(key, value)
75 self._keys.append(key)
78 for key in self._keys:
82 collections.OrderedDict = _OrderedDict
85 def read_config(path, encoding=None):
86 "Read config from json file"
95 self.running_data = {}
99 def __init__(self, header=None, footer=None):
103 def write(self, database, views, stream=None):
106 byte_stream = sys.stdout.buffer
107 except AttributeError: # Python 2
108 byte_stream = sys.stdout
109 stream = codecs.getwriter(encoding=_ENCODING)(stream=byte_stream)
110 self._write_header(views=views, stream=stream)
112 self._write_view(database=database, view=view, stream=stream)
113 self._write_footer(views=views, stream=stream)
115 def _write_header(self, views, stream):
117 stream.write(self.header)
119 def _write_footer(self, views, stream):
121 stream.write(self.footer)
123 def _write_view(self, database, view, stream):
124 if 'query-string' not in view:
125 query = view['query']
126 view['query-string'] = ' and '.join(query)
127 q = notmuch.Query(database, view['query-string'])
128 q.set_sort(notmuch.Query.SORT.OLDEST_FIRST)
129 threads = self._get_threads(messages=q.search_messages())
130 self._write_view_header(view=view, stream=stream)
131 self._write_threads(threads=threads, stream=stream)
133 def _get_threads(self, messages):
134 threads = collections.OrderedDict()
135 for message in messages:
136 thread_id = message.get_thread_id()
137 if thread_id in threads:
138 thread = threads[thread_id]
141 threads[thread_id] = thread
142 thread.running_data, display_data = self._message_display_data(
143 running_data=thread.running_data, message=message)
144 thread.append(display_data)
145 return list(threads.values())
147 def _write_view_header(self, view, stream):
150 def _write_threads(self, threads, stream):
151 for thread in threads:
152 for message_display_data in thread:
154 ('{date:10.10s} {from:20.20s} {subject:40.40s}\n'
155 '{message-id-term:>72}\n'
156 ).format(**message_display_data))
157 if thread != threads[-1]:
160 def _message_display_data(self, running_data, message):
161 headers = ('thread-id', 'message-id', 'date', 'from', 'subject')
163 for header in headers:
164 if header == 'thread-id':
165 value = message.get_thread_id()
166 elif header == 'message-id':
167 value = message.get_message_id()
168 data['message-id-term'] = 'id:"{0}"'.format(value)
169 elif header == 'date':
170 value = str(datetime.datetime.utcfromtimestamp(
171 message.get_date()).date())
173 value = message.get_header(header)
175 (value, addr) = email.utils.parseaddr(value)
177 value = addr.split('@')[0]
179 next_running_data = data.copy()
180 for header, value in data.items():
181 if header in ['message-id', 'subject']:
183 if value == running_data.get(header, None):
185 return (next_running_data, data)
188 class HtmlPage (Page):
189 _slug_regexp = re.compile('\W+')
191 def _write_header(self, views, stream):
192 super(HtmlPage, self)._write_header(views=views, stream=stream)
193 stream.write('<ul>\n')
196 view['id'] = self._slug(view['title'])
198 '<li><a href="#{id}">{title}</a></li>\n'.format(**view))
199 stream.write('</ul>\n')
201 def _write_view_header(self, view, stream):
202 stream.write('<h3 id="{id}">{title}</h3>\n'.format(**view))
203 stream.write('<p>\n')
204 if 'comment' in view:
205 stream.write(view['comment'])
208 '<p>This view is generated from the following query:',
212 'notmuch search ' + view['query-string'],
219 def _write_threads(self, threads, stream):
222 stream.write('<table>\n')
223 for thread in threads:
224 stream.write(' <tbody>\n')
225 for message_display_data in thread:
227 ' <tr class="message-first">\n'
230 ' <td>{subject}</td>\n'
232 ).format(**message_display_data))
233 stream.write(' </tbody>\n')
234 if thread != threads[-1]:
236 ' <tbody><tr><td colspan="2"><br /></td></tr></tbody>\n')
237 stream.write('</table>\n')
239 def _message_display_data(self, *args, **kwargs):
240 running_data, display_data = super(
241 HtmlPage, self)._message_display_data(
243 if 'subject' in display_data and 'message-id' in display_data:
245 'message-id': quote(display_data['message-id']),
246 'subject': xml.sax.saxutils.escape(display_data['subject']),
248 display_data['subject'] = (
249 '<a href="http://mid.gmane.org/{message-id}">{subject}</a>'
251 for key in ['message-id', 'from']:
252 if key in display_data:
253 display_data[key] = xml.sax.saxutils.escape(display_data[key])
254 return (running_data, display_data)
256 def _slug(self, string):
257 return self._slug_regexp.sub('-', string)
259 parser = argparse.ArgumentParser()
260 parser.add_argument('--text', help='output plain text format',
262 group = parser.add_mutually_exclusive_group()
263 group.add_argument('--config', help='path to configuration file',
265 group.add_argument('--query', help='path to configuration file',
267 parser.add_argument('--list-views', help='list views',
270 args = parser.parse_args()
273 config = read_config(path=args.config)
275 config = json.loads(DEFAULT_CONFIG.format(query=args.query))
277 print ('''To use notmuch-to-html, you need to provide a notmuch query. Try:
279 notmuch-to-html --query=tag:inbox
281 Or 'notmuch-to-html --help' for additional options.''')
284 _PAGES['text'] = Page()
285 _PAGES['html'] = HtmlPage(
286 header='''<!DOCTYPE html>
289 <meta http-equiv="Content-Type" content="text/html; charset={encoding}" />
290 <title>{title}</title>
291 <style media="screen" type="text/css">
295 tr.message-first td {{
296 padding-top: {inter_message_padding};
298 tr.message-last td {{
299 padding-bottom: {inter_message_padding};
302 padding-left: {border_radius};
303 padding-right: {border_radius};
305 tr:first-child td:first-child {{
306 border-top-left-radius: {border_radius};
308 tr:first-child td:last-child {{
309 border-top-right-radius: {border_radius};
311 tr:last-child td:first-child {{
312 border-bottom-left-radius: {border_radius};
314 tr:last-child td:last-child {{
315 border-bottom-right-radius: {border_radius};
317 tbody:nth-child(4n+1) tr td {{
318 background-color: #ffd96e;
320 tbody:nth-child(4n+3) tr td {{
321 background-color: #bce;
330 '''.format(title=config['meta']['title'],
331 blurb=config['meta']['blurb'],
333 inter_message_padding='0.25em',
334 border_radius='0.5em'),
336 <p>Generated: {date} courtesy of <a href="http://notmuchmail.org">notmuch</a> and <a href="http://git.cworth.org/git/notmuch-to-html">notmuch-to-html</a>.
339 '''.format(date=datetime.datetime.utcnow().date())
343 for view in config['views']:
347 # only import notmuch if needed
351 page = _PAGES['text']
353 page = _PAGES['html']
355 db = notmuch.Database(mode=notmuch.Database.MODE.READ_ONLY)
356 page.write(database=db, views=config['views'])
359 print ('''To customize the output use 'notmuch-to-html --config=CONFIG_FILE' after
360 placing the following content into CONFIG_FILE (note that you can add
361 additional views with their own queries):
364 ''', file=sys.stderr)
365 print (json.dumps(config, indent=4, separators=(',',':')), file=sys.stderr)