-
Notifications
You must be signed in to change notification settings - Fork 10
/
plugin.py
338 lines (285 loc) · 11.6 KB
/
plugin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
"""md
# Mkdocs Simple Plugin
A plugin for MkDocs that builds a documentation website from markdown content
interspersed within your code, in markdown files or in block comments in your
source files.
`simple` will search your project directory tree for documentation. By default,
Markdown files and graphics files will be copied to your documentation site.
Source files will also be searched for markdown embedded in minimally-structured
comment blocks; these will be extracted into additional markdown files included
in the documentation site.
## Installation
Install the plugin with pip.
```bash
pip install mkdocs-simple-plugin
```
{% include "versions.snippet" %}
## Usage
Create a `mkdocs.yml` file in the root of your directory and add the `simple`
plugin to its plugin list.
```yaml
site_name: "My site"
plugins:
- search:
- simple:
```
### Example usage (defaults)
{% include "mkdocs_simple_plugin/example.snippet" %}
### Inline parameters
Inline parameters configure a block's extraction.
{% include "mkdocs_simple_plugin/inline_params.snippet" %}
### Ignoring files
You can add a `.mkdocsignore` file to ignore a directory or files by glob
pattern.
See [example mkdocsignore usage](../examples/ok-mkdocsignore/README.md)
## Default settings
Below are the default settings of the plugin.
```yaml
{{ config.mkdocs_simple_config }}
```
!!!Note
If you add your own settings but want to also use any of these, you
must reiterate the settings you want in your `mkdocs.yml` file.
## Configuration scheme
{% include "mkdocs_simple_plugin/config_scheme.snippet" %}
## Build
You can build mkdocs from the command line using the standard command
```bash
mkdocs build
```
or you can generate and build at the same time [see generator](generator.md).
## Run a local server
One of the best parts of mkdocs is the ability to serve (and update!) your
documentation site locally.
```bash
mkdocs serve
```
"""
import os
import shutil
import tempfile
import yaml
from mkdocs.plugins import BasePlugin
from mkdocs.config import config_options
from mkdocs import config as mkdocs_config
from mkdocs import utils
from mkdocs_simple_plugin.simple import Simple
class SimplePlugin(BasePlugin):
"""SimplePlugin adds documentation throughout your repo to a mkdocs wiki."""
# md file=config_scheme.snippet
config_scheme = (
# ### include_folders
#
# Directories whose name matches a glob pattern in this list will be
# searched for documentation
('include_folders', config_options.Type(list, default=['*'])),
#
# ### ignore_folders
#
# Directories whose name matches a glob pattern in this list will NOT be
# searched for documentation.
('ignore_folders', config_options.Type(list, default=[])),
#
# ### ignore_hidden
#
# Hidden directories will not be searched if this is true.
('ignore_hidden', config_options.Type(bool, default=True)),
#
# ### merge_docs_dir
#
# If true, the contents of the docs directory (if any) will be merged
# at the same level as all other documentation.
# Otherwise, the docs directory will be retained as a subdirectory in
# the result.
('merge_docs_dir', config_options.Type(bool, default=True)),
#
# ### build_docs_dir
#
# If set, the directory where docs will be collated to be build.
# Otherwise, the build docs directory will be a temporary directory.
('build_docs_dir', config_options.Type(str, default='')),
#
# ### include_extensions
#
# Any file in the searched directories whose name contains a string in
# this list will simply be copied to the generated documentation.
('include_extensions',
config_options.Type(
list,
default=[
".bmp", ".tif", ".tiff", ".gif", ".svg", ".jpeg",
".jpg", ".jif", ".jiff", ".jp2", ".jpx", ".j2k",
".j2c", ".fpx", ".pcd", ".png", ".pdf", "CNAME",
".snippet", ".pages", "LICENSE"
])),
#
# ### semiliterate
#
# The semiliterate settings allows the extraction of markdown from
# inside source files.
# It is defined as a list of blocks of settings for different
# filename patterns (typically matching filename extensions).
# All regular expression parameters use ordinary Python `re` syntax.
#
# {% include "mkdocs_simple_plugin/Semiliterate.snippet" %}
#
# {% include "mkdocs_simple_plugin/ExtractionPattern.snippet" %}
# /md
('semiliterate',
config_options.Type(
list,
default=[
{
'pattern': r'.*',
'terminate': r'^\W*md-ignore',
'extract': [
{
# md file="example.snippet"
# block comments starting with: `"""md`
'start': r'^\s*"""\W?md\b',
'stop': r'^\s*"""\s*$',
#
# ```python
# """md
# This is a documentation comment.
# """
# ```
#
},
{
# line comments starting with:
# `# md` and ending with `# /md`,
'start': r'^\s*#+\W?md\b',
'stop': r'^\s*#\s?\/md\s*$',
# stripping leading spaces and `#``,
# and only capturing comment lines.
'replace': [r'^\s*# ?(.*\n?)$', r'^.*$'],
#
# ```python
# # md
# # This is a documentation comment.
# # /md
# ```
#
},
{
# block comments starting with: `/** md`
'start': r'^\s*/\*+\W?md\b',
'stop': r'^\s*\*\*/\s*$',
#
# ```c
# /** md
# This is a documentation comment.
# **/
# ```
#
},
{
# in line comments starting with
# `// md`, ending with `// end md`,
'start': r'^\s*\/\/+\W?md\b',
'stop': r'^\s*\/\/\send\smd\s*$',
# stripping leading spaces and `//`,
# and only capturing comment lines.
'replace': [r'^\s*\/\/\s?(.*\n?)$', r'^.*$'],
#
# ```c
# // md
# // This is a documentation comment.
# // end md
# ```
#
},
{
# block comments starting with
# `<!-- md` and ending with `-->`
'start': r'<!--\W?md\b',
'stop': r'-->\s*$',
#
# ```xml
# <!-- md
# This is a documentation comment.
# -->
# ```
#
}
]
}
]))
)
# /md
def __init__(self):
"""Set up internal variables."""
self.orig_docs_dir = None
self.paths = None
def on_config(self, config, **kwargs):
"""Update configuration to use a temporary build directory."""
default_config = dict((name, config_option.default)
for name, config_option in self.config_scheme)
config['mkdocs_simple_config'] = yaml.dump(
default_config,
sort_keys=False,
default_flow_style=False,
allow_unicode=True,
encoding=None)
# Create a temporary build directory, and set some options to serve it
# PY2 returns a byte string by default. The Unicode prefix ensures a
# Unicode string is returned. And it makes MkDocs temp dirs easier to
# identify.
build_docs_dir = self.config['build_docs_dir']
if not build_docs_dir:
build_docs_dir = tempfile.mkdtemp(
prefix="mkdocs_simple_" +
os.path.basename(
os.path.dirname(
config.config_file_path)))
utils.log.info(
"mkdocs-simple-plugin: build_docs_dir: %s",
build_docs_dir)
self.config['build_docs_dir'] = build_docs_dir
# Clean out build folder on config
shutil.rmtree(build_docs_dir, ignore_errors=True)
os.makedirs(build_docs_dir, exist_ok=True)
# Save original docs directory location
self.orig_docs_dir = config['docs_dir']
# Update the docs_dir with our temporary one
config['docs_dir'] = build_docs_dir
# Add all markdown extensions to include list
self.config['include_extensions'] = list(utils.markdown_extensions) + \
self.config['include_extensions']
# Always ignore the output paths
self.config["ignore_paths"] = [
get_config_site_dir(config.config_file_path),
config['site_dir'],
self.config['build_docs_dir']]
return config
def on_pre_build(self, *, config):
"""Build documentation directory with files according to settings."""
# Configure simple
simple = Simple(**self.config)
# Merge docs
if self.config["merge_docs_dir"]:
simple.merge_docs(self.orig_docs_dir)
# Copy all of the valid doc files into build_docs_dir
self.paths = simple.build_docs()
def on_serve(self, server, *, config, builder):
"""Add files to watch server."""
# watch the original docs/ directory
if os.path.exists(self.orig_docs_dir):
server.watch(self.orig_docs_dir)
# watch all the doc files
for path in self.paths:
server.watch(path)
return server
def get_config_site_dir(config_file_path: str) -> str:
"""Get configuration directory from mkdocs.yml file.
This is needed in the case you are running mkdocs serve, which
overwrites the path with a temporary one.
"""
orig_config = mkdocs_config.load_config(config_file_path)
utils.log.debug(
"mkdocs-simple-plugin: loading file: %s", config_file_path)
utils.log.debug(
"mkdocs-simple-plugin: User config site_dir: %s",
orig_config.data['site_dir'])
return orig_config.data['site_dir']