Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c78ea53

Browse files
committed
feat(mkdocs): cli postprocessing support
That way, a single huge markdown file containing documentation for commands and methods can be split up into multiple files for individual inclusion in mkdocs. It's done by a post-processor which is loaded by mako-render, providing access to the entire context. Said processor may also drop results altogether and thus prevent files to be written that have been split up by it.
1 parent 2e74d91 commit c78ea53

7 files changed

Lines changed: 81 additions & 7 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ gen/*-cli/
66
*.go
77
*.pyc
88
**target/
9+
**docs/
910
**build_html/
1011
.*.deps
1112
**Cargo.lock

etc/api/type-cli.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ mkdocs:
22
## A directory to bring us from the mkdocs invocation directory to the gen-root
33
gen_root_dir: ..
44
site_dir: build_html
5+
# if docs_dir changes, remember to update the sources as well.
6+
docs_dir: docs
7+
mako:
8+
post_processor_module: cli
59
make:
610
id: cli
711
target_name: CLIs
@@ -12,6 +16,7 @@ make:
1216
templates:
1317
- source: ../LICENSE.md
1418
- source: ../Cargo.toml
19+
- source: docs/commands.yml
1520
- source: mkdocs.yml
1621
- source: README.md
1722
- source: main.rs

etc/bin/mako-render

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,11 @@ def cmdline(argv=None):
271271
"parent directory of the file provided.")
272272
parser.add_argument('-io', nargs="+",
273273
help="input and ouptut pairs. can be used multiple times, use TEMPLATE_FILE_IN=[OUTPUT_FILE])")
274+
parser.add_argument('--post-process-python-module', default="",
275+
help="Specify a python module with a `module.process_template_result(r, output_file|None) -> None|r'."
276+
"If it returns None, no output file will be written. Use it to perform any operation on "
277+
"the template's result. The module, like 'foo.handler' will be imported and thus "
278+
" needs to be in the PYTHONPATH.")
274279

275280
options = parser.parse_args(argv)
276281
if len(options.io) == 0:
@@ -284,6 +289,16 @@ def cmdline(argv=None):
284289
data_converted.update(dict([varsplit(var) for var in options.var]))
285290
del data
286291

292+
post_processor = lambda r, of: r
293+
if options.post_process_python_module:
294+
fn_name = 'process_template_result'
295+
pm = __import__(options.post_process_python_module, globals(), locals(), [])
296+
post_processor = getattr(pm, fn_name, None)
297+
if post_processor is None:
298+
raise AssertionError("python module '%s' must have a function called '%s'"
299+
% (options.post_process_python_module, fn_name))
300+
# end handle post processor
301+
287302
seen_stdin = False
288303
for input_file, output_file in options.io:
289304
if input_file == '-':
@@ -306,7 +321,9 @@ def cmdline(argv=None):
306321
_exit()
307322

308323
try:
309-
result = template.render(**data_converted)
324+
result = post_processor(template.render(**data_converted), output_file)
325+
if result is None:
326+
continue
310327
if output_file:
311328
dir = dirname(output_file)
312329
if dir and not os.path.isdir(dir):

src/mako/cli/main.rs.mako

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
<%namespace name="docopt" file="lib/docopt.mako"/>\
2+
<%namespace name="util" file="../lib/util.mako"/>\
23
<%
3-
from util import new_context
4+
from util import (new_context, rust_comment)
45
56
c = new_context(schemas, resources, context.get('methods'))
67
default_user_agent = "google-cli-rust-client/" + cargo.build_version
78
%>\
9+
<%block filter="rust_comment">\
10+
<%util:gen_info source="${self.uri}" />\
11+
</%block>
812
#![feature(plugin)]
913
#![plugin(docopt_macros)]
1014

src/mako/cli/mkdocs.yml.mako

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
1-
<%! from util import put_and %>\
1+
<%
2+
from util import (put_and, new_context)
3+
from cli import (subcommand_md_filename, mangle_subcommand)
4+
5+
c = new_context(schemas, resources, context.get('methods'))
6+
%>\
27
<%namespace name="util" file="../lib/util.mako"/>\
38
site_name: ${util.canonical_name()} v${util.crate_version()}
49
site_url: ${cargo.doc_base_url}/${util.crate_name()}
510
site_description: Write integrating applications with bcore
611

712
repo_url: ${util.github_source_root_url()}
813

9-
docs_dir: docs
14+
docs_dir: ${mkdocs.docs_dir}
1015
site_dir: ${mkdocs.site_dir}
1116

1217
pages:
1318
- ['index.md', 'Home']
14-
## - ['be.md', 'Features', 'BE - universal commandline tool']
19+
% for resource in sorted(c.rta_map.keys()):
20+
- ['${subcommand_md_filename(resource)}', 'Commands', '${' '.join(s.capitalize() for s in mangle_subcommand(resource).split('-'))}']
21+
% endfor
1522

1623
theme: readthedocs
1724

src/mako/deps.mako

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@
2828
agsuffix = make.aggregated_target_suffix
2929
global_targets = make.get('global_targets', False)
3030
31+
post_processor_arg = ''
32+
if mako is not UNDEFINED:
33+
post_processor_arg = '--post-process-python-module=%s' % mako.post_processor_module
34+
3135
try:
3236
root = directories.mako_src + '/' + make.id + '/lib'
3337
lib_files = [os.path.join(root, file_name) for file_name in os.listdir(root)]
@@ -85,7 +89,7 @@ ${api_common}: $(RUST_SRC)/${make.id}/cmn.rs $(lastword $(MAKEFILE_LIST)) ${gen_
8589

8690
${gen_root_stamp}: ${' '.join(i[0] for i in sds)} ${' '.join(lib_files)} ${api_json_inputs} $(MAKO_STANDARD_DEPENDENCIES) ${depends_on_target}
8791
@echo Generating ${api_target}
88-
@$(MAKO) -io ${' '.join("%s=%s" % (s, d) for s, d in sds)} --data-files ${api_json_inputs}
92+
@$(MAKO) -io ${' '.join("%s=%s" % (s, d) for s, d in sds)} ${post_processor_arg} --data-files ${api_json_inputs}
8993
@touch $@
9094

9195
${api_target}: ${api_common}
@@ -105,7 +109,7 @@ ${api_doc_index}: ${api_common}
105109
% else:
106110
@echo mkdocs ${api_doc_index}
107111
## Our README is the landing page, and thus will serve multiple roles at once !
108-
@cd ${gen_root} && (mkdir -p docs && cd docs && ln -s ../README.md index.md &>/dev/null) || : && $(MKDOCS) build --clean
112+
@cd ${gen_root} && (mkdir -p ${mkdocs.docs_dir} && cd ${mkdocs.docs_dir} && ln -s ../README.md index.md &>/dev/null) || : && $(MKDOCS) build --clean
109113
% endif
110114

111115
${api_doc}: ${api_doc_index}

src/mako/lib/cli.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,41 @@
11
import util
22

3+
import os
4+
import re
5+
6+
SPLIT_START = '>>>>>>>'
7+
SPLIT_END = '<<<<<<<'
8+
9+
re_splitters = re.compile(r"%s ([\w\-\.]+)\n(.*?)\n%s" % (SPLIT_START, SPLIT_END), re.MULTILINE|re.DOTALL)
10+
311
# transform name to be a suitable subcommand
412
def mangle_subcommand(name):
513
return util.camel_to_under(name).replace('_', '-').replace('.', '-')
14+
15+
16+
# transform the resource name into a suitable filename to contain the markdown documentation for it
17+
def subcommand_md_filename(resource):
18+
return mangle_subcommand(resource) + '.md'
19+
20+
21+
# split the result along split segments
22+
def process_template_result(r, output_file):
23+
found = False
24+
dir = None
25+
if output_file:
26+
dir = os.path.dirname(output_file)
27+
if not os.path.isdir(dir):
28+
os.makedirs(dir)
29+
# end handle output directory
30+
31+
for m in re_splitters.finditer(r):
32+
found = True
33+
fh = open(os.path.join(dir, m.group(1)), 'wb')
34+
fh.write(m.group(2))
35+
fh.close()
36+
# end for each match
37+
38+
if found:
39+
r = None
40+
41+
return r

0 commit comments

Comments
 (0)