U C^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 Cstd|_||_||_||_dD],} tt|| trtdt|| | | fq||_||_ ||_ ||_ ||_ 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). N)readerparserwriterzpassed string "%s" as "%s" parameter; pass an instance, or use the "%s_name" parameter instead (in docutils.core.publish_* convenience functions).)documentrrr isinstancegetattrstrAssertionErrorsource source_class destinationdestination_classsettingsr _stderr) selfrrrrrrrr componentr3/tmp/pip-install-6_kvzl1k/docutils/docutils/core.py__init__ s zPublisher.__init__cCs$t|}||||_|jj|_dS)zSet `self.reader` by name.N)rZget_reader_classrr)r reader_namer parser_nameZ reader_classrrr set_readerPs  zPublisher.set_readercCst|}||_dS)zSet `self.writer` by name.N)r Zget_writer_classr)r writer_nameZ writer_classrrr set_writerVs zPublisher.set_writercCs^|jdkr|||j||jdkrF|jjdkr<|j||jj|_|jdkrZ||dSN)rr$r set_parserrr&)rr"r#r%rrr set_components[s      zPublisher.set_componentscKs`|r<|st}||_|}t|dkr<|ddkrsz2Publisher.process_command_line..)r6sysargv version_inforZlocale_encoding parse_argsr)rrFr.r/r3r0r-r5rrCr process_command_lines   zPublisher.process_command_linecCs0|jdkr|j|d|jdkr,|j|ddS)N) source_path)destination_path)r set_sourcerset_destination)rrJrKrrr set_ios   zPublisher.set_iocCsd|dkr|jj}n||j_z|j|||jjd|_Wn*tk r^|j|||jjd|_YnXdS)N)rrJencoding)rZ_sourcerZinput_encodingr TypeError)rrrJrrr rLs  zPublisher.set_sourcecCs:|dkr|jj}n||j_|j|||jj|jjd|_dS)N)rrKrOZ error_handler)rZ _destinationroutput_encodingoutput_encoding_error_handlerr)rrrKrrr rMs zPublisher.set_destinationcCs4|jj|j|j|jj|j|jf|jjdSr') r transformerZpopulate_from_componentsrrrrrapply_transformsrrrr rTs zPublisher.apply_transformsFc Cs>d}zl|jdkr*|j|||||f|p&i||j|j|j|j|_||j |j|j } |j Wnxt k r} zd}| j} W5d} ~ XYnNtk r} z0|js|jjr||| d}d} W5d} ~ XYnX||r*|jr*|jjj|jjkr*t|jjjdn|r:t| | 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 )rrIrNrreadrrrrTrwriterZassemble_parts SystemExitcode Exceptionr9debugging_dumpsreport_ExceptionZreporterZ max_levelZexit_status_levelrEexit) rrFr.r/r3r<r0enable_exit_statusr^outputerror exit_statusrrr publishsR   zPublisher.publishcCs|js dS|jjr8td|jdtt|jj|jd|jjrftd|jdtt|jj|jd|jj rtd|jdtd|jdttdd|jj j D|jd|jj rtd|jdt|j 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)rApriorityZxclasspendingkwargsrrr rDs  z-Publisher.debugging_dumps..z ::: Pseudo-XML:Zraw_unicode_escape)rrZ dump_settingsprintrpprintpformat__dict__Zdump_internalsZdump_transformsrSZappliedZdump_pseudo_xmlencoderUrrr r\s6 zPublisher.debugging_dumpscCst|tjr||nt|tr.||nt|tjrP|j dt |njt|tj rr|j dt |nHt dt ||jdt dt tjrdtjpdtjdf|jddS) Nz-Unable to open source file for reading: %s z2Unable to open destination file for writing: %s z%srdzExiting 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 InputErrorrrXr Z OutputErrorrlrdocutilsrrEversionr1rrarrr r] s4       zPublisher.report_ExceptioncCs&td|jtjj|jf|jddS)Nz,Exiting due to level-%s (%s) system message.rd)rllevelrZReporterZlevelsrrwrrr rr!s zPublisher.report_SystemMessagec CsZ|j|j|j}|jdt||jj|dd|dd|jj t t 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)objectstartendrrXr rrQrprRrrErvr1)rradatarrr rt&s   zPublisher.report_UnicodeError)NNNN)NNNN)NNNNN)NN)NN)NN)NNNNNNF)rgrf __qualname____doc__r FileInput FileOutputr!r$r&r)r6r8r=rIrNrLrMrTrcr\r]rrrtrrrr rsL 0         )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| | | ||| | 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). )rr0r_rr)rc)rr"rr#rr%rr3r<r0r_rFr.r/pubr`rrr publish_cmdlineJsrFcCs6ttj||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`. rrrJrrrKrr"rr#rr%rr3r<r0r_)publish_programmaticallyrrr)rrJrrKrr"rr#rr%rr3r<r0r_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`. Nr)rr StringInput StringOutput)rrJrKrr"rr#rr%rr3r<r0r_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)rrJrrrrKrr"rr#rr%rr3r<r0r_)rrrrr4)rrJrrKrr"rr#rr%rr3r<r0r_r`rrrr publish_partss( rc Cs\t||d||tjd} | ||d| || | | ||| dd| 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)rrrrrrnullr_) rrZ NullOutputr)r=rLrMrcr)rrJrrr"rr#rr3r<r0r_rr`rrr publish_doctrees   rc Csdtjjjdd} t| d|t|tj|d} |s>|r>| || |||| 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)rrrr) rurZdoctreeReaderrrZ DocTreeInputrr&r=rMrc) rrKrr%rr3r<r0r_rrrrr publish_from_doctrees  rc Cs<t|||||d}|||||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). )rrrr)rr"rr#rr%rr3r<r0r_rFr.r/rrrr`rrr publish_cmdline_to_binary s rcCs\t||| | ||d}||| | || |||||||||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? )rrrr)rr)r=rLrMrc)rrrJrrrKrr"rr#rr%rr3r<r0r_rr`rrr r,sd    r)NNNNNrNrNrNNNNF) NNNrNrNrNNNNF)NNrNNNNF)"rZ __docformat__rErmrurrrrrrrr Zdocutils.frontendr Zdocutils.transformsr Zdocutils.utils.error_reportingr r Zdocutils.readers.doctreerZ default_usageZdefault_descriptionrrrrrrrZBinaryFileOutputrrrrrr s   ,   $ "  ' !