
    Ki{9                       d Z ddlmZ 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 d0 d1e      Z  G d2 d3e      Z! G d4 d5e      Z" G d6 d7e       Z# G d8 d9e      Z$ G d: d;e      Z% G d< d=e      Z&y)>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/
    )annotationsreStructuredTextN)classdirdisplaystyleidnoncescriptlevelstyletabindexc                       e Zd ZdZdZ	 dZ	 d fdZed        ZddZ	ddZ
d fdZd fdZd	 Zd
 Z fdZd ZddZd ZdddZddZddZ xZS )MathElementzBase class for MathML elements.Nc                    |j                         D ci c]%  \  }}|j                         | j                  |      ' }}}t        |   | j
                  j                  fi | | j                  |       yc c}}w )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>'

        N)itemslowera_strsuper__init__	__class____name__extend)selfchildren
attributeskvattribr   s         o/home/jay/workspace/scripts/.codegraph-venv/lib/python3.12/site-packages/docutils/utils/math/mathml_elements.pyr   zMathElement.__init__E   sg     8B7G7G7IJtq!!'')TZZ]*JJ00;F;H Ks   *A8c                j    t        | t              rt        |       j                         S t        |       S N)
isinstanceboolstrr   )r   s    r   r   zMathElement.a_strW   s'     aq6<<>!1v    c                   | D cg c]  }t        |       }}| j                  r$|j                  t        | j                               | j                  | j                  j                  k7  r|j                  d| j                          t        | dd      r|j                  d       || j                         D cg c]  \  }}|	| d| c}}z  }| j                   ddj                  |       dS c c}w c c}}w )	z"Return full string representation.z
nchildren=switchNzswitch=True=(, ))	reprtextappend	nchildrenr   getattrr   tagjoin)r   childargsr   r   s        r   __repr__zMathElement.__repr__^   s    )-.U..99KKTYY(>>T^^555KK*T^^$45644(KK&$**,H$!Q!-A3auHH((1TYYt_-Q// / Is   C7<
C<	C<c                    | j                   rt        | j                         }ndj                  d | D              }| j                   d| dS )z/Return concise, informal string representation.r)   c              3  "   K   | ]  }|  	 y wr     ).0r2   s     r   	<genexpr>z&MathElement.__str__.<locals>.<genexpr>o   s     :EwZ:s   r(   r*   )r,   r+   r1   r0   )r   r3   s     r   __str__zMathElement.__str__j   sA    99		?D99:T::D((1TF!$$r$   c                D    t         |   || j                  |             y r    )r   setr   )r   keyvaluer   s      r   r<   zMathElement.setr   s    CE*+r$   c                F   | j                   dk(  rt        d|  d      t        |t              r| |_        nZ| j                   r@t        |       t        |      z   | j                   kD  rt        d|  d| j                    d      |D ]	  }| |_         t        |   ||       y )Nr   	Element "z" does not take children.z" takes only 	 children)r.   	TypeErrorr!   r   parentlenr   __setitem__)r   r=   r>   er   s       r   rE   zMathElement.__setitem__u   s    >>Qiv-FGHHe[)EL~~#d)c%j"84>>"I)D6t~~>N +!, - -   C'r$   c                R    | j                   duxr t        |       | j                   k\  S )z;Return boolean indicating whether children may be appended.N)r.   rD   r   s    r   is_fullzMathElement.is_full   s#    ~~T)Ic$i4>>.IIr$   c                    t        |       | _        | j                  }|/|j                         r|j                  }||j                         r|S )z:Close element and return first non-full anchestor or None.)rD   r.   rC   rI   )r   rC   s     r   closezMathElement.close   sD    T V^^%5]]F  V^^%5r$   c                    | j                         r1| j                  rd| j                   d}nd}t        d|  d| d      t        |   |       | |_        | j                         r| j                         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 rA   zdoes not take childrenr@   z" .)rI   r.   rB   r   r-   rC   rK   )r   elementstatusr   s      r   r-   zMathElement.append   st     <<>~~&t~~&6i@1ivRxq9::w<<>::<r$   c                :    | }|D ]  }| j                  |      } |S )ziSequentially append `elements`. Return new "current node".

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

        Used to find out whether we are in inline vs. displayed maths.
        displayFblock)getrC   in_blockAttributeErrorrH   s    r   rZ   zMathElement.in_block   sU    
 88I&{{++-- xx	"g-- " s   A 	AAc                2    t        j                  | ||       y)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                    | j                         D ]i  }t        |t              s+|j                  r|j                  j	                         |_        |j
                  sK|j
                  j	                         |_        k y)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!   	MathTokenr,   rstriptail)r   rF   s     r   unindent_xmlzMathElement.unindent_xml   sR      	)Aa+vv		)r$   c                    t        j                  | |xs dd      }	 |j                  dd      }|S # t        $ r% |j                  dj	                  |      d      }Y |S w xY w)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;)r]   tostringreplacerB   encode)r   encodingxmls      r   toxmlzMathElement.toxml   sn     kk$ 5I/46	M++h(9:C 
  	M++hooh79KLC
	Ms   2 *A A returnNone)rr   r#   ))z  r   r    )r   
