
    -Ph8                     D   d Z ddlZddlmc mZ dZ	  G d dej                  Z G d de          Z	 G d d	e          Z
 G d
 de          Z G d de	          Z G d de          Z G d de          Z G d de          Z G d de          Z G d de          Z G d de	          Z G d de
          Z G d de	          Z G d de
          Z G d  d!e	          Z G d" d#e	          Z G d$ d%e	          Z G d& d'e	          Z G d( d)e	          Z G d* d+e
          Z G d, d-e
          Z G d. d/e
          Z G d0 d1e          Z G d2 d3e          Z G d4 d5e          Z  G d6 d7e          Z! G d8 d9e	          Z" G d: d;e	          Z#dS )<ah  MathML element classes based on `xml.etree`.

The module is intended for programmatic generation of MathML
and covers the part of `MathML Core`_ that is required by
Docutil's *TeX math to MathML* converter.

This module is PROVISIONAL:
the API is not settled and may change with any minor Docutils version.

.. _MathML Core: https://www.w3.org/TR/mathml-core/
    N)classdirdisplaystyleidnoncescriptlevelstyletabindexc                        e Zd ZdZdZ	 dZ	  fdZed             Zd Z	d Z
 fdZ fdZd	 Zd
 Z fdZd ZddZd ZddZd ZddZ xZS )MathElementzBase class for MathML elements.Nc                       fd|                                 D             } t                      j         j        j        fi |                      |           dS )a  Set up node with `children` and `attributes`.

        Attribute names are normalised to lowercase.
        You may use "CLASS" to set a "class" attribute.
        Attribute values are converted to strings
        (with True -> "true" and False -> "false").

        >>> math(CLASS='test', level=3, split=True)
        math(class='test', level='3', split='true')
        >>> math(CLASS='test', level=3, split=True).toxml()
        '<math class="test" level="3" split="true"></math>'

        c                 f    i | ]-\  }}|                                                     |          .S  )lowera_str).0kvselfs      c/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/docutils/utils/math/mathml_elements.py
