o
    bx                     @   sf  d Z dZddlZddlZddlZddlZddlmZmZm	Z	m
Z
mZmZmZ ddlmZ ddlmZ G dd dZd	Zd
ZddddddddddddeefddZ							d ddZ							d!ddZde
jddddddddddddfddZde
jdddddddddfddZ					d"ddZddddddddddddeede
jfddZdd ZdS )#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:
    https://docutils.sourceforge.io/docs/api/publisher.html
ZreStructuredText    N)__version____version_details__SettingsSpecioutilsreaderswriters)OptionParser)doctreec                   @   s   e Zd ZdZddddejdejdfddZdd Zdd Z	d	d
 Z
		d*ddZdd Z		d*ddZdd Z		d+ddZd,ddZd,ddZd,ddZdd Z			d-ddZd d! Zd"d# Zd$d% Zd&d' Zd(d) ZdS ).	PublisherzK
    A facade encapsulating the high-level logic of a Docutils system.
    Nc	           
      C   s   d| _ 	 || _	 || _	 || _	 dD ]}	tt| |	tr(J dt| |	|	|	f q|| _	 || _	 || _		 || _
	 || _	 t | _dS )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)readerparserwriterzpassed string "%s" as "%s" parameter; pass an instance, or use the "%s_name" parameter instead (in docutils.core.publish_* convenience functions).)documentr   r   r   
isinstancegetattrstrsourcesource_classdestinationdestination_classsettingsr   ZErrorOutput_stderr)
selfr   r   r   r   r   r   r   r   Z	component r   1usr/lib/python3.10/site-packages/docutils/core.py__init__#   s2   
zPublisher.__init__c                 C   s$   t |}|||| _| jj| _dS )zSet `self.reader` by name.N)r   Zget_reader_classr   r   )r   reader_namer   parser_nameZreader_classr   r   r   
set_readerS   s   
zPublisher.set_readerc                 C   s   t |}| | _dS )zSet `self.writer` by name.N)r   Zget_writer_classr   )r   writer_nameZwriter_classr   r   r   
set_writerY   s   
zPublisher.set_writerc                 C   sb   | j d u r| || j| | jd u r#| j jd u r| j | | j j| _| jd u r/| | d S d S N)r   r   r   Z
set_parserr   r!   )r   r   r   r    r   r   r   set_components^   s   



zPublisher.set_componentsc                 K   sl   t jdtdd |r&|st }||_| }t|dkr&|d dkr&dg|_t| j	| j
| j|f|d||d	S )
NzRPublisher.setup_option_parser is deprecated, and will be removed in Docutils 0.21.   )
stacklevel   ZapplicationZapplicationsT)
componentsdefaultsZread_config_filesusagedescription)warningswarnDeprecationWarningr   config_sectionsplitlenZconfig_section_dependenciesr	   r   r   r   )r   r*   r+   settings_specr/   r)   partsr   r   r   setup_option_parserh   s   zPublisher.setup_option_parserc                 O   sJ   t   t jdtd | j|i |W  d    S 1 sw   Y  d S )Nignore)category)r,   catch_warningsfilterwarningsr.   r4   )r   argskwargsr   r   r   _setup_settings_parser{   s   
$z Publisher._setup_settings_parserc                 K   s(   | j ||||fi |}| | _| jS )a  
        Return settings from components and config files.

        Please set components first (`self.set_reader` & `self.set_writer`).
        Use keyword arguments to override component defaults
        (before updating from configuration files).

        Calling this function also sets `self.settings` which makes
        `self.publish()` skip parsing command line options.
        )r;   Zget_default_valuesr   )r   r*   r+   r2   r/   r)   option_parserr   r   r   get_settings   s   
zPublisher.get_settingsc                 C   sD   | j d u r |r| ni }|dd | jd||d| d S d S )N	tracebackT)r2   r/   r   )r   copy
setdefaultr=   )r   r2   settings_overridesr/   r)   r   r   r   process_programmatic_settings   s   

z'Publisher.process_programmatic_settingsc                 K   s>   | j ||||fi |}|du rtjdd }||| _dS )z
        Parse command line arguments and set ``self.settings``.

        Pass an empty sequence to `argv` to avoid reading `sys.argv`
        (the default behaviour).

        Set components first (`self.set_reader` & `self.set_writer`).
        Nr&   )r;   sysargv
parse_argsr   )r   rD   r*   r+   r2   r/   r)   r<   r   r   r   process_command_line   s   zPublisher.process_command_linec                 C   s4   | j d u r| j|d | jd u r| j|d d S d S )N)source_path)destination_path)r   
set_sourcer   set_destination)r   rG   rH   r   r   r   set_io   s
   

