<s> """<STR_LIT>""" <EOL> __docformat__ = '<STR_LIT>' <EOL> import re <EOL> import sys <EOL> import time <EOL> from docutils import nodes , utils <EOL> from docutils . transforms import TransformError , Transform <EOL> from docutils . utils import smartquotes <EOL> class Decorations ( Transform ) : <EOL> """<STR_LIT>""" <EOL> default_priority = <NUM_LIT> <EOL> def apply ( self ) : <EOL> header_nodes = self . generate_header ( ) <EOL> if header_nodes : <EOL> decoration = self . document . get_decoration ( ) <EOL> header = decoration . get_header ( ) <EOL> header . extend ( header_nodes ) <EOL> footer_nodes = self . generate_footer ( ) <EOL> if footer_nodes : <EOL> decoration = self . document . get_decoration ( ) <EOL> footer = decoration . get_footer ( ) <EOL> footer . extend ( footer_nodes ) <EOL> def generate_header ( self ) : <EOL> return None <EOL> def generate_footer ( self ) : <EOL> settings = self . document . settings <EOL> if settings . generator or settings . datestamp or settings . source_link or settings . source_url : <EOL> text = [ ] <EOL> if settings . source_link and settings . _source or settings . source_url : <EOL> if settings . source_url : <EOL> source = settings . source_url <EOL> else : <EOL> source = utils . relative_path ( settings . _destination , <EOL> settings . _source ) <EOL> text . extend ( [ <EOL> nodes . reference ( '<STR_LIT>' , '<STR_LIT>' , <EOL> refuri = source ) , <EOL> nodes . Text ( '<STR_LIT>' ) ] ) <EOL> if settings . datestamp : <EOL> datestamp = time . strftime ( settings . datestamp , time . gmtime ( ) ) <EOL> text . append ( nodes . Text ( '<STR_LIT>' + datestamp + '<STR_LIT>' ) ) <EOL> if settings . generator : <EOL> text . extend ( [ <EOL> nodes . Text ( '<STR_LIT>' ) , <EOL> nodes . reference ( '<STR_LIT>' , '<STR_LIT>' , refuri = <EOL> '<STR_LIT>' ) , <EOL> nodes . Text ( '<STR_LIT>' ) , <EOL> nodes . reference ( '<STR_LIT>' , '<STR_LIT>' , refuri = '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> nodes . Text ( '<STR_LIT>' ) ] ) <EOL> return [ nodes . paragraph ( '<STR_LIT>' , '<STR_LIT>' , * text ) ] <EOL> else : <EOL> return None <EOL> class ExposeInternals ( Transform ) : <EOL> """<STR_LIT>""" <EOL> default_priority = <NUM_LIT> <EOL> def not_Text ( self , node ) : <EOL> return not isinstance ( node , nodes . Text ) <EOL> def apply ( self ) : <EOL> if self . document . settings . expose_internals : <EOL> for node in self . document . traverse ( self . not_Text ) : <EOL> for att in self . document . settings . expose_internals : <EOL> value = getattr ( node , att , None ) <EOL> if value is not None : <EOL> node [ '<STR_LIT>' + att ] = value <EOL> class Messages ( Transform ) : <EOL> """<STR_LIT>""" <EOL> default_priority = <NUM_LIT> <EOL> def apply ( self ) : <EOL> unfiltered = self . document . transform_messages <EOL> threshold = self . document . reporter . report_level <EOL> messages = [ ] <EOL> for msg in unfiltered : <EOL> if msg [ '<STR_LIT>' ] >= threshold and not msg . parent : <EOL> messages . append ( msg ) <EOL> if messages : <EOL> section = nodes . section ( classes = [ '<STR_LIT>' ] ) <EOL> section += nodes . title ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> section += messages <EOL> self . document . transform_messages [ : ] = [ ] <EOL> self . document += section <EOL> class FilterMessages ( Transform ) : <EOL> """<STR_LIT>""" <EOL> default_priority = <NUM_LIT> <EOL> def apply ( self ) : <EOL> for node in self . document . traverse ( nodes . system_message ) : <EOL> if node [ '<STR_LIT>' ] < self . document . reporter . report_level : <EOL> node . parent . remove ( node ) <EOL> class TestMessages ( Transform ) : <EOL> """<STR_LIT>""" <EOL> default_priority = <NUM_LIT> <EOL> def apply ( self ) : <EOL> for msg in self . document . transform_messages : <EOL> if not msg . parent : <EOL> self . document += msg <EOL> class StripComments ( Transform ) : <EOL> """<STR_LIT>""" <EOL> default_priority = <NUM_LIT> <EOL> def apply ( self ) : <EOL> if self . document . settings . strip_comments : <EOL> for node in self . document . traverse ( nodes . comment ) : <EOL> node . parent . remove ( node ) <EOL> class StripClassesAndElements ( Transform ) : <EOL> """<STR_LIT>""" <EOL> default_priority = <NUM_LIT> <EOL> def apply ( self ) : <EOL> if not ( self . document . settings . strip_elements_with_classes <EOL> or self . document . settings . strip_classes ) : <EOL> return <EOL> self . strip_elements = dict ( <EOL> [ ( key , None ) <EOL> for key in ( self . document . settings . strip_elements_with_classes <EOL> or [ ] ) ] ) <EOL> self . strip_classes = dict ( <EOL> [ ( key , None ) for key in ( self . document . settings . strip_classes <EOL> or [ ] ) ] ) <EOL> for node in self . document . traverse ( self . check_classes ) : <EOL> node . parent . remove ( node ) <EOL> def check_classes ( self , node ) : <EOL> if isinstance ( node , nodes . Element ) : <EOL> for class_value in node [ '<STR_LIT>' ] [ : ] : <EOL> if class_value in self . strip_classes : <EOL> node [ '<STR_LIT>' ] . remove ( class_value ) <EOL> if class_value in self . strip_elements : <EOL> return <NUM_LIT:1> <EOL> class SmartQuotes ( Transform ) : <EOL> """<STR_LIT>""" <EOL> default_priority = <NUM_LIT> <EOL> def __init__ ( self , document , startnode ) : <EOL> Transform . __init__ ( self , document , startnode = startnode ) <EOL> self . unsupported_languages = set ( ) <EOL> def get_tokens ( self , txtnodes ) : <EOL> texttype = { True : '<STR_LIT>' , <EOL> False : '<STR_LIT>' } <EOL> for txtnode in txtnodes : <EOL> nodetype = texttype [ isinstance ( txtnode . parent , <EOL> ( nodes . literal , <EOL> nodes . math , <EOL> nodes . image , <EOL> nodes . raw , <EOL> nodes . problematic ) ) ] <EOL> yield ( nodetype , txtnode . astext ( ) ) <EOL> def apply ( self ) : <EOL> smart_quotes = self . document . settings . smart_quotes <EOL> if not smart_quotes : <EOL> return <EOL> try : <EOL> alternative = smart_quotes . startswith ( '<STR_LIT>' ) <EOL> except AttributeError : <EOL> alternative = False <EOL> document_language = self . document . settings . language_code <EOL> for node in self . document . traverse ( nodes . TextElement ) : <EOL> if isinstance ( node , ( nodes . FixedTextElement , nodes . Special ) ) : <EOL> continue <EOL> if isinstance ( node . parent , nodes . TextElement ) : <EOL> continue <EOL> txtnodes = [ txtnode for txtnode in node . traverse ( nodes . Text ) <EOL> if not isinstance ( txtnode . parent , <EOL> nodes . option_string ) ] <EOL> lang = node . get_language_code ( document_language ) <EOL> if alternative : <EOL> if '<STR_LIT>' in lang : <EOL> lang = lang . replace ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> else : <EOL> lang += '<STR_LIT>' <EOL> for tag in utils . normalize_language_tag ( lang ) : <EOL> if tag in smartquotes . smartchars . quotes : <EOL> lang = tag <EOL> break <EOL> else : <EOL> if lang not in self . unsupported_languages : <EOL> self . document . reporter . warning ( '<STR_LIT>' <EOL> '<STR_LIT>' % lang , base_node = node ) <EOL> self . unsupported_languages . add ( lang ) <EOL> lang = '<STR_LIT>' <EOL> teacher = smartquotes . educate_tokens ( self . get_tokens ( txtnodes ) , <EOL> attr = '<STR_LIT:2>' , language = lang ) <EOL> for txtnode , newtext in zip ( txtnodes , teacher ) : <EOL> txtnode . parent . replace ( txtnode , nodes . Text ( newtext ) ) <EOL> self . unsupported_languages = set ( ) </s>
