<s> from __future__ import unicode_literals <EOL> from datetime import datetime <EOL> import io <EOL> import logging <EOL> import os <EOL> from jinja2 . exceptions import TemplateNotFound <EOL> import jinja2 <EOL> import json <EOL> from mkdocs import nav , search , utils <EOL> from mkdocs . utils import filters <EOL> from mkdocs . relative_path_ext import RelativePathExtension <EOL> import mkdocs <EOL> log = logging . getLogger ( __name__ ) <EOL> def convert_markdown ( markdown_source , config , site_navigation = None ) : <EOL> """<STR_LIT>""" <EOL> extensions = [ <EOL> RelativePathExtension ( site_navigation , config [ '<STR_LIT:strict>' ] ) <EOL> ] + config [ '<STR_LIT>' ] <EOL> return utils . convert_markdown ( <EOL> markdown_source = markdown_source , <EOL> extensions = extensions , <EOL> extension_configs = config [ '<STR_LIT>' ] <EOL> ) <EOL> def get_global_context ( nav , config ) : <EOL> """<STR_LIT>""" <EOL> site_name = config [ '<STR_LIT>' ] <EOL> if config [ '<STR_LIT>' ] : <EOL> site_favicon = nav . url_context . make_relative ( '<STR_LIT:/>' + config [ '<STR_LIT>' ] ) <EOL> else : <EOL> site_favicon = None <EOL> page_description = config [ '<STR_LIT>' ] <EOL> extra_javascript = utils . create_media_urls ( nav , config [ '<STR_LIT>' ] ) <EOL> extra_css = utils . create_media_urls ( nav , config [ '<STR_LIT>' ] ) <EOL> return { <EOL> '<STR_LIT>' : site_name , <EOL> '<STR_LIT>' : config [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : site_favicon , <EOL> '<STR_LIT>' : page_description , <EOL> '<STR_LIT>' : config [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : config [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : nav , <EOL> '<STR_LIT>' : nav . url_context . make_relative ( '<STR_LIT:/>' ) , <EOL> '<STR_LIT>' : nav . homepage . url , <EOL> '<STR_LIT>' : config [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : extra_css , <EOL> '<STR_LIT>' : extra_javascript , <EOL> '<STR_LIT>' : config [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : config [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : config [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : config [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : mkdocs . __version__ , <EOL> '<STR_LIT>' : datetime . utcnow ( ) , <EOL> '<STR_LIT>' : config <EOL> } <EOL> def get_page_context ( page , content , toc , meta , config ) : <EOL> """<STR_LIT>""" <EOL> if page . is_homepage or page . title is None : <EOL> page_title = None <EOL> else : <EOL> page_title = page . title <EOL> if page . is_homepage : <EOL> page_description = config [ '<STR_LIT>' ] <EOL> else : <EOL> page_description = None <EOL> if config [ '<STR_LIT>' ] : <EOL> base = config [ '<STR_LIT>' ] <EOL> if not base . endswith ( '<STR_LIT:/>' ) : <EOL> base += '<STR_LIT:/>' <EOL> canonical_url = utils . urljoin ( <EOL> base , page . abs_url . lstrip ( '<STR_LIT:/>' ) ) <EOL> else : <EOL> canonical_url = None <EOL> return { <EOL> '<STR_LIT>' : page_title , <EOL> '<STR_LIT>' : page_description , <EOL> '<STR_LIT:content>' : content , <EOL> '<STR_LIT>' : toc , <EOL> '<STR_LIT>' : meta , <EOL> '<STR_LIT>' : canonical_url , <EOL> '<STR_LIT>' : page , <EOL> '<STR_LIT>' : page . previous_page , <EOL> '<STR_LIT>' : page . next_page <EOL> } <EOL> def build_template ( template_name , env , config , site_navigation = None ) : <EOL> log . debug ( "<STR_LIT>" , template_name ) <EOL> try : <EOL> template = env . get_template ( template_name ) <EOL> except TemplateNotFound : <EOL> return False <EOL> if site_navigation is not None : <EOL> context = get_global_context ( site_navigation , config ) <EOL> else : <EOL> context = { } <EOL> output_content = template . render ( context ) <EOL> output_path = os . path . join ( config [ '<STR_LIT>' ] , template_name ) <EOL> utils . write_file ( output_content . encode ( '<STR_LIT:utf-8>' ) , output_path ) <EOL> return True <EOL> def _build_page ( page , config , site_navigation , env , dump_json ) : <EOL> input_path = os . path . join ( config [ '<STR_LIT>' ] , page . input_path ) <EOL> try : <EOL> input_content = io . open ( input_path , '<STR_LIT:r>' , encoding = '<STR_LIT:utf-8>' ) . read ( ) <EOL> except IOError : <EOL> log . error ( '<STR_LIT>' , input_path ) <EOL> raise <EOL> html_content , table_of_contents , meta = convert_markdown ( <EOL> markdown_source = input_content , <EOL> config = config , <EOL> site_navigation = site_navigation <EOL> ) <EOL> context = get_global_context ( site_navigation , config ) <EOL> context . update ( get_page_context ( <EOL> page , html_content , table_of_contents , meta , config <EOL> ) ) <EOL> if '<STR_LIT>' in meta : <EOL> template = env . get_template ( meta [ '<STR_LIT>' ] [ <NUM_LIT:0> ] ) <EOL> else : <EOL> template = env . get_template ( '<STR_LIT>' ) <EOL> output_content = template . render ( context ) <EOL> output_path = os . path . join ( config [ '<STR_LIT>' ] , page . output_path ) <EOL> if dump_json : <EOL> json_context = { <EOL> '<STR_LIT:content>' : context [ '<STR_LIT:content>' ] , <EOL> '<STR_LIT:title>' : context [ '<STR_LIT>' ] . title , <EOL> '<STR_LIT:url>' : context [ '<STR_LIT>' ] . abs_url , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> json_output = json . dumps ( json_context , indent = <NUM_LIT:4> ) . encode ( '<STR_LIT:utf-8>' ) <EOL> utils . write_file ( json_output , output_path . replace ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> else : <EOL> utils . write_file ( output_content . encode ( '<STR_LIT:utf-8>' ) , output_path ) <EOL> return html_content , table_of_contents , meta <EOL> def build_extra_templates ( extra_templates , config , site_navigation = None ) : <EOL> log . debug ( "<STR_LIT>" ) <EOL> for extra_template in extra_templates : <EOL> input_path = os . path . join ( config [ '<STR_LIT>' ] , extra_template ) <EOL> with io . open ( input_path , '<STR_LIT:r>' , encoding = '<STR_LIT:utf-8>' ) as template_file : <EOL> template = jinja2 . Template ( template_file . read ( ) ) <EOL> if site_navigation is not None : <EOL> context = get_global_context ( site_navigation , config ) <EOL> else : <EOL> context = { } <EOL> output_content = template . render ( context ) <EOL> output_path = os . path . join ( config [ '<STR_LIT>' ] , extra_template ) <EOL> utils . write_file ( output_content . encode ( '<STR_LIT:utf-8>' ) , output_path ) <EOL> def build_pages ( config , dump_json = False ) : <EOL> """<STR_LIT>""" <EOL> site_navigation = nav . SiteNavigation ( config [ '<STR_LIT>' ] , config [ '<STR_LIT>' ] ) <EOL> loader = jinja2 . FileSystemLoader ( config [ '<STR_LIT>' ] + [ config [ '<STR_LIT>' ] , ] ) <EOL> env = jinja2 . Environment ( loader = loader ) <EOL> env . filters [ '<STR_LIT>' ] = filters . tojson <EOL> search_index = search . SearchIndex ( ) <EOL> build_template ( '<STR_LIT>' , env , config , site_navigation ) <EOL> if not build_template ( '<STR_LIT>' , env , config , site_navigation ) : <EOL> log . debug ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> build_template ( '<STR_LIT>' , env , config , site_navigation ) <EOL> build_extra_templates ( config [ '<STR_LIT>' ] , config , site_navigation ) <EOL> for page in site_navigation . walk_pages ( ) : <EOL> try : <EOL> log . debug ( "<STR_LIT>" , page . input_path ) <EOL> build_result = _build_page ( page , config , site_navigation , env , <EOL> dump_json ) <EOL> html_content , table_of_contents , _ = build_result <EOL> search_index . add_entry_from_context ( <EOL> page , html_content , table_of_contents ) <EOL> except Exception : <EOL> log . error ( "<STR_LIT>" , page . input_path ) <EOL> raise <EOL> search_index = search_index . generate_search_index ( ) <EOL> json_output_path = os . path . join ( config [ '<STR_LIT>' ] , '<STR_LIT>' , '<STR_LIT>' ) <EOL> utils . write_file ( search_index . encode ( '<STR_LIT:utf-8>' ) , json_output_path ) <EOL> def build ( config , live_server = False , dump_json = False , clean_site_dir = False ) : <EOL> """<STR_LIT>""" <EOL> if clean_site_dir : <EOL> log . info ( "<STR_LIT>" ) <EOL> utils . clean_directory ( config [ '<STR_LIT>' ] ) <EOL> if not live_server : <EOL> log . info ( "<STR_LIT>" , config [ '<STR_LIT>' ] ) <EOL> if not clean_site_dir and site_directory_contains_stale_files ( config [ '<STR_LIT>' ] ) : <EOL> log . info ( "<STR_LIT>" ) <EOL> if dump_json : <EOL> build_pages ( config , dump_json = True ) <EOL> return <EOL> for theme_dir in reversed ( config [ '<STR_LIT>' ] ) : <EOL> log . debug ( "<STR_LIT>" , theme_dir ) <EOL> utils . copy_media_files ( <EOL> theme_dir , config [ '<STR_LIT>' ] , exclude = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> ) <EOL> log . debug ( "<STR_LIT>" ) <EOL> utils . copy_media_files ( config [ '<STR_LIT>' ] , config [ '<STR_LIT>' ] ) <EOL> log . debug ( "<STR_LIT>" ) <EOL> build_pages ( config ) <EOL> def site_directory_contains_stale_files ( site_directory ) : <EOL> """<STR_LIT>""" <EOL> if os . path . exists ( site_directory ) : <EOL> if os . listdir ( site_directory ) : <EOL> return True <EOL> return False </s>
