Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jinja UndefinedError with header tags #142

Closed
NiklasRosenstein opened this issue Feb 6, 2017 · 11 comments
Closed

Jinja UndefinedError with header tags #142

NiklasRosenstein opened this issue Feb 6, 2017 · 11 comments
Assignees
Milestone

Comments

@NiklasRosenstein
Copy link

@NiklasRosenstein NiklasRosenstein commented Feb 6, 2017

I get this error when I place a < or > in a Markdown header, like

# <some.hpp>

Or even when I juse HTML tags like

<h2 id="nr.pvrq2.RenderJob">RenderJob</h2>

Error traceback:

  File "c:\users\niklas\repos\pydoc-markdown\.env\lib\site-packages\mkdocs\__main__.py", line 127, in serve_command
    livereload=livereload
  File "c:\users\niklas\repos\pydoc-markdown\.env\lib\site-packages\mkdocs\commands\serve.py", line 82, in serve
    config = builder()
  File "c:\users\niklas\repos\pydoc-markdown\.env\lib\site-packages\mkdocs\commands\serve.py", line 78, in builder
    build(config, live_server=live_server, dirty=dirty)
  File "c:\users\niklas\repos\pydoc-markdown\.env\lib\site-packages\mkdocs\commands\build.py", line 379, in build
    build_pages(config, dirty=dirty)
  File "c:\users\niklas\repos\pydoc-markdown\.env\lib\site-packages\mkdocs\commands\build.py", line 332, in build_pages
    dump_json)
  File "c:\users\niklas\repos\pydoc-markdown\.env\lib\site-packages\mkdocs\commands\build.py", line 212, in _build_page
    output_content = template.render(context)
  File "c:\users\niklas\repos\pydoc-markdown\.env\lib\site-packages\jinja2\environment.py", line 989, in render
    return self.environment.handle_exception(exc_info, True)
  File "c:\users\niklas\repos\pydoc-markdown\.env\lib\site-packages\jinja2\environment.py", line 754, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "c:\users\niklas\repos\pydoc-markdown\.env\lib\site-packages\jinja2\_compat.py", line 37, in reraise
    raise value.with_traceback(tb)
  File "c:\users\niklas\repos\pydoc-markdown\.env\lib\site-packages\material\main.html", line 1, in top-level template code
    {% extends "base.html" %}
  File "c:\users\niklas\repos\pydoc-markdown\.env\lib\site-packages\material\base.html", line 87, in top-level template code
    {% block site_nav %}
  File "c:\users\niklas\repos\pydoc-markdown\.env\lib\site-packages\material\base.html", line 92, in block "site_nav"
    {% include "partials/nav.html" %}
  File "c:\users\niklas\repos\pydoc-markdown\.env\lib\site-packages\material\partials\nav.html", line 18, in top-level template code
    {% include "partials/nav-item.html" %}
  File "c:\users\niklas\repos\pydoc-markdown\.env\lib\site-packages\material\partials\nav-item.html", line 19, in top-level template code
    {% include "partials/nav-item.html"  %}
  File "c:\users\niklas\repos\pydoc-markdown\.env\lib\site-packages\material\partials\nav-item.html", line 29, in top-level template code
    {% set toc_ = (toc_ | first).children %}
  File "c:\users\niklas\repos\pydoc-markdown\.env\lib\site-packages\jinja2\environment.py", line 408, in getattr
    return getattr(obj, attribute)
jinja2.exceptions.UndefinedError: No first item, sequence was empty.
@squidfunk
Copy link
Owner

@squidfunk squidfunk commented Feb 6, 2017

Please provide the complete source of the page which is causing the issue (you can replace with lorem ipsum, though) and some information on versions. See the issue template for this reason.

@NiklasRosenstein
Copy link
Author

@NiklasRosenstein NiklasRosenstein commented Feb 6, 2017

Version

mkdocs-material==1.0.2

index.md

<h1 id="nr.pvrq2">nr.pvrq2</h1>

FooBar Baz

mkdocs.yml

site_name: My Docs
theme: material

Error