<dictcomp>z(MathElement.__init__.<locals>.<dictcomp>O   s1    JJJtq!!''))TZZ]]JJJ    N)itemssuper__init__	__class____name__extend)r   children
attributesattribr   s   `   r   r   zMathElement.__init__A   sh     KJJJz7G7G7I7IJJJ0;;F;;;Hr   c                     t          | t                    r!t          |                                           S t          |           S N)
isinstanceboolstrr   )r   s    r   r   zMathElement.a_strS   s5     a 	"q66<<>>!1vvr   c                    d | D             }| j         r'|                    t          | j                              | j        | j        j        k    r|                    d| j                    t          | dd          r|                    d           |d |                                 D             z  }| j         dd                    |           d	S )
z"Return full string representation.c                 ,    g | ]}t          |          S r   )reprr   childs     r   
<listcomp>z(MathElement.__repr__.<locals>.<listcomp>\   s    ...U...r   z
nchildren=switchNzswitch=Truec                 &    g | ]\  }}|| d|S )N=r   )r   r   r   s      r   r,   z(MathElement.__repr__.<locals>.<listcomp>c   s'    HHH$!Q!-A---r   (, ))	textappendr)   	nchildrenr   getattrr   tagjoinr   argss     r   __repr__zMathElement.__repr__Z   s    .....9 	)KKTY(((>T^555KK5T^5566644(( 	'KK&&&HH$**,,HHHH(//TYYt__////r   c                     | j         rt          | j                   }nd                    d | D                       }| j         d| dS )z/Return concise, informal string representation.r1   c              3      K   | ]}| V  d S r#   r   r*   s     r   	<genexpr>z&MathElement.__str__.<locals>.<genexpr>k   s$      ::EZ::::::r   r0   r2   )r3   r)   r8   r7   r9   s     r   __str__zMathElement.__str__f   sT    9 	;	??DD99::T:::::D($$T$$$$r   c                 r    t                                          ||                     |                     d S r#   )r   setr   )r   keyvaluer   s      r   rA   zMathElement.setn   s-    CE**+++++r   c                 n   | j         dk    rt          d|  d          t          |t                    r| |_        nV| j         rCt          |           t          |          z   | j         k    rt          d|  d| j          d          |D ]	}| |_        
t                                          ||           d S )Nr   	Element "z" does not take children.z" takes only 	 children)r5   	TypeErrorr$   r   parentlenr   __setitem__)r   rB   rC   er   s       r   rJ   zMathElement.__setitem__q   s    >QGGGGHHHe[)) 	 ELL~ -#d))c%jj"84>"I"I !,D !, !,t~ !, !, !, - - -    C'''''r   c                 D    | j         duot          |           | j         k    S )z;Return boolean indicating whether children may be appended.N)r5   rI   r   s    r   is_fullzMathElement.is_full~   s!    ~T)Ic$ii4>.IIr   c                     t          |           | _        | j        }|1|                                r|j        }||                                |S )z:Close element and return first non-full anchestor or None.)rI   r5   rH   rN   )r   rH   s     r   closezMathElement.close   sN    T V^^%5%5 ]F  V^^%5%5 r   c                 &   |                                  r+| j        rd| j         d}nd}t          d|  d| d          t                                          |           | |_        |                                  r|                                 S | S )aL  Append `element` and return new "current node" (insertion point).

        Append as child element and set the internal `parent` attribute.

        If self is already full, raise TypeError.

        If self is full after appending, call `self.close()`
        (returns first non-full anchestor or None) else return `self`.
        ztakes only rF   zdoes not take childrenrE   z" .)rN   r5   rG   r   r4   rH   rP   )r   elementstatusr   s      r   r4   zMathElement.append   s     <<>> 	;~ 2@t~@@@1999999:::w<<>> 	 ::<<r   c                 >    | }|D ]}|                      |          }|S )ziSequentially append `elements`. Return new "current node".

        Raise TypeError if overfull.
        )r4   )r   elementscurrent_noderS   s       r   r   zMathElement.extend   s1    
  	0 	0G;;w//LLr   c                     | |         }| |= |S r#   r   )r   indexrS   s      r   popzMathElement.pop   s    u+Kr   c                     |                      d          +	 | j                                        S # t          $ r Y dS w xY w|                      d          dk    S )zReturn True, if `self` or an ancestor has ``display='block'``.

        Used to find out whether we are in inline vs. displayed maths.
        displayNFblock)getrH   in_blockAttributeErrorrM   s    r   r`   zMathElement.in_block   sh    
 88I&{++---!   uuxx	""g--s   0 
>>  r   c                 2    t          j        | ||           dS )zFormat XML output with indents.

        Use with care:
          Formatting whitespace is permanently added to the
          `text` and `tail` attributes of `self` and anchestors!
        N)ETindent)r   spacelevels      r   
