Skip to content
This repository has been archived by the owner on Jan 25, 2019. It is now read-only.

Commit

Permalink
Add page.url field.
Browse files Browse the repository at this point in the history
  • Loading branch information
mythmon authored and Mike Cooper committed May 17, 2011
1 parent 64e48e9 commit 85e5ea9
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
1 change: 1 addition & 0 deletions sample/content/tests.mkd
@@ -1,5 +1,6 @@
title: Tests Main
slug: tests
type: index
url: /index.html
---
These are the tests
2 changes: 1 addition & 1 deletion sample/templates/index.html
Expand Up @@ -7,7 +7,7 @@ <h1>{{page.title}}</h1>
<hr>

{% for subpage in page.subpages %}
<h2>{{subpage.title}}</h2>
<a href="{{subpage.url}}"><h2>{{subpage.title}}</h2></a>
{{subpage.content}}
{% endfor %}

Expand Down
31 changes: 21 additions & 10 deletions wok/page.py
Expand Up @@ -90,6 +90,7 @@ def build_meta(self):
`page.published` - will exist.
`page.datetime` - will be a datetime.
`page.tags` - will be a list.
`page.url` - will be the url of the page, relative to the web root.
"""

if self.meta is None:
Expand Down Expand Up @@ -125,6 +126,8 @@ def build_meta(self):
self.meta['category'] = self.meta['category'].split('/')
else:
self.meta['category'] = []
if self.meta['category'] == None:
self.meta = []
# Guarantee: category exists, is a list

if not 'published' in self.meta:
Expand All @@ -147,6 +150,15 @@ def build_meta(self):
format(self.slug, self.meta['tags']))
# Guarantee: tags exists, is a list

if not 'url' in self.meta:
# /category/subcategory/slug.html
util.out.debug('building the url', self.categories)
self.meta['url'] = '/'
for cat in self.category:
self.meta['url']= os.path.join(self.meta['url'], cat)
self.meta['url'] = os.path.join(self.meta['url'],
self.slug + '.html')

def render(self, templ_vars=None):
"""
Renders the page to full html with the template engine.
Expand All @@ -161,22 +173,21 @@ def render(self, templ_vars=None):
})
self.html = template.render(templ_vars)

def write(self, path=None):
def write(self):
"""Write the page to an html file on disk."""

# Use what we are passed, or the default given, or the current dir
if not path:
path = self.options.get('output_dir', '.')
for cat in self.category:
path = os.path.join(path, cat)
path = self.options.get('output_dir', '.')
path += self.url

try:
os.makedirs(path)
os.makedirs(os.path.dirname(path))
except OSError as e:
pass
# probably that the dir already exists, so thats ok.

path = os.path.join(path, self.slug + '.html')
util.out.debug('writing files', 'makedirs failed for {0}'.format(
os.path.basename(path)))
# Probably that the dir already exists, so thats ok.
# TODO: double check this. Permission errors are something to worry
# about

f = open(path, 'w')
f.write(self.html)
Expand Down

0 comments on commit 85e5ea9

Please sign in to comment.