Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/about/release-notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ and user created and third-party templates should be updated as outlined below:

### Other Changes and Additions to Version 1.0.0

* Internal refactor of Markdown processing (#713)
* Removed special error message for mkdocs-bootstrap and mkdocs-bootswatch
themes (#1168)
* The legacy pages config is no longer supported (#1168)
Expand Down
103 changes: 17 additions & 86 deletions mkdocs/commands/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

from mkdocs import nav, search, utils
from mkdocs.utils import filters
from mkdocs.relative_path_ext import RelativePathExtension
import mkdocs


Expand All @@ -31,40 +30,15 @@ def filter(self, record):
log.addFilter(DuplicateFilter())


def get_complete_paths(config, page):
"""
Return the complete input/output paths for the supplied page.
"""
input_path = os.path.join(config['docs_dir'], page.input_path)
output_path = os.path.join(config['site_dir'], page.output_path)
return input_path, output_path


def convert_markdown(markdown_source, config, site_navigation=None):
"""
Convert the Markdown source file to HTML as per the config and
site_navigation. Return a tuple of the HTML as a string, the parsed table
of contents, and a dictionary of any metadata that was specified in the
Markdown file.
"""

extensions = [
RelativePathExtension(site_navigation, config['strict'])
] + config['markdown_extensions']

return utils.convert_markdown(
markdown_source=markdown_source,
extensions=extensions,
extension_configs=config['mdx_configs']
)


def get_global_context(nav, config):
def get_context(nav, config, page=None):
"""
Given the SiteNavigation and config, generate the context which is relevant
to app pages.
"""

if nav is None:
return {'page', page}

extra_javascript = utils.create_media_urls(nav, config['extra_javascript'])

extra_css = utils.create_media_urls(nav, config['extra_css'])
Expand All @@ -85,27 +59,10 @@ def get_global_context(nav, config):
'build_date_utc': datetime.utcfromtimestamp(timestamp),

'config': config,
'page': page,
}


def get_page_context(page, content, toc, meta, config):
"""
Generate the page context by extending the global context and adding page
specific variables.
"""
if config['site_url']:
page.set_canonical_url(config['site_url'])

if config['repo_url']:
page.set_edit_url(config['repo_url'], config['edit_uri'])

page.content = content
page.toc = toc
page.meta = meta

return {'page': page}


def build_template(template_name, env, config, site_navigation=None):

log.debug("Building template: %s", template_name)
Expand All @@ -115,9 +72,7 @@ def build_template(template_name, env, config, site_navigation=None):
except TemplateNotFound:
return False

context = {'page': None}
if site_navigation is not None:
context.update(get_global_context(site_navigation, config))
context = get_context(site_navigation, config)

output_content = template.render(context)
output_path = os.path.join(config['site_dir'], template_name)
Expand All @@ -127,41 +82,23 @@ def build_template(template_name, env, config, site_navigation=None):

def _build_page(page, config, site_navigation, env, dirty=False):

# Get the input/output paths
input_path, output_path = get_complete_paths(config, page)

# Read the input file
try:
input_content = io.open(input_path, 'r', encoding='utf-8').read()
except IOError:
log.error('file not found: %s', input_path)
raise

# Process the markdown text
html_content, table_of_contents, meta = convert_markdown(
markdown_source=input_content,
config=config,
site_navigation=site_navigation
)
page.load_markdown()
page.render(config, site_navigation)

context = get_global_context(site_navigation, config)
context.update(get_page_context(
page, html_content, table_of_contents, meta, config
))
context = get_context(site_navigation, config, page)

# Allow 'template:' override in md source files.
if 'template' in meta:
template = env.get_template(meta['template'][0])
if 'template' in page.meta:
template = env.get_template(page.meta['template'])
else:
template = env.get_template('main.html')

# Render the template.
output_content = template.render(context)

# Write the output file.
utils.write_file(output_content.encode('utf-8'), output_path)

return html_content, table_of_contents, meta
utils.write_file(output_content.encode('utf-8'), page.abs_output_path)


def build_extra_templates(extra_templates, config, site_navigation=None):
Expand All @@ -175,9 +112,7 @@ def build_extra_templates(extra_templates, config, site_navigation=None):
with io.open(input_path, 'r', encoding='utf-8') as template_file:
template = jinja2.Template(template_file.read())

context = {'page': None}
if site_navigation is not None:
context.update(get_global_context(site_navigation, config))
context = get_context(site_navigation, config)

output_content = template.render(context)
output_path = os.path.join(config['site_dir'], extra_template)
Expand All @@ -188,7 +123,7 @@ def build_pages(config, dirty=False):
"""
Builds all the pages and writes them into the build directory.
"""
site_navigation = nav.SiteNavigation(config['pages'], config['use_directory_urls'])
site_navigation = nav.SiteNavigation(config)
loader = jinja2.FileSystemLoader(config['theme_dir'] + [config['mkdocs_templates'], ])
env = jinja2.Environment(loader=loader)

Expand Down Expand Up @@ -218,18 +153,14 @@ def build_pages(config, dirty=False):
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.
input_path, output_path = get_complete_paths(config, page)
if dirty and (utils.modified_time(input_path) < utils.modified_time(output_path)):
if dirty and (utils.modified_time(page.abs_input_path) < utils.modified_time(page.abs_output_path)):
continue

log.debug("Building page %s", page.input_path)
build_result = _build_page(page, config, site_navigation, env)
html_content, table_of_contents, _ = build_result
search_index.add_entry_from_context(
page, html_content, table_of_contents)
_build_page(page, config, site_navigation, env)
search_index.add_entry_from_context(page)
except Exception:
log.error("Error building page %s", page.input_path)
raise
Expand Down
2 changes: 1 addition & 1 deletion mkdocs/config/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@

# PyMarkdown extension names.
('markdown_extensions', config_options.MarkdownExtensions(
builtins=['meta', 'toc', 'tables', 'fenced_code'],
builtins=['toc', 'tables', 'fenced_code'],
configkey='mdx_configs', default=[])),

# PyMarkdown Extension Configs. For internal use only.
Expand Down
Loading