3 E\s@sTdZdZddlZddlZddlmZmZmZddlmZm Z m Z m Z m Z ddl mZddlmZddlmZmZddlZGd d d Zd Zd Zdd ddddddddddeefddZd"ddZd#ddZde jddd dddddddddfddZde jdd dddddddf ddZd$ddZdd ddddddddddeede jfddZ d d!Z!dS)%a Calling the ``publish_*`` convenience functions (or instantiating a `Publisher` object) with component names will result in default behavior. For custom behavior (setting component options), create custom component objects first, and pass *them* to ``publish_*``/`Publisher`. See `The Docutils Publisher`_. .. _The Docutils Publisher: http://docutils.sf.net/docs/api/publisher.html ZreStructuredTextN) __version____version_details__ SettingsSpec)frontendioutilsreaderswriters) OptionParser) Transformer) ErrorOutput ErrorStringc@seZdZdZddddejdejdfddZddZddZ d d Z d&d d Z d'd dZ ddZ d(ddZd)ddZd*ddZd+ddZddZd,ddZddZd d!Zd"d#Zd$d%ZdS)- PublisherzK A facade encapsulating the high-level logic of a Docutils system. Nc Cszd|_||_||_||_x6dD].} tt|| t stdt|| | | fqW||_||_ ||_ ||_ ||_ t |_dS)z Initial setup. If any of `reader`, `parser`, or `writer` are not specified, the corresponding ``set_...`` method should be called with a component name (`set_reader` sets the parser as well). Nreaderparserwriterzpassed string "%s" as "%s" parameter; pass an instance, or use the "%s_name" parameter instead (in docutils.core.publish_* convenience functions).)rrr)documentrrr isinstancegetattrstrAssertionErrorsource source_class destinationdestination_classsettingsr _stderr) selfrrrrrrrr componentrg/private/var/folders/pf/wv4htv3x0qs2c2mp0dnn0kchsvlck3/T/pip-install-emcbgzcf/docutils/docutils/core.py__init__ s  zPublisher.__init__cCs$tj|}||||_|jj|_dS)zSet `self.reader` by name.N)rZget_reader_classrr)r reader_namer parser_nameZ reader_classrrr set_readerPs  zPublisher.set_readercCstj|}||_dS)zSet `self.writer` by name.N)r Zget_writer_classr)r writer_nameZ writer_classrrr set_writerVs zPublisher.set_writercCs^|jdkr|j||j||jdkrF|jjdkr<|jj||jj|_|jdkrZ|j|dS)N)rr$r set_parserrr&)rr"r#r%rrr set_components[s      zPublisher.set_componentscKs`|r<|st}||_|j}t|dkr<|ddkrsz2Publisher.process_command_line..)r=r)r5sysargv version_inforZlocale_encoding parse_argsr)rrEr,r-r2r/r+r4r)rBr process_command_lines   zPublisher.process_command_linecCs0|jdkr|j|d|jdkr,|j|ddS)N) source_path)destination_path)r set_sourcerset_destination)rrIrJrrr set_ios   zPublisher.set_ioc Csd|dkr|jj}n||j_y|j|||jjd|_Wn*tk r^|j|||jjd|_YnXdS)N)rrIencoding)r_sourcerZinput_encodingr TypeError)rrrIrrr rKs zPublisher.set_sourcecCs:|dkr|jj}n||j_|j|||jj|jjd|_dS)N)rrJrNZ error_handler)rZ _destinationroutput_encodingoutput_encoding_error_handlerr)rrrJrrr rLs zPublisher.set_destinationcCs4|jjj|j|j|jj|j|jf|jjjdS)N) r transformerZpopulate_from_componentsrrrrrapply_transforms)rrrr rTs zPublisher.apply_transformsFc Cs>d}yl|jdkr*|j|||||f|p&i|j|jj|j|j|j|_|j|j j |j|j } |j j Wnxt k r} zd}| j} WYdd} ~ XnNtk r} z2|js|jjr|j|j| d}d} WYdd} ~ XnX|j|r*|jr*|jjj|jjkr*tj|jjjdn|r:tj| | S)z Process command line options and arguments (if `self.settings` not already set), run `self.reader` and then `self.writer`. Return `self.writer`'s output. Nr)T )rrHrMrreadrrrrTrwriterZassemble_parts SystemExitcode Exceptionr8debugging_dumpsreport_ExceptionZreporterZ max_levelZexit_status_levelrDexit) rrEr,r-r2r;r/enable_exit_statusr]outputerror exit_statusrrr publishs@       zPublisher.publishcCs|js dS|jjr8td|jdttj|jj|jd|jjrftd|jdttj|jj|jd|jj rtd|jdtd|jdttjdd|jj j D|jd|jj rtd|jdt|jjj d |jddS) Nz ::: Runtime settings:)filez ::: Document internals:z ::: Transforms applied:z@ (priority, transform class, pending node details, keyword args)cSs2g|]*\}}}}|d|j|jf|o(|j|fqS)z%s.%s) __module____name__details)r@priorityZxclasspendingkwargsrrr rCsz-Publisher.debugging_dumps..z ::: Pseudo-XML:Zraw_unicode_escape)rrZ dump_settingsprintrpprintpformat__dict__Zdump_internalsZdump_transformsrSZappliedZdump_pseudo_xmlencode)rrrr r[s&  zPublisher.debugging_dumpscCst|tjr|j|nt|tr.|j|nt|tjrP|jj dt |njt|tj rr|jj dt |nHt dt ||jdt dt tjrdtjpdtjjdf|jddS) Nz-Unable to open source file for reading: %s z2Unable to open destination file for writing: %s z%s)rczExiting due to error. Use "--traceback" to diagnose. Please report errors to . Include "--traceback" output, Docutils version (%s%s), Python version (%s), your OS type & version, and the command line used.z [%s]r)rrZ SystemMessagereport_SystemMessageUnicodeEncodeErrorreport_UnicodeErrorrZ InputErrorrrWr Z OutputErrorrjrdocutilsrrDversionr0)rr`rrr r\ s"       zPublisher.report_ExceptioncCs&td|jtjj|jf|jddS)Nz,Exiting due to level-%s (%s) system message.)rc)rjlevelrZReporterZlevelsr)rr`rrr rp!szPublisher.report_SystemMessagec CsZ|j|j|j}|jjdt||jj|jdd|jdd|jj t t j j dfdS)Na%s The specified output encoding (%s) cannot handle all of the output. Try setting "--output-encoding-error-handler" to * "xmlcharrefreplace" (for HTML & XML output); the output will contain "%s" and should be usable. * "backslashreplace" (for other output formats); look for "%s" in the output. * "replace"; look for "?" in the output. "--output-encoding-error-handler" is currently set to "%s". Exiting due to error. Use "--traceback" to diagnose. If the advice above doesn't eliminate the error, please report it to . Include "--traceback" output, Docutils version (%s), Python version (%s), your OS type & version, and the command line used. r>xmlcharrefreplacebackslashreplacer)objectstartendrrWr rrQrnrRrrDrtr0)rr`datarrr rr&s  zPublisher.report_UnicodeError)NNNN)NNNN)NNNNN)NN)NN)NN)NNNNNNF)rerd __qualname____doc__r FileInput FileOutputr!r$r&r(r5r7r<rHrMrKrLrTrbr[r\rprrrrrr rs4 -         'rz*%prog [options] [ []]zReads from (default is stdin) and writes to (default is stdout). See for the full reference. standalonerestructuredtext pseudoxmlTc Cs:t||||d}|j||||j| | | ||| | d}|S)a% Set up & run a `Publisher` for command-line-based file I/O (input and output file paths taken automatically from the command line). Return the encoded string output also. Parameters: see `publish_programmatically` for the remainder. - `argv`: Command-line argument list to use instead of ``sys.argv[1:]``. - `usage`: Usage string, output if there's a problem parsing the command line. - `description`: Program description, output for the "--help" option (along with command-line option descriptions). )r)r/r^)rr(rb)rr"rr#rr%rr2r;r/r^rEr,r-pubr_rrr publish_cmdlineJs   rFcCs6ttj||tj|||||||| | | | | |d\}}|S)z Set up & run a `Publisher` for programmatic use with file-like I/O. Return the encoded string output also. Parameters: see `publish_programmatically`. )rrrIrrrJrr"rr#rr%rr2r;r/r^)publish_programmaticallyrr~r)rrIrrJrr"rr#rr%rr2r;r/r^r_rrrr publish_filees  rcCs6ttj||tjd|||||||| | | | | d\}}|S)aB Set up & run a `Publisher` for programmatic use with string I/O. Return the encoded string or Unicode string output. For encoded string output, be sure to set the 'output_encoding' setting to the desired encoding. Set it to 'unicode' for unencoded Unicode string output. Here's one way:: publish_string(..., settings_overrides={'output_encoding': 'unicode'}) Similarly for Unicode string input (`source`):: publish_string(..., settings_overrides={'input_encoding': 'unicode'}) Parameters: see `publish_programmatically`. N)rrrIrrrJrr"rr#rr%rr2r;r/r^)rr StringInput StringOutput)rrIrJrr"rr#rr%rr2r;r/r^r_rrrr publish_strings rcCs8t|||tjd||||||| | | | | |d\}}|jjS)a Set up & run a `Publisher`, and return a dictionary of document parts. Dictionary keys are the names of parts, and values are Unicode strings; encoding is up to the client. For programmatic use with string I/O. For encoded string input, be sure to set the 'input_encoding' setting to the desired encoding. Set it to 'unicode' for unencoded Unicode string input. Here's how:: publish_parts(..., settings_overrides={'input_encoding': 'unicode'}) Parameters: see `publish_programmatically`. N)rrIrrrrJrr"rr#rr%rr2r;r/r^)rrrrr3)rrIrrJrr"rr#rr%rr2r;r/r^r_rrrr publish_partss rc Cs\t||d||tjd} | j||d| j|| | | j||| jdd| j| d} | jS)a Set up & run a `Publisher` for programmatic use with string I/O. Return the document tree. For encoded string input, be sure to set the 'input_encoding' setting to the desired encoding. Set it to 'unicode' for unencoded Unicode string input. Here's one way:: publish_doctree(..., settings_overrides={'input_encoding': 'unicode'}) Parameters: see `publish_programmatically`. N)rrrrrrnull)r^) rrZ NullOutputr(r<rKrLrbr)rrIrrr"rr#rr2r;r/r^rr_rrr publish_doctrees     rc Csftjjjdd} t| d|tj|tj|d} | r@|r@| j|| j |||| j d|| j |dS)aJ Set up & run a `Publisher` to render from an existing document tree data structure, for programmatic use with string I/O. Return the encoded string output. Note that document.settings is overridden; if you want to use the settings of the original `document`, pass settings=document.settings. Also, new document.transformer and document.reporter objects are generated. For encoded string output, be sure to set the 'output_encoding' setting to the desired encoding. Set it to 'unicode' for unencoded Unicode string output. Here's one way:: publish_from_doctree( ..., settings_overrides={'output_encoding': 'unicode'}) Parameters: `document` is a `docutils.nodes.document` object, an existing document tree. Other parameters: see `publish_programmatically`. r)r#N)rrr)r^) rsrZdoctreeReaderrrZ DocTreeInputrr&r<rLrb) rrJrr%rr2r;r/r^rrrrr publish_from_doctrees     rc Cs<t|||||d}|j||||j| | | ||| | d}|S)a Set up & run a `Publisher` for command-line-based file I/O (input and output file paths taken automatically from the command line). Return the encoded string output also. This is just like publish_cmdline, except that it uses io.BinaryFileOutput instead of io.FileOutput. Parameters: see `publish_programmatically` for the remainder. - `argv`: Command-line argument list to use instead of ``sys.argv[1:]``. - `usage`: Usage string, output if there's a problem parsing the command line. - `description`: Program description, output for the "--help" option (along with command-line option descriptions). )rr)r/r^)rr(rb)rr"rr#rr%rr2r;r/r^rEr,r-rrrr_rrr publish_cmdline_to_binary s   rcCs\t||| | ||d}|j|| | |j| |||j|||j|||j|d}||fS)a7 Set up & run a `Publisher` for custom programmatic use. Return the encoded string output and the Publisher object. Applications should not need to call this function directly. If it does seem to be necessary to call this function directly, please write to the Docutils-develop mailing list . Parameters: * `source_class` **required**: The class for dynamically created source objects. Typically `io.FileInput` or `io.StringInput`. * `source`: Type depends on `source_class`: - If `source_class` is `io.FileInput`: Either a file-like object (must have 'read' and 'close' methods), or ``None`` (`source_path` is opened). If neither `source` nor `source_path` are supplied, `sys.stdin` is used. - If `source_class` is `io.StringInput` **required**: The input string, either an encoded 8-bit string (set the 'input_encoding' setting to the correct encoding) or a Unicode string (set the 'input_encoding' setting to 'unicode'). * `source_path`: Type depends on `source_class`: - `io.FileInput`: Path to the input file, opened if no `source` supplied. - `io.StringInput`: Optional. Path to the file or object that produced `source`. Only used for diagnostic output. * `destination_class` **required**: The class for dynamically created destination objects. Typically `io.FileOutput` or `io.StringOutput`. * `destination`: Type depends on `destination_class`: - `io.FileOutput`: Either a file-like object (must have 'write' and 'close' methods), or ``None`` (`destination_path` is opened). If neither `destination` nor `destination_path` are supplied, `sys.stdout` is used. - `io.StringOutput`: Not used; pass ``None``. * `destination_path`: Type depends on `destination_class`: - `io.FileOutput`: Path to the output file. Opened if no `destination` supplied. - `io.StringOutput`: Path to the file or object which will receive the output; optional. Used for determining relative paths (stylesheets, source links, etc.). * `reader`: A `docutils.readers.Reader` object. * `reader_name`: Name or alias of the Reader class to be instantiated if no `reader` supplied. * `parser`: A `docutils.parsers.Parser` object. * `parser_name`: Name or alias of the Parser class to be instantiated if no `parser` supplied. * `writer`: A `docutils.writers.Writer` object. * `writer_name`: Name or alias of the Writer class to be instantiated if no `writer` supplied. * `settings`: A runtime settings (`docutils.frontend.Values`) object, for dotted-attribute access to runtime settings. It's the end result of the `SettingsSpec`, config file, and option processing. If `settings` is passed, it's assumed to be complete and no further setting/config/option processing is done. * `settings_spec`: A `docutils.SettingsSpec` subclass or object. Provides extra application-specific settings definitions independently of components. In other words, the application becomes a component, and its settings data is processed along with that of the other components. Used only if no `settings` specified. * `settings_overrides`: A dictionary containing application-specific settings defaults that override the defaults of other components. Used only if no `settings` specified. * `config_section`: A string, the name of the configuration file section for this application. Overrides the ``config_section`` attribute defined by `settings_spec`. Used only if no `settings` specified. * `enable_exit_status`: Boolean; enable exit status at end of processing? )rrr)r^)rr(r<rKrLrb)rrrIrrrJrr"rr#rr%rr2r;r/r^rr_rrr r,sd     r)NNNNNrNrNrNNNNF) NNNrNrNrNNNNF)NNrNNNNF)"r}Z __docformat__rDrkrsrrrrrrrr Zdocutils.frontendr Zdocutils.transformsr Zdocutils.utils.error_reportingr r Zdocutils.readers.doctreerZ default_usageZdefault_descriptionrrrrrrrZBinaryFileOutputrrrrrr  s  ,     #