Skip to content

Multiple templates #30

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
21 changes: 21 additions & 0 deletions doc/ref/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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.
Expand Down
11 changes: 9 additions & 2 deletions src/markdoc/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
20 changes: 20 additions & 0 deletions test/builder.doctest
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -117,3 +118,22 @@ You can render whole documents using `Builder.render_document()`:
</html>

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')
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Hello custom.</title>
<link rel="stylesheet" type="text/css" href="/other.css" />
</head>
<body>
<h1>Custom Template</h1>
<h1>Hello custom.</h1>
</body>
</html>
3 changes: 3 additions & 0 deletions test/cli.doctest
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions test/example/_templates/custom-template.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>{{ title }}</title>
<link rel="stylesheet" type="text/css" href="/other.css" />
</head>
<body>
<h1>Custom Template</h1>
{{ content }}
</body>
</html>
5 changes: 5 additions & 0 deletions test/example/markdoc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,8 @@ hide-prefix: "_"
use-default-templates: no
use-default-static: no
9d03e32c6cb1455388a170e3bb0c2030: 7010d5d5871143d089cb662cb540cbd5

custom-templates:
-
regexps: ["custom/.*" ]
template: "custom-template.html"
1 change: 1 addition & 0 deletions test/example/wiki/custom/hello.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Hello custom.
4 changes: 3 additions & 1 deletion test/listing.doctest
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:

Expand Down