diff --git a/.bumpversion.cfg b/.bumpversion.cfg index c422629..2796621 100644 --- a/.bumpversion.cfg +++ b/.bumpversion.cfg @@ -1,5 +1,5 @@ [bumpversion] -current_version = 3.8.1 +current_version = 3.9.0 [bumpversion:file:mkdocs_include_markdown_plugin/__init__.py] diff --git a/.editorconfig b/.editorconfig index 077bcf0..6fd1d19 100644 --- a/.editorconfig +++ b/.editorconfig @@ -7,7 +7,7 @@ indent_style = space indent_size = 2 trim_trailing_whitespace = true -[*.{py,cfg}] +[*.{py,cfg,ini}] indent_size = 4 [*.{py,md}] diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a4ec11f..a1316ca 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -13,13 +13,15 @@ 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 @@ -27,23 +29,21 @@ jobs: 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/') diff --git a/.github/workflows/pre-commit-autoupdate.yml b/.github/workflows/pre-commit-autoupdate.yml index 5fb410c..4b3bb4b 100644 --- a/.github/workflows/pre-commit-autoupdate.yml +++ b/.github/workflows/pre-commit-autoupdate.yml @@ -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 diff --git a/.github/workflows/pre-commit.yml b/.github/workflows/pre-commit.yml index a4be678..c38ee14 100644 --- a/.github/workflows/pre-commit.yml +++ b/.github/workflows/pre-commit.yml @@ -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 diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index b7ef0c1..6c01a88 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,6 +1,6 @@ repos: - repo: https://github.com/asottile/pyupgrade - rev: v2.38.0 + rev: v2.38.2 hooks: - id: pyupgrade args: @@ -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 @@ -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 diff --git a/mkdocs_include_markdown_plugin/__init__.py b/mkdocs_include_markdown_plugin/__init__.py index 9b44e07..cf59bca 100644 --- a/mkdocs_include_markdown_plugin/__init__.py +++ b/mkdocs_include_markdown_plugin/__init__.py @@ -1,2 +1,2 @@ __title__ = 'mkdocs_include_markdown_plugin' -__version__ = '3.8.1' +__version__ = '3.9.0' diff --git a/mkdocs_include_markdown_plugin/event.py b/mkdocs_include_markdown_plugin/event.py index a127215..debae9d 100644 --- a/mkdocs_include_markdown_plugin/event.py +++ b/mkdocs_include_markdown_plugin/event.py @@ -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): @@ -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': { @@ -312,6 +315,7 @@ def found_include_tag(match): default_preserve_includer_indent, default_dedent, default_trailing_newlines, + build=build, ) # trailing newlines right stripping @@ -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': { @@ -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 @@ -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']), @@ -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, ) diff --git a/mkdocs_include_markdown_plugin/plugin.py b/mkdocs_include_markdown_plugin/plugin.py index 7640e35..94e41a9 100644 --- a/mkdocs_include_markdown_plugin/plugin.py +++ b/mkdocs_include_markdown_plugin/plugin.py @@ -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, + ) diff --git a/setup.cfg b/setup.cfg index ddcacf9..acc1eb7 100644 --- a/setup.cfg +++ b/setup.cfg @@ -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 @@ -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 diff --git a/tox.ini b/tox.ini new file mode 100644 index 0000000..6a3c36a --- /dev/null +++ b/tox.ini @@ -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}