microsoft / DevCookbook

An open source cookbook for everyone!
https://microsoft.github.io/DevCookbook/
MIT License
43 stars 26 forks source link

Directions subsections cannot be parsed #7

Open t-eckert opened 3 years ago

t-eckert commented 3 years ago

First seen with pull request #5. The directions are have subheadings which cannot be parsed by the compiler.

## Directions

1. Place oven rack in the lower-middle position and preheat oven to 400°. Line a baking sheet with foil.
### Start the Squash
2. Brush or otherwise coat the squash with 2 Tbsp olive oil. Place the squash cut side down onto the prepared baking sheet and bake until easily pierced with a knife or skewer, 40-45 minutes. Proceed with the remaining steps as the squash roasts.

### Make the Breadcrumbs
(This step is optional-skip if using panko or other premade crumbs)
3. Melt 2 Tbsp butter in a large 12" skillet over medium heat. 
4. Add the cubed bread and toss to coat in the melted butter. Continue to toast the bread until golden brown and crunchy, about 10 minutes. Sprinkle generously with salt and pepper and set aside on paper towel to drain.

When the compiler is run, the following exception is raised:

Traceback (most recent call last):
  File "/home/t_eck/code/forks/DevCookbook/compile.py", line 44, in <module>
    [render_recipe_page(recipe, env) for recipe in recipes],
  File "/home/t_eck/code/forks/DevCookbook/compile.py", line 44, in <listcomp>
    [render_recipe_page(recipe, env) for recipe in recipes],
  File "/home/t_eck/code/forks/DevCookbook/recipe_compiler/render.py", line 16, in render_recipe_page
    return env.get_template("recipe.html").render(recipe=recipe)
  File "/home/t_eck/code/forks/DevCookbook/.venv/lib/python3.9/site-packages/jinja2/environment.py", line 1090, in render
    self.environment.handle_exception()
  File "/home/t_eck/code/forks/DevCookbook/.venv/lib/python3.9/site-packages/jinja2/environment.py", line 832, in handle_exception
    reraise(*rewrite_traceback_stack(source=source))
  File "/home/t_eck/code/forks/DevCookbook/.venv/lib/python3.9/site-packages/jinja2/_compat.py", line 28, in reraise
    raise value.with_traceback(tb)
  File "/home/t_eck/code/forks/DevCookbook/recipe_compiler/templates/recipe.html", line 1, in top-level template code
    {% extends "index.html" %} {% block content %}
  File "/home/t_eck/code/forks/DevCookbook/recipe_compiler/templates/index.html", line 15, in top-level template code
    {% block content %} {% endblock content %}
  File "/home/t_eck/code/forks/DevCookbook/recipe_compiler/templates/recipe.html", line 20, in block "content"
    {% for ingredient in recipe.ingredients %}
TypeError: 'NoneType' object is not iterable

The parser should be changed to accept subheadings and display them correctly.

t-eckert commented 3 years ago

This issue is also seen in PR #8.

t-eckert commented 3 years ago

The way I want to fix this is to modify Recipe.instructions to be a str instead of a list[str] and let the Marko markdown parser handle more of the work.

Currently, we parse in the children of list within a section and create a list of strings from that. Instead, we should have a get_markdown_between_headers(start_header: str, end_header: str = None) -> str which will return all of the text between the given headers. If the end_header is None, just parse to the end of the file.

This will replace get_list_within_section for gathering the ingredients and the directions.