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

Find scripts relative to mkdocs.yml, not current directory #17

Merged
merged 2 commits into from
Aug 21, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pip install mkdocs-gen-files

## Usage

Activate the plugin in **mkdocs.yml** (`scripts` is a required list of Python scripts to execute):
Activate the plugin in **mkdocs.yml** (`scripts` is a required list of Python scripts to execute, always relative to **mkdocs.yml**):

```yaml
plugins:
Expand All @@ -28,7 +28,7 @@ plugins:
- gen_pages.py # or any other name or path
```

Then create such a script **gen_pages.py** (this is relative to the root, *not* to the *docs* directory).
Then create such a script **gen_pages.py** (this is relative to the root, *not* to the **docs** directory).

```python
import mkdocs_gen_files
Expand Down
3 changes: 1 addition & 2 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pip install mkdocs-gen-files

## Usage

Activate the plugin in **mkdocs.yml** (`scripts` is a required list of Python scripts to execute):
Activate the plugin in **mkdocs.yml** (`scripts` is a required list of Python scripts to execute, always relative to **mkdocs.yml**):

```yaml
plugins:
Expand Down Expand Up @@ -60,4 +60,3 @@ Note that this happens before MkDocs reads any of the doc files, so all of the o
All file modes are supported (even e.g. `ab+`). You could even open a file to read it, replace something in it, and write it back anew. Though at that point you may be better served by the ["macros" plugin](https://github.com/fralau/mkdocs_macros_plugin/).

Note that this function is separate from the top-level built-in `open()`, which is unaffected and can still be used normally, relative to the current working directory (which is *not* changed to **./docs/**, instead it's just the directory that you ran `mkdocs` from).

25 changes: 25 additions & 0 deletions mkdocs_gen_files/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from mkdocs.config.config_options import File, OptionallyRequired


class ListOfItems(OptionallyRequired):
Copy link
Owner Author

Choose a reason for hiding this comment

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

This one is copied from mkdocs/mkdocs#2938 commit "Add generic ListOfItems config, refactor existing configs based on it"

def __init__(self, option_type, default=[], required=False):
super().__init__(default, required)
self.option_type = option_type
self.option_type.warnings = self.warnings

def pre_validation(self, config, key_name):
self._config = config
self._key_name = key_name

def run_validation(self, value):
oprypin marked this conversation as resolved.
Show resolved Hide resolved
result = []
for item in value:
self.option_type.pre_validation(self._config, self._key_name)
result.append(self.option_type.validate(item))
self.option_type.post_validation(self._config, self._key_name)
return result


class ListOfFiles(ListOfItems):
def __init__(self):
super().__init__(File(exists=True))
9 changes: 5 additions & 4 deletions mkdocs_gen_files/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import urllib.parse

import mkdocs.utils
from mkdocs.config import Config, config_options
from mkdocs.config import Config
from mkdocs.plugins import BasePlugin
from mkdocs.structure.files import Files
from mkdocs.structure.pages import Page
Expand All @@ -15,19 +15,20 @@
except ImportError:
PluginError = SystemExit

from . import editor
from .config import ListOfFiles
from .editor import FilesEditor

log = logging.getLogger(f"mkdocs.plugins.{__name__}")
log.addFilter(mkdocs.utils.warning_filter)


class GenFilesPlugin(BasePlugin):
config_scheme = (("scripts", config_options.Type(list)),)
config_scheme = (("scripts", ListOfFiles()),)

def on_files(self, files: Files, config: Config) -> Files:
self._dir = tempfile.TemporaryDirectory(prefix="mkdocs_gen_files_")

with editor.FilesEditor(files, config, self._dir.name) as ed:
with FilesEditor(files, config, self._dir.name) as ed:
for file_name in self.config["scripts"]:
try:
runpy.run_path(file_name)
Expand Down