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

Commit

Permalink
Add an option to force absolute or relative urls.
Browse files Browse the repository at this point in the history
Set `relative_url: true` in the config file to force pages to not have a
leading `/` in urls, or `relative_url: false` (the default) to force
pages to have a leading `/` in their url.
  • Loading branch information
mythmon committed Jan 2, 2013
1 parent 50c9405 commit ab94aac
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 4 deletions.
1 change: 1 addition & 0 deletions docs/config
@@ -1,3 +1,4 @@
site_title: Wok
url_include_index: no
url_pattern: "/{category}/{slug}/{page}/index.html"
relative_urls: true
5 changes: 4 additions & 1 deletion docs/content/docs/config.mkd
Expand Up @@ -25,9 +25,12 @@ are:
like`/category/subcategory/foo.html`. To get "wordpress style" urls, you
could use `/{category}/{slug}/index.html`. For more information, please see
the [URL management page][URLs].
- `url_include_index` (yes) - If this option is turned off, then `index.*` on
- `url_include_index` (true) - If this option is turned off, then `index.*` on
the end of urls will be removed in templates. This will turn the url
`/docs/config/index.html` into `/docs/config/`.
- `relative_urls` (false) - If this option is turned on, then any urls
generated will not include a leading '/'. If this is false, all urls
generated will include a leading '/'.

[content]: /docs/content/
[URLs]: /docs/urls/
1 change: 1 addition & 0 deletions wok/engine.py
Expand Up @@ -28,6 +28,7 @@ class Engine(object):
'site_title': 'Some random Wok site',
'url_pattern': '/{category}/{slug}{page}.{ext}',
'url_include_index': True,
'relative_urls': False,
}
SITE_ROOT = os.getcwd()

Expand Down
15 changes: 12 additions & 3 deletions wok/page.py
Expand Up @@ -314,12 +314,17 @@ def build_meta(self):

# Get rid of extra slashes
self.meta['url'] = re.sub(r'//+', '/', self.meta['url'])
logging.debug('{0} will be written to {1}'
.format(self.meta['slug'], self.meta['url']))

# If we have been asked to, rip out any plain "index.html"s
if not self.options['url_include_index']:
self.meta['url'] = re.sub(r'/index\.html$', '/', self.meta['url'])

# Some urls should start with /, some should not.
if self.options['relative_urls'] and self.meta['url'][0] == '/':
self.meta['url'] = self.meta['url'][1:]
if not self.options['relative_urls'] and self.meta['url'][0] != '/':
self.meta['url'] = '/' + self.meta['url']

logging.debug('url is: ' + self.meta['url'])

# subpages
Expand Down Expand Up @@ -457,7 +462,10 @@ def write(self):

# Use what we are passed, or the default given, or the current dir
path = self.options.get('output_dir', '.')
path += self.meta['url']
url = self.meta['url']
if url and url[0] == '/':
url = url[1:]
path = os.path.join(path, url)
if path.endswith('/'):
path += 'index.' + self.meta['ext']

Expand All @@ -471,6 +479,7 @@ def write(self):
# about
logging.info('writing to {0}'.format(path))

logging.debug('Writing {0} to {1}'.format(self.meta['slug'], path))
f = open(path, 'w')
f.write(self.rendered.encode('utf-8'))
f.close()
Expand Down

0 comments on commit ab94aac

Please sign in to comment.