Skip to content

Commit

Permalink
Add a file resolver to generate URLs.
Browse files Browse the repository at this point in the history
  • Loading branch information
mblayman committed Jun 28, 2015
1 parent 8398cd6 commit a35d928
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 0 deletions.
11 changes: 11 additions & 0 deletions handroll/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ def __init__(self):
# Keep the parser to allow extensions to get configuration file data.
self.parser = ConfigParser()

self._domain = None

@property
def domain(self):
if self._domain is None:
raise AbortError(
_('You are missing a domain setting in the site section.'))
return self._domain

def load_from_arguments(self, args):
"""Load any configuration attributes from the provided command line
arguments. Arguments have the highest precedent so overwrite any other
Expand All @@ -37,6 +46,8 @@ def load_from_file(self, config_file):
"""Load any configuration attributes from the provided config file."""
with open(config_file, 'r') as f:
self.parser.readfp(f)
if self.parser.has_option('site', 'domain'):
self._domain = self.parser.get('site', 'domain')
if self.parser.has_option('site', 'outdir'):
self.outdir = os.path.abspath(os.path.expanduser(
self.parser.get('site', 'outdir')))
Expand Down
24 changes: 24 additions & 0 deletions handroll/resolver.py
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
5 changes: 5 additions & 0 deletions handroll/tests/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ def make_blog_post(self):
}
return BlogPost(**kwargs)

def make_configuration(self):
config = Configuration()
config._domain = 'http://www.example.com'
return config

def make_director(self):
config = Configuration()
site = self.make_site()
Expand Down
16 changes: 16 additions & 0 deletions handroll/tests/test_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,19 @@ def test_no_boolean_extension(self):
f.write(conf_file.encode('utf-8'))

self.assertRaises(AbortError, configuration.build_config, f.name, args)

def test_no_domain_aborts(self):
config = configuration.Configuration()
self.assertRaises(AbortError, lambda: config.domain)

def test_loads_domain_from_site(self):
conf_file = inspect.cleandoc(
"""[site]
domain = a_fake_domain""")
args = FakeArgs()
with tempfile.NamedTemporaryFile(delete=False) as f:
f.write(conf_file.encode('utf-8'))

config = configuration.build_config(f.name, args)

self.assertEqual('a_fake_domain', config.domain)
19 changes: 19 additions & 0 deletions handroll/tests/test_resolver.py
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)

0 comments on commit a35d928

Please sign in to comment.