o
    Ni!(                     @  sD  d dl mZ d dlZd dlmZ d dlmZ zd dlmZ W n e	y-   d dl
mZ Y nw zd dlmZ W n e	yE   d dlmZ Y nw d5dd	Zd
d Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zd d! Zd"d# Zd$d% Zd&d' Zd(d) Zd*d+ Zd,d- Z d.d/ Z!d6d1d2Z"d3d4 Z#dS )7    )annotationsN)nodes)Lexer)Parser)Node c                 C  s   d}t t | j}W d   n1 sw   Y  |du s"|dkr,t| | j  nt| | j d|  | j}|rQ|durSt||d  |j}|dusAdS dS dS )a  
    A convenience function to ease debugging.
    It will print the node structure that's returned from CommonMark.

    The usage would be something like:

    >>> content = Parser().parse('Some big text block
===================

with content
')
    >>> custom_walker(content)
    document
        heading
            text    Some big text block
        paragraph
            text    with content

    Spaces are used to convey nesting
    r   N	z    )	
contextlibsuppress	Exceptionliteralprinttfirst_childcustom_walkernxt)nodespacetxtcur r   F/home/ubuntu/.local/lib/python3.10/site-packages/sphinxarg/markdown.pyr      s   r   c                 C  sR   d}| j dur
| j }tdd|}| jd d |_t| D ]}|| q|S )zB
    Process a paragraph, which includes all content under it
    r   N r   )string_contentr   	paragraphjoin	sourceposlinemarkdownappend)r   textonr   r   r   r   3   s   
r   c                 C  s   t | jS )z
    Text in a paragraph
    )r   Textr   r   r   r   r   r    B   s   r    c                 C  
   t dS )z*
    A <br /> in html or "
" in ascii
    
r   r#   r$   r   r   r   	hardbreakI      
r(   c                 C  r%   )z!
    A line ending or space.
    r&   r'   r$   r   r   r   	softbreakP   r)   r*   c                 C  s<   t  }| j|d< | jr| j|d< t| D ]}||7 }q|S )zp
    A hyperlink. Note that alt text doesn't work,
    since there's no apparent way to do that in docutils
    refuriname)r   	referencedestinationtitler   r   r!   r"   r   r   r   r-   W   s   


r-   c                 C  "   t  }t| D ]}||7 }q|S )z
    An italicized section
    )r   emphasisr   r0   r   r   r   r2   e      
r2   c                 C  r1   )z
    A bolded section
    )r   strongr   r0   r   r   r   r4   o   r3   r4   c                   s   g }z j dur fddt j j ddD }W n	 ty"   Y nw dg} j dur1| j  t|dkrGtj|d}|D ]}||7 }q?ntj j|d	}t D ]}||7 }qS|S )
z
    Inline code
    Nc                   "   g | ]} j |d  |d dqS r      )classesr    inline.0_r$   r   r   
<listcomp>       zliteral.<locals>.<listcomp>long
tokennamescoder   r8   r    r8   )infor   r   r   r   lenr   r   r   renderedr8   r!   elementr"   r   r$   r   r   y   s,   




r   c                   s   g }z j dur fddt j j ddD }W n	 ty"   Y nw dg} j dur1| j  t|dkrGtj|d}|D ]}||7 }q?ntj j|d	} jd d |_	t
 D ]}||7 }q[|S )
z
    A block of code
    Nc                   r5   r6   r9   r;   r$   r   r   r>      r?   z!literal_block.<locals>.<listcomp>r@   rA   rC   r   rD   rE   )rF   r   r   r   r   rG   r   literal_blockr   r   r   rH   r   r$   r   rK      s.   




rK   c                 C  sH   t j| j| jdd}| jdur| jd d |_t| D ]}||7 }q|S )z1
    Add some raw html (possibly as a block)
    html)formatNr   )r   rawr   r   r   r   r0   r   r   r   rN      s   

rN   c                 C  s   t  S )z3
    An <hr> tag in html. This has no children
    )r   
