diff --git a/doc/ref/configuration.md b/doc/ref/configuration.md index 9a89fb5..694de70 100644 --- a/doc/ref/configuration.md +++ b/doc/ref/configuration.md @@ -32,6 +32,15 @@ Markdoc’s behavior: listing-filename: "_list.html" use-default-static: true use-default-templates: true + + # Custom templates + custom-templates: + - + regexps: ["about.md", "tips/.*" ] + template: "other-template.html" + - + regexps: ["index.md" ] + template: "index-template.html" # Rendering markdown: @@ -122,6 +131,18 @@ is an acceptable value. [rsync-docs]: http://www.samba.org/ftp/rsync/rsync.html +### Custom templates + +A *optional* list of custom templates used for override the default one. + +Each item of the list defines: + +`regexps` +: A list of regexp to match files. + +`template` +: The template to use for the matching files. + ### Building These settings affect Markdoc’s behavior during the build process. diff --git a/src/markdoc/builder.py b/src/markdoc/builder.py index c646753..ac6c752 100644 --- a/src/markdoc/builder.py +++ b/src/markdoc/builder.py @@ -181,8 +181,15 @@ def render_document(self, path, cache=True): context['title'] = self.title(path) context['crumbs'] = self.crumbs(path) context['make_relative'] = lambda href: make_relative(path, href) - - template = self.config.template_env.get_template('document.html') + + template = 'document.html' + # custom templates + for item in self.config.get('custom-templates', []): + if (any(re.match(regexp, path) for regexp in item['regexps'])): + template = item['template'] + break + + template = self.config.template_env.get_template(template) return template.render(context) def render_listing(self, path): diff --git a/test/builder.doctest b/test/builder.doctest index b24576f..1bbf148 100644 --- a/test/builder.doctest +++ b/test/builder.doctest @@ -42,6 +42,7 @@ You can also walk through all files in the wiki, using the `walk()` method: file1.md file2.md file3.md + custom/hello.md subdir/hello.md Rendering @@ -117,3 +118,22 @@ You can render whole documents using `Builder.render_document()`: This uses the `document.html` Jinja2 template, by default located in `WIKI_ROOT/.templates/`, to produce the documents. + +Custom templates +---------------- + +If the document has a custom template defined in the configuration, it's applied. + + >>> print b.render_document('custom/hello.md') + + + + + Hello custom. + + + +

Custom Template

+

Hello custom.

+ + diff --git a/test/cli.doctest b/test/cli.doctest index 1307cec..4160d29 100644 --- a/test/cli.doctest +++ b/test/cli.doctest @@ -14,6 +14,8 @@ The `show-config` command will show the current Markdoc config: >>> exit_code = markdoc('show-config') # doctest: +ELLIPSIS {'9d03e32c6cb1455388a170e3bb0c2030': '7010d5d5871143d089cb662cb540cbd5', + 'custom-templates': [{'regexps': ['custom/.*'], + 'template': 'custom-template.html'}], 'hide-prefix': '_', 'meta.config-file': '.../example/markdoc.yaml', 'meta.root': '.../example', @@ -43,6 +45,7 @@ There are three other commands -- `clean-html`, `clean-temp`, and `sync-html` -- >>> print '\n'.join(sorted(os.listdir(CONFIG.html_dir))) _list.html an_empty_file.html + custom example.css file1.html file2.html diff --git a/test/example/_templates/custom-template.html b/test/example/_templates/custom-template.html new file mode 100644 index 0000000..397f283 --- /dev/null +++ b/test/example/_templates/custom-template.html @@ -0,0 +1,12 @@ + + + + + {{ title }} + + + +

Custom Template

+ {{ content }} + + diff --git a/test/example/markdoc.yaml b/test/example/markdoc.yaml index 79ea2eb..3e5e23c 100644 --- a/test/example/markdoc.yaml +++ b/test/example/markdoc.yaml @@ -2,3 +2,8 @@ hide-prefix: "_" use-default-templates: no use-default-static: no 9d03e32c6cb1455388a170e3bb0c2030: 7010d5d5871143d089cb662cb540cbd5 + +custom-templates: + - + regexps: ["custom/.*" ] + template: "custom-template.html" diff --git a/test/example/wiki/custom/hello.md b/test/example/wiki/custom/hello.md new file mode 100644 index 0000000..5bacc1f --- /dev/null +++ b/test/example/wiki/custom/hello.md @@ -0,0 +1 @@ +# Hello custom. diff --git a/test/listing.doctest b/test/listing.doctest index 0d95d74..611a734 100644 --- a/test/listing.doctest +++ b/test/listing.doctest @@ -13,6 +13,7 @@ The wiki should now be built in the HTML root: >>> print '\n'.join(os.listdir(CONFIG.html_dir)) _list.html an_empty_file.html + custom example.css file1.html file2.html @@ -60,7 +61,8 @@ Now we can get the listing for the HTML root itself, just by passing the empty s 'size': 254, 'slug': 'file2', 'title': u'World'}], - 'sub_directories': [{'basename': 'subdir', 'href': '/subdir/'}]} + 'sub_directories': [{'basename': 'custom', 'href': '/custom/'}, + {'basename': 'subdir', 'href': '/subdir/'}]} We can also get the listings for subdirectories, by passing in their paths, relative to the HTML root: