
    -PhIQ                         d Z dZddlZddlmZmZmZ ddlmZm	Z	  G d de	          Z
 G d d	e
          Z G d
 de
          Z G d de	          ZdS )a  
Transforms_ related to the front matter of a document or a section
(information found before the main text):

- `DocTitle`: Used to transform a lone top level section's title to
  the document title, promote a remaining lone top-level section's
  title to the document subtitle, and determine the document's title
  metadata (document['title']) based on the document title and/or the
  "title" setting.

- `SectionSubTitle`: Used to transform a lone subsection into a
  subtitle.

- `DocInfo`: Used to transform a bibliographic field list into docinfo
  elements.

.. _transforms: https://docutils.sourceforge.io/docs/api/transforms.html
reStructuredText    N)nodesparsersutils)TransformError	Transformc                   $    e Zd ZdZd Zd Zd ZdS )TitlePromoterzJ
    Abstract base class for DocTitle and SectionSubTitle transforms.
    c                    t          |t          j                  st          d          t	          |          r"t          |d         t          j                  rJ |                     |          \  }}|dS |                    |dd           |dd         |d|         z   |dd         z   |dd<   t          |d         t          j                  sJ dS )a  
        Transform the following tree::

            <node>
                <section>
                    <title>
                    ...

        into ::

            <node>
                <title>
                ...

        `node` is normally a document.
        %node must be of Element-derived type.r   NFTreplace
and_source   )
isinstancer   Element	TypeErrorlentitlecandidate_indexupdate_all_atts_concatenating)selfnodesectionindexs       _/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/docutils/transforms/frontmatter.pypromote_titlezTitlePromoter.promote_title&   s    $ $.. 	ECDDD IIC*T!Wek"B"BCCC--d33=5 	**7D6: 	+ 	< 	< 	< 2A2;&5&\" QRR[!QQQ $q'5;/////t    c                 b   t          |t          j                  st          d          |                     |          \  }}|dS t          j                    }|                    |dd           |d         dd         |dd<   |dd         |gz   |d|         z   |dd         z   |dd<   dS )a  
        Transform the following node tree::

            <node>
                <title>
                <section>
                    <title>
                    ...

        into ::

            <node>
                <title>
                <subtitle>
                ...
        r   NFTr   r   r   )r   r   r   r   r   subtitler   )r   r   
subsectionr   r    s        r   promote_subtitlezTitlePromoter.promote_subtitleQ   s    $ $.. 	ECDDD 0066
E=5>## 	..z4:> 	/ 	@ 	@ 	@
 !mAAA&8Z  !E']#
  ^$QQQ tr   c                     |                     t          j                  }|6t          |          |dz   k    s t	          ||         t          j                  sdS ||         |fS )z
        Find and return the promotion candidate and its index.

        Return (None, None) if no valid candidate was found.
        Nr   )NN)first_child_not_matching_classr   PreBibliographicr   r   r   )r   r   r   s      r   r   zTitlePromoter.candidate_index~   sd     33"$ $MSYY%!)44d5k5=99 5:;%%r   N)__name__
__module____qualname____doc__r   r"   r    r   r   r
   r
       sM         ) ) )V+ + +Z& & & & &r   r
   c                   "    e Zd ZdZdZd Zd ZdS )DocTitlea  
    In reStructuredText_, there is no way to specify a document title
    and subtitle explicitly. Instead, we can supply the document title
    (and possibly the subtitle as well) implicitly, and use this
    two-step transform to "raise" or "promote" the title(s) (and their
    corresponding section contents) to the document level.

    1. If the document contains a single top-level section as its first
       element (instances of `nodes.PreBibliographic` are ignored),
       the top-level section's title becomes the document's title, and
       the top-level section's contents become the document's immediate
       contents. The title is also used for the <document> element's
       "title" attribute default value.

    2. If step 1 successfully determines the document title, we
       continue by checking for a subtitle.

       If the lone top-level section itself contains a single second-level
       section as its first "non-PreBibliographic" element, that section's
       title is promoted to the document's subtitle, and that section's
       contents become the document's immediate contents.

    Example:
       Given this input text::

           =================
            Top-Level Title
           =================

           Second-Level Title
           ~~~~~~~~~~~~~~~~~~

           A paragraph.

       After parsing and running the DocTitle transform, the result is::

           <document names="top-level title">
               <title>
                   Top-Level Title
               <subtitle names="second-level title">
                   Second-Level Title
               <paragraph>
                   A paragraph.

       (Note that the implicit hyperlink target generated by the
       "Second-Level Title" is preserved on the <subtitle> element
       itself.)

    Any `nodes.PreBibliographic` instances occurring before the
    document title or subtitle are accumulated and inserted as
    the first body elements after the title(s).

    .. _reStructuredText: https://docutils.sourceforge.io/rst.html
    i@  c                 ^   | j                             d          s| j         j        j        | j         j        j        | j         d<   dS t	          | j                   rPt          | j         d         t          j                  r-| j         d                                         | j         d<   dS dS dS dS )a  
        Set document['title'] metadata title from the following
        sources, listed in order of priority:

        * Existing document['title'] attribute.
        * "title" setting.
        * Document title node (as promoted by promote_title).
        r   Nr   )documenthasattrsettingsr   r   r   r   astextr   s    r   set_metadatazDocTitle.set_metadata   s     }$$W-- 	C}%+7)-)?)Eg&&&T]## C
