Skip to content

Commit

Permalink
Watch included files when serving with Mkdocs (#91)
Browse files Browse the repository at this point in the history
* Watch included files when serving with Mkdocs

* Update .gitignore

* Fix error in code

* Revert change

* Fix error

* Bump version

* Add tox configuration

* Add Python3.6 to envlist

* Try fix

* Stop testing against Python3.6 in CI

* Add MacOS to CI

* Add Mkdocs v1.2.4 to tests

* Fix error in CI

* Fix error in CI

* Try to solve problem in CI

* Minor change

* Update actions

* Update pre-commit hooks
  • Loading branch information
mondeja authored Sep 28, 2022
1 parent 3896ccf commit 89ef479
Show file tree
Hide file tree
Showing 11 changed files with 122 additions and 42 deletions.
2 changes: 1 addition & 1 deletion .bumpversion.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 3.8.1
current_version = 3.9.0

[bumpversion:file:mkdocs_include_markdown_plugin/__init__.py]

Expand Down
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ indent_style = space
indent_size = 2
trim_trailing_whitespace = true

[*.{py,cfg}]
[*.{py,cfg,ini}]
indent_size = 4

[*.{py,md}]
Expand Down
16 changes: 8 additions & 8 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,37 @@ jobs:
test:
name: Test
runs-on: ${{ matrix.os }}
env:
PYTEST_ADDOPTS: -vv
strategy:
matrix:
os:
- ubuntu-latest
- windows-latest
- macos-latest
python-version:
- 3.6
- 3.7
- 3.8
- 3.9
- '3.10'
steps:
- uses: actions/checkout@v3
- name: Set up Python v${{ matrix.python-version }}
uses: actions/setup-python@v3
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip coveralls
python -m pip install .[test]
pip list
- name: Test with pytest
run: pytest -svv
python -m pip install -U pip coveralls tox tox-gh-actions
- name: Run tests
run: tox
- name: Coveralls
run: coveralls
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
COVERALLS_SERVICE_NAME: github
- name: Test examples
run: pytest -svv tests/_test_examples.py
run: tox -e examples

build-sdist:
if: startsWith(github.ref, 'refs/tags/')
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/pre-commit-autoupdate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v3
uses: actions/setup-python@v4
with:
python-version: 3.x
- name: Install pre-commit
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/pre-commit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v3
uses: actions/setup-python@v4
with:
python-version: 3.x
- name: Run pre-commit
Expand Down
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
repos:
- repo: https://github.com/asottile/pyupgrade
rev: v2.38.0
rev: v2.38.2
hooks:
- id: pyupgrade
args:
Expand All @@ -15,7 +15,7 @@ repos:
- id: double-quote-string-fixer
name: double-quote-string-fixer
- repo: https://github.com/asottile/add-trailing-comma
rev: v2.2.3
rev: v2.3.0
hooks:
- id: add-trailing-comma
name: add-trailing-comma
Expand All @@ -42,7 +42,7 @@ repos:
- --filter-files
files: \.py$
- repo: https://github.com/mondeja/mdpo
rev: v1.0.1
rev: v1.0.3
hooks:
- id: md2po2md
files: ^README\.md
Expand Down
2 changes: 1 addition & 1 deletion mkdocs_include_markdown_plugin/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
__title__ = 'mkdocs_include_markdown_plugin'
__version__ = '3.8.1'
__version__ = '3.9.0'
9 changes: 9 additions & 0 deletions mkdocs_include_markdown_plugin/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ def get_file_content(
default_trailing_newlines,
default_comments=CONFIG_DEFAULTS['comments'],
cumulative_heading_offset=0,
build=None,
):

def found_include_tag(match):
Expand Down Expand Up @@ -199,6 +200,8 @@ def found_include_tag(match):
f':{lineno}',
)
return ''
elif build is not None:
build.included_files.extend(file_paths_to_include)

bool_options = {
'preserve-includer-indent': {
Expand Down Expand Up @@ -312,6 +315,7 @@ def found_include_tag(match):
default_preserve_includer_indent,
default_dedent,
default_trailing_newlines,
build=build,
)

# trailing newlines right stripping
Expand Down Expand Up @@ -435,6 +439,8 @@ def found_include_markdown_tag(match):
f':{lineno}',
)
return ''
elif build is not None:
build.included_files.extend(file_paths_to_include)