indent_xmlzMathElement.indent_xml   s     		$u%%%%%r   c                     |                                  D ]a}t          |t                    s%|j        r|j                                        |_        |j        r|j                                        |_        bdS )zStrip whitespace at the end of `text` and `tail` attributes...

        to revert changes made by the `indent_xml()` method.
        Use with care, trailing whitespace from the original may be lost.
        N)iterr$   	MathTokenr3   rstriptail)r   rK   s     r   unindent_xmlzMathElement.unindent_xml   sm      	) 	)Aa++ ) )v )		) 	)r   c                     t          j        | |pdd          }	 |                    dd          }n9# t          $ r, |                    d                    |          d          }Y nw xY w|S )a  Return an XML representation of the element.

        By default, the return value is a `str` instance. With an explicit
        `encoding` argument, the result is a `bytes` instance in the
        specified encoding. The XML default encoding is UTF-8, any other
        encoding must be specified in an XML document header.

        Name and encoding handling match `xml.dom.minidom.Node.toxml()`
        while `etree.Element.tostring()` returns `bytes` by default.
        unicodeF)short_empty_elementsu   ⁡z&ApplyFunction;s   &ApplyFunction;)rd   tostringreplacerG   encode)r   encodingxmls      r   toxmlzMathElement.toxml   s     k$ 5I/46 6 6	M++h(9::CC 	M 	M 	M++hooh779KLLCCC	M
s   2 3A('A()rX   )rb   r   r#   )r   
__module____qualname____doc__r5   rH   r   staticmethodr   r;   r?   rA   rJ   rN   rP   r4   r   r[   r`   rh   rn   rw   __classcell__r   s   @r   r   r   8   s\       ))I-F-    $   \
0 
0 
0% % %, , , , ,( ( ( ( (J J J      ,     

. 
. 
.& & & &
) 
) 
)       r   r   c                       e Zd ZdZdS )MathRowz:Base class for elements treating content as a single mrow.Nr   rx   ry   rz   r   r   r   r   r      s        DDDDr   r   c                   0     e Zd ZdZdZ fdZ fdZ xZS )
MathSchemazBase class for schemata expecting 2 or more children.

    The special attribute `switch` indicates that the last two child
    elements are in reversed order and must be switched before XML-export.
    See `msub` for an example.
       c                 p    |                     dd          | _         t                      j        |i | d S )Nr-   F)r[   r-   r   r   )r   r   kwargsr   s      r   r   zMathSchema.__init__   s:    jj511(-f-----r   c                     t                                          |          }| j        r2|                                 r| d         | d         c| d<   | d<   d| _        |S )z2Append element. Normalize order and close if full.rX   F)r   r4   r-   rN   )r   rS   rW   r   s      r   r4   zMathSchema.append  sZ    ww~~g..; 	 4<<>> 	 !%b48DHd2hDKr   )r   rx   ry   rz   r5   r   r4   r|   r}   s   @r   r   r      sb          I. . . . .        r   r   c                   &     e Zd ZdZdZ fdZ xZS )rk   zgToken Element: contains textual data instead of children.

    Expect text data on initialisation.
    r   c                      t                      j        di | t          |t          t          j        f          st          d| d          t          |          | _        d S )Nz0MathToken element expects `str` or number, not "z".r   )r   r   r$   r&   numbersNumber
ValueErrorr3   )r   r3   r    r   s      r   r   zMathToken.__init__  ss    &&:&&&$gn 566 	0 /&*/ / / 0 0 0II			r   )r   rx   ry   rz   r5   r   r|   r}   s   @r   rk   rk     sI          I        r   rk   c                       e Zd ZdZdS )mathz8Top-level MathML element, a single mathematical formula.Nr   r   r   r   r   r   !  s        BBBBr   r   c                       e Zd ZdZdS )mtextz*Arbitrary text with no notational meaning.Nr   r   r   r   r   r   (  s        4444r   r   c                       e Zd ZdZdS )mizCIdentifier, such as a function name, variable or symbolic constant.Nr   r   r   r   r   r   ,  s        MMMMr   r   c                       e Zd ZdZdS )mnzNumeric literal.

    >>> mn(3.41).toxml()
    '<mn>3.41</mn>'

    Normally a sequence of digits with a possible separator (a dot or a comma).
    (Values with comma must be specified as `str`.)
    Nr   r   r   r   r   r   0  s           r   r   c                       e Zd ZdZdS )moa  Operator, Fence, Separator, or Accent.

    >>> mo('<').toxml()
    '<mo>&lt;</mo>'

    Besides operators in strict mathematical meaning, this element also
    includes "operators" like parentheses, separators like comma and
    semicolon, or "absolute value" bars.
    Nr   r   r   r   r   r   ;  s           r   r   c                       e Zd ZdZdZdS )mspacezBlank space, whose size is set by its attributes.

    Takes additional attributes `depth`, `height`, `width`.
    Takes no children and no text.

    See also `mphantom`.
    r   Nr   rx   ry   rz   r5   r   r   r   r   r   G  s          IIIr   r   c                   (     e Zd ZdZd Z fdZ xZS )mrowzyGeneric element to group children as a horizontal row.

    Removed on closing if not required (see `mrow.close()`).
    c                 8   ddd}|                                  D ]\  }}|dv r`|r^|                    |          rI||                             |                    |                              ||                   |f          }|                    ||           dS )zTransfer attributes from self to other.

        "List values" (class, style) are appended to existing values,
        other values replace existing values.
         z; )r   r	   N)r   r_   r8   rl   rA   )r   other
