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

fix(plugin): handle no git-timestamp on page meta #12

Closed

Conversation

rachmadaniHaryono
Copy link

@rachmadaniHaryono rachmadaniHaryono commented Dec 31, 2021

Proposed changes

when i write drafted text and run mkdocs serve mkdocs will raise error that page.meta don't have 'git-timestamp'

first it happened here

page.meta["git-timestamp"] = timestamp

(sorry forget the traceback for this one)

after af8b50a there is also error on other place for example

> poetry run mkdocs serve
INFO     -  Building documentation...
INFO     -  Cleaning site directory
ERROR    -  Error building page 'index.md': 'git-timestamp'
Traceback (most recent call last):
  File "/home/r3r/.cache/pypoetry/virtualenvs/private-xkr2mhDC-py3.10/bin/mkdocs", line 8, in <module>
    sys.exit(cli())
  File "/home/r3r/.cache/pypoetry/virtualenvs/private-xkr2mhDC-py3.10/lib/python3.10/site-packages/click/core.py", line 1128, in __call__
    return self.main(*args, **kwargs)
  File "/home/r3r/.cache/pypoetry/virtualenvs/private-xkr2mhDC-py3.10/lib/python3.10/site-packages/click/core.py", line 1053, in main
    rv = self.invoke(ctx)
  File "/home/r3r/.cache/pypoetry/virtualenvs/private-xkr2mhDC-py3.10/lib/python3.10/site-packages/click/core.py", line 1659, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/home/r3r/.cache/pypoetry/virtualenvs/private-xkr2mhDC-py3.10/lib/python3.10/site-packages/click/core.py", line 1395, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/home/r3r/.cache/pypoetry/virtualenvs/private-xkr2mhDC-py3.10/lib/python3.10/site-packages/click/core.py", line 754, in invoke
    return __callback(*args, **kwargs)
  File "/home/r3r/.cache/pypoetry/virtualenvs/private-xkr2mhDC-py3.10/lib/python3.10/site-packages/mkdocs/__main__.py", line 177, in serve_command
    serve.serve(dev_addr=dev_addr, livereload=livereload, **kwargs)
  File "/home/r3r/.cache/pypoetry/virtualenvs/private-xkr2mhDC-py3.10/lib/python3.10/site-packages/mkdocs/commands/serve.py", line 54, in serve
    config = builder()
  File "/home/r3r/.cache/pypoetry/virtualenvs/private-xkr2mhDC-py3.10/lib/python3.10/site-packages/mkdocs/commands/serve.py", line 49, in builder
    build(config, live_server=live_server, dirty=dirty)
  File "/home/r3r/.cache/pypoetry/virtualenvs/private-xkr2mhDC-py3.10/lib/python3.10/site-packages/mkdocs/commands/build.py", line 314, in build
    _build_page(file.page, config, doc_files, nav, env, dirty)
  File "/home/r3r/.cache/pypoetry/virtualenvs/private-xkr2mhDC-py3.10/lib/python3.10/site-packages/mkdocs/commands/build.py", line 220, in _build_page
    output = config['plugins'].run_event(
  File "/home/r3r/.cache/pypoetry/virtualenvs/private-xkr2mhDC-py3.10/lib/python3.10/site-packages/mkdocs/plugins.py", line 102, in run_event
    result = method(item, **kwargs)
  File "/home/r3r/.cache/pypoetry/virtualenvs/private-xkr2mhDC-py3.10/lib/python3.10/site-packages/mkdocs_blogging_plugin/plugin.py", line 249, in on_post_page
    sorted_entries = {tag: self.sorted_pages(
  File "/home/r3r/.cache/pypoetry/virtualenvs/private-xkr2mhDC-py3.10/lib/python3.10/site-packages/mkdocs_blogging_plugin/plugin.py", line 249, in <dictcomp>
    sorted_entries = {tag: self.sorted_pages(
  File "/home/r3r/.cache/pypoetry/virtualenvs/private-xkr2mhDC-py3.10/lib/python3.10/site-packages/mkdocs_blogging_plugin/plugin.py", line 279, in sorted_pages
    return sorted(pages,
  File "/home/r3r/.cache/pypoetry/virtualenvs/private-xkr2mhDC-py3.10/lib/python3.10/site-packages/mkdocs_blogging_plugin/plugin.py", line 280, in <lambda>
    key=lambda page: page.meta["git-timestamp"],
KeyError: 'git-timestamp'

so i write function to replace lambda where page is sorted by git-timestamp

Types of changes

What types of changes does your code introduce?
Put an x in the boxes that apply

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation Update (if none of the other choices apply)

Checklist

Put an x in the boxes that apply. You can also fill these out after creating the PR. If you're unsure about any of them, don't hesitate to ask. We're here to help! This is simply a reminder of what we are going to look for before merging your code.

  • I have added tests that prove my fix is effective or that my feature works

Further comments

  • file is formatted using darker and isort
  • there is pytest test but i don't add pytest to setup
  • if there is no git-timestamp order by url first then title. there is no reason for this order

unrelated but there is also this warning when running pytest

../../../../home/r3r/.cache/pypoetry/virtualenvs/mkdocs-blogging-plugin-6qHKpuQG-py3.10/lib/python3.10/site-packages/mkdocs/utils/filters.py:14
  /home/r3r/.cache/pypoetry/virtualenvs/mkdocs-blogging-plugin-6qHKpuQG-py3.10/lib/python3.10/site-packages/mkdocs/utils/filters.py:14: DeprecationWarning: 'contextfilter' is renamed to 'pass_context', the old name will be removed in Jinja 3.1.
    def url_filter(context, value):

-- Docs: https://docs.pytest.org/en/stable/warnings.html

i will create another pr for this later


this pr is based from this template https://github.com/appium/appium/blob/master/.github/PULL_REQUEST_TEMPLATE.md

@liang2kl
Copy link
Owner

liang2kl commented Jan 1, 2022

Thank you for the pull request!

Actually, git-timestamp is set for every page:

page.meta["git-timestamp"] = timestamp

so it shouldn't raise a key error. Can you provide a demo project to reproduce this error?

@rachmadaniHaryono
Copy link
Author

unfortunately i can't reproduce first error

similar step i can get

> poetry new p1
Created package p1 in p1
> cd p1
> poetry add mkdocs-blogging-plugin
Creating virtualenv p1-TJeFODVO-py3.10 in /home/r3r/.cache/pypoetry/virtualenvs
Using version ^1.2.0 for mkdocs-blogging-plugin

Updating dependencies
Resolving dependencies... (29.2s)

Writing lock file

Package operations: 28 installs, 0 updates, 0 removals

  • Installing six (1.16.0)
  • Installing markupsafe (2.0.1)
  • Installing pyparsing (3.0.6)
  • Installing python-dateutil (2.8.2)
  • Installing pyyaml (6.0)
  • Installing smmap (5.0.0)
  • Installing zipp (3.7.0)
  • Installing click (8.0.3)
  • Installing ghp-import (2.0.2)
  • Installing gitdb (4.0.9)
  • Installing importlib-metadata (4.10.0)
  • Installing jinja2 (3.0.3)
  • Installing markdown (3.3.6)
  • Installing mergedeep (1.3.4)
  • Installing packaging (21.3)
  • Installing pytz (2021.3)
  • Installing pyyaml-env-tag (0.1)
  • Installing watchdog (2.1.6)
  • Installing attrs (21.4.0)
  • Installing babel (2.9.1)
  • Installing gitpython (3.1.24)
  • Installing mkdocs (1.2.3)
  • Installing more-itertools (8.12.0)
  • Installing wcwidth (0.2.5)
  • Installing py (1.11.0)
  • Installing pluggy (0.13.1)
  • Installing mkdocs-blogging-plugin (1.2.0)
  • Installing pytest (5.4.3)
> poetry run mkdocs new .
> # edit mkdocs.yml
> mkdir docs/blog/2022/01/

mkdocs.yml

site_name: My Docs
plugins:
  - blogging:
      dirs:  # The directories to be included
        - blog
      features:
        tags: {}

actual package installed on project where this error occur

> poetry show
argcomplete                 1.12.3                                                                                              Bash tab completion for argparse
babel                       2.9.1                                                                                               Internationalization utilities
bracex                      2.2.1                                                                                               Bash style brace expander.
cfgv                        3.3.1                                                                                               Validate configuration and produce human readable error messages.
click                       8.0.3                                                                                               Composable command line interface toolkit
colorama                    0.4.4                                                                                               Cross-platform colored terminal text.
commitizen                  2.20.3                                                                                              Python commitizen client tool
decli                       0.5.2                                                                                               Minimal, easy-to-use, declarative cli tool
distlib                     0.3.4                                                                                               Distribution utilities
filelock                    3.4.2                                                                                               A platform independent file lock.
ghp-import                  2.0.2                                                                                               Copy your docs directly to the gh-pages branch.
gitdb                       4.0.9                                                                                               Git Object Database
gitpython                   3.1.24                                                                                              GitPython is a python library used to interact with Git reposi...
identify                    2.4.1                                                                                               File identification library for Python
importlib-metadata          4.10.0                                                                                              Read metadata from Python packages
jinja2                      3.0.3                                                                                               A very fast and expressive template engine.
markdown                    3.3.6                                                                                               Python implementation of Markdown.
markupsafe                  2.0.1                                                                                               Safely add untrusted strings to HTML/XML markup.
mergedeep                   1.3.4                                                                                               A deep merge function for 🐍.
mkdocs                      1.2.3                                                                                               Project documentation with Markdown.
mkdocs-awesome-pages-plugin 2.6.1                                                                                               An MkDocs plugin that simplifies configuring page titles and t...
mkdocs-blogging-plugin      1.2.1rc2 https://github.com/rachmadaniHaryono/mkdocs-blogging-plugin/archive/refs/heads/develop.zip Mkdocs plugin that generates a blog index page sorted by creat...
mkdocs-material             8.1.3                                                                                               A Material Design theme for MkDocs
mkdocs-material-extensions  1.0.3                                                                                               Extension pack for Python Markdown.
nodeenv                     1.6.0                                                                                               Node.js virtual environment builder
packaging                   21.3                                                                                                Core utilities for Python packages
platformdirs                2.4.1                                                                                               A small Python module for determining appropriate platform-spe...
pre-commit                  2.16.0                                                                                              A framework for managing and maintaining multi-language pre-co...
prompt-toolkit              3.0.24                                                                                              Library for building powerful interactive command lines in Python
pygments                    2.11.0                                                                                              Pygments is a syntax highlighting package written in Python.
pymdown-extensions          9.1                                                                                                 Extension pack for Python Markdown.
pyparsing                   3.0.6                                                                                               Python parsing module
python-dateutil             2.8.2                                                                                               Extensions to the standard Python datetime module
pytz                        2021.3                                                                                              World timezone definitions, modern and historical
pyyaml                      6.0                                                                                                 YAML parser and emitter for Python
pyyaml-env-tag              0.1                                                                                                 A custom YAML tag for referencing environment variables in YAM...
questionary                 1.10.0                                                                                              Python library to build pretty command line user prompts ⭐️
six                         1.16.0                                                                                              Python 2 and 3 compatibility utilities
smmap                       5.0.0                                                                                               A pure Python implementation of a sliding window memory map ma...
termcolor                   1.1.0                                                                                               ANSII Color formatting for output in terminal.
toml                        0.10.2                                                                                              Python Library for Tom's Obvious, Minimal Language
tomlkit                     0.8.0                                                                                               Style preserving TOML library
typing-extensions           4.0.1                                                                                               Backported and Experimental Type Hints for Python 3.6+
virtualenv                  20.12.0                                                                                             Virtual Python Environment builder
watchdog                    2.1.6                                                                                               Filesystem events monitoring
wcmatch                     8.3                                                                                                 Wildcard/glob file name matcher.
wcwidth                     0.2.5                                                                                               Measures the displayed width of unicode strings in a terminal
zipp                        3.7.0                                                                                               Backport of pathlib-compatible object wrapper for zip files

@liang2kl
Copy link
Owner

liang2kl commented Jan 1, 2022

Can you upload a complete directory containing mkdocs.yml and all your markdown files? I cannot get useful information from only the environment.

@rachmadaniHaryono
Copy link
Author

p2.tar.gz

> poetry run mkdocs serve
INFO     -  Building documentation...
INFO     -  Cleaning site directory
ERROR    -  Error building page 'index.md': 'git-timestamp'
Traceback (most recent call last):
  File "/home/r3r/.cache/pypoetry/virtualenvs/p2-P7z4mDpE-py3.10/bin/mkdocs", line 8, in <module>
    sys.exit(cli())
  File "/home/r3r/.cache/pypoetry/virtualenvs/p2-P7z4mDpE-py3.10/lib/python3.10/site-packages/click/core.py", line 1128, in __call__
    return self.main(*args, **kwargs)
  File "/home/r3r/.cache/pypoetry/virtualenvs/p2-P7z4mDpE-py3.10/lib/python3.10/site-packages/click/core.py", line 1053, in main
    rv = self.invoke(ctx)
  File "/home/r3r/.cache/pypoetry/virtualenvs/p2-P7z4mDpE-py3.10/lib/python3.10/site-packages/click/core.py", line 1659, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/home/r3r/.cache/pypoetry/virtualenvs/p2-P7z4mDpE-py3.10/lib/python3.10/site-packages/click/core.py", line 1395, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/home/r3r/.cache/pypoetry/virtualenvs/p2-P7z4mDpE-py3.10/lib/python3.10/site-packages/click/core.py", line 754, in invoke
    return __callback(*args, **kwargs)
  File "/home/r3r/.cache/pypoetry/virtualenvs/p2-P7z4mDpE-py3.10/lib/python3.10/site-packages/mkdocs/__main__.py", line 177, in serve_command
    serve.serve(dev_addr=dev_addr, livereload=livereload, **kwargs)
  File "/home/r3r/.cache/pypoetry/virtualenvs/p2-P7z4mDpE-py3.10/lib/python3.10/site-packages/mkdocs/commands/serve.py", line 54, in serve
    config = builder()
  File "/home/r3r/.cache/pypoetry/virtualenvs/p2-P7z4mDpE-py3.10/lib/python3.10/site-packages/mkdocs/commands/serve.py", line 49, in builder
    build(config, live_server=live_server, dirty=dirty)
  File "/home/r3r/.cache/pypoetry/virtualenvs/p2-P7z4mDpE-py3.10/lib/python3.10/site-packages/mkdocs/commands/build.py", line 314, in build
    _build_page(file.page, config, doc_files, nav, env, dirty)
  File "/home/r3r/.cache/pypoetry/virtualenvs/p2-P7z4mDpE-py3.10/lib/python3.10/site-packages/mkdocs/commands/build.py", line 220, in _build_page
    output = config['plugins'].run_event(
  File "/home/r3r/.cache/pypoetry/virtualenvs/p2-P7z4mDpE-py3.10/lib/python3.10/site-packages/mkdocs/plugins.py", line 102, in run_event
    result = method(item, **kwargs)
  File "/home/r3r/.cache/pypoetry/virtualenvs/p2-P7z4mDpE-py3.10/lib/python3.10/site-packages/mkdocs_blogging_plugin/plugin.py", line 249, in on_post_page
    sorted_entries = {tag: self.sorted_pages(
  File "/home/r3r/.cache/pypoetry/virtualenvs/p2-P7z4mDpE-py3.10/lib/python3.10/site-packages/mkdocs_blogging_plugin/plugin.py", line 249, in <dictcomp>
    sorted_entries = {tag: self.sorted_pages(
  File "/home/r3r/.cache/pypoetry/virtualenvs/p2-P7z4mDpE-py3.10/lib/python3.10/site-packages/mkdocs_blogging_plugin/plugin.py", line 279, in sorted_pages
    return sorted(pages,
  File "/home/r3r/.cache/pypoetry/virtualenvs/p2-P7z4mDpE-py3.10/lib/python3.10/site-packages/mkdocs_blogging_plugin/plugin.py", line 280, in <lambda>
    key=lambda page: page.meta["git-timestamp"],

@liang2kl
Copy link
Owner

liang2kl commented Jan 1, 2022

The issue is: there is a flaw in the original judgement for whether a page is within the blog directory specified in the configuration.

This issue is fixed in a0d41ac. I will release a patch soon.

@liang2kl
Copy link
Owner

liang2kl commented Jan 1, 2022

You can check v1.2.1 for the fixes. As each page involved in the sorting has a valid timestamp, there is no longer a need for other sorting methods. Thank you for the pull request!

@liang2kl liang2kl closed this Jan 1, 2022
@rachmadaniHaryono rachmadaniHaryono deleted the bugfix/plugin branch January 1, 2022 05:08
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