<s> import re <EOL> import urllib <EOL> from robotide . lib . robot . errors import DataError <EOL> from robotide . lib . robot . htmldata import HtmlFileWriter , ModelWriter , JsonWriter , LIBDOC <EOL> from robotide . lib . robot . utils import get_timestamp , html_escape , html_format , NormalizedDict <EOL> from robotide . lib . robot . utils . htmlformatters import HeaderFormatter <EOL> class LibdocHtmlWriter ( object ) : <EOL> def write ( self , libdoc , output ) : <EOL> model_writer = LibdocModelWriter ( output , libdoc ) <EOL> HtmlFileWriter ( output , model_writer ) . write ( LIBDOC ) <EOL> class LibdocModelWriter ( ModelWriter ) : <EOL> def __init__ ( self , output , libdoc ) : <EOL> self . _output = output <EOL> formatter = DocFormatter ( libdoc . keywords , libdoc . doc , libdoc . doc_format ) <EOL> self . _libdoc = JsonConverter ( formatter ) . convert ( libdoc ) <EOL> def write ( self , line ) : <EOL> self . _output . write ( '<STR_LIT>' ) <EOL> self . write_data ( ) <EOL> self . _output . write ( '<STR_LIT>' ) <EOL> def write_data ( self ) : <EOL> JsonWriter ( self . _output ) . write_json ( '<STR_LIT>' , self . _libdoc ) <EOL> class JsonConverter ( object ) : <EOL> def __init__ ( self , doc_formatter ) : <EOL> self . _doc_formatter = doc_formatter <EOL> def convert ( self , libdoc ) : <EOL> return { <EOL> '<STR_LIT:name>' : libdoc . name , <EOL> '<STR_LIT>' : self . _doc_formatter . html ( libdoc . doc , intro = True ) , <EOL> '<STR_LIT:version>' : libdoc . version , <EOL> '<STR_LIT>' : libdoc . named_args , <EOL> '<STR_LIT>' : libdoc . scope , <EOL> '<STR_LIT>' : get_timestamp ( daysep = '<STR_LIT:->' , millissep = None ) , <EOL> '<STR_LIT>' : self . _get_keywords ( libdoc . inits ) , <EOL> '<STR_LIT>' : self . _get_keywords ( libdoc . keywords ) , <EOL> '<STR_LIT>' : tuple ( libdoc . all_tags ) , <EOL> '<STR_LIT>' : bool ( libdoc . all_tags ) <EOL> } <EOL> def _get_keywords ( self , keywords ) : <EOL> return [ self . _convert_keyword ( kw ) for kw in keywords ] <EOL> def _convert_keyword ( self , kw ) : <EOL> return { <EOL> '<STR_LIT:name>' : kw . name , <EOL> '<STR_LIT:args>' : kw . args , <EOL> '<STR_LIT>' : self . _doc_formatter . html ( kw . doc ) , <EOL> '<STR_LIT>' : kw . shortdoc , <EOL> '<STR_LIT>' : tuple ( kw . tags ) , <EOL> '<STR_LIT>' : True <EOL> } <EOL> class DocFormatter ( object ) : <EOL> _header_regexp = re . compile ( r'<STR_LIT>' ) <EOL> _name_regexp = re . compile ( '<STR_LIT>' ) <EOL> def __init__ ( self , keywords , introduction , doc_format = '<STR_LIT>' ) : <EOL> self . _doc_to_html = DocToHtml ( doc_format ) <EOL> self . _targets = self . _get_targets ( keywords , introduction , <EOL> robot_format = doc_format == '<STR_LIT>' ) <EOL> def _get_targets ( self , keywords , introduction , robot_format ) : <EOL> targets = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> for kw in keywords : <EOL> targets [ kw . name ] = kw . name <EOL> if robot_format : <EOL> for header in self . _yield_header_targets ( introduction ) : <EOL> targets [ header ] = header <EOL> return self . _escape_and_encode_targets ( targets ) <EOL> def _yield_header_targets ( self , introduction ) : <EOL> headers = HeaderFormatter ( ) <EOL> for line in introduction . splitlines ( ) : <EOL> match = headers . match ( line ) <EOL> if match : <EOL> yield match . group ( <NUM_LIT:2> ) <EOL> def _escape_and_encode_targets ( self , targets ) : <EOL> return NormalizedDict ( ( html_escape ( key ) , self . _encode_uri_component ( value ) ) <EOL> for key , value in targets . iteritems ( ) ) <EOL> def _encode_uri_component ( self , value ) : <EOL> return urllib . quote ( value . encode ( '<STR_LIT>' ) , safe = "<STR_LIT>" ) <EOL> def html ( self , doc , intro = False ) : <EOL> doc = self . _doc_to_html ( doc ) <EOL> if intro : <EOL> doc = self . _header_regexp . sub ( r'<STR_LIT>' , doc ) <EOL> return self . _name_regexp . sub ( self . _link_keywords , doc ) <EOL> def _link_keywords ( self , match ) : <EOL> name = match . group ( <NUM_LIT:1> ) <EOL> if name in self . _targets : <EOL> return '<STR_LIT>' % ( self . _targets [ name ] , name ) <EOL> return '<STR_LIT>' % name <EOL> class DocToHtml ( object ) : <EOL> def __init__ ( self , doc_format ) : <EOL> self . _formatter = self . _get_formatter ( doc_format ) <EOL> def _get_formatter ( self , doc_format ) : <EOL> try : <EOL> return { '<STR_LIT>' : html_format , <EOL> '<STR_LIT>' : self . _format_text , <EOL> '<STR_LIT>' : self . _format_html , <EOL> '<STR_LIT>' : self . _format_rest } [ doc_format ] <EOL> except KeyError : <EOL> raise DataError ( "<STR_LIT>" % doc_format ) <EOL> def _format_text ( self , doc ) : <EOL> return '<STR_LIT>' % html_escape ( doc ) <EOL> def _format_html ( self , doc ) : <EOL> return '<STR_LIT>' % doc <EOL> def _format_rest ( self , doc ) : <EOL> try : <EOL> from docutils . core import publish_parts <EOL> except ImportError : <EOL> raise DataError ( "<STR_LIT>" ) <EOL> parts = publish_parts ( doc , writer_name = '<STR_LIT:html>' ) <EOL> return self . _format_html ( parts [ '<STR_LIT>' ] ) <EOL> def __call__ ( self , doc ) : <EOL> return self . _formatter ( doc ) </s>
