<s> """<STR_LIT>""" <EOL> from __future__ import unicode_literals <EOL> import logging <EOL> import markdown <EOL> import os <EOL> import pkg_resources <EOL> import shutil <EOL> import sys <EOL> import yaml <EOL> import fnmatch <EOL> from mkdocs import toc , exceptions <EOL> try : <EOL> from urllib . parse import urlparse , urlunparse , urljoin <EOL> from urllib . request import pathname2url <EOL> from collections import UserDict <EOL> except ImportError : <EOL> from urlparse import urlparse , urlunparse , urljoin <EOL> from urllib import pathname2url <EOL> from UserDict import UserDict <EOL> PY3 = sys . version_info [ <NUM_LIT:0> ] == <NUM_LIT:3> <EOL> if PY3 : <EOL> string_types = str , <EOL> text_type = str <EOL> else : <EOL> string_types = basestring , <EOL> text_type = unicode <EOL> log = logging . getLogger ( __name__ ) <EOL> def yaml_load ( source , loader = yaml . Loader ) : <EOL> """<STR_LIT>""" <EOL> def construct_yaml_str ( self , node ) : <EOL> """<STR_LIT>""" <EOL> return self . construct_scalar ( node ) <EOL> class Loader ( loader ) : <EOL> """<STR_LIT>""" <EOL> Loader . add_constructor ( '<STR_LIT>' , construct_yaml_str ) <EOL> try : <EOL> return yaml . load ( source , Loader ) <EOL> finally : <EOL> if hasattr ( source , '<STR_LIT>' ) : <EOL> source . close ( ) <EOL> def reduce_list ( data_set ) : <EOL> """<STR_LIT>""" <EOL> seen = set ( ) <EOL> return [ item for item in data_set if <EOL> item not in seen and not seen . add ( item ) ] <EOL> def copy_file ( source_path , output_path ) : <EOL> """<STR_LIT>""" <EOL> output_dir = os . path . dirname ( output_path ) <EOL> if not os . path . exists ( output_dir ) : <EOL> os . makedirs ( output_dir ) <EOL> shutil . copy ( source_path , output_path ) <EOL> def write_file ( content , output_path ) : <EOL> """<STR_LIT>""" <EOL> output_dir = os . path . dirname ( output_path ) <EOL> if not os . path . exists ( output_dir ) : <EOL> os . makedirs ( output_dir ) <EOL> open ( output_path , '<STR_LIT:wb>' ) . write ( content ) <EOL> def clean_directory ( directory ) : <EOL> """<STR_LIT>""" <EOL> if not os . path . exists ( directory ) : <EOL> return <EOL> for entry in os . listdir ( directory ) : <EOL> if entry . startswith ( '<STR_LIT:.>' ) : <EOL> continue <EOL> path = os . path . join ( directory , entry ) <EOL> if os . path . isdir ( path ) : <EOL> shutil . rmtree ( path , True ) <EOL> else : <EOL> os . unlink ( path ) <EOL> def copy_media_files ( from_dir , to_dir , exclude = None ) : <EOL> """<STR_LIT>""" <EOL> for ( source_dir , dirnames , filenames ) in os . walk ( from_dir ) : <EOL> relative_path = os . path . relpath ( source_dir , from_dir ) <EOL> output_dir = os . path . normpath ( os . path . join ( to_dir , relative_path ) ) <EOL> exclude_patterns = [ '<STR_LIT>' ] <EOL> exclude_patterns . extend ( exclude or [ ] ) <EOL> for pattern in exclude_patterns : <EOL> filenames = [ f for f in filenames if not fnmatch . fnmatch ( f , pattern ) ] <EOL> dirnames [ : ] = [ d for d in dirnames if not d . startswith ( '<STR_LIT:.>' ) ] <EOL> for filename in filenames : <EOL> if not is_markdown_file ( filename ) : <EOL> source_path = os . path . join ( source_dir , filename ) <EOL> output_path = os . path . join ( output_dir , filename ) <EOL> copy_file ( source_path , output_path ) <EOL> def get_html_path ( path ) : <EOL> """<STR_LIT>""" <EOL> path = os . path . splitext ( path ) [ <NUM_LIT:0> ] <EOL> if os . path . basename ( path ) == '<STR_LIT:index>' : <EOL> return path + '<STR_LIT>' <EOL> return "<STR_LIT:/>" . join ( ( path , '<STR_LIT>' ) ) <EOL> def get_url_path ( path , use_directory_urls = True ) : <EOL> """<STR_LIT>""" <EOL> path = get_html_path ( path ) <EOL> url = '<STR_LIT:/>' + path . replace ( os . path . sep , '<STR_LIT:/>' ) <EOL> if use_directory_urls : <EOL> return url [ : - len ( '<STR_LIT>' ) ] <EOL> return url <EOL> def is_homepage ( path ) : <EOL> return os . path . splitext ( path ) [ <NUM_LIT:0> ] == '<STR_LIT:index>' <EOL> def is_markdown_file ( path ) : <EOL> """<STR_LIT>""" <EOL> ext = os . path . splitext ( path ) [ <NUM_LIT:1> ] . lower ( ) <EOL> return ext in [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> def is_css_file ( path ) : <EOL> """<STR_LIT>""" <EOL> ext = os . path . splitext ( path ) [ <NUM_LIT:1> ] . lower ( ) <EOL> return ext in [ <EOL> '<STR_LIT>' , <EOL> ] <EOL> def is_javascript_file ( path ) : <EOL> """<STR_LIT>""" <EOL> ext = os . path . splitext ( path ) [ <NUM_LIT:1> ] . lower ( ) <EOL> return ext in [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ] <EOL> def is_html_file ( path ) : <EOL> """<STR_LIT>""" <EOL> ext = os . path . splitext ( path ) [ <NUM_LIT:1> ] . lower ( ) <EOL> return ext in [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> def is_template_file ( path ) : <EOL> """<STR_LIT>""" <EOL> ext = os . path . splitext ( path ) [ <NUM_LIT:1> ] . lower ( ) <EOL> return ext in [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> def create_media_urls ( nav , path_list ) : <EOL> """<STR_LIT>""" <EOL> final_urls = [ ] <EOL> for path in path_list : <EOL> parsed = urlparse ( path ) <EOL> if parsed . netloc : <EOL> final_urls . append ( path ) <EOL> continue <EOL> url = path_to_url ( path ) <EOL> relative_url = '<STR_LIT>' % ( nav . url_context . make_relative ( '<STR_LIT:/>' ) , url ) <EOL> final_urls . append ( relative_url ) <EOL> return final_urls <EOL> def create_relative_media_url ( nav , url ) : <EOL> """<STR_LIT>""" <EOL> parsed = urlparse ( url ) <EOL> if parsed . netloc : <EOL> return url <EOL> if url . startswith ( '<STR_LIT:/>' ) : <EOL> base = '<STR_LIT:/>' <EOL> url = url [ <NUM_LIT:1> : ] <EOL> else : <EOL> base = nav . url_context . base_path <EOL> relative_base = nav . url_context . make_relative ( base ) <EOL> if relative_base == "<STR_LIT:.>" and url . startswith ( "<STR_LIT>" ) : <EOL> relative_url = url <EOL> else : <EOL> relative_url = '<STR_LIT>' % ( relative_base , url ) <EOL> if ( nav . file_context . current_file . endswith ( "<STR_LIT>" ) is False and <EOL> nav . url_context . base_path != '<STR_LIT:/>' and <EOL> relative_url . startswith ( "<STR_LIT>" ) ) : <EOL> relative_url = "<STR_LIT>" % relative_url <EOL> return relative_url <EOL> def path_to_url ( path ) : <EOL> """<STR_LIT>""" <EOL> if os . path . sep == '<STR_LIT:/>' : <EOL> return path <EOL> if sys . version_info < ( <NUM_LIT:3> , <NUM_LIT:0> ) : <EOL> path = path . encode ( '<STR_LIT:utf8>' ) <EOL> return pathname2url ( path ) <EOL> def convert_markdown ( markdown_source , extensions = None , extension_configs = None ) : <EOL> """<STR_LIT>""" <EOL> md = markdown . Markdown ( <EOL> extensions = extensions or [ ] , <EOL> extension_configs = extension_configs or { } <EOL> ) <EOL> html_content = md . convert ( markdown_source ) <EOL> meta = getattr ( md , '<STR_LIT:Meta>' , { } ) <EOL> toc_html = getattr ( md , '<STR_LIT>' , '<STR_LIT>' ) <EOL> table_of_contents = toc . TableOfContents ( toc_html ) <EOL> return ( html_content , table_of_contents , meta ) <EOL> def get_themes ( ) : <EOL> """<STR_LIT>""" <EOL> themes = { } <EOL> builtins = pkg_resources . get_entry_map ( dist = '<STR_LIT>' , group = '<STR_LIT>' ) <EOL> for theme in pkg_resources . iter_entry_points ( group = '<STR_LIT>' ) : <EOL> if theme . name in builtins and theme . dist . key != '<STR_LIT>' : <EOL> raise exceptions . ConfigurationError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" . format ( theme . name , theme . dist . key ) ) <EOL> elif theme . name in themes : <EOL> multiple_packages = [ themes [ theme . name ] . dist . key , theme . dist . key ] <EOL> log . warning ( "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> theme . name , '<STR_LIT:U+002C>' . join ( multiple_packages ) , theme . dist . key ) <EOL> themes [ theme . name ] = theme <EOL> themes = dict ( ( name , os . path . dirname ( os . path . abspath ( theme . load ( ) . __file__ ) ) ) <EOL> for name , theme in themes . items ( ) ) <EOL> return themes <EOL> def get_theme_names ( ) : <EOL> """<STR_LIT>""" <EOL> return get_themes ( ) . keys ( ) <EOL> def filename_to_title ( filename ) : <EOL> title = os . path . splitext ( filename ) [ <NUM_LIT:0> ] <EOL> title = title . replace ( '<STR_LIT:->' , '<STR_LIT:U+0020>' ) . replace ( '<STR_LIT:_>' , '<STR_LIT:U+0020>' ) <EOL> if title . lower ( ) == title : <EOL> title = title . capitalize ( ) <EOL> return title <EOL> def dirname_to_title ( dirname ) : <EOL> title = dirname <EOL> title = title . replace ( '<STR_LIT:->' , '<STR_LIT:U+0020>' ) . replace ( '<STR_LIT:_>' , '<STR_LIT:U+0020>' ) <EOL> if title . lower ( ) == title : <EOL> title = title . capitalize ( ) <EOL> return title <EOL> def find_or_create_node ( branch , key ) : <EOL> """<STR_LIT>""" <EOL> for node in branch : <EOL> if not isinstance ( node , dict ) : <EOL> continue <EOL> if key in node : <EOL> return node [ key ] <EOL> new_branch = [ ] <EOL> node = { key : new_branch } <EOL> branch . append ( node ) <EOL> return new_branch <EOL> def nest_paths ( paths ) : <EOL> """<STR_LIT>""" <EOL> nested = [ ] <EOL> for path in paths : <EOL> if os . path . sep not in path : <EOL> nested . append ( path ) <EOL> continue <EOL> directory , _ = os . path . split ( path ) <EOL> parts = directory . split ( os . path . sep ) <EOL> branch = nested <EOL> for part in parts : <EOL> part = dirname_to_title ( part ) <EOL> branch = find_or_create_node ( branch , part ) <EOL> branch . append ( path ) <EOL> return nested </s>