zPublisher.set_ioc                 C   :   |d u r	| j j}n|| j _| j||| j j| j jd| _d S )N)r   rG   encodingerror_handler)r   _sourcer   Zinput_encodingZinput_encoding_error_handlerr   )r   r   rG   r   r   r   rI         
zPublisher.set_sourcec                 C   rL   )N)r   rH   rM   rN   )r   Z_destinationr   output_encodingoutput_encoding_error_handlerr   )r   r   rH   r   r   r   rJ      rP   zPublisher.set_destinationc                 C   s4   | j j| j| j| jj| j| jf | j j  d S r"   )	r   transformerZpopulate_from_componentsr   r   r   r   r   apply_transformsr   r   r   r   rT      s   zPublisher.apply_transformsFc              
   C   sH  d}z<| j du r| j|||||fi |pi  |   |   | j| j| j| j | _| 	  | j
| j| j}	| j
  W n> tyU }
 zd}|
j}W Y d}
~
n,d}
~
w ty| }
 z| j s` | j jri|    | |
 d}d}W Y d}
~
nd}
~
ww |   |r| jr| jjj| j jkrt| jjjd  |	S |rt| |	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
   )r   rF   rK   promptr   readr   r   r   rT   r   writer   Zassemble_parts
SystemExitcode	Exceptionr>   debugging_dumpsreport_ExceptionZreporterZ	max_levelZexit_status_levelrC   exit)r   rD   r*   r+   r2   rA   r/   enable_exit_statusr_   outputerrorZexit_statusr   r   r   publish   sR   


	

zPublisher.publishc                 C   s   | j sd S | jjrtd| jd tt| jj| jd | jjr3td| jd tt| j j| jd | jj	rWtd| jd td| jd ttdd | j j
jD | jd | jjrqtd| jd t| j  d	| jd d S d S )
Nz
::: Runtime settings:filez
::: Document internals:z
::: Transforms applied:z@ (priority, transform class, pending node details, keyword args)c                 S   s2   g | ]\}}}}|d |j |jf |o|j|fqS )z%s.%s)
__module____name__details).0priorityZxclasspendingr:   r   r   r   
<listcomp>  s
    

z-Publisher.debugging_dumps.<locals>.<listcomp>z
::: Pseudo-XML:Zraw_unicode_escape)r   r   Zdump_settingsprintr   pprintpformat__dict__Zdump_internalsZdump_transformsrS   ZappliedZdump_pseudo_xmlencoderU   r   r   r   r]      s8   
zPublisher.debugging_dumpsc              
   C   s   z| j  r| j sW dS W n
 ty   Y dS w tjdkr!dnd}d}d}z| jjd }| jjd }W n tt	fy@   Y nw t
dt d	| d
| d| d	| jd dS )z=Print info and prompt when waiting for input from a terminal.NntzCtrl+ZzCtrl+D on an empty line zuseful formatsr   z	Docutils z/ <https://docutils.sourceforge.io>
converting "z" into "zT".
Call with option "--help" for more info.
.. Waiting for source text (finish with z):rd   )r   isattyr   AttributeErrorosnamer   Z	supportedr   
IndexErrorrm   r   )r   Zeot_keyZ	in_formatZ
out_formatr   r   r   rW     s2   

zPublisher.promptc                 C   s   t |tjr| | d S t |tr| | d S t |tjr,| j	dt
|  d S t |tjr?| j	dt
|  d S tdt
| | jd tdt trWdt dnd d	tj d
  d| jd d S )Nz-Unable to open source file for reading:
  %s
z2Unable to open destination file for writing:
  %s
z%srd   zExiting due to error.  Use "--traceback" to diagnose.
Please report errors to <docutils-users@lists.sourceforge.net>.
Include "--traceback" output, Docutils version (z []rs   z),
Python version (r   z5), your OS type & version, and the command line used.)r   r   ZSystemMessagereport_SystemMessageUnicodeEncodeErrorreport_UnicodeErrorr   Z
InputErrorr   rY   error_stringZOutputErrorrm   r   r   rC   versionr0   r   rb   r   r   r   r^   &  s0   


zPublisher.report_Exceptionc                 C   s&   t d|jtjj|j f | jd d S )Nz,Exiting due to level-%s (%s) system message.rd   )rm   levelr   ZReporterlevelsr   r   r   r   r   rz   <  s
   
zPublisher.report_SystemMessagec                 C   s\   |j |j|j }| jdt|| jj|	dd|	dd| jj
ttj d f  d S )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 <docutils-users@lists.sourceforge.net>.
Include "--traceback" output, Docutils version (%s),
Python version (%s), your OS type & version, and the
command line used.
asciixmlcharrefreplacebackslashreplacer   )objectstartendr   rY   r   r}   r   rQ   rq   rR   r   rC   r~   r0   )r   rb   datar   r   r   r|   A  s   

zPublisher.report_UnicodeError)NNNN)NNNNN)NN)NNNNNNF)rg   rf   __qualname____doc__r   	FileInput
FileOutputr   r   r!   r#   r4   r;   r=   rB   rF   rK   rI   rJ   rT   rc   r]   rW   r^   rz   r|   r   r   r   r   r      sD    
0









*r   z*%prog [options] [<source> [<destination>]]zReads from <source> (default is stdin) and writes to <destination> (default is stdout).  See https://docutils.sourceforge.io/docs/user/config.html for a detailed settings reference.
standalonerestructuredtext	pseudoxmlTc              	   C   s:   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).
    )r   r/   r`   r   r#   rc   )r   r   r   r   r   r    r   r2   rA   r/   r`   rD   r*   r+   pubra   r   r   r   publish_cmdlinep  s   
