Skip to content

Commit

Permalink
Implemented options for default URL styles
Browse files Browse the repository at this point in the history
  • Loading branch information
mitsuhiko committed Dec 19, 2015
1 parent 17ebf85 commit 93de7d9
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 12 deletions.
8 changes: 8 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ Changelog

These are all the changes in Lektor since the first public release.

1.0
---

Release date to be decided.

- Improved ghpages and rsync deployments.
- Implemented options for default URL styles.

0.96
----

Expand Down
9 changes: 2 additions & 7 deletions lektor/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from werkzeug.local import LocalStack, LocalProxy

from lektor.reporter import reporter
from lektor.utils import make_relative_url


_ctx_stack = LocalStack()
Expand Down Expand Up @@ -142,17 +141,13 @@ def base_url(self):
return self.source.url_path
return '/'

def url_to(self, path, alt=None, absolute=False, external=False):
def url_to(self, path, alt=None, absolute=None, external=None):
"""Returns a URL to another path."""
if self.source is None:
raise RuntimeError('Can only generate paths to other pages if '
'the context has a source document set.')
rv = self.source.url_to(path, alt=alt, absolute=True)
if absolute:
return rv
elif external:
return self.pad.make_absolute_url(rv)
return make_relative_url(self.base_url, rv)
return self.pad.make_url(rv, self.base_url, absolute, external)

def sub_artifact(self, *args, **kwargs):
"""Decorator version of :func:`add_sub_artifact`."""
Expand Down
23 changes: 23 additions & 0 deletions lektor/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from lektor.environment import PRIMARY_ALT
from lektor.databags import Databags
from lektor.filecontents import FileContents
from lektor.utils import make_relative_url


def _process_slug(slug, last_segment=False):
Expand Down Expand Up @@ -1150,6 +1151,28 @@ def make_absolute_url(self, url):
'the URL in the project config.')
return url_join(base_url.rstrip('/') + '/', url.lstrip('/'))

def make_url(self, url, base_url=None, absolute=None, external=None):
"""Helper method that creates a finalized URL based on the parameters
provided and the config.
"""
url_style = self.db.config.url_style
if absolute is None:
absolute = url_style == 'absolute'
if external is None:
external = url_style == 'external'
if external:
external_base_url = self.db.config.base_url
if external_base_url is None:
raise RuntimeError('To use absolute URLs you need to '
'configure the URL in the project config.')
return url_join(external_base_url, url.lstrip('/'))
if absolute:
return url_join(self.db.config.base_path, url.lstrip('/'))
if base_url is None:
raise RuntimeError('Cannot calculate a relative URL if no base '
'URL has been provided.')
return make_relative_url(base_url, url)

def resolve_url_path(self, url_path, include_invisible=False,
include_assets=True, alt_fallback=True):
"""Given a URL path this will find the correct record which also
Expand Down
27 changes: 27 additions & 0 deletions lektor/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

from inifile import IniFile

from werkzeug.utils import cached_property
from werkzeug.urls import url_parse

from lektor.utils import tojson_filter, secure_url, format_lat_long, \
bool_from_string
from lektor.i18n import get_i18n_block
Expand Down Expand Up @@ -58,6 +61,7 @@
'name': None,
'locale': 'en_US',
'url': None,
'url_style': 'relative',
},
'PACKAGES': {},
'ALTERNATIVES': {},
Expand Down Expand Up @@ -326,6 +330,29 @@ def primary_alternative(self):
"""The identifier that acts as primary alternative."""
return self.values['PRIMARY_ALTERNATIVE']

@cached_property
def base_url(self):
"""The external base URL."""
url = self.values['PROJECT'].get('url')
if url and url_parse(url).scheme:
return url.rstrip('/') + '/'

@cached_property
def base_path(self):
"""The base path of the URL."""
url = self.values['PROJECT'].get('url')
if url:
return url_parse(url).path.rstrip('/') + '/'
return '/'

@cached_property
def url_style(self):
"""The intended URL style."""
style = self.values['PROJECT'].get('url_style')
if style in ('relative', 'absolute', 'external'):
return style
return 'relative'


def lookup_from_bag(*args):
pieces = '.'.join(x for x in args if x)
Expand Down
8 changes: 3 additions & 5 deletions lektor/sourceobj.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from weakref import ref as weakref
from lektor.environment import PRIMARY_ALT
from lektor.utils import make_relative_url, cleanup_path
from lektor.utils import cleanup_path


class SourceObject(object):
Expand Down Expand Up @@ -78,7 +78,7 @@ def is_child_of(self, path, strict=False):
return this_path[:len(crumbs)] == crumbs and \
(not strict or len(this_path) > len(crumbs))

def url_to(self, path, alt=None, absolute=False, external=False):
def url_to(self, path, alt=None, absolute=None, external=None):
"""Calculates the URL from the current source object to the given
other source object. Alternatively a path can also be provided
instead of a source object. If the path starts with a leading
Expand All @@ -102,9 +102,7 @@ def url_to(self, path, alt=None, absolute=False, external=False):

if absolute:
return path
elif external:
return self.pad.make_absolute_url(path)
return make_relative_url(self.url_path, path)
return self.pad.make_url(path, self.url_path, absolute, external)


class VirtualSourceObject(SourceObject):
Expand Down

0 comments on commit 93de7d9

Please sign in to comment.