Use relative URLs everywhere and remove 'base_url' setting

This commit is contained in:
Tom Christie
2014-01-25 15:37:37 +00:00
parent 4ef138da90
commit 0ff98aa982
6 changed files with 45 additions and 34 deletions

View File

@@ -44,7 +44,7 @@ def build_pages(config):
"""
Builds all the pages and writes them into the build directory.
"""
site_navigation = nav.SiteNavigation(config['pages'], config['base_url'] + '/')
site_navigation = nav.SiteNavigation(config['pages'])
loader = jinja2.FileSystemLoader(config['theme_dir'])
env = jinja2.Environment(loader=loader)
@@ -71,7 +71,7 @@ def build_pages(config):
'config': config,
'url': page.url,
'base_url': config['base_url'],
'base_url': site_navigation.url_context.make_relative('/'),
'homepage_url': site_navigation.homepage.url,
'previous_url': page.previous_page and page.previous_page.url,
'next_url': page.next_page and page.next_page.url,

View File

@@ -7,7 +7,6 @@ import yaml
DEFAULT_CONFIG = {
'project_name': None,
'pages': None,
'base_url': '',
'theme': 'bootstrap',
@@ -36,9 +35,4 @@ def load_config(filename='mkdocs.yaml', options=None):
package_dir = os.path.dirname(__file__)
config['theme_dir'] = os.path.join(package_dir, 'themes', config['theme'])
if config['local_files']:
build_dir = os.path.join(os.getcwd(), config['build_dir'])
build_path = build_dir.replace(os.path.pathsep, '/')
config['base_url'] = 'file://%s' % build_path
return config

View File

@@ -7,12 +7,14 @@ This consists of building a set of interlinked page and header objects.
"""
from mkdocs import utils
import posixpath
class SiteNavigation(object):
def __init__(self, pages_config, base_url='', local_file_urls=False):
def __init__(self, pages_config, local_file_urls=False):
self.url_context = URLContext()
self.nav_items, self.pages = \
_generate_site_navigation(pages_config, base_url, local_file_urls)
_generate_site_navigation(pages_config, self.url_context, local_file_urls)
self.homepage = self.pages[0] if self.pages else None
def __str__(self):
@@ -31,20 +33,34 @@ class SiteNavigation(object):
"""
page = self.homepage
page.set_active()
self.url_context.set_current_url(page.abs_url)
yield page
while page.next_page:
page.set_active(False)
page = page.next_page
page.set_active()
self.url_context.set_current_url(page.abs_url)
yield page
page.set_active(False)
class URLContext(object):
def __init__(self):
self.base_path = '/'
def set_current_url(self, current_url):
self.base_path = posixpath.dirname(current_url)
def make_relative(self, url):
return posixpath.relpath(url, start=self.base_path)
class Page(object):
def __init__(self, title, url, path):
def __init__(self, title, url, path, url_context):
self.title = title
self.url = url
self.abs_url = url
self.active = False
self.url_context = url_context
# Relative paths to the input markdown file and output html file.
self.input_path = path
@@ -55,13 +71,17 @@ class Page(object):
self.next_page = None
self.ancestors = []
@property
def url(self):
return self.url_context.make_relative(self.abs_url)
def __str__(self):
return self._indent_print()
def _indent_print(self, depth=0):
indent = ' ' * depth
active_marker = ' [*]' if self.active else ''
return '%s%s - %s%s\n' % (indent, self.title, self.url, active_marker)
return '%s%s - %s%s\n' % (indent, self.title, self.abs_url, active_marker)
def set_active(self, active=True):
self.active = active
@@ -86,7 +106,7 @@ class Header(object):
return ret
def _generate_site_navigation(pages_config, base_url='', local_file_urls=False):
def _generate_site_navigation(pages_config, url_context, local_file_urls=False):
"""
Returns a list of Page and Header instances that represent the
top level site navigation.
@@ -96,23 +116,23 @@ def _generate_site_navigation(pages_config, base_url='', local_file_urls=False):
previous = None
for path, title in pages_config:
url = utils.get_url_path(path, base_url, local_file_urls)
url = utils.get_url_path(path, local_file_urls)
title, sep, child_title = title.partition('/')
title = title.strip()
child_title = child_title.strip()
if not child_title:
# New top level page.
page = Page(title=title, url=url, path=path)
page = Page(title=title, url=url, path=path, url_context=url_context)
nav_items.append(page)
elif not nav_items or (nav_items[-1].title != title):
# New second level page.
page = Page(title=child_title, url=url, path=path)
page = Page(title=child_title, url=url, path=path, url_context=url_context)
header = Header(title=title, children=[page])
nav_items.append(header)
page.ancestors = [header]
else:
# Additional second level page.
page = Page(title=child_title, url=url, path=path)
page = Page(title=child_title, url=url, path=path, url_context=url_context)
header = nav_items[-1]
header.children.append(page)
page.ancestors = [header]

View File

@@ -73,7 +73,6 @@ def serve(config, options=None):
# Create a temporary build directory, and set some options to serve it
tempdir = tempfile.mkdtemp()
options['build_dir'] = tempdir
options['base_url'] = 'http://%s' % config['dev_addr']
# Perform the initial build
config = load_config(options=options)

View File

@@ -25,10 +25,10 @@ class UtilsTests(unittest.TestCase):
def test_url_path(self):
expected_results = {
'index.md': '',
'api-guide.md': 'api-guide/',
'api-guide/index.md': 'api-guide/',
'api-guide/testing.md': 'api-guide/testing/',
'index.md': '/',
'api-guide.md': '/api-guide/',
'api-guide/index.md': '/api-guide/',
'api-guide/testing.md': '/api-guide/testing/',
}
for file_path, expected_html_path in expected_results.items():
html_path = utils.get_url_path(file_path)
@@ -136,7 +136,7 @@ class SiteNavigationTests(unittest.TestCase):
Home - /
About - /about/
""")
site_navigation = nav.SiteNavigation(pages, base_url='/')
site_navigation = nav.SiteNavigation(pages)
self.assertEqual(str(site_navigation).strip(), expected)
self.assertEqual(len(site_navigation.nav_items), 2)
self.assertEqual(len(site_navigation.pages), 2)
@@ -160,7 +160,7 @@ class SiteNavigationTests(unittest.TestCase):
Release notes - /about/release-notes/
License - /about/license/
""")
site_navigation = nav.SiteNavigation(pages, base_url='/')
site_navigation = nav.SiteNavigation(pages)
self.assertEqual(str(site_navigation).strip(), expected)
self.assertEqual(len(site_navigation.nav_items), 3)
self.assertEqual(len(site_navigation.pages), 6)
@@ -180,7 +180,7 @@ class SiteNavigationTests(unittest.TestCase):
About - /about/ [*]
""")
]
site_navigation = nav.SiteNavigation(pages, base_url='/')
site_navigation = nav.SiteNavigation(pages)
for index, page in enumerate(site_navigation.walk_pages()):
self.assertEqual(str(site_navigation).strip(), expected[index])
@@ -255,7 +255,7 @@ class SiteNavigationTests(unittest.TestCase):
License - /about/license/ [*]
""")
]
site_navigation = nav.SiteNavigation(pages, base_url='/')
site_navigation = nav.SiteNavigation(pages)
for index, page in enumerate(site_navigation.walk_pages()):
self.assertEqual(str(site_navigation).strip(), expected[index])

View File

@@ -60,21 +60,19 @@ def get_html_path(path):
return os.path.join(path, 'index.html')
def get_url_path(path, base_url='', local_file_urls=False):
def get_url_path(path, local_file_urls=False):
"""
Map a source file path to an output html path.
Paths like 'index.md' will be converted to ''
Paths like 'about.md' will be converted to 'about/'
Paths like 'api-guide/core.md' will be converted to 'api-guide/core/'
If `base_url` is set, it will be prepended to the return result.
Paths like 'index.md' will be converted to '/'
Paths like 'about.md' will be converted to '/about/'
Paths like 'api-guide/core.md' will be converted to '/api-guide/core/'
If `local_file_urls` is `True`, returned URLs will include the a trailing
`index.html` rather than just returning the directory path.
"""
path = get_html_path(path)
url = base_url + path.replace(os.path.pathsep, '/')
url = '/' + path.replace(os.path.pathsep, '/')
if not local_file_urls:
return url[:-len('index.html')]
return url