| Server IP : 3.147.158.171 / Your IP : 216.73.216.229 Web Server : Apache/2.4.67 (Amazon Linux) OpenSSL/3.5.5 System : Linux ip-172-31-2-178.us-east-2.compute.internal 6.1.172-216.329.amzn2023.x86_64 #1 SMP PREEMPT_DYNAMIC Wed May 20 06:31:34 UTC 2026 x86_64 User : ec2-user ( 1000) PHP Version : 8.4.21 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : OFF Directory : /tsai/repo/api/app/utilities/__pycache__/ |
Upload File : |
o
�Kjn �
@ s� d Z ddlmZ ddlZe�d�Ze�d�Zi dd�dd �d
d�dd
�dd�dd�dd�dd�dd�dd�dd�dd�dd�dd�dd �d!d"�d#d$�i d%d&�d'd$�d(d&�d)d*�d+d*�d,d-�d.d/�d0d1�d2d3�d4d5�d6d7�d8d9�d:d;�d<d=�d>d?�d@dA�dBdC��i dDdE�dFdG�dHdI�dJdK�dLdM�dNdO�dPdQ�dRdS�dTdU�dVdW�dXdY�dZd[�d\d]�d^d_�d`da�dbdc�ddde��i dfdg�dhdi�djdk�dldm�dndo�dpdq�drds�dtdu�dvdw�dxdy�dzdy�d{d|�d}d~�dd��d�d��d�d��d�d���i d�d��d�d��d�d��d�d��d�d��d�d��d�d��d�d��d�d��d�d��d�d��d�d��d�d��d�d��d�d��d�d��d�d���d�d�d�d�d�d�d�d�d�d�d�d���Ze�d�d��e ee
d�d��� d� �Ze�d�ej�Z
e�d��Ze�d��Ze�d��Ze�d��Ze�d��Ze�d��Ze�d��Zd�d�dƄZd�d�dɄZdS )�u( Inline LaTeX-math normalization for article HTML.
AI-generated article bodies occasionally wrap short formulas in TeX math
delimiters, e.g. ``$$\nabla_\text{rad} > \nabla_\text{ad}$$``. The shared
``MarkdownIt`` parser (commonmark) has no math plugin and the WordPress themes
load no MathJax/KaTeX, so the raw TeX survives rendering and leaks onto the
published page as literal ``$$...$$`` text.
`convert_math` runs on the *final* HTML (after markdown rendering, like
`normalize_citations`) and rewrites the small subset of TeX these articles use
into plain Unicode + ``<sub>``/``<sup>`` markup that renders everywhere with no
front-end dependency:
$$\nabla_\text{rad} > \nabla_\text{ad}$$
-> <span class="tsai-math">∇<sub>rad</sub> > ∇<sub>ad</sub></span>
It is a best-effort transform: constructs it does not recognize degrade to
cleaned-up text (delimiters and backslashes stripped) rather than raw TeX. It
is idempotent — once the ``$``-delimiters are gone there is nothing left to
match — so it is safe to re-run on its own output.
� )�annotationsNz@\\(?:text|mathrm|mathbf|mathit|mathsf|mathcal|operatorname)\s*\{z'\\(?:,|;|:|!|quad|qquad|thinspace|;|\s)�times� ×Zcdot� ·�div� ÷�pm� ±�mpu ∓Zlequ ≤�leZgequ ≥�geZnequ ≠�neZapproxu ≈�equivu ≡�simu ∼Zsimequ ≃Zproptou ∝Zllu ≪Zggu ≫zll zgg Z
rightarrowu →�toZ leftarrowu ←Zleftrightarrowu ↔Z
Rightarrowu ⇒Z Leftarrowu ⇐Zmapstou ↦Zinftyu ∞�partialu ∂�nablau ∇�sqrtu √�sumu ∑�produ ∏�intu ∫Zointu ∮Zodotu ⊙�oplusu ⊕�otimesu ⊗�staru ⋆�astu ∗�primeu ′�circu ∘�deg� °Zangleu ∠�inu ∈�notinu ∉Zsubsetu ⊂Zsupsetu ⊃�cupu ∪�capu ∩�forallu ∀�existsu ∃Zlangleu ⟨Zrangleu ⟩Zldotsu …Zcdotsu ⋯�alphau α�betau β�gammau γ�deltau δ�epsilonu εZ
varepsilon�zetau ζ�etau η�thetau θZvarthetau ϑ�iotau ι�kappau κ�lambdau λ�mu� µ�nuu ν�xiu ξ�piu πZvarpiu ϖ�rhou ρZvarrhou ϱ�sigmau σZvarsigmau ς�tauu τ�upsilonu υ�phiu φZvarphiu ϕ�chiu χ�psiu ψ�omegau ωu Γu Δu Θu Λu Ξu Πu Σu Υu Φu Ψu Ω)�Gamma�Delta�Theta�Lambda�Xi�Pi�Sigma�Upsilon�Phi�Psi�Omegaz\\(�|T)�key�reversez
)(?![A-Za-z])z
\$\$(.+?)\$\$z\$(?!\$)([^$\n]+?)\$z
_\{([^{}]*)\}z\^\{([^{}]*)\}z_([^\s_^{}<&])z\^([^\s_^{}<&])z$\\frac\s*\{([^{}]*)\}\s*\{([^{}]*)\}z
\\([A-Za-z]+)�tex�str�returnc C s� | }t �d|�}t�d|�}t�d|�}t�dd� |�}t�d|�}t�d|�}t�d|�}t�d|�}t �d|�}|�
dd ��
d
d �}t�dd|��� }|S )zBTranslate a single TeX math fragment (delimiters already removed).� z\1/\2�{c S s t | �d� S )N� )�_SYMBOLS�group��m� rU �(/tsai/repo/api/app/utilities/math_tex.py�<lambda>\ s z_convert_tex.<locals>.<lambda>z
<sub>\1</sub>z
<sup>\1</sup>z\1� �}z [ \t]{2,})
�_SPACING�sub�_FRAC�
_FONT_CMDS�
_SYMBOL_RE�
_SUB_BRACE�
_SUP_BRACE� _SUB_CHAR� _SUP_CHAR�
_LEFTOVER_CMD�replace�re�strip)rK �srU rU rV �_convert_texV s rh �htmlc C s@ | rd| vr| S ddd�}ddd �}t �|| �} t�|| �} | S )a� Rewrite TeX math spans in ``html`` to Unicode + ``<sub>``/``<sup>`` markup.
Handles display ``$$...$$`` and inline ``$...$`` delimiters. Inline spans are
only converted when they actually look like math (contain a backslash, brace,
or sub/superscript) so ordinary prose with currency amounts is left alone.
Idempotent and safe to run on HTML that contains no math.
�$rT �'re.Match[str]'rM rL c S s dt | �d��� d�S )N�<span class="tsai-math">rP �</span>)rh rR rS rU rU rV �
_display_replr s z#convert_math.<locals>._display_replc S s0 | � d�}t�d|�s| � d�S dt|�� d�S )NrP z[\\{}_^]r rl rm )rR re �searchrh )rT �bodyrU rU rV �_inline_replu s
z"convert_math.<locals>._inline_replN)rT rk rM rL )�_DISPLAYr[ �_INLINE)ri rn rq rU rU rV �convert_mathg s
rt )rK rL rM rL )ri rL rM rL )�__doc__�
__future__r re �compiler] rZ rQ �join�sorted�lenr^ �Srr rs r_ r` ra rb r\ rc rh rt rU rU rU rV �<module> s�
������������������������������� � � � �
�
�
�
�
���������
�
�
�
����������������������������������
��