import os
import re
import glob
import simplejson as json
import mistune
import app.settings.app_cfg as cfg
import app.site.s3 as s3
renderer = mistune.Renderer(escape=False)
markdown = mistune.Markdown(renderer=renderer)
def parse_markdown(metadata, sections, s3_path, skip_h1=False):
"""
parse page into sections, preprocess the markdown to handle our modifications
"""
groups = []
current_group = []
for section in sections:
if skip_h1 and section.startswith('# '):
continue
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(format_metadata(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))
content = "".join(groups)
return content
def fix_images(lines, s3_path):
"""
do our own tranformation 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 "
url, tail = tail.split(')', 1)
if ':' in alt_text:
tail, alt_text = alt_text.split(':', 1)
img_tag = "".format(s3_path + url, alt_text.replace("'", ""))
if len(alt_text):
line = "