[...]
  File "c:\python35-32\lib\site-packages\mkdocs\commands\build.py", line 212, in _build_page
    output_content = template.render(context)
  File "c:\python35-32\lib\site-packages\jinja2\environment.py", line 989, in render
    return self.environment.handle_exception(exc_info, True)
  File "c:\python35-32\lib\site-packages\jinja2\environment.py", line 754, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "c:\python35-32\lib\site-packages\jinja2\_compat.py", line 37, in reraise
    raise value.with_traceback(tb)
  File "c:\python35-32\lib\site-packages\material\main.html", line 1, in top-level template code
    {% extends "base.html" %}
  File "c:\python35-32\lib\site-packages\material\base.html", line 87, in top-level template code
    {% block site_nav %}
  File "c:\python35-32\lib\site-packages\material\base.html", line 92, in block "site_nav"
    {% include "partials/nav.html" %}
  File "c:\python35-32\lib\site-packages\material\partials\nav.html", line 18, in top-level template code
    {% include "partials/nav-item.html" %}
  File "c:\python35-32\lib\site-packages\material\partials\nav-item.html", line 29, in top-level template code
    {% set toc_ = (toc_ | first).children %}
  File "c:\python35-32\lib\site-packages\jinja2\environment.py", line 408, in getattr
    return getattr(obj, attribute)
jinja2.exceptions.UndefinedError: No first item, sequence was empty.
@NiklasRosenstein
Copy link
Author

@NiklasRosenstein NiklasRosenstein commented Feb 6, 2017

I also just tried it with 1.0.3

@NiklasRosenstein
Copy link
Author

@NiklasRosenstein NiklasRosenstein commented Feb 6, 2017

Actually I can not reproduce the # <some.hpp> example anymore, so ignore that for now. But the HTML header tags issue is reproducible.

@squidfunk
Copy link
Owner

@squidfunk squidfunk commented Feb 6, 2017

Well, it has to do with the table of contents extension. The TOC cannot be generated when you use HTML tags, only when using the Markdown. The error shouldn't be there and there should be no TOC. Will fix it as soon as I find the time.

EDIT: for now you can just use the standard Markdown Syntax and everything should be fine.

# nr.pvrq2
@squidfunk squidfunk self-assigned this Feb 6, 2017
@squidfunk squidfunk added the bug label Feb 6, 2017
@squidfunk squidfunk added this to the 1.1.0 milestone Feb 6, 2017
@NiklasRosenstein
Copy link
Author

@NiklasRosenstein NiklasRosenstein commented Feb 6, 2017

The TOC cannot be generated when you use HTML tags, only when using the Markdown. The error shouldn't be there and there should be no TOC. Will fix it as soon as I find the time.

By the way, do you know why that is? Is it a limitation or design decision of MkDocs? I need to generate HTML tags to assign them correct IDs, and eventually additional styling (eg. parts of the header in a smaller font and/or in monospace).

@squidfunk
Copy link
Owner

@squidfunk squidfunk commented Feb 6, 2017

This is not a limitation of MkDocs, but of the Python Markdown Extensions MkDocs uses. Monospaced and smaller text is possible and supported by Material:

# Header with `code` and <small>small text</small>

You could add your anchor as HTML in the header, though:

# <a id="nr.pvrq2"></a> nr.pvrq2

Untested, may break other stuff.

@squidfunk
Copy link
Owner

@squidfunk squidfunk commented Feb 6, 2017

I now know why it breaks - I had to apply a hack to check whether the content contains a h1 headline to see if the page title should be set as a fallback (if it doesn't) and this matches your code exactly. You can fix it temporarily by adding another tag in front of the h1:

<h1 class="" id="nr.pvrq2">nr.pvrq2</h1>

I will provide a fix.

@squidfunk
Copy link
Owner

@squidfunk squidfunk commented Feb 8, 2017

Fix #144 is in master now. Please checkout the master and confirm. Instructions on how to use Material from master are here (2 easy steps):

  1. http://squidfunk.github.io/mkdocs-material/getting-started/#cloning-from-github
  2. http://squidfunk.github.io/mkdocs-material/getting-started/#usage
@squidfunk
Copy link
Owner

@squidfunk squidfunk commented Feb 16, 2017

Released in 1.0.4

@NiklasRosenstein
Copy link
Author

@NiklasRosenstein NiklasRosenstein commented Mar 4, 2017

Thanks, I can confirm it's working now! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.