| @@ -38,17 +38,14 @@ def media_url(context, 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 | |||
| def content_url(context, 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 | |||
| @@ -126,7 +126,7 @@ class Config(Expando): | |||
| media_root='media', | |||
| layout_root='layout', | |||
| media_url='/media', | |||
| site_url='/', | |||
| base_url="/", | |||
| not_found='404.html', | |||
| plugins = [] | |||
| ) | |||
| @@ -174,9 +174,9 @@ class Config(Expando): | |||
| @property | |||
| 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 | |||
| def layout_root_path(self): | |||
| @@ -61,7 +61,7 @@ class Resource(Processable): | |||
| @property | |||
| 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) | |||
| @@ -90,6 +90,13 @@ class Resource(Processable): | |||
| relative_deploy_path = property(get_relative_deploy_path, set_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): | |||
| """ | |||
| Represents any folder that is processed by hyde | |||
| @@ -188,7 +195,7 @@ class Node(Processable): | |||
| @property | |||
| def full_url(self): | |||
| return self.site.config.base_url.rstrip('/') + self.url | |||
| return self.site.full_url(self.relative_path) | |||
| class RootNode(Node): | |||
| """ | |||
| @@ -356,3 +363,37 @@ class Site(object): | |||
| """ | |||
| 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()) | |||
| assert q | |||
| print q | |||
| assert q('li').length == 2 | |||
| 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' | |||
| @@ -100,13 +100,14 @@ class TestConfig(object): | |||
| def test_default_configuration(self): | |||
| c = Config(sitepath=TEST_SITE, config_dict={}) | |||
| for root in ['content', 'layout', 'media']: | |||
| for root in ['content', 'layout']: | |||
| name = root + '_root' | |||
| path = name + '_path' | |||
| assert hasattr(c, name) | |||
| assert getattr(c, name) == root | |||
| assert hasattr(c, path) | |||
| 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 len(c.plugins) == 0 | |||
| assert c.deploy_root_path == TEST_SITE.child_folder('deploy') | |||
| @@ -119,7 +120,7 @@ class TestConfig(object): | |||
| def test_conf2(self): | |||
| c = Config(sitepath=TEST_SITE, config_dict=yaml.load(self.conf2)) | |||
| 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.deploy_root_path == Folder('~/deploy_site') | |||
| @@ -127,7 +128,7 @@ class TestConfig(object): | |||
| File(TEST_SITE.child('site.yaml')).write(self.conf2) | |||
| c = Config(sitepath=TEST_SITE) | |||
| 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.deploy_root_path == Folder('~/deploy_site') | |||
| @@ -135,7 +136,7 @@ class TestConfig(object): | |||
| File(TEST_SITE.child('another.yaml')).write(self.conf2) | |||
| c = Config(sitepath=TEST_SITE, config_file='another.yaml') | |||
| 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.deploy_root_path == Folder('~/deploy_site') | |||
| @@ -150,6 +151,6 @@ class TestConfig(object): | |||
| c = Config(sitepath=TEST_SITE, config_file='another.yaml') | |||
| assert c.mode == 'production' | |||
| 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.deploy_root_path == Folder('~/deploy_site') | |||
| @@ -178,7 +178,7 @@ class TestSiteWithConfig(object): | |||
| cls.SITE_PATH.delete() | |||
| def test_load_with_config(self): | |||
| s = Site(self.SITE_PATH, config = self.config) | |||
| s = Site(self.SITE_PATH, config=self.config) | |||
| s.load() | |||
| path = 'blog/2010/december' | |||
| node = s.content.node_from_relative_path(path) | |||
| @@ -188,4 +188,34 @@ class TestSiteWithConfig(object): | |||
| resource = s.content.resource_from_relative_path(path) | |||
| assert resource | |||
| 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 | |||