o
    i{9                     @  s  d Z ddlmZ dZddlZddlm  mZ dZ		 G dd dej
ZG dd	 d	eZG d
d deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG d d! d!eZG d"d# d#eZG d$d% d%eZG d&d' d'eZG d(d) d)eZG d*d+ d+eZG d,d- d-eZG d.d/ d/eZG d0d1 d1eZ G d2d3 d3eZ!G d4d5 d5eZ"G d6d7 d7e Z#G d8d9 d9eZ$G d:d; d;eZ%G d<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/
    )annotationsreStructuredTextN)classdirdisplaystyleidnoncescriptlevelstyletabindexc                      s   e Zd ZdZdZ	 dZ	 d' fddZedd Zd(d
dZ	d(ddZ
d' fddZd' fddZdd Zdd Z fddZdd Zd)ddZdd Zd*d'd!d"Zd'd#d$Zd+d%d&Z  ZS ),MathElementzBase class for MathML elements.NreturnNonec                   s<    fd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                   s    i | ]\}}|   |qS  )lowera_str.0kvselfr   i/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/docutils/utils/math/mathml_elements.py
<dictcomp>S   s     z(MathElement.__init__.<locals>.<dictcomp>N)itemssuper__init__	__class____name__extend)r   children
attributesattribr   r   r   r   E   s   zMathElement.__init__c                 C  s   t | trt|  S t| S N)
isinstanceboolstrr   )r   r   r   r   r   W   s   
zMathElement.a_strr'   c                 C  s   dd | D }| j r|t| j  | j| jjkr"|d| j  t| ddr-|d |dd |  D 7 }| j dd	| d
S )z"Return full string representation.c                 S  s   g | ]}t |qS r   )reprr   childr   r   r   
<listcomp>`   s    z(MathElement.__repr__.<locals>.<listcomp>z
nchildren=switchNzswitch=Truec                 S  s&   g | ]\}}|d ur| d|qS )N=r   r   r   r   r   r+   g   s   & (, ))	textappendr(   	nchildrenr   getattrr   tagjoinr   argsr   r   r   __repr__^   s   
zMathElement.__repr__c                 C  s8   | j r	t| j }n
ddd | D }| j d| dS )z/Return concise, informal string representation.r/   c                 s  s    | ]}| V  qd S r$   r   r)   r   r   r   	<genexpr>o   s    z&MathElement.__str__.<locals>.<genexpr>r.   r0   )r1   r(   r6   r5   r7   r   r   r   __str__j   s   zMathElement.__str__c                   s   t  || | d S r$   )r   setr   )r   keyvaluer#   r   r   r<   r   s   zMathElement.setc                   s   | j dkrtd|  dt|tr| |_n"| j r0t| t| | j kr0td|  d| j  d|D ]}| |_q2t || d S )Nr   	Element "z" does not take children.z" takes only 	 children)r3   	TypeErrorr%   r   parentlenr   __setitem__)r   r=   r>   er#   r   r   rD   u   s   

zMathElement.__setitem__c                 C  s   | j duot| | j kS )z;Return boolean indicating whether children may be appended.N)r3   rC   r   r   r   r   is_full   s   zMathElement.is_fullc                 C  s:   t | | _| j}|dur| r|j}|dur| s|S )z:Close element and return first non-full anchestor or None.N)rC   r3   rB   rF   )r   rB   r   r   r   close   s   
zMathElement.closec                   s^   |   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 r@   zdoes not take childrenr?   z" .)rF   r3   rA   r   r2   rB   rG   )r   elementstatusr#   r   r   r2      s   
zMathElement.appendc                 C  s   | }|D ]}|  |}q|S )ziSequentially append `elements`. Return new "current node".

        Raise TypeError if overfull.
        )r2   )r   elementscurrent_noderI   r   r   r   r      s   zMathElement.extendc                 C  s   | | }| |= |S r$   r   )r   indexrI   r   r   r   pop   s   zMathElement.popc                 C  s>   |  ddu rz| j W S  ty   Y dS 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)getrB   in_blockAttributeErrorr   r   r   r   rS      s   zMathElement.in_block  r   c                 C  s   t | || 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levelr   r   r   
indent_xml   s   zMathElement.indent_xmlc                 C  s@   |   D ]}t|ts|jr|j |_|jr|j |_qd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%   	MathTokenr1   rstriptail)r   rE   r   r   r   unindent_xml   s   zMathElement.unindent_xmlc                 C  sN   t j| |pddd}z	|dd}W |S  ty&   |d|d}Y |S 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;)rV   tostringreplacerA   encode)r   encodingxmlr   r   r   toxml   s   zMathElement.toxmlr   r   )r   r'   )rM   )rU   r   r$   )r   
__module____qualname____doc__r3   rB   r   staticmethodr   r9   r;   r<   rD   rF   rG   r2   r   rO   rS   rZ   r_   rg   __classcell__r   r   r#   r   r   <   s,    





	r   c                   @     e Zd ZdZdS )MathRowz:Base class for elements treating content as a single mrow.Nr   ri   rj   rk   r   r   r   r   ro          ro   c                      s2   e Zd ZdZdZd	 fddZ fddZ  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.
       r   r   c                   s$   | dd| _t j|i | d S )Nr,   F)rO   r,   r   r   )r   r    kwargsr#   r   r   r     s   zMathSchema.__init__c                   s>   t  |}| jr|  r| d | d | d< | d< d| _|S )z2Append element. Normalize order and close if full.rM   F)r   r2   r,   rF   )r   rI   rL   r#   r   r   r2     s
   zMathSchema.appendrh   )r   ri   rj   rk   r3   r   r2   rm   r   r   r#   r   rr      s
    rr   c                      s&   e Zd ZdZdZd fddZ  ZS )r\   zgToken Element: contains textual data instead of children.

    Expect text data on initialisation.
    r   r   r   c                   s@   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
ValueErrorr1   )r   r1   r!   r#   r   r   r     s   
zMathToken.__init__rh   )r   ri   rj   rk   r3   r   rm   r   r   r#   r   r\     s    r\   c                   @  rn   )mathz8Top-level MathML element, a single mathematical formula.Nrp   r   r   r   r   ry   %  rq   ry   c                   @  rn   )mtextz*Arbitrary text with no notational meaning.Nrp   r   r   r   r   rz   ,  rq   rz   c                   @  rn   )mizCIdentifier, such as a function name, variable or symbolic constant.Nrp   r   r   r   r   r{   0  rq   r{   c                   @  rn   )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`.)
    Nrp   r   r   r   r   r|   4  rq   r|   c                   @  rn   )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.
    Nrp   r   r   r   r   r}   ?  rq   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   ri   rj   rk   r3   r   r   r   r   r   K  s    r   c                      s*   e Zd ZdZdddZ fddZ  ZS )	mrowzyGeneric element to group children as a horizontal row.

    Removed on closing if not required (see `mrow.close()`).
    r   r   c                 C  sd   ddd}|   D ]&\}}|dv r)|r)||r)|| |||| |f}||| q	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   rR   r6   r]   r<   )r   other
