summaryrefslogtreecommitdiff
path: root/megapixels/app/site/parser.py
blob: 6b71e041075a6a50567d47dee60d2c71b25ad59f (plain)
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
import os
from os.path import join
import re
import glob
import simplejson as json
import mistune
from jinja2 import Environment, FileSystemLoader, select_autoescape

import app.settings.app_cfg as cfg
import app.site.s3 as s3

renderer = mistune.Renderer(escape=False)
markdown = mistune.Markdown(renderer=renderer)

includes_env = Environment(
  loader=FileSystemLoader(cfg.DIR_SITE_INCLUDES),
  autoescape=select_autoescape([])
)

footnote_count = 0

def parse_markdown(metadata, sections, s3_path, skip_h1=False):
  """
  parse page into sections, preprocess the markdown to handle our modifications
  """
  groups = []
  current_group = []
  footnotes = []
  in_stats = False
  in_columns = False
  in_footnotes = False
  ignoring = False

  if 'desc' in metadata and 'subdesc' in metadata:
    groups.append(intro_section(metadata, s3_path))

  for section in sections:
    if skip_h1 and section.startswith('# '):
      continue
    elif section.strip().startswith('---'):
      continue
    elif section.lower().strip().startswith('ignore text'):
      ignoring = True
      continue
    elif section.strip().startswith('### Footnotes'):
      groups.append(format_section(current_group, s3_path))
      current_group = []
      footnotes = []
      in_footnotes = True
    elif in_footnotes:
      footnotes.append(section)
    elif ignoring:
      continue

    elif '### statistics' in section.lower() or '### sidebar' in section.lower():
      if len(current_group):
        groups.append(format_section(current_group, s3_path))
      current_group = [format_include("{% include 'sidebar.html' %}", metadata)]
      if 'sidebar' not in section.lower():
        current_group.append(section)
      in_stats = True
    elif in_stats and not section.strip().startswith('## ') and 'end sidebar' not in section.lower():
      current_group.append(section)
    elif in_stats and section.strip().startswith('## ') or 'end sidebar' in section.lower():
      current_group = [format_section(current_group, s3_path, 'left-sidebar', tag='div')]
      if 'end sidebar' not in section.lower():
        current_group.append(section)
      in_stats = False

    elif '=== columns' in section.lower():
      if len(current_group):
        groups.append(format_section(current_group, s3_path))
      current_group = []
      in_columns = True
      column_partz = section.split(' ')
      if len(column_partz) == 3:
        column_count = column_partz[2]
      else:
        column_count = "N"
      groups.append("<section><div class='columns columns-{}'>".format(column_count))
    elif in_columns is True and '===' in section:
      groups.append(format_section(current_group, s3_path, type='column', tag='div'))
      current_group = []
      if 'end columns' in section:
        groups.append("</div></section>")
        in_columns = False
        current_group = []

    elif section.strip().startswith('{% include'):
      groups.append(format_section(current_group, s3_path))
      current_group = []
      current_group.append(section)
      if section.strip().endswith(' %}'):
        groups.append(format_include("\n\n".join(current_group), metadata))
        current_group = []
    elif section.strip().startswith('```'):
      groups.append(format_section(current_group, s3_path))
      current_group = []
      current_group.append(section)
      if section.strip().endswith('```'):
        groups.append(format_applet("\n\n".join(current_group), s3_path))
        current_group = []
    elif section.strip().endswith('```'):
      current_group.append(section)
      groups.append(format_applet("\n\n".join(current_group), s3_path))
      current_group = []
    elif section.startswith('+ '):
      groups.append(format_section(current_group, s3_path))
      groups.append('<section>' + format_metadata(section) + '<section>')
      current_group = []
    elif '![fullwidth:' in section:
      groups.append(format_section(current_group, s3_path))
      groups.append(format_section([section], s3_path, type='fullwidth'))
      current_group = []
    elif '![wide:' in section:
      groups.append(format_section(current_group, s3_path))
      groups.append(format_section([section], s3_path, type='wide'))
      current_group = []
    elif '![' in section:
      groups.append(format_section(current_group, s3_path))
      groups.append(format_section([section], s3_path, type='images'))
      current_group = []
    else:
      current_group.append(section)
  groups.append(format_section(current_group, s3_path))

  footnote_txt = ''
  footnote_lookup = {}

  if len(footnotes):
    footnote_txt, footnote_lookup = format_footnotes(footnotes, s3_path)

  content = "".join(groups)

  if footnote_lookup:
    for key, index in footnote_lookup.items():
      global footnote_count
      footnote_count = 0
      letters = "abcdefghijklmnopqrstuvwxyz"
      footnote_backlinks = []
      def footnote_tag(match):
        global footnote_count
        footnote_count += 1
        footnote_backlinks.append('<a href="#{}_{}">{}</a>'.format(key, footnote_count, letters[footnote_count-1]))
        return '<a class="footnote_shim" name="{}_{}"> </a><a href="#{}" class="footnote" title="Footnote {}">{}</a>'.format(key, footnote_count, key, index, index)
      key_regex = re.compile(key.replace('[', '\\[').replace('^', '\\^').replace(']', '\\]'))
      content = key_regex.sub(footnote_tag, content)
      footnote_txt = footnote_txt.replace("{}_BACKLINKS".format(key), "".join(footnote_backlinks))
    content += '<section>'
    content += '<h3>References</h3>'
    content += footnote_txt
    content += '</section>'
  return content


