
    lj%                        d Z ddlmZ dgZddlZddlmZ ddlmZm	Z	 ddl
mZmZ ddlmZ dd	lmZ dd
lmZmZmZ ddlmZ ddlT ddlmZ ddlmZ ddlmZ ddlmZm Z  ddl!m"Z" ddl#m$Z$m%Z%  G d de e          Z&dS )z6Mobject representing highlighted source code listings.    )annotationsCodeN)Path)AnyLiteral)BeautifulSoupTag)	highlight)HtmlFormatter)get_lexer_by_nameguess_lexerguess_lexer_for_filename)get_all_styles)*Dot)SurroundingRectangle)ConvertToOpenGL)VGroupVMobject)StrPath)WHITE
ManimColorc                       e Zd ZU dZdZded<   d ed          edddd	Zd
ed<   dddddZ	d
ed<   ded<   	 	 	 	 	 	 	 	 	 	 d,d- fd(Z
ed.d+            Z xZS )/r   aS
  A highlighted source code listing.

    Examples
    --------

    Normal usage::

        listing = Code(
            "helloworldcpp.cpp",
            tab_width=4,
            formatter_style="emacs",
            background="window",
            language="cpp",
            background_config={"stroke_color": WHITE},
            paragraph_config={"font": "Noto Sans Mono"},
        )

    We can also render code passed as a string. As the automatic language
    detection can be a bit flaky, it is recommended to specify the language
    explicitly:

    .. manim:: CodeFromString
        :save_last_frame:

        class CodeFromString(Scene):
            def construct(self):
                code = '''from manim import Scene, Square

        class FadeInSquare(Scene):
            def construct(self):
                s = Square()
                self.play(FadeIn(s))
                self.play(s.animate.scale(2))
                self.wait()'''

                rendered_code = Code(
                    code_string=code,
                    language="python",
                    background="window",
                    background_config={"stroke_color": "maroon"},
                )
                self.add(rendered_code)

    Parameters
    ----------
    code_file
        The path to the code file to display.
    code_string
        Alternatively, the code string to display.
    language
        The programming language of the code. If not specified, it will be
        guessed from the file extension or the code itself.
    formatter_style
        The style to use for the code highlighting. Defaults to ``"vim"``.
        A list of all available styles can be obtained by calling
        :meth:`.Code.get_styles_list`.
    tab_width
        The width of a tab character in spaces. Defaults to 4.
    add_line_numbers
        Whether to display line numbers. Defaults to ``True``.
    line_numbers_from
        The first line number to display. Defaults to 1.
    background
        The type of background to use. Can be either ``"rectangle"`` (the
        default) or ``"window"``.
    background_config
        Keyword arguments passed to the background constructor. Default
        settings are stored in the class attribute
        :attr:`.default_background_config` (which can also be modified
        directly).
    paragraph_config
        Keyword arguments passed to the constructor of the
        :class:`.Paragraph` objects holding the code, and the line
        numbers. Default settings are stored in the class attribute
        :attr:`.default_paragraph_config` (which can also be modified
        directly).
    Nzlist[str] | None_styles_list_cacheg333333?z#222g?   )buff
fill_colorstroke_colorcorner_radiusstroke_widthfill_opacityzdict[str, Any]default_background_config	Monospace   g      ?T)font	font_sizeline_spacingdisable_ligaturesdefault_paragraph_configr   codevim   	rectangle	code_fileStrPath | Nonecode_string
str | Nonelanguageformatter_stylestr	tab_widthintadd_line_numbersboolline_numbers_from
backgroundLiteral['rectangle', 'window']background_configdict[str, Any] | Noneparagraph_configc           
     ,
   t                                                       |;t          |          }|                    d          }t	          |j        |          }n3|"|t          |          }nt          |          }nt          d          |	                    |          }t          |dd          }t          t          |||          d	          }|                    d
          | _        t          | j        t                     sJ g }g }d}| j        j        D ]}|j        dk    r	 |d         }t%          j        d|          }|d n|                    d          }n# t*          $ r d }Y nw xY w|                    ||t/          |j                  z   |f           |t/          |j                  z  }|j        D ]'}|dk    r|                    |           g }d}"|dz  }(|                    |           | j                                                            d                              d          }|
i }
| j                                        }|                    |
           ddlm }  ||i || _!        tE          | j!        |d          D ]+\  }}|D ]#\  }}}|||         #                    |           $,|r|                    ddi            |d tI          ||t/          | j!                  z             D             i || _%        | j%        &                    | j!        tN                    (                    | j!        tR                     | *                    | j%                   | j!        D ]}d |D             |_+        | *                    | j!                   |	i }	| j,                                        }|                    |	           |dk    rt[          | fi || _.        n|dk    rt_          d dD                       0                    tb          d          }|&                    | tR          d          (                    | tN                    2                    tN          dz             t[          t_          | |          fi || _.        |2                    tR          dz  tN          dz  z              | j.        *                    |           nt          d|           | 3                    | j.                   d S ) Nzutf-8)encodingz6Either a code file or a code string must be specified.)tabsizeT )style	noclasses