transitionr$   r   r   r   rO      s   rO   c                 C  s   t | jj| jjS )z*
    A title node. It has no children
    )r   r/   r   r   r$   r   r   r   r/      s   r/   c                 C  sP   d}| j dur| j jdkr| j j j}tj|g|gd}t| D ]}||7 }q|S )zh
    A section in reStructuredText, which needs a title (the first child)
    This is a custom type
    r   Nheading)idsnames)r   r   r   r   sectionr   )r   r/   r!   r"   r   r   r   rS      s   

rS   c                 C  s2   t  }| jd d |_t| D ]}||7 }q|S )z
    A block quote
    r   )r   block_quoter   r   r   r0   r   r   r   rT      s
   
rT   c                 C  s(   t j| jd}| jdur| jj|d< |S )ze
    An image element

    The first child is the alt text. reStructuredText can't handle titles
    )uriNalt)r   imager.   r   r   )r   r!   r   r   r   rW      s   
rW   c                 C  r1   )z
    An item in a list
    )r   	list_itemr   r0   r   r   r   rX      r3   rX   c                 C  sX   | j d dkrtj| j d d}ntj| j d d| j d d}t| D ]}||7 }q#|S )	zc
    A list (numbered or not)
    For numbered lists, the suffix is only rendered as . in html
    typebulletbullet_char)rZ   	delimiterarabicstart)suffixenumtyper^   )	list_datar   bullet_listenumerated_listr   r0   r   r   r   	list_node   s   
rd   c                 C  s  | j }g }|dur|j}|dkr|t| n|dkr$|t| n|dkr0|t| n|dkr<|t| n|dkrH|t| n|dkrT|t| n|dkr`|t	| n|d	krl|t
| n||d
krx|t| np|dkr|t| nd|dks|dkr|t| nT|dkr|t| nH|dkr|t| n<|dkr|t| n0|dkr|t| n$|dkr|t| n|dkr|t| ntd| d |  |j}|dus	|S )zZ
    Returns a list of nodes, containing CommonMark nodes converted to docutils nodes
    Nr   r    r*   	linebreaklinkrP   emphr4   rC   
code_blockhtml_inline
html_blockrT   thematic_breakrW   listitem	MDsectionzReceived unhandled type: z. Full print of node:)r   r   r   r   r    r*   r(   r-   r/   r2   r4   r   rK   rN   rT   rO   rW   rd   rX   rS   r   prettyr   )r   r   outputr   r   r   r   r     sV   *r   c                 C  s>   | j }| j}|durd|_|dur| |_|j}|dusdS dS )z3
    Correct the nxt and parent for each child
    N)r   
last_childr   parent)rS   r   lastr   r   r   finalize_sectionE  s   rt   r7   c           	      C  s  | j }|durg }d}|dur#|jdkr|j|krd}n|j}|dus|s'dS tdd}| |_| j }|dur||jdkrS|j|krS|j durSt| || tdd}|j}|j du rq|jdkrk|j|krk|| n|| n|| |}|dus6|j durt| || d| _ d| _	|d }|D ])}|jdkrt
||d | j du r|| _ n|| j	_| |_d|_| j	|_|| _	qdS dS )	a  
    Sections aren't handled by CommonMark at the moment.
    This function adds sections to a block of nodes.
    'title' nodes with an assigned level below 'level' will be put in a child section.
    If there are no child nodes with titles of level 'level' then nothing is done
    NFrP   Trn   r   r7   )level)r   r   ru   r   r   rr   rt   r   append_childrq   nest_sectionsprev)	blockru   r   childrennestrS   r   
next_levelchildr   r   r   rw   S  s^   









)rw   c                 C  s   t  | }t| t|S )z
    Parses a block of text, returning a list of docutils nodes

    >>> parse_markdown_block("Some
====

block of text

Header
======

blah
")
    []
    )r   parserw   r   )r    ry   r   r   r   parse_markdown_block  s   r   )r   )r7   )$
__future__r   r	   docutilsr   docutils.utils.code_analyzerr   
commonmarkr   ImportError
CommonMarkcommonmark.noder   CommonMark.noder   r   r    r(   r*   r-   r2   r4   r   rK   rN   rO   r/   rS   rT   rW   rX   rd   r   rt   rw   r   r   r   r   r   <module>   sH    
!


5
A