delimitersr   r   r   r   r   transfer_attributes_  s   

zmrow.transfer_attributesc              	     sl   | j }|dur1t| dkr1| d }z||t|| < ||_ W n ttfy+   Y dS 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   )	rB   rC   listrN   rT   rx   r   r   rG   )r   rB   r*   r#   r   r   rG   m  s   


z
mrow.closerh   )r   ri   rj   rk   r   rG   rm   r   r   r#   r   r   Y  s    
r   c                   @  rn   )mfraczAFractions or fraction-like objects such as binomial coefficients.Nrp   r   r   r   r   r     rq   r   c                   @  r~   )msqrtzSquare root. See also `mroot`.r   Nr   r   r   r   r   r         r   c                   @  rn   )mrootz/Roots with an explicit index. See also `msqrt`.Nrp   r   r   r   r   r     rq   r   c                   @  rn   )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.
    Nrp   r   r   r   r   r     rq   r   c                   @  rn   )merrorz#Display contents as error messages.Nrp   r   r   r   r   r     rq   r   c                   @  r~   )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    r   c                   @  rn   )mpaddedzAdjust space around content.Nrp   r   r   r   r   r     rq   r   c                   @  r~   )mphantomz8Placeholder: Rendered invisibly but dimensions are kept.r   Nr   r   r   r   r   r     r   r   c                   @  rn   )msubz$Attach a subscript to an expression.Nrp   r   r   r   r   r     rq   r   c                   @  rn   )msupz&Attach a superscript to an expression.Nrp   r   r   r   r   r     rq   r   c                   @  r~   )msubsupz;Attach both a subscript and a superscript to an expression.   Nr   r   r   r   r   r     r   r   c                   @  rn   )munderz0Attach an accent or a limit under an expression.Nrp   r   r   r   r   r     rq   r   c                   @  rn   )moverz/Attach an accent or a limit over an expression.Nrp   r   r   r   r   r     rq   r   c                   @  rn   )
munderoverz;Attach accents or limits both under and over an expression.Nrp   r   r   r   r   r     rq   r   c                   @  rn   )mtablezTable or matrix element.Nrp   r   r   r   r   r     rq   r   c                   @  rn   )mtrzRow in a table or a matrix.Nrp   r   r   r   r   r     rq   r   c                   @  rn   )mtdzCell in a table or a matrixNrp   r   r   r   r   r     rq   r   )'rk   
__future__r   __docformat__rv   xml.etree.ElementTreeetreeElementTreerV   GLOBAL_ATTRIBUTESElementr   ro   rr   r\   ry   rz   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>   sH   	 9&	