r   Fc                 C   s|   t di dtjd| d|dtjd|d|d|d|d	|d
|d|d|	d|
d|d|d|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`.
    r   r   rG   r   r   rH   r   r   r   r   r   r    r   r2   rA   r/   r`   Nr   )publish_programmaticallyr   r   r   )r   rG   r   rH   r   r   r   r   r   r    r   r2   rA   r/   r`   ra   r   r   r   r   publish_file  sH   	
r   c                 C   s|   t di dtjd| d|dtjddd|d|d	|d
|d|d|d|d|	d|
d|d|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`.
    r   r   rG   r   r   NrH   r   r   r   r   r   r    r   r2   rA   r/   r`   r   )r   r   StringInputStringOutput)r   rG   rH   r   r   r   r   r   r    r   r2   rA   r/   r`   ra   r   r   r   r   publish_string  sH   	
r   c                 C   s~   t di d| d|d|dtjddd|d|d	|d
|d|d|d|	d|
d|d|d|d|\}}|jjS )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`.
    r   rG   r   r   r   NrH   r   r   r   r   r   r    r   r2   rA   r/   r`   r   )r   r   r   r   r3   )r   rG   r   rH   r   r   r   r   r   r    r   r2   rA   r/   r`   ra   r   r   r   r   publish_parts  sH   	
r   c                 C   s\   t ||d||tjd}|||d |||	|
 || | |dd |j|d |jS )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)r   r   r   r   r   r   nullr`   )	r   r   Z
NullOutputr#   rB   rI   rJ   rc   r   )r   rG   r   r   r   r   r   r   r2   rA   r/   r`   r   r   r   r   publish_doctree  s   r   c	                 C   s`   t jdd}	t|	d|t| tj|d}
|s|r|
| |
||| |
d| |
j	|dS )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)r   r   r   r   )
r
   Readerr   r   ZDocTreeInputr   r!   rB   rJ   rc   )r   rH   r   r    r   r2   rA   r/   r`   r   r   r   r   r   publish_from_doctree  s   
r   c              	   C   s<   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).
    )r   r   r   r   )r   r   r   r   r   r    r   r2   rA   r/   r`   rD   r*   r+   r   r   r   ra   r   r   r   publish_cmdline_to_binary7  s   

r   c                 C   s\   t |||
|| |d}|||	| |||| ||| ||| |j|d}||fS )a@  
    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
    <https://docutils.sourceforge.io/docs/user/mailing-lists.html#docutils-develop>.

    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?
    )r   r   r   r   )r   r#   rB   rI   rJ   rc   )r   r   rG   r   r   rH   r   r   r   r   r   r    r   r2   rA   r/   r`   r   ra   r   r   r   r   ]  s   
dr   )NNNNNr   Nr   Nr   NNNNF)NNNr   Nr   Nr   NNNNF)NNr   NNNNF)r   Z__docformat__rn   rv   rC   r,   docutilsr   r   r   r   r   r   r   Zdocutils.frontendr	   Zdocutils.readersr
   r   Zdefault_usageZdefault_descriptionr   r   r   r   r   r   r   ZBinaryFileOutputr   r   r   r   r   r   <module>   s   $  E


%
#
 
(
&