4=3C38;)@ )@ C)-q)9)@)@)B)Bg&&&	C 	CC C C Cr   c                     | j         j                            dd          r4|                     | j                   r|                     | j                    |                                  d S )Ndoctitle_xformT)r.   r0   
setdefaultr   r"   r3   r2   s    r   applyzDocTitle.apply   sh    =!,,-=tDD 	5!!$-00 5 %%dm444r   N)r&   r'   r(   r)   default_priorityr3   r7   r*   r   r   r,   r,      sH        5 5n C C C     r   r,   c                       e Zd ZdZdZd ZdS )SectionSubTitlea  
    This works like document subtitles, but for sections.  For example, ::

        <section>
            <title>
                Title
            <section>
                <title>
                    Subtitle
                ...

    is transformed into ::

        <section>
            <title>
                Title
            <subtitle>
                Subtitle
            ...

    For details refer to the docstring of DocTitle.
    i^  c                     | j         j                            dd          sd S | j                             t          j                  D ]}|                     |           d S )Nsectsubtitle_xformT)r.   r0   r6   findallr   r   r"   )r   r   s     r   r7   zSectionSubTitle.apply   si    }%001EtLL 	F},,U];; 	+ 	+G !!'****		+ 	+r   N)r&   r'   r(   r)   r8   r7   r*   r   r   r:   r:      s5         . + + + + +r   r:   c                   `   e Zd ZdZdZej        ej        ej        ej	        ej
        ej        ej        ej        ej        ej        ej        ej        dZ	 d Zd Zd Zd Z ej        dej                  d	f ej        d
ej                  df ej        d          dfgZd Zd Zd Zd ZdS )DocInfoan  
    This transform is specific to the reStructuredText_ markup syntax;
    see "Bibliographic Fields" in the `reStructuredText Markup
    Specification`_ for a high-level description. This transform
    should be run *after* the `DocTitle` transform.

    If the document contains a field list as the first element (instances
    of `nodes.PreBibliographic` are ignored), registered bibliographic
    field names are transformed to the corresponding DTD elements,
    becoming child elements of the <docinfo> element (except for a
    dedication and/or an abstract, which become <topic> elements after
    <docinfo>).

    For example, given this document fragment after parsing::

        <document>
            <title>
                Document Title
            <field_list>
                <field>
                    <field_name>
                        Author
                    <field_body>
                        <paragraph>
                            A. Name
                <field>
                    <field_name>
                        Status
                    <field_body>
                        <paragraph>
                            $RCSfile$
            ...

    After running the bibliographic field list transform, the
    resulting document tree would look like this::

        <document>
            <title>
                Document Title
            <docinfo>
                <author>
                    A. Name
                <status>
                    frontmatter.py
            ...

    The "Status" field contained an expanded RCS keyword, which is
    normally (but optionally) cleaned up by the transform. The sole
    contents of the field body must be a paragraph containing an
    expanded RCS keyword of the form "$keyword: expansion text $". Any
    RCS keyword can be processed in any bibliographic field. The
    dollar signs and leading RCS keyword name are removed. Extra
    processing is done for the following RCS keywords:

    - "RCSfile" expands to the name of the file in the RCS or CVS
      repository, which is the name of the source file with a ",v"
      suffix appended. The transform will remove the ",v" suffix.

    - "Date" expands to the format "YYYY/MM/DD hh:mm:ss" (in the UTC
      time zone). The RCS Keywords transform will extract just the
      date itself and transform it to an ISO 8601 format date, as in
      "2000-12-31".

      (Since the source file for this text is itself stored under CVS,
      we can't show an example of the "Date" RCS keyword because we
      can't prevent any RCS keywords used in this explanation from
      being expanded. Only the "RCSfile" keyword is stable; its
      expansion text changes only if the file name changes.)

    .. _reStructuredText: https://docutils.sourceforge.io/rst.html
    .. _reStructuredText Markup Specification:
       https://docutils.sourceforge.io/docs/ref/rst/restructuredtext.html
    iT  )authorauthorsorganizationaddresscontactversionrevisionstatusdate	copyright
