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

Using with custom theme #73

Open
ghost opened this issue Aug 5, 2022 · 3 comments
Open

Using with custom theme #73

ghost opened this issue Aug 5, 2022 · 3 comments

Comments

@ghost
Copy link

ghost commented Aug 5, 2022

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 ghost changed the title Using with theme: null Using with custom theme Aug 5, 2022
@timvink
Copy link
Owner

timvink commented Aug 16, 2022

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:

# Add pointer to theme specific css files
if self.config.get("include_css"):
file = "print-site-%s.css" % get_theme_name(config)
if file in os.listdir(os.path.join(HERE, "css")):
config["extra_css"] = ["css/%s" % file] + config["extra_css"]
else:
msg = f"[mkdocs-print-site] Theme '{get_theme_name(config)}' not yet supported\n"
msg += "which means print margins and page breaks might be off. Feel free to open an issue!"
logger.warning(msg)

def get_theme_name(config) -> str:

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

if len(self.context) == 0:
msg = "Could not find a template context.\n"
msg += "Report an issue at https://github.com/timvink/mkdocs-print-site-plugin\n"
msg += f"And mention the template you're using: {get_theme_name(config)}"
raise PluginError(msg)

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:

# Note a theme can have multiple templates
# Found a bug where in the mkdocs theme,
# the "sitemap.xml" static template
# has incorrect 'extra_css' and 'extra_js' paths
# leading to breaking the print page
# at random (when sitemap.xml was rendered last)
# we're assuming here all templates have a 404.html template
# print(f"\nName: {template_name}\nContext: {context.get('extra_css')}")
if template_name == "404.html":
self.context = context
# Make sure paths are OK
if config.get('extra_css'):
self.context['extra_css'] = [get_relative_url(f, self.print_page.file.url) for f in config.get('extra_css')]
if config.get('extra_javascript'):
self.context['extra_javascript'] = [get_relative_url(f, self.print_page.file.url) for f in config.get('extra_javascript')]

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!

@ghost
Copy link
Author

ghost commented Aug 21, 2022

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

@ghost
Copy link
Author

ghost commented Jun 13, 2023

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:

if file in os.listdir(os.path.join(HERE, "css")):

if css_file in os.listdir(os.path.join(HERE, "css")):

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:

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).

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

No branches or pull requests

1 participant