Skip to content

Support theme customization with blocks - #947

Merged
d0ugal merged 2 commits into
mkdocs:masterfrom
waylan:template
May 31, 2016
Merged

d0ugal merged 2 commits into
mkdocs:masterfrom
waylan:template

Conversation

@waylan

@waylan waylan commented May 25, 2016

Copy link
Copy Markdown
Contributor

Partially addresses #607.

Note that we have to rename base.html to main.html and then create a new base.html which is an empty file that "extends" main.html. We could reverse the roles of those two files, but that would be backward incompatible with existing custom/third party themes as all themes are loaded from base.html. Existing custom themes may not have a main.html and switching the role of the two files, despite being more sensible, could break existing custom and third party themes. Of course, if we want to make the backward incompatible change, now is the time to do it. Let me know and I'll update this PR.

Also note that this provides a way for users to override the behavior of next/prev links when include_next_prev is deprecated (see #633, #679, #681, & #946), use a different analytics service, override search, and various other common requests -- all without any new config settings or Plugin API.

@waylan
waylan force-pushed the template branch 4 times, most recently from 2e7907f to e44a615 Compare May 25, 2016 14:51
@waylan

waylan commented May 25, 2016

Copy link
Copy Markdown
Contributor Author

I was able to work out a solution for the base.html vs. main.html issue. Now the user overrides the main.html template which "extends" base.html which makes more sense. And if a custom/third party theme does not contain a main.html template, MkDocs falls back to using base.html, but a warning is raised suggesting that the theme should be updated prior to version 1.0 when the fallback will be removed, which provides a clean upgrade path for custom and third party templates. And if third party templates follow our lead and use the main.html and base.html templates like we are, their updated templates should continue to work with older versions of MkDocs (which will load base.html directly).

If this backward incompatible change (with a clean upgrade path) is acceptable, I'll clean up the commit history so that the files aren't juggled back and forth (and so blame will still work on the lengthy base.html template files). If this change is not acceptable, I'll just back out the last commit. Any input @d0ugal?

@d0ugal

d0ugal commented May 25, 2016

Copy link
Copy Markdown
Contributor

I've only had time to scan this very quickly - but it looks cool. I'll try and review properly today/tomorrow.

@waylan
waylan force-pushed the template branch 3 times, most recently from 1c44d11 to 8dd6adf Compare May 25, 2016 15:53
Comment thread docs/about/release-notes.md Outdated
To enable this feature, the primary entry point for page templates has been
changed from `base.html` to `main.html`. This allows `base.html` to continue to
exist while allowing users to override `main.html` and extend `base.html`. For
version 1.16, `base.html` will continue to work if no `main.html` template

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be version 0.16

@d0ugal

d0ugal commented May 26, 2016 •

Copy link
Copy Markdown
Contributor

I generally like this approach, I think it should work well and help a bunch of users. The only bit I don't like is the config setting theme_dir - I realise that you only used this as it already exists. However, I think it is ugly and doesn't really make it obvious how we expect it to be used (for customisations, not full theme replacements). I don't have a good suggestion at the moment, I am going to give it some thought. However, I am open to renaming this if we can come up with a better name (maybe we can even do a convention based approach like we do with docs dir and set a default).

I see two use cases for theme_dir at the moment.

  • Full theme replacements, for this, we should allow the "theme" setting to be a path and overload that. Then theme: my_custom_theme would be valid (we might want an explicit syntax like file://my_custom_theme or something simple like local:my_custom_theme).
  • Theme customisations, which is what this PR is all about. These should be smaller tweaks for most users. We could add a new setting, theme_customisations would be obvious (unless you are american and expect a z!) but it is really long so I am not a big fan.

Perhaps we could make the theme setting more flexible. Something like this might work (but again, I am not a huge fan of this idea):

theme:
    base: mkdocs
    custom: project_tweaks

Some of this could be done as a follow up, as I don't want to block this work but I think it is relevant to the discussion.

@waylan

waylan commented May 26, 2016 •

Copy link
Copy Markdown
Contributor Author

I see two use cases for theme_dir at the moment.

Right. As is stands now, if theme is set to a theme (mkdocs by default), then theme_dir is used to override the theme as addressed here. However, if theme is set to null (null is the YAML value which equates to None in python), then theme_dir is a full theme. Documentation for that was added in 4bb0c1c. The documentation here builds on that. I think with the two together, everything should be clear. Although, the docs on building full themes could probably use some more work. However, as you note, that is separate from this.

I'll address the other issues you raise when I have some time. Thanks for the feedback. You raise some good points.

@waylan

waylan commented May 26, 2016 •

Copy link
Copy Markdown
Contributor Author

I'm removing the search_modal block in the mkdocs theme and moving the search modal to a separate file (search.html) which is being "included" in base.html. If a user wants to override search, they can just replace that file in their theme_dir. The readthedocs theme already has a separate search.html file, so both follow a similar pattern now. The only difference is that in readthedocs, search is a separate page, whereas in mkdocs, it is included in the base page. With the changes here, a user could theoretically customize either theme to use the other's search model.

@waylan

waylan commented May 26, 2016 •

Copy link
Copy Markdown
Contributor Author

I just pushed another commit with should address your concerns @d0ugal. I added some more to the docs which should help clear things up. I left the cleanup as a separate commit so you can see what I changed. With your okay, I'll rebase and squash all of the commits before merging.

@waylan

waylan commented May 26, 2016

Copy link
Copy Markdown
Contributor Author

Perhaps we could make the theme setting more flexible. Something like this might work (but again, I am not a huge fan of this idea):

theme:
    base: mkdocs
    custom: project_tweaks

Actually, I've thought for some time that something like this would be useful. The theme setting could have a string value, in which case it would work as it does now. Or, the theme setting could have a set of nested values, in which case those settings would alter the behavior of the theme. Third party themes could potentially take advantage of this to offer features we don't want to support out-of-the-box.

Some of this could be done as a follow up, as I don't want to block this work but I think it is relevant to the discussion.

I agree. Elsewhere we discussed creating a theme class or somesuch. I expect this would be part of that.

@d0ugal

d0ugal commented May 26, 2016

Copy link
Copy Markdown
Contributor

Great, thanks! I'll try and give it another quick look tonight before merging, but if I don't manage it I am away until Monday.

@d0ugal

d0ugal commented May 26, 2016

Copy link
Copy Markdown
Contributor

By the way, we can ignore the codecov failure. I think it is just complaining that out of the changed lines, the coverage decreased in mkdocs/commands/build.py - I'll try and find an option to turn that failure off, I don't find it particularly useful.

waylan added 2 commits May 27, 2016 11:03
This is a backward incompatable change as it switches the primary
template from base.html to main.html.  See the release notes for
details.

Partially addresses mkdocs#607.
@waylan

waylan commented May 27, 2016

Copy link
Copy Markdown
Contributor Author

I just cleaned up the history into two commits. All previous work was squashed into one commit and the second commit is my response to review. @d0ugal when you get a change to review and if it looks okay, you could merge is as-is, or if you prefer, let me know and I'll squash the two commits. Either is fine with me.

@waylan waylan mentioned this pull request May 30, 2016
@d0ugal
d0ugal merged commit 176b722 into mkdocs:master May 31, 2016
@d0ugal

d0ugal commented May 31, 2016

Copy link
Copy Markdown
Contributor

Looks good. Thanks! I think this will be a great feature for many users.

@waylan
waylan deleted the template branch June 2, 2016 18:04
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

Successfully merging this pull request may close these issues.

2 participants