dedicationabstractc                    | j         j                            dd          sd S | j         }|                    t          j                  }|d S ||         }t          |t          j                  rW|                    t          j        t          j	        t          j
        f          }|                     |          }||= ||||<   d S d S )Ndocinfo_xformT)r.   r0   r6   r$   r   r%   r   
field_listTitular
Decorativemetaextract_bibliographic)r   r.   r   	candidatebiblioindexnodelists         r   r7   zDocInfo.applyf  s    }%00$GG 	F=77$& &=FUO	i!122 	9"AA=%"2EJ?A AK11)<<H08H[,---	9 	9r   c                    t          j                    }| j        j        }| j        j        }d d d}|D ]}	 |d         d                                         }t          j        |          }t          |          dk    r||v r|                     ||          st          ||         }	| j
        |	         }
t          |
t           j                  rm|                     ||          st          t          j        |d         d         | j                   |                     |
ddg|d         d         R             nt          |
t           j                  r|                     |||           nt          |
t           j                  r||	         r9|dxx         | j        j                            d|z  |          z  cc<   t          t          j        |||	                   }||	         |d         _         |
d|g|d         j        R d	|	gi||	<   n'|                     |
dg|d         j        R             	# t          $ r t          |d                   dk    rLt7          |d         d         t           j                  r&t          j        |d         d         | j                   t          j        |          }|r|d	                             |           |                    |           Y w xY wg }t          |          dk    r|                    |           dD ]%}||         r|                    ||                    &|S )
N)rJ   rK   r      r    z!There can only be one "%s" field.	base_nodeclasses)r   docinfolanguagebibliographic_fieldslabelsr1   fully_normalize_namer   check_empty_biblio_fieldr   biblio_nodes
issubclassTextElementcheck_compound_biblio_fieldr   clean_rcs_keywordsrcs_keyword_substitutionsappendrA   extract_authorstopicr.   reporterwarningr   	rawsourcechildrenr   	paragraphmake_id)r   rN   r]   bibliofieldsr`   topicsfieldname
normedname	canonicalbiblioclassr   
classvaluerU   s                 r   rR   zDocInfo.extract_bibliographicv  s   -//}9% $$77 &	& &	&E%&Qx{))++"7==
E

aJ,,F,F 99%FF -G(((4	"/	:k5+<== H;;E4HH -,,,(1+t'EG G GNN;;r2#Da#D#D#DEEEEU];; H((g>>>>U[99 Hi( -b			T]%;%C%C?$F&+ &D &- &- -			 -,!KfY.?@@E)/	):E!H&(3E)L9>q9J)L )L )L,5;)L )LF9%% NN;;r#FE!H4E#F#F#FGGG! 	& 	& 	&uRy>>Q&&%eBilEODD ',b	!d&DF F F #]:66
 8)$++J777u%%%%%	& w<<1OOG$$$. 	. 	.Dd| .t---s   HH<<B5K54K5c                     t          |d                   dk     r5|dxx         | j        j                            d| d|          z  cc<   dS dS )NrY   r   z*Cannot extract empty bibliographic field "z".rZ   FT)r   r.   rl   rm   )r   rt   ru   s      r   rb   z DocInfo.check_empty_biblio_field  sf    uRy>>A"III/77GtGGG! 8 # # #III 5tr   c                 .   |d         }t          |          dk    r"t          |d         t          j                  rdS t          |d         t          j                  rd|j                                        vrt          j        d|j	        j
                  }t          j                                        }|                    d|j        z   |           t          |j                  dk    r3t          |j        d         t          j                  r|j        |_        dS d |j        D             }t          |          dk    rd	d
                    |          z   dz   }nd|d         z   }|| j	        j                            d| d| d|          z  }dS )NrY   r   r   T
z*DocInfo transform*\c                 $    g | ]}d |j          dS )<>)tagname).0es     r   
<listcomp>z7DocInfo.check_compound_biblio_field.<locals>.<listcomp>  s&    ===#qy###===r   [z, ]za zBibliographic field "z)"
must contain a single <paragraph>, not .rZ   F)r   r   r   rp   enumerated_listrn   stripr   new_documentr.   r0   r   rstParserparsero   joinrl   rm   )r   rt   ru   f_body	_documentparsercontents          r   rf   z#DocInfo.check_compound_biblio_field  s    rv;;!
6!9eo F F4 vay%"788 
	F,224444*+@+0>+BD DI[''))FLLf..	:::I&''1,,y1!4eoFF -"+"4t==V_===w<<!DIIg...4GGWQZ'G$-(00>d > >3:> > > % 1 ' ' 	' ur   z8\$Date: (\d\d\d\d)[-/](\d\d)[-/](\d\d)[ T][\d:]+[^$]* \$z\1-\2-\3z\$RCSfile: (.+),v \$z\1z\$[a-zA-Z]+: (.+) \$c                    	 t          |d                   dk    rt          |d         d         t          j                  r|                     |          }nXt          |d         d         t          j                  r|                     |          }nt          |                     |          }d |D             }t          |          dk    r'|	                    t          j
        dg|R             d S t          # t          $ rX |dxx         | j        j                            d| d| dd                    | j        j                   d	|
          z  cc<    w xY w)Nr   r   c                 8    g | ]}|t          j        d d g|R  S )rX   )r   r@   )r   r@   s     r   r   z+DocInfo.extract_authors.<locals>.<listcomp>  s@     < < <%F<5<B8888 < < <r   rX   rY   zCannot extract "z1" from bibliographic field:
Bibliographic field "zR" must contain either
 a single paragraph (with author names separated by one of "z"),
 multiple paragraphs (one per author),
 or a bullet list with one author name per item.
Note: Leading initials can cause (mis)recognizing names as enumerated list.rZ   )r   r   r   rp   authors_from_one_paragraphbullet_listauthors_from_bullet_listr   authors_from_paragraphsri   rA   r.   rl   rm   r   r^   author_separators)r   rt   ru   r]   rA   authornodess         r   rj   zDocInfo.extract_authors  s   	58}}!!eAhqk5?;; )"==eDDGGaU->?? )";;EBBGG((66u==< <)0< < <K;1$$u}R>+>>>?????$$ 	 	 	"III/77&4 & &(,& & GGDM;<<& & &   8 	! 	! 	!III 	s   C1C< 5C< <A"Ec                 D   d                     d |d                             t          j                  D                       }|st          | j        j        D ]1}d|z  }t          j        ||          }t          |          dk    r n2d |D             }d |D             S )zReturn list of Text nodes with author names in `field`.

        Author names must be separated by one of the "autor separators"
        defined for the document language (default: ";" or ",").
        rX   c              3   4   K   | ]}t          |          V  d S N)str)r   r   s     r   	<genexpr>z5DocInfo.authors_from_one_paragraph.<locals>.<genexpr>  sD       A A 4yy A A A A A Ar   r   z(?<! )%sc              3   >   K   | ]}|                                 V  d S r   )r   r   ru   s     r   r   z5DocInfo.authors_from_one_paragraph.<locals>.<genexpr>  s*      <<tzz||<<<<<<r   c                 <    g | ]}|t          j        |          gS r*   )r   Textr   s     r   r   z6DocInfo.authors_from_one_paragraph.<locals>.<listcomp>  s*    CCCtdCD!!"CCCr   )
r   r=   r   r   r   r^   r   resplitr   )r   rt   text	authorseppatternauthornamess         r   r   z"DocInfo.authors_from_one_paragraph  s     ww A A#(8#3#3EJ#?#?A A A A A 	!  8 	 	I#i/G(7D11K;!## $<<<<<CC{CCCCr   c                 (   g }|d         d         D ]w}t          |t          j                  rt          |          dk    s t          |d         t          j                  st
          |                    |d         j                   x|st
          |S )Nr   r   )r   r   commentr   rp   r   ri   ro   )r   rt   rA   items       r   r   z DocInfo.authors_from_bullet_list
  s    !HQK 	- 	-D$.. 4yyA~~ZQ%I%I~$$NN47+,,,, 	!  r   c                     |d         D ]/}t          |t          j        t          j        f          st          0d |d         D             }|S )Nr   c                 P    g | ]#}t          |t          j                  |j        $S r*   )r   r   r   ro   )r   r   s     r   r   z3DocInfo.authors_from_paragraphs.<locals>.<listcomp>  s:     ; ; ;T$T5=99;4= ; ; ;r   )r   r   rp   r   r   )r   rt   r   rA   s       r   r   zDocInfo.authors_from_paragraphs  sa    !H 	% 	%DdU_em$DEE %$$%; ;U1X ; ; ;r   N)r&   r'   r(   r)   r8   r   r@   rA   rB   rC   rD   rE   rF   rG   rH   rI   rk   rc   r7   rR   rb   rf   r   compile
IGNORECASErh   rj   r   r   r   r*   r   r   r?   r?     sR       H HT  L],]]]nL*k# #L*9 9 9 2 2 2h    B 2: "#%=2 23>@2:12=AA5
I2:-..
6	!8  <D D D(
 
 
    r   r?   )r)   __docformat__r   docutilsr   r   r   docutils.transformsr   r   r
   r,   r:   r?   r*   r   r   <module>r      s$  
 & # 				 * * * * * * * * * * 9 9 9 9 9 9 9 9j& j& j& j& j&I j& j& j&ZS S S S S} S S Sl"+ "+ "+ "+ "+m "+ "+ "+JT T T T Ti T T T T Tr   