__module____qualname____doc__r.   rC   r   staticmethodr   r4   r:   r<   rE   rI   rK   r-   r   rU   rZ   ra   rg   rp   __classcell__r   s   @r   r   r   <   sq    )I-F-$  
0%,(J,

.&
)r$   r   c                      e Zd ZdZy)MathRowz:Base class for elements treating content as a single mrow.Nr   ru   rv   rw   r7   r$   r   r|   r|      s    Dr$   r|   c                  2     e Zd ZdZdZd 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                R    |j                  dd      | _        t        |   |i | y )Nr&   F)rU   r&   r   r   )r   r   kwargsr   s      r   r   zMathSchema.__init__  s'    jj51(-f-r$   c                    t         |   |      }| j                  r(| j                         r| d   | d   c| d<   | d<   d| _        |S )z2Append element. Normalize order and close if full.rt   F)r   r-   r&   rI   )r   rN   rR   r   s      r   r-   zMathSchema.append  sI    w~g.;;4<<>!%b48DHd2hDKr$   rq   )r   ru   rv   rw   r.   r   r-   ry   rz   s   @r   r   r      s     I. r$   r   c                  (     e Zd ZdZdZd fdZ xZS )rd   zgToken Element: contains textual data instead of children.

    Expect text data on initialisation.
    r   c                    t        |   di | t        |t        t        j
                  f      st        d| d      t        |      | _        y )Nz0MathToken element expects `str` or number, not "z".r7   )r   r   r!   r#   numbersNumber
ValueErrorr,   )r   r,   r   r   s      r   r   zMathToken.__init__  sO    &:&$gnn 56 &&*V2/ 0 0I	r$   rq   )r   ru   rv   rw   r.   r   ry   rz   s   @r   rd   rd     s     I r$   rd   c                      e Zd ZdZy)mathz8Top-level MathML element, a single mathematical formula.Nr}   r7   r$   r   r   r   %  s    Br$   r   c                      e Zd ZdZy)mtextz*Arbitrary text with no notational meaning.Nr}   r7   r$   r   r   r   ,  s    4r$   r   c                      e Zd ZdZy)mizCIdentifier, such as a function name, variable or symbolic constant.Nr}   r7   r$   r   r   r   0  s    Mr$   r   c                      e Zd ZdZy)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}   r7   r$   r   r   r   4  s    r$   r   c                      e Zd ZdZy)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}   r7   r$   r   r   r   ?  s    r$   r   c                      e Zd ZdZdZy)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   ru   rv   rw   r.   r7   r$   r   r   r   K  s     Ir$   r   c                  *     e Zd ZdZd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                    ddd}| j                         D ]e  \  }}|dv rJ|rH|j                  |      r7||   j                  |j                  |      j                  ||         |f      }|j	                  ||       g y)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   rY   r1   re   r<   )r   other
delimitersr   r   s        r   transfer_attributeszmrow.transfer_attributes_  s~      #T2
JJL 	DAq&&199Q<"1**1,,Z];Q?AAIIaO	r$   c                    | j                   }|It        |       dk(  r;| d   }	 ||t        |      j                  |       <   ||_         | j                  |       t        | !         S # t        t
        f$ r Y yw xY w)z|Close element and return first non-full anchestor or None.

        Remove <mrow> if it has only one child element.
        N   r   )	rC   rD   listrT   r[   r   r   r   rK   )r   rC   r2   r   s      r   rK   z
mrow.closem  s    
 #d)q.GE38tF|))$/0% $$U+w} #J/ s   $A' 'A98A9rq   )r   ru   rv   rw   r   rK   ry   rz   s   @r   r   r   Y  s    
 r$   r   c                      e Zd ZdZy)mfraczAFractions or fraction-like objects such as binomial coefficients.Nr}   r7   r$   r   r   r     s    Kr$   r   c                      e Zd ZdZdZy)msqrtzSquare root. See also `mroot`.r   Nr   r7   r$   r   r   r     s
    (Ir$   r   c                      e Zd ZdZy)mrootz/Roots with an explicit index. See also `msqrt`.Nr}   r7   r$   r   r   r         9r$   r   c                      e Zd ZdZy)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}   r7   r$   r   r   r     s    r$   r   c                      e Zd ZdZy)merrorz#Display contents as error messages.Nr}   r7   r$   r   r   r     s    -r$   r   c                      e Zd ZdZdZy)menclosezRenders content inside an enclosing notation...

    ... specified by the notation attribute.

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