Some build.py refactoring and cleanup."

This commit is contained in:
Waylan Limberg
2017-03-27 19:42:43 -04:00
parent b9af8b1fba
commit 7985c72c2e

View File

@@ -64,6 +64,7 @@ def get_context(nav, config, page=None):
def build_template(template_name, env, config, site_navigation=None):
""" Build a template using the theme environment. """
log.debug("Building template: %s", template_name)
@@ -80,7 +81,29 @@ def build_template(template_name, env, config, site_navigation=None):
return True
def build_error_templates(templates, env, config, site_navigation):
"""
Build error templates.
Force absolute URLs in the nav of error pages and account for the
possability that the docs root might be different than the server root.
See https://github.com/mkdocs/mkdocs/issues/77
"""
site_navigation.url_context.force_abs_urls = True
default_base = site_navigation.url_context.base_path
site_navigation.url_context.base_path = utils.urlparse(config['site_url']).path
for template in templates:
build_template(template, env, config, site_navigation)
# Reset nav behavior to the default
site_navigation.url_context.force_abs_urls = False
site_navigation.url_context.base_path = default_base
def _build_page(page, config, site_navigation, env, dirty=False):
""" Build a Markdown page and pass to theme template. """
# Process the markdown text
page.load_markdown()
@@ -102,8 +125,9 @@ def _build_page(page, config, site_navigation, env, dirty=False):
def build_extra_templates(extra_templates, config, site_navigation=None):
""" Build user templates which are not part of the theme. """
log.debug("Building extra_templates page")
log.debug("Building extra_templates pages")
for extra_template in extra_templates:
@@ -120,9 +144,8 @@ def build_extra_templates(extra_templates, config, site_navigation=None):
def build_pages(config, dirty=False):
"""
Builds all the pages and writes them into the build directory.
"""
""" Build all pages and write them into the build directory. """
site_navigation = nav.SiteNavigation(config)
loader = jinja2.FileSystemLoader(config['theme_dir'] + [config['mkdocs_templates'], ])
env = jinja2.Environment(loader=loader)
@@ -130,16 +153,7 @@ def build_pages(config, dirty=False):
env.filters['tojson'] = filters.tojson
search_index = search.SearchIndex()
# Force absolute URLs in the nav of error pages and account for the
# possability that the docs root might be different than the server root.
# See https://github.com/mkdocs/mkdocs/issues/77
site_navigation.url_context.force_abs_urls = True
default_base = site_navigation.url_context.base_path
site_navigation.url_context.base_path = utils.urlparse(config['site_url']).path
build_template('404.html', env, config, site_navigation)
# Reset nav behavior to the default
site_navigation.url_context.force_abs_urls = False
site_navigation.url_context.base_path = default_base
build_error_templates(['404.html'], env, config, site_navigation)
if not build_template('search.html', env, config, site_navigation):
log.debug("Search is enabled but the theme doesn't contain a "
@@ -151,7 +165,6 @@ def build_pages(config, dirty=False):
build_extra_templates(config['extra_templates'], config, site_navigation)
for page in site_navigation.walk_pages():
try:
# When --dirty is used, only build the page if the markdown has been modified since the
# previous build of the output.
@@ -171,9 +184,8 @@ def build_pages(config, dirty=False):
def build(config, live_server=False, dirty=False):
"""
Perform a full site build.
"""
""" Perform a full site build. """
if not dirty:
log.info("Cleaning site directory")
utils.clean_directory(config['site_dir'])
@@ -204,13 +216,6 @@ def build(config, live_server=False, dirty=False):
def site_directory_contains_stale_files(site_directory):
"""
Check if the site directory contains stale files from a previous build.
Right now the check returns true if the directory is not empty.
A more sophisticated approach should be found to trigger only if there are
files that won't be overwritten anyway.
"""
if os.path.exists(site_directory):
if os.listdir(site_directory):
return True
return False
""" Check if the site directory contains stale files from a previous build. """
return True if os.path.exists(site_directory) and os.listdir(site_directory) else False