-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a file resolver to generate URLs.
- Loading branch information
Showing
5 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Copyright (c) 2015, Matt Layman | ||
|
||
import os | ||
|
||
|
||
class FileResolver(object): | ||
"""Resolve source files to their expected output location.""" | ||
|
||
def __init__(self, site_path, composers, config): | ||
self.site_path = site_path | ||
self.composers = composers | ||
self.config = config | ||
|
||
def as_url(self, source_path): | ||
"""Resolve the output URL of the provided source path.""" | ||
path = os.path.relpath(source_path, self.site_path) | ||
url_path = self._convert_to_url(path) | ||
return "/".join([self.config.domain, url_path]) | ||
|
||
def _convert_to_url(self, path): | ||
""""Convert the path to a URL path by swapping the extension.""" | ||
composer = self.composers.select_composer_for(path) | ||
root, ext = os.path.splitext(path) | ||
return root + composer.output_extension |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Copyright (c) 2015, Matt Layman | ||
|
||
import os | ||
|
||
from handroll.composers import Composers | ||
from handroll.resolver import FileResolver | ||
from handroll.tests import TestCase | ||
|
||
|
||
class TestFileResolver(TestCase): | ||
|
||
def test_as_url(self): | ||
site = self.factory.make_site() | ||
composers = Composers() | ||
config = self.factory.make_configuration() | ||
resolver = FileResolver(site.path, composers, config) | ||
md_file = os.path.join(site.path, 'a_dir', 'test.md') | ||
url = resolver.as_url(md_file) | ||
self.assertEqual('http://www.example.com/a_dir/test.html', url) |