bool_options = {
'rewrite-relative-urls': {
Expand Down Expand Up @@ -583,6 +589,7 @@ def found_include_markdown_tag(match):
default_dedent,
default_trailing_newlines,
default_comments=default_comments,
build=build,
)

# trailing newlines right stripping
Expand Down Expand Up @@ -674,6 +681,7 @@ def on_page_markdown(
page,
docs_dir,
config={},
build=None,
):
escaped_opening_tag = re.escape(
config.get('opening_tag', CONFIG_DEFAULTS['opening_tag']),
Expand Down Expand Up @@ -713,4 +721,5 @@ def on_page_markdown(
config.get('dedent', CONFIG_DEFAULTS['dedent']),
config.get('trailing_newlines', CONFIG_DEFAULTS['trailing_newlines']),
default_comments=config.get('comments', CONFIG_DEFAULTS['comments']),
build=build,
)
74 changes: 64 additions & 10 deletions mkdocs_include_markdown_plugin/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,67 @@
)


class IncludeMarkdownPlugin(mkdocs.plugins.BasePlugin):
config_scheme = CONFIG_SCHEME

def on_page_markdown(self, markdown, page, **kwargs):
return _on_page_markdown(
markdown,
page,
kwargs['config']['docs_dir'],
config=self.config,
)
mkdocs__version_info__ = tuple(
int(num) for num in mkdocs.__version__.split('.') if num.isdigit()
)

if mkdocs__version_info__ < (1, 4, 0):
class IncludeMarkdownPlugin(mkdocs.plugins.BasePlugin):
config_scheme = CONFIG_SCHEME

def on_page_markdown(self, markdown, page, **kwargs):
return _on_page_markdown(
markdown,
page,
kwargs['config']['docs_dir'],
config=self.config,
build=None,
)
else:

class WatchingFiles:
def __init__(self):
self.prev_included_files = []
self.included_files = []

SERVER = None
WATCHING_FILES = None

class IncludeMarkdownPlugin(mkdocs.plugins.BasePlugin):
config_scheme = CONFIG_SCHEME

def _watch_included_files(self):
for filepath in WATCHING_FILES.prev_included_files:
if filepath not in WATCHING_FILES.included_files:
SERVER.unwatch(filepath)
WATCHING_FILES.prev_included_files = (
WATCHING_FILES.included_files[:]
)

for filepath in WATCHING_FILES.included_files:
SERVER.watch(filepath, recursive=False)
WATCHING_FILES.included_files = []

def on_page_content(self, html, *args, **kwargs):
if SERVER:
self._watch_included_files()
return html

def on_serve(self, server, builder, **kwargs):
global SERVER
if SERVER is None:
SERVER = server
self._watch_included_files()

@mkdocs.plugins.event_priority(-100)
def on_page_markdown(self, markdown, page, **kwargs):
global WATCHING_FILES
if WATCHING_FILES is None:
WATCHING_FILES = WatchingFiles()
return _on_page_markdown(
markdown,
page,
kwargs['config']['docs_dir'],
config=self.config,
build=WATCHING_FILES,
)
23 changes: 7 additions & 16 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = mkdocs_include_markdown_plugin
version = 3.8.1
version = 3.9.0
description = Mkdocs Markdown includer plugin.
long_description = file: README.md
long_description_content_type = text/markdown
Expand Down Expand Up @@ -42,25 +42,16 @@ mkdocs.plugins =
[options.extras_require]
dev =
bump2version==1.0.1
flake8==3.9.2
flake8-implicit-str-concat==0.2.0
flake8-print==4.0.0
isort==5.9.1
mdpo==0.3.61
mkdocs==1.3.1
pre-commit==2.13.0
pytest==6.2.5
mkdocs==1.4.0
pre-commit
pytest==7.1.3
pytest-cov==3.0.0
pyupgrade==2.19.4
yamllint==1.26.1
tox
test =
mkdocs==1.3.1
pytest==6.2.5
mkdocs==1.4.0
pytest==7.1.3
pytest-cov==3.0.0

[tool:pytest]
addopts = --cov=mkdocs_include_markdown_plugin --cov-report=html --cov-config=setup.cfg

[coverage:run]
omit = mkdocs_include_markdown_plugin/plugin.py

Expand Down
26 changes: 26 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[tox]
envlist = py3{7,8,9,10}-mkdocs{124,131,140}
skip_missing_interpreters = true

[gh-actions]
python =
3.7: py37
3.8: py38
3.9: py39
3.10: py310

[testenv]
extras =
test
deps =
mkdocs124: mkdocs==1.2.4
mkdocs131: mkdocs==1.3.1
mkdocs140: mkdocs==1.4.0
commands = pytest --cov=mkdocs_include_markdown_plugin \
--cov-report=html \
--cov-config=setup.cfg \
{posargs}
package = wheel

[testenv:examples]
commands = pytest tests/_test_examples.py {posargs}

0 comments on commit 89ef479

Please sign in to comment.