Permalink
Browse files

Add an option to force absolute or relative urls.

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...
1 parent 50c9405 commit ab94aacac4e72839db86ab26d76001ef17b0c7ef @mythmon committed Jan 2, 2013
Showing with 18 additions and 4 deletions.
  1. +1 −0 docs/config
  2. +4 −1 docs/content/docs/config.mkd
  3. +1 −0 wok/engine.py
  4. +12 −3 wok/page.py
View
@@ -1,3 +1,4 @@
site_title: Wok
url_include_index: no
url_pattern: "/{category}/{slug}/{page}/index.html"
+relative_urls: true
@@ -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/
View
@@ -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()
View
@@ -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
@@ -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']
@@ -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()

0 comments on commit ab94aac

Please sign in to comment.