delimitersr   r   s        r   transfer_attributeszmrow.transfer_attributes[  s      #T22
JJLL 	 	DAq&&&1&99Q<< A"1**1,,Z];;Q?A AAIIaOOOO	 	r   c                 B   | j         }|vt          |           dk    rc| d         }	 ||t          |                              |           <   ||_         n# t          t
          f$ r Y dS w xY w|                     |           t                                                      S )z|Close element and return first non-full anchestor or None.

        Remove <mrow> if it has only one child element.
        N   r   )	rH   rI   listrZ   ra   r   r   r   rP   )r   rH   r+   r   s      r   rP   z
mrow.closei  s    
 #d))q..GE38tF||))$//0%"J/   tt$$U+++ww}}s   ,A A)(A))r   rx   ry   rz   r   rP   r|   r}   s   @r   r   r   U  sQ         
          r   r   c                       e Zd ZdZdS )mfraczAFractions or fraction-like objects such as binomial coefficients.Nr   r   r   r   r   r   {  s        KKKKr   r   c                       e Zd ZdZdZdS )msqrtzSquare root. See also `mroot`.r   Nr   r   r   r   r   r     s        ((IIIr   r   c                       e Zd ZdZdS )mrootz/Roots with an explicit index. See also `msqrt`.Nr   r   r   r   r   r             9999r   r   c                       e Zd ZdZdS )mstylezStyle Change.

    In modern browsers, <mstyle> is equivalent to an <mrow> element.
    However, <mstyle> may still be relevant for compatibility with
    MathML implementations outside browsers.
    Nr   r   r   r   r   r     s           r   r   c                       e Zd ZdZdS )merrorz#Display contents as error messages.Nr   r   r   r   r   r     s        ----r   r   c                       e Zd ZdZdZdS )menclosezRenders content inside an enclosing notation...

    ... specified by the notation attribute.

    Non-standard but still required by Firefox for boxed expressions.
    r   Nr   r   r   r   r   r     s          IIIr   r   c                       e Zd ZdZdS )mpaddedzAdjust space around content.Nr   r   r   r   r   r     s        &&&&r   r   c                       e Zd ZdZdZdS )mphantomz8Placeholder: Rendered invisibly but dimensions are kept.r   Nr   r   r   r   r   r     s        BBIIIr   r   c                       e Zd ZdZdS )msubz$Attach a subscript to an expression.Nr   r   r   r   r   r     s        ....r   r   c                       e Zd ZdZdS )msupz&Attach a superscript to an expression.Nr   r   r   r   r   r     s        0000r   r   c                       e Zd ZdZdZdS )msubsupz;Attach both a subscript and a superscript to an expression.   Nr   r   r   r   r   r     s        EEIIIr   r   c                       e Zd ZdZdS )munderz0Attach an accent or a limit under an expression.Nr   r   r   r   r   r     s        ::::r   r   c                       e Zd ZdZdS )moverz/Attach an accent or a limit over an expression.Nr   r   r   r   r   r     r   r   r   c                       e Zd ZdZdS )
munderoverz;Attach accents or limits both under and over an expression.Nr   r   r   r   r   r     s        EEEEr   r   c                       e Zd ZdZdS )mtablezTable or matrix element.Nr   r   r   r   r   r     s        """"r   r   c                       e Zd ZdZdS )mtrzRow in a table or a matrix.Nr   r   r   r   r   r             %%%%r   r   c                       e Zd ZdZdS )mtdzCell in a table or a matrixNr   r   r   r   r   r     r   r   r   )$rz   r   xml.etree.ElementTreeetreeElementTreerd   GLOBAL_ATTRIBUTESElementr   r   r   rk   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>r      s  
 
   " " " " " " " " " l l l l l"* l l lpE E E E Ek E E E       2       $C C C C C7 C C C5 5 5 5 5I 5 5 5N N N N N N N N       	 	 	 	 	 	 	 	    [   # # # # #7 # # #LL L L L LJ L L L    G   
: : : : :J : : :    W   . . . . .W . . .    w   ' ' ' ' 'g ' ' '
    w   / / / / /: / / /1 1 1 1 1: 1 1 1    j   $; ; ; ; ;T ; ; ;: : : : :D : : :F F F F F F F F# # # # #[ # # #& & & & &' & & && & & & &' & & & & &r   