def intro_section(metadata, s3_path):
  """
  Build the intro section for datasets
  """

  section = "<section class='intro_section' style='background-image: url({})'>".format(s3_path + metadata['image'])
  section += "<div class='inner'>"

  parts = []
  if 'desc' in metadata:
    desc = metadata['desc']
    # colorize the first instance of the database name in the header
    if 'color' in metadata and metadata['title'] in desc:
      desc = desc.replace(metadata['title'], "<span style='color: {}'>{}</span>".format(metadata['color'], metadata['title']), 1)
    section += "<div class='hero_desc'><span class='bgpad'>{}</span></div>".format(desc, desc)

  if 'subdesc' in metadata:
    subdesc = markdown(metadata['subdesc']).replace('<p>', '').replace('</p>', '')
    section += "<div class='hero_subdesc'><span class='bgpad'>{}</span></div>".format(subdesc, subdesc)

  section += "</div>"
  section += "</section>"

  if 'caption' in metadata:
    section += "<section><div class='image'><div class='intro-caption caption'>{}</div></div></section>".format(metadata['caption'])

  return section


def fix_images(lines, s3_path):
  """
  do our own transformation of the markdown around images to handle wide images etc
  lines: markdown lines
  """
  real_lines = []
  block = "\n\n".join(lines)
  for line in block.split("\n"):
    if "![" in line:
      line = line.replace('![', '')
      alt_text, tail = line.split('](', 1)
      url, tail = tail.split(')', 1)
      tag = ''
      if ':' in alt_text:
        tag, alt_text = alt_text.split(':', 1)
      img_tag = "<img src='{}' alt='{}'>".format(s3_path + url, alt_text.replace("'", ""))
      if 'sideimage' in tag:
        line = "<div class='sideimage'>{}<div>{}</div></div>".format(img_tag, markdown(tail))
      elif len(alt_text):
        line = "<div class='image'>{}<div class='caption'>{}</div></div>".format(img_tag, alt_text)
      else:
        line = "<div class='image'>{}</div>".format(img_tag, alt_text)
    real_lines.append(line)
  return "\n".join(real_lines)


def format_section(lines, s3_path, type='', tag='section'):
  """
  format a normal markdown section
  """
  if len(lines):
    lines = fix_meta(lines)
    lines = fix_images(lines, s3_path)
    if type:
      return "<{} class='{}'>{}</{}>".format(tag, type, markdown(lines), tag)
    else:
      return "<{}>{}</{}>".format(tag, markdown(lines), tag)
  return ""

def fix_meta(lines):
  """
  Format metadata sections before passing to markdown
  """
  new_lines = []
  for line in lines:
    if line.startswith('+ '):
      line = format_metadata(line)
    new_lines.append(line)
  return new_lines

def format_metadata(section):
  """
  format a metadata section (+ key: value pairs)
  """
  meta = []
  for line in section.split('\n'):
    if ': ' not in line:
      continue
    key, value = line[2:].split(': ', 1)
    meta.append("<div><div class='gray'>{}</div><div>{}</div></div>".format(key, value))
  return "<div class='meta'>{}</div>".format(''.join(meta)) 

def format_footnotes(footnotes, s3_path):
  """
  Format the footnotes section separately and produce a lookup we can use to update the main site
  """
  footnotes = '\n'.join(footnotes).split('\n')
  index = 1
  footnote_index_lookup = {}
  footnote_list = []
  for footnote in footnotes:
    if not len(footnote) or '[^' not in footnote:
      continue
    key, note = footnote.split(': ', 1)
    footnote_index_lookup[key] = index
    footnote_list.append('<a name="{}" class="footnote_shim"></a><span class="backlinks">{}_BACKLINKS</span>'.format(key, key) + markdown(note))
    index += 1

  footnote_txt = '<section><ul class="footnotes"><li>' + '</li><li>'.join(footnote_list) + '</li></ul></section>'
  return footnote_txt, footnote_index_lookup

def format_include(section, metadata):
  """
  Include html template
  """
  include_fn = section.strip().strip('\n').strip().strip('{%').strip().strip('%}').strip()
  include_fn = include_fn.strip('include').strip().strip('"').strip().strip("'").strip()
  return includes_env.get_template(include_fn).render(metadata=metadata)

def format_applet(section, s3_path):
  """
  Format the applets, which load javascript modules like the map and CSVs
  """
  # print(section)
  payload = section.strip('```').strip().strip('```').strip().split('\n')
  applet = {}
  # print(payload)
  if ': ' in payload[0]:
    command, opt = payload[0].split(': ', 1)
  else:
    command = payload[0]
    opt = None
  print(command)
  if command == 'python' or command == 'javascript' or command == 'code':
    return format_section([ section ], s3_path)
  if command == '':
    return ''

  applet['command'] = command
  if opt:
    applet['opt'] = opt
  if command == 'load_file':
    if opt[0:4] != 'http':
      applet['opt'] = s3_path + opt
  if len(payload) > 1:
    applet['fields'] = payload[1:]
  return "<section class='applet_container'><div class='applet' data-payload='{}'></div></section>".format(json.dumps(applet))


def parse_research_index(research_posts):
  """
  Generate an index file for the research pages
  """
  content = "<div class='research_index'>"
  for post in research_posts:
    print(post)
    if 'path' not in post:
      print("No path attribute for post")
      return ""
    s3_path = s3.make_s3_path(cfg.S3_SITE_PATH, post['path'])
    if 'image' in post:
      post_image = s3_path + post['image']
    else:
      post_image = 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw=='
    row = "<a href='{}'><section class='wide'><img src='{}' alt='Research post' /><section><h1>{}</h1><h2>{}</h2></section></section></a>".format(
      post['path'],
      post_image,
      post['title'],
      post['tagline'])
    content += row
  content += '</div>'
  return content