cssclasseszhtml.parser)featuresprer   spanrD   z(color: (#[A-Fa-f0-9]{6}|#[A-Fa-f0-9]{3})r   
)	ParagraphF)strict	alignmentrightc                ,    g | ]}t          |          S  )r5   ).0is     Z/home/agentuser/manim-venv/lib/python3.11/site-packages/manim/mobject/text/code_mobject.py
<listcomp>z!Code.__init__.<locals>.<listcomp>   s.        FF      )	directionc                <    g | ]}t          |t                    |S rP   )
isinstancer   )rQ   cs     rS   rT   z!Code.__init__.<locals>.<listcomp>   s'    JJJaz!S7I7IJJJJrU   r.   windowc              3  :   K   | ]}t          d d|          V  dS )皙?r   )radiusr!   colorNr   )rQ   button_colors     rS   	<genexpr>z Code.__init__.<locals>.<genexpr>   sE          3QlCCC     rU   )z#ff5f56z#ffbd2ez#27c93fr\   )r   zUnknown background type: )4super__init__r   	read_textr   namer   r   
ValueError
expandtabsr   r   r
   find
_code_htmlrX   r	   childrenrematchgroupKeyErrorappendlentextget_textremovesuffixsplitr*   copyupdatemanim.mobject.text.text_mobjectrK   
code_lineszip	set_colorrangeline_numbersnext_toLEFTalign_toUPaddsubmobjectsr#   r   r;   r   arrangeRIGHTshiftadd_to_back) selfr/   r1   r3   r4   r6   r8   r:   r;   r=   r?   lexer	formattersoupcolor_rangescurrent_line_color_rangescurrent_line_char_indexchildchild_stylematch_r^   charrw   base_paragraph_configrK   linecolor_rangestartendbackground_config_basebuttons	__class__s                                   rS   rb   zCode.__init__|   s    	 YI#--w-??K,Y^[IIEE$#)(33#K00UVVV!,,Y,??!!
 
 
	
 k5)44}
 
 
 ))E**$/3///// $&!"#_- 	5 	5EzV##!"'.KXC[ F %+NDDQEE ! ! ! EEE!)00//#ej//A   (3uz??:''!J 5 5Dt||$++,EFFF46123///14//5 	5666_--//<<TBBHHNN
#! $ = B B D D$$%5666======#)
#
 
 "%T_l5!Q!Q!Q 	1 	1D+%0 1 1!sEU3Y))%00001  	(!((+w)?@@@ )	 ")+<s4??S?S+S   ! (! !D %%do%FFOO   HHT&'''O 	K 	KDJJ4JJJD!!!$ "!%!?!D!D!F!F%%&7888$$2 ( DOO 8##  $E     ge#g&&  OOD"3O//88tDDJJ4RU:VVV2tW%% ( DO MM"s(TCZ/000O((((EEEFFF)))))s   06E''E65E6return	list[str]c                ^    | j          t          t                                | _         | j         S )z/Get the list of all available formatter styles.)r   listr   )clss    rS   get_styles_listzCode.get_styles_list  s,     !)%).*:*:%;%;C"%%rU   )
NNNr,   r-   Tr   r.   NN)r/   r0   r1   r2   r3   r2   r4   r5   r6   r7   r8   r9   r:   r7   r;   r<   r=   r>   r?   r>   )r   r   )__name__
__module____qualname____doc__r   __annotations__r   r   r#   r*   rb   classmethodr   __classcell__)r   s   @rS   r   r      s$        L L\ ,0//// j((1 1     !	0 0     NNN %)"&#$!%!"5@3726G* G* G* G* G* G* G*R & & & [& & & & &rU   )	metaclass)'r   
__future__r   __all__rj   pathlibr   typingr   r   bs4r   r	   pygmentsr
   pygments.formatters.htmlr   pygments.lexersr   r   r   pygments.stylesr   manim.constantsmanim.mobject.geometry.arcr   %manim.mobject.geometry.shape_matchersr   )manim.mobject.opengl.opengl_compatibilityr   &manim.mobject.types.vectorized_mobjectr   r   manim.typingr   manim.utils.colorr   r   r   rP   rU   rS   <module>r      s   < < " " " " " "  
			               " " " " " " " "       2 2 2 2 2 2 T T T T T T T T T T * * * * * *     * * * * * * F F F F F F E E E E E E C C C C C C C C             / / / / / / / /n& n& n& n& n&8 n& n& n& n& n& n&rU   