diff options
| author | Jules Laplace <julescarbon@gmail.com> | 2018-12-05 16:19:50 +0100 |
|---|---|---|
| committer | Jules Laplace <julescarbon@gmail.com> | 2018-12-05 16:19:50 +0100 |
| commit | 2a1b884e841efe562e0c84885a404819433b3405 (patch) | |
| tree | 34b2ef4c37099a84f341ae54c60e0a19af271ab4 /builder/parser.py | |
| parent | d69086a1b2d7d6e6def55f35e30d0623701de011 (diff) | |
styling images
Diffstat (limited to 'builder/parser.py')
| -rw-r--r-- | builder/parser.py | 57 |
1 files changed, 42 insertions, 15 deletions
diff --git a/builder/parser.py b/builder/parser.py index ea273556..529d21fa 100644 --- a/builder/parser.py +++ b/builder/parser.py @@ -1,4 +1,5 @@ import os +import re import glob import mistune from paths import * @@ -10,19 +11,28 @@ def fix_images(lines, s3_path): real_lines = [] block = "\n\n".join(lines) for line in block.split("\n"): - if " + if " + url, tail = tail.split(')', 1) + if ':' in alt_text: + tail, alt_text = alt_text.split(':', 1) + img_tag = "<img src='{}' alt='{}'>".format(s3_path + url, alt_text.replace("'", "")) + if 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 wide_section(line, s3_path): - lines = fix_images(lines, s3_path) - return "<section class='wide'>" + markdown(lines) + "</section>" - -def normal_section(lines, s3_path): +def format_section(lines, s3_path, type=''): if len(lines): lines = fix_images(lines, s3_path) - return "<section>" + markdown(lines) + "</section>" + if type: + return "<section class='{}'>{}</section>".format(type, markdown(lines)) + else: + return "<section>" + markdown(lines) + "</section>" return "" def parse_markdown(sections, s3_path): @@ -31,13 +41,17 @@ def parse_markdown(sections, s3_path): for section in sections: if section.startswith('# '): continue - if '![wide]' in section: - groups.append(normal_section(current_group, s3_path)) - groups.append(wide_section([section], s3_path)) + if '![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(normal_section(current_group, s3_path)) + groups.append(format_section(current_group, s3_path)) content = "".join(groups) return content @@ -88,16 +102,22 @@ def parse_metadata(fn, sections): for key in default_metadata: if key not in metadata: metadata[key] = default_metadata[key] + + basedir = os.path.dirname(fn.replace(content_path, '')) basename = os.path.basename(fn) - metadata['path'] = os.path.dirname(fn.replace(content_path, '')) + '/' - if basename == 'index.md': + if basedir == '/': + metadata['path'] = '/' + metadata['url'] = '/' + elif basename == 'index.md': + metadata['path'] = basedir + '/' metadata['url'] = metadata['path'] else: + metadata['path'] = basedir + '/' metadata['url'] = metadata['path'] + basename.replace('.md', '') + '/' if metadata['status'] == 'published|draft|private': metadata['status'] = 'published' - metadata['authors'] = '<br>'.join(metadata['authors'].split(',')) + metadata['author_html'] = '<br>'.join(metadata['authors'].split(',')) return metadata, valid_sections def read_research_post_index(): @@ -107,5 +127,12 @@ def read_research_post_index(): if metadata is None or metadata['status'] == 'private' or metadata['status'] == 'draft': continue posts.append(metadata) + if not len(posts): + posts.append({ + 'title': 'Placeholder', + 'slug': 'placeholder', + 'date': 'Placeholder', + 'url': '/', + }) return posts |
