mkdocs-print-site-plugin icon indicating copy to clipboard operation
mkdocs-print-site-plugin copied to clipboard

Using with custom theme

Open ghost opened this issue 3 years ago • 3 comments

Can I use this plugin with a custom theme? I.e.

theme:
  name: null
  custom_dir: path/to/basic/plain/theme/main.html

(See below for main.html).

When I do, I get:

Could not find a template context.
Report an issue at https://github.com/timvink/mkdocs-print-site-plugin
And mention the template you're using: None

I saw in get_theme_name() the comment "Supports the case when using overrides (using theme: null)". So I tried:

theme: null

but that renders the one-pager in the default MkDocs theme.

Any ideas? Thanks. (Happy to attempt to delve deeper into the code. But would like to know if it even makes sense for this plugin.)


My main.html:

<!DOCTYPE html>
<html>
<body>
    {{ page.content }}
</body>
</html>

ghost avatar Aug 05 '22 05:08 ghost

Hi @fotrimzi

Thanks for reporting. Indeed specifying theme: null should not throw an error.

There are two bits to this. The first is that we inject a .css file specific to the theme being used. This CSS file does things like hide the navigation and header bars. When you use a custom theme (theme: null), you should set the option include_css to False and then include your own custom css using the extra css option in mkdocs.yml. So these two sections of code can be changed to give a better error/warning message:

https://github.com/timvink/mkdocs-print-site-plugin/blob/311873dfe9b1937b0a86f492d522fa27e9675484/mkdocs_print_site_plugin/plugin.py#L122-L130

https://github.com/timvink/mkdocs-print-site-plugin/blob/311873dfe9b1937b0a86f492d522fa27e9675484/mkdocs_print_site_plugin/utils.py#L4

The second issue is more complex, and that's the error message you have now:

https://github.com/timvink/mkdocs-print-site-plugin/blob/311873dfe9b1937b0a86f492d522fa27e9675484/mkdocs_print_site_plugin/plugin.py#L294-L298

We need the context object so we can apply it to our newly created print_site page also, and it is being saved during the on_template_context() mkdocs event:

https://github.com/timvink/mkdocs-print-site-plugin/blob/311873dfe9b1937b0a86f492d522fa27e9675484/mkdocs_print_site_plugin/plugin.py#L268-L282

So it looks like the theme: null doesn't have a 404.html template. I'm not sure if it has a context at all, that's something to figure out using a breakpoint in the code.

The fix would be to find the correct context for the theme: null case, or another way to ensure the css and javascript links are correct on the print page.


Is this something you'd like to dive into and try to make a PR for? Would be appreciated!

timvink avatar Aug 16 '22 08:08 timvink

@timvink Thanks for the tips and insights. I will delve deeper and have a go.

ghost avatar Aug 21 '22 21:08 ghost

Hi @timvink Sorry for the hiatus. (A new day job is to blame.) I took a look, got something working, but very crudely.

As a reminder: I wanted to have the plugin work on a completely theme-less site. (nothing but the content, as in my original comment). I want to convert that one print_page.html to ORG mode via pandoc.

There are two bits to this. The first is that we inject a .css file specific to the theme being used.

A css/print-site-None.css gets me past these tests in plugin.py:

https://github.com/timvink/mkdocs-print-site-plugin/blob/0b44625cf62baf3b80f11eb87821e4407cd3afe6/mkdocs_print_site_plugin/plugin.py#L126

https://github.com/timvink/mkdocs-print-site-plugin/blob/0b44625cf62baf3b80f11eb87821e4407cd3afe6/mkdocs_print_site_plugin/plugin.py#L329

Later I'll try changing get_theme_name() to return none instead of name so I can instead use lowercase print-site-none.css:

https://github.com/timvink/mkdocs-print-site-plugin/blob/0b44625cf62baf3b80f11eb87821e4407cd3afe6/mkdocs_print_site_plugin/utils.py#LL19C1-L26C20

We need the context object ...

Skip (comment out) the context size test:

https://github.com/timvink/mkdocs-print-site-plugin/blob/0b44625cf62baf3b80f11eb87821e4407cd3afe6/mkdocs_print_site_plugin/plugin.py#L300

Later, self.context["page"] is set and that seems to be enough (for me).

I need to now learn what a context is and why the qualifying test is 'not empty'.

Please comment with your hints, suggestions, and any 'mkdocs plugin debug' tips and resources I might not have already found (i.e. virtually none).

ghost avatar Jun 13 '23 11:06 ghost