diff options
| author | Jules Laplace <julescarbon@gmail.com> | 2018-12-04 21:12:59 +0100 |
|---|---|---|
| committer | Jules Laplace <julescarbon@gmail.com> | 2018-12-04 21:12:59 +0100 |
| commit | d69086a1b2d7d6e6def55f35e30d0623701de011 (patch) | |
| tree | 1f73899aa4bcb9ecf0600f0d95f5909c79818780 /builder/parser.py | |
| parent | 966e27c7418d6e188ea4b1f651a5e6c67495b765 (diff) | |
embedding images
Diffstat (limited to 'builder/parser.py')
| -rw-r--r-- | builder/parser.py | 111 |
1 files changed, 111 insertions, 0 deletions
diff --git a/builder/parser.py b/builder/parser.py new file mode 100644 index 00000000..ea273556 --- /dev/null +++ b/builder/parser.py @@ -0,0 +1,111 @@ +import os +import glob +import mistune +from paths import * + +renderer = mistune.Renderer(escape=False) +markdown = mistune.Markdown(renderer=renderer) + +def fix_images(lines, s3_path): + real_lines = [] + block = "\n\n".join(lines) + for line in block.split("\n"): + if " + 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): + if len(lines): + lines = fix_images(lines, s3_path) + return "<section>" + markdown(lines) + "</section>" + return "" + +def parse_markdown(sections, s3_path): + groups = [] + current_group = [] + 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)) + current_group = [] + else: + current_group.append(section) + groups.append(normal_section(current_group, s3_path)) + content = "".join(groups) + return content + +def read_metadata(fn): + with open(fn, "r") as file: + data = file.read() + data = data.replace("\n ", "\n") + if "\n" in data: + data = data.replace("\r", "") + else: + data = data.replace("\r", "\n") + sections = data.split("\n\n") + return parse_metadata(fn, sections) + +default_metadata = { + 'status': 'published', + 'title': 'Untitled Page', + 'desc': '', + 'slug': '', + 'published': '2018-12-31', + 'updated': '2018-12-31', + 'authors': 'Adam Harvey', +} + +def parse_metadata_section(metadata, section): + for line in section.split("\n"): + if ': ' not in line: + continue + key, value = line.split(': ', 1) + metadata[key.lower()] = value + +def parse_metadata(fn, sections): + found_meta = False + metadata = {} + valid_sections = [] + for section in sections: + if not found_meta and ': ' in section: + found_meta = True + parse_metadata_section(metadata, section) + continue + if '-----' in section: + continue + if found_meta: + valid_sections.append(section) + + if 'title' not in metadata: + print('warning: {} has no title'.format(fn)) + for key in default_metadata: + if key not in metadata: + metadata[key] = default_metadata[key] + basename = os.path.basename(fn) + metadata['path'] = os.path.dirname(fn.replace(content_path, '')) + '/' + if basename == 'index.md': + metadata['url'] = metadata['path'] + else: + metadata['url'] = metadata['path'] + basename.replace('.md', '') + '/' + + if metadata['status'] == 'published|draft|private': + metadata['status'] = 'published' + metadata['authors'] = '<br>'.join(metadata['authors'].split(',')) + return metadata, valid_sections + +def read_research_post_index(): + posts = [] + for fn in sorted(glob.glob(os.path.join(content_path, 'research/**/index.md'), recursive=True)): + metadata, valid_sections = read_metadata(fn) + if metadata is None or metadata['status'] == 'private' or metadata['status'] == 'draft': + continue + posts.append(metadata) + return posts + |
