@@ -38,17 +38,14 @@ def media_url(context, path): | |||||
""" | """ | ||||
Returns the media url given a partial path. | Returns the media url given a partial path. | ||||
""" | """ | ||||
site = context['site'] | |||||
return Folder(site.config.media_url).child(path) | |||||
return context['site'].media_url(path) | |||||
@contextfunction | @contextfunction | ||||
def content_url(context, path): | def content_url(context, path): | ||||
""" | """ | ||||
Returns the content url given a partial path. | Returns the content url given a partial path. | ||||
""" | """ | ||||
site = context['site'] | |||||
return Folder(site.config.base_url).child(path) | |||||
return context['site'].content_url(path) | |||||
@contextfilter | @contextfilter | ||||
@@ -126,7 +126,7 @@ class Config(Expando): | |||||
media_root='media', | media_root='media', | ||||
layout_root='layout', | layout_root='layout', | ||||
media_url='/media', | media_url='/media', | ||||
site_url='/', | |||||
base_url="/", | |||||
not_found='404.html', | not_found='404.html', | ||||
plugins = [] | plugins = [] | ||||
) | ) | ||||
@@ -174,9 +174,9 @@ class Config(Expando): | |||||
@property | @property | ||||
def media_root_path(self): | def media_root_path(self): | ||||
""" | """ | ||||
Derives the media root path from the site path | |||||
Derives the media root path from the content path | |||||
""" | """ | ||||
return self.sitepath.child_folder(self.media_root) | |||||
return self.content_root_path.child_folder(self.media_root) | |||||
@property | @property | ||||
def layout_root_path(self): | def layout_root_path(self): | ||||
@@ -61,7 +61,7 @@ class Resource(Processable): | |||||
@property | @property | ||||
def relative_path(self): | def relative_path(self): | ||||
""" | """ | ||||
Gets the path relative to the root folder (Content, Media, Layout) | |||||
Gets the path relative to the root folder (Content) | |||||
""" | """ | ||||
return self.source_file.get_relative_path(self.node.root.source_folder) | return self.source_file.get_relative_path(self.node.root.source_folder) | ||||
@@ -90,6 +90,13 @@ class Resource(Processable): | |||||
relative_deploy_path = property(get_relative_deploy_path, set_relative_deploy_path) | relative_deploy_path = property(get_relative_deploy_path, set_relative_deploy_path) | ||||
url = relative_deploy_path | url = relative_deploy_path | ||||
@property | |||||
def full_url(self): | |||||
""" | |||||
Returns the full url for the resource. | |||||
""" | |||||
return self.site.full_url(self.relative_path) | |||||
class Node(Processable): | class Node(Processable): | ||||
""" | """ | ||||
Represents any folder that is processed by hyde | Represents any folder that is processed by hyde | ||||
@@ -188,7 +195,7 @@ class Node(Processable): | |||||
@property | @property | ||||
def full_url(self): | def full_url(self): | ||||
return self.site.config.base_url.rstrip('/') + self.url | |||||
return self.site.full_url(self.relative_path) | |||||
class RootNode(Node): | class RootNode(Node): | ||||
""" | """ | ||||
@@ -356,3 +363,37 @@ class Site(object): | |||||
""" | """ | ||||
self.content.load() | self.content.load() | ||||
def content_url(self, path): | |||||
""" | |||||
Returns the content url by appending the base url from the config | |||||
with the given path. | |||||
""" | |||||
return Folder(self.config.base_url).child(path) | |||||
def media_url(self, path): | |||||
""" | |||||
Returns the media url by appending the media base url from the config | |||||
with the given path. | |||||
""" | |||||
return Folder(self.config.media_url).child(path) | |||||
def full_url(self, path): | |||||
""" | |||||
Determines if the given path is media or content based on the | |||||
configuration and returns the appropriate url. | |||||
""" | |||||
if self.is_media(path): | |||||
return self.media_url( | |||||
FS(path).get_relative_path( | |||||
self.config.media_root_path)) | |||||
else: | |||||
return self.content_url(path) | |||||
def is_media(self, path): | |||||
""" | |||||
Given the relative path, determines if it is content or media. | |||||
""" | |||||
folder = self.content.source.child_folder(path) | |||||
return folder.is_descendant_of(self.config.media_root_path) |
@@ -84,7 +84,9 @@ class TestTagger(object): | |||||
q = PyQuery(File(tags_folder.child('sad.html')).read_all()) | q = PyQuery(File(tags_folder.child('sad.html')).read_all()) | ||||
assert q | assert q | ||||
print q | |||||
assert q('li').length == 2 | assert q('li').length == 2 | ||||
assert q('li a:first-child').attr('href') == '/blog/another-sad-post.html' | assert q('li a:first-child').attr('href') == '/blog/another-sad-post.html' | ||||
assert q('li a:eq(1)').attr('href') == '/blog/sad-post.html' | assert q('li a:eq(1)').attr('href') == '/blog/sad-post.html' | ||||
@@ -100,13 +100,14 @@ class TestConfig(object): | |||||
def test_default_configuration(self): | def test_default_configuration(self): | ||||
c = Config(sitepath=TEST_SITE, config_dict={}) | c = Config(sitepath=TEST_SITE, config_dict={}) | ||||
for root in ['content', 'layout', 'media']: | |||||
for root in ['content', 'layout']: | |||||
name = root + '_root' | name = root + '_root' | ||||
path = name + '_path' | path = name + '_path' | ||||
assert hasattr(c, name) | assert hasattr(c, name) | ||||
assert getattr(c, name) == root | assert getattr(c, name) == root | ||||
assert hasattr(c, path) | assert hasattr(c, path) | ||||
assert getattr(c, path) == TEST_SITE.child_folder(root) | assert getattr(c, path) == TEST_SITE.child_folder(root) | ||||
assert c.media_root_path == c.content_root_path.child_folder('media') | |||||
assert hasattr(c, 'plugins') | assert hasattr(c, 'plugins') | ||||
assert len(c.plugins) == 0 | assert len(c.plugins) == 0 | ||||
assert c.deploy_root_path == TEST_SITE.child_folder('deploy') | assert c.deploy_root_path == TEST_SITE.child_folder('deploy') | ||||
@@ -119,7 +120,7 @@ class TestConfig(object): | |||||
def test_conf2(self): | def test_conf2(self): | ||||
c = Config(sitepath=TEST_SITE, config_dict=yaml.load(self.conf2)) | c = Config(sitepath=TEST_SITE, config_dict=yaml.load(self.conf2)) | ||||
assert c.content_root_path == TEST_SITE.child_folder('site/stuff') | assert c.content_root_path == TEST_SITE.child_folder('site/stuff') | ||||
assert c.media_root_path == TEST_SITE.child_folder('mmm') | |||||
assert c.media_root_path == c.content_root_path.child_folder('mmm') | |||||
assert c.media_url == TEST_SITE.child_folder('/media') | assert c.media_url == TEST_SITE.child_folder('/media') | ||||
assert c.deploy_root_path == Folder('~/deploy_site') | assert c.deploy_root_path == Folder('~/deploy_site') | ||||
@@ -127,7 +128,7 @@ class TestConfig(object): | |||||
File(TEST_SITE.child('site.yaml')).write(self.conf2) | File(TEST_SITE.child('site.yaml')).write(self.conf2) | ||||
c = Config(sitepath=TEST_SITE) | c = Config(sitepath=TEST_SITE) | ||||
assert c.content_root_path == TEST_SITE.child_folder('site/stuff') | assert c.content_root_path == TEST_SITE.child_folder('site/stuff') | ||||
assert c.media_root_path == TEST_SITE.child_folder('mmm') | |||||
assert c.media_root_path == c.content_root_path.child_folder('mmm') | |||||
assert c.media_url == TEST_SITE.child_folder('/media') | assert c.media_url == TEST_SITE.child_folder('/media') | ||||
assert c.deploy_root_path == Folder('~/deploy_site') | assert c.deploy_root_path == Folder('~/deploy_site') | ||||
@@ -135,7 +136,7 @@ class TestConfig(object): | |||||
File(TEST_SITE.child('another.yaml')).write(self.conf2) | File(TEST_SITE.child('another.yaml')).write(self.conf2) | ||||
c = Config(sitepath=TEST_SITE, config_file='another.yaml') | c = Config(sitepath=TEST_SITE, config_file='another.yaml') | ||||
assert c.content_root_path == TEST_SITE.child_folder('site/stuff') | assert c.content_root_path == TEST_SITE.child_folder('site/stuff') | ||||
assert c.media_root_path == TEST_SITE.child_folder('mmm') | |||||
assert c.media_root_path == c.content_root_path.child_folder('mmm') | |||||
assert c.media_url == TEST_SITE.child_folder('/media') | assert c.media_url == TEST_SITE.child_folder('/media') | ||||
assert c.deploy_root_path == Folder('~/deploy_site') | assert c.deploy_root_path == Folder('~/deploy_site') | ||||
@@ -150,6 +151,6 @@ class TestConfig(object): | |||||
c = Config(sitepath=TEST_SITE, config_file='another.yaml') | c = Config(sitepath=TEST_SITE, config_file='another.yaml') | ||||
assert c.mode == 'production' | assert c.mode == 'production' | ||||
assert c.content_root_path == TEST_SITE.child_folder('site/stuff') | assert c.content_root_path == TEST_SITE.child_folder('site/stuff') | ||||
assert c.media_root_path == TEST_SITE.child_folder('xxx') | |||||
assert c.media_root_path == c.content_root_path.child_folder('xxx') | |||||
assert c.media_url == TEST_SITE.child_folder('/media') | assert c.media_url == TEST_SITE.child_folder('/media') | ||||
assert c.deploy_root_path == Folder('~/deploy_site') | assert c.deploy_root_path == Folder('~/deploy_site') |
@@ -178,7 +178,7 @@ class TestSiteWithConfig(object): | |||||
cls.SITE_PATH.delete() | cls.SITE_PATH.delete() | ||||
def test_load_with_config(self): | def test_load_with_config(self): | ||||
s = Site(self.SITE_PATH, config = self.config) | |||||
s = Site(self.SITE_PATH, config=self.config) | |||||
s.load() | s.load() | ||||
path = 'blog/2010/december' | path = 'blog/2010/december' | ||||
node = s.content.node_from_relative_path(path) | node = s.content.node_from_relative_path(path) | ||||
@@ -188,4 +188,34 @@ class TestSiteWithConfig(object): | |||||
resource = s.content.resource_from_relative_path(path) | resource = s.content.resource_from_relative_path(path) | ||||
assert resource | assert resource | ||||
assert resource.relative_path == path | assert resource.relative_path == path | ||||
assert not s.content.resource_from_relative_path('/happy-festivus.html') | |||||
assert not s.content.resource_from_relative_path('/happy-festivus.html') | |||||
def test_content_url(self): | |||||
s = Site(self.SITE_PATH, config=self.config) | |||||
s.load() | |||||
path = 'blog/2010/december' | |||||
assert s.content_url(path) == "/" + path | |||||
def test_media_url(self): | |||||
s = Site(self.SITE_PATH, config=self.config) | |||||
s.load() | |||||
path = 'css/site.css' | |||||
assert s.media_url(path) == "/media/" + path | |||||
def test_is_media(self): | |||||
s = Site(self.SITE_PATH, config=self.config) | |||||
s.load() | |||||
assert s.is_media('media/css/site.css') | |||||
s.config.media_root = 'monkey' | |||||
assert not s.is_media('media/css/site.css') | |||||
assert s.is_media('monkey/css/site.css') | |||||
def test_media_url_from_resource(self): | |||||
s = Site(self.SITE_PATH, config=self.config) | |||||
s.load() | |||||
path = 'css/site.css' | |||||
resource = s.content.resource_from_relative_path( | |||||
Folder("media").child(path)) | |||||
assert resource | |||||
assert resource.full_url == "/media/" + path |