From 188de0d8e7ee737f450339b531fcc093aa2b9aed Mon Sep 17 00:00:00 2001 From: awmatheson Date: Wed, 1 Jul 2020 11:05:49 -0700 Subject: [PATCH] start jinja --- mystify/convert.py | 94 +++++++++++++++++++++++++++++----------------- 1 file changed, 59 insertions(+), 35 deletions(-) diff --git a/mystify/convert.py b/mystify/convert.py index 54f647f..a9f379a 100644 --- a/mystify/convert.py +++ b/mystify/convert.py @@ -1,44 +1,68 @@ import json +import jinja +from myst_parser.main import default_parser + +SOURCE = """ +{source} +""" + +OUTPUT = """ +{output} +{{}} +""" + +MARKDOWN = """ +--- +{{}} +--- +""" + +CODE = """ +% cell +{{}} +{{}} +% endcell +""" + +METADATA = """{metadata} +{{}} +""" + +CELL_METADATA = """{cell_meta} +--- +{{}} +--- +""" def to_myst(model): - pass + sections = model['cells'] + model_metadata = model['metadata'] def to_model(myst): pass -def code_convert(cell): - # convert to myst markdown here - """ - % cell - --- - cell_type: code - execution_count: 1 - --- - ```python - print("fbddddoo") - ``` - - foo - % endcell - """ +def _parse_cell(cell): + cell_meta = yaml.load(cell[0].content) + for cell_section in cell[1:]: + if cell_section.info == "{source}": + cell_meta["source"] = re.findall("\n", cell_section.content) + + from IPython import embed; embed() + return cell_meta - pass -def markdown_convert(cell): - # convert to myst markdown - """ - % cell - --- - cell_type: markdown - --- - testtest - -------- - - *test* of markdown2 - % endcell - """ - pass - -def format_metadata(): - # format metadata - pass +def _split_sections(tokens): + sections = {"cells": []} + append_to_cell = None + for token in tokens: + if token.type == 'fence' and token.info == '{metadata}': + # Metadata yaml block + sections['metadata'] = yaml.load(token.content) + if append_to_cell is not None: + append_to_cell.append(token) + if token.type == 'myst_line_comment' and token.content == 'cell': + append_to_cell = [] + if token.type == 'myst_line_comment' and token.content == 'endcell': + sections['cells'].append(_parse_cell(append_to_cell)) + append_to_cell = None + return sections \ No newline at end of file