diff --git a/jupyter_book/book_template/content/features/markdown.md b/jupyter_book/book_template/content/features/markdown.md index 3f758bee4..1a793aa35 100644 --- a/jupyter_book/book_template/content/features/markdown.md +++ b/jupyter_book/book_template/content/features/markdown.md @@ -1,3 +1,18 @@ +--- +jupyter: + jupytext: + formats: md + text_representation: + extension: .md + format_name: markdown + format_version: '1.1' + jupytext_version: 1.2.1 + kernelspec: + display_name: Python 3 + language: python + name: python3 +--- + # Creating book content The two kinds of files that contain course content are: @@ -10,6 +25,7 @@ Each are contained in the `content/` folder and referenced from `_data/toc.yml`. If the file is markdown, it will be copied over with front-matter YAML added so that Jekyll can parse it. + ```python print("Python (and any language-specific) code still works as expected") ``` @@ -17,6 +33,7 @@ print("Python (and any language-specific) code still works as expected") ``` As does non-language code. ``` + ## Page navigation Table of Contents @@ -45,6 +62,7 @@ P(A_1 \cup A_2 \cup A_3) ~ = ~ P(B \cup A_3) &= ~ P(B) + P(A_3) - P(BA_3) \\ And here is the code that was used to generate it: + ```python \begin{align*} P(A_1 \cup A_2 \cup A_3) ~ = ~ P(B \cup A_3) &= ~ P(B) + P(A_3) - P(BA_3) \\ @@ -52,6 +70,7 @@ P(A_1 \cup A_2 \cup A_3) ~ = ~ P(B \cup A_3) &= ~ P(B) + P(A_3) - P(BA_3) \\ &= ~ \sum_{i=1}^3 P(A_i) - \mathop{\sum \sum}_{1 \le i < j \le 3} P(A_iA_j) + P(A_1A_2A_3) \end{align*} ``` + **Note**: If you print your page (using the print button), then mathematics may not show diff --git a/jupyter_book/page.py b/jupyter_book/page.py index b79641f77..14606b431 100644 --- a/jupyter_book/page.py +++ b/jupyter_book/page.py @@ -4,8 +4,10 @@ from nbconvert.exporters import HTMLExporter from nbconvert.writers import FilesWriter import jupytext as jpt +import nbformat as nbf +from ruamel.yaml import YAML -from .utils import _clean_markdown_cells +from .utils import _clean_markdown_cells, _split_yaml from .run import run_ntbk @@ -35,8 +37,28 @@ def build_page(path_ntbk, path_html_output, path_media_output=None, execute=Fals ######################################## # Load in the notebook - notebook_name = op.splitext(op.basename(path_ntbk))[0] - ntbk = jpt.read(path_ntbk) + notebook_name, suff = op.splitext(op.basename(path_ntbk)) + + is_raw_markdown_file = False + if suff in ['.md', '.markdown']: + # If it's a markdown file, we need to check whether it's a jupytext format + with open(path_ntbk, 'r') as ff: + lines = ff.readlines() + yaml_lines, content = _split_yaml(lines) + yaml = YAML().load(''.join(yaml_lines)) + + if (yaml is not None) and yaml.get('jupyter', {}).get('jupytext'): + # If we have jupytext metadata, then use it to read the markdown file + ntbk = jpt.reads(''.join(lines), 'md') + else: + # Otherwise, create an empty notebook and add all of the file contents as a markdown file + is_raw_markdown_file = True + ntbk = nbf.v4.new_notebook() + ntbk['cells'].append(nbf.v4.new_markdown_cell(source=''.join(content))) + else: + # If it's not markdown, we assume it's either ipynb or a jupytext format + ntbk = jpt.read(path_ntbk) + if _is_jupytext_file(ntbk): execute = True @@ -91,6 +113,18 @@ def build_page(path_ntbk, path_html_output, path_media_output=None, execute=Fals # Now write the markdown and resources writer = FilesWriter(config=c) writer.write(html, resources, notebook_name=notebook_name) + + # Add the frontmatter to the yaml file in case it's wanted + if is_raw_markdown_file and len(yaml_lines) > 0: + with open(op.join(path_html_output, notebook_name + '.html'), 'r') as ff: + md_lines = ff.readlines() + md_lines.insert(0, '---\n') + for iline in yaml_lines[::-1]: + md_lines.insert(0, iline + '\n') + md_lines.insert(0, '---\n') + with open(op.join(path_html_output, notebook_name + '.html'), 'w') as ff: + ff.writelines(md_lines) + if verbose: print("Finished writing notebook to {}".format(path_html_output))