From aca72d841c7659c54d6b6439b64bf4a8868cad27 Mon Sep 17 00:00:00 2001
From: David Lopez
Date: Tue, 10 Sep 2013 11:19:19 +0200
Subject: [PATCH 1/5] custom templates initial commit
---
doc/.markdoc.yaml | 9 +++++++++
src/markdoc/builder.py | 11 +++++++++--
test/builder.doctest | 20 ++++++++++++++++++++
test/cli.doctest | 3 +++
test/example/_templates/custom-template.html | 12 ++++++++++++
test/example/markdoc.yaml | 5 +++++
test/example/wiki/custom/hello.md | 1 +
test/listing.doctest | 4 +++-
8 files changed, 62 insertions(+), 3 deletions(-)
create mode 100644 test/example/_templates/custom-template.html
create mode 100644 test/example/wiki/custom/hello.md
diff --git a/doc/.markdoc.yaml b/doc/.markdoc.yaml
index ea69257..d618f56 100644
--- a/doc/.markdoc.yaml
+++ b/doc/.markdoc.yaml
@@ -4,6 +4,15 @@ google-analytics: UA-9915287-4
wiki-dir: "."
static-dir: ".static"
+custom-templates:
+ -
+ regexps: ["/about.md","/tips/*.md" ]
+ template: "other1.html"
+ -
+ regexps: ["/index.md" ]
+ template: "index.html"
+
+
markdown:
extensions:
- codehilite
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..181e6cb 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': ['/wiki/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:
From 37b8aa4b8bd2e85a4dd9f853be9690bde1dc6b3c Mon Sep 17 00:00:00 2001
From: David Lopez
Date: Tue, 10 Sep 2013 11:39:23 +0200
Subject: [PATCH 2/5] reverted to config without templates
---
doc/.markdoc.yaml | 9 ---------
1 file changed, 9 deletions(-)
diff --git a/doc/.markdoc.yaml b/doc/.markdoc.yaml
index d618f56..ea69257 100644
--- a/doc/.markdoc.yaml
+++ b/doc/.markdoc.yaml
@@ -4,15 +4,6 @@ google-analytics: UA-9915287-4
wiki-dir: "."
static-dir: ".static"
-custom-templates:
- -
- regexps: ["/about.md","/tips/*.md" ]
- template: "other1.html"
- -
- regexps: ["/index.md" ]
- template: "index.html"
-
-
markdown:
extensions:
- codehilite
From 632dad52c30c8fbab331b7cbb0dbbf8434b7afc3 Mon Sep 17 00:00:00 2001
From: David Lopez
Date: Tue, 10 Sep 2013 11:39:43 +0200
Subject: [PATCH 3/5] fixed spaces alignment
---
test/cli.doctest | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/test/cli.doctest b/test/cli.doctest
index 181e6cb..4160d29 100644
--- a/test/cli.doctest
+++ b/test/cli.doctest
@@ -14,7 +14,7 @@ The `show-config` command will show the current Markdoc config:
>>> exit_code = markdoc('show-config') # doctest: +ELLIPSIS
{'9d03e32c6cb1455388a170e3bb0c2030': '7010d5d5871143d089cb662cb540cbd5',
- 'custom-templates': [{'regexps': ['/wiki/custom/.*'],
+ 'custom-templates': [{'regexps': ['custom/.*'],
'template': 'custom-template.html'}],
'hide-prefix': '_',
'meta.config-file': '.../example/markdoc.yaml',
From b59fe1a5503dd78908132ffe40e82daff17bf4b1 Mon Sep 17 00:00:00 2001
From: David Lopez
Date: Tue, 10 Sep 2013 11:40:14 +0200
Subject: [PATCH 4/5] a brief documentation about custom templates feature
---
doc/ref/configuration.md | 19 +++++++++++++++++++
1 file changed, 19 insertions(+)
diff --git a/doc/ref/configuration.md b/doc/ref/configuration.md
index 9a89fb5..f900c9d 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,16 @@ is an acceptable value.
[rsync-docs]: http://www.samba.org/ftp/rsync/rsync.html
+### Custom templates
+
+A *optional* list of custom templates. 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.
From 0c4c7ec62518765d6cc52f300f5278eaf291a30c Mon Sep 17 00:00:00 2001
From: David Lopez
Date: Tue, 10 Sep 2013 12:20:07 +0200
Subject: [PATCH 5/5] doc enhancement
---
doc/ref/configuration.md | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/doc/ref/configuration.md b/doc/ref/configuration.md
index f900c9d..694de70 100644
--- a/doc/ref/configuration.md
+++ b/doc/ref/configuration.md
@@ -133,7 +133,9 @@ is an acceptable value.
### Custom templates
-A *optional* list of custom templates. Each item of the list defines:
+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.