o $aG @sdZddlZddlZddlmZddlmZmZmZm Z m Z ddl m Z ddl mZmZddlmZddlmZdd lmZdd lmZdd lmZdd lmZdd lmZmZddlm Z ddl!m"Z"erlddl#m$Z$e%e&Z'Gddde Z(d(de e)de*de e)e*fde e)fddZ+de dede)de j,fddZ-Gddde Z.Gd d!d!Z/Gd"d#d#e Z0d$d%dee)effd&d'Z1dS))z sphinx.directives.code ~~~~~~~~~~~~~~~~~~~~~~ :copyright: Copyright 2007-2021 by the Sphinx team, see AUTHORS. :license: BSD, see LICENSE for details. N) unified_diff) TYPE_CHECKINGAnyDictListTuple)nodes)ElementNode) directives) StringList)addnodes)Config) optional_int)__)logging parselinenos)SphinxDirective) OptionSpec)Sphinxc@sJeZdZUdZdZdZdZdZej ej dZ e e d<deefdd Zd S) Highlightzt Directive to set the highlighting language for code blocks, as well as the threshold for line numbers. Fr)forcelinenothreshold option_specreturncCsF|jd}|jdtj}d|jv}||jjd<tj |||dgS)Nrrrhighlight_language)langrr) argumentsstripoptionsgetsysmaxsizeenv temp_datar highlightlang)selflanguagerrr)8/usr/lib/python3/dist-packages/sphinx/directives/code.pyrun0s  z Highlight.runN)__name__ __module__ __qualname____doc__ has_contentrequired_argumentsoptional_argumentsfinal_argument_whitespacer flag positive_intrr__annotations__rr r+r)r)r)r*r!s rlinesdedentlocationrcs|s td|dStfdd|Dr!tjtd|dg}|D]}|d}|dr6|s6d}| |q%|S)NTc3s |] }|dVqdSN)r).0sr8r)r* ?zdedent_lines..z!non-whitespace stripped by dedentr9 ) textwrapr8join splitlinesanyloggerwarningrendswithappend)r7r8r9 new_lineslinenew_liner)r>r* dedent_lines;s  rN directive literal_nodecaptioncCstjdddgd}t}|jt|gdd|j|t|dtjr1t d|d }t |t|dtjr[tj |dj dg|djR}|j|_|j|_||7}||7}|St)Nr:Tzliteral-block-wrapper) literal_blockclassessourcerzInvalid caption: %s)r containerr state nested_parser content_offset isinstancesystem_messagerastext ValueErrorrQ rawsourcechildrenrUrL RuntimeError)rOrPrQcontainer_nodeparsedmsg caption_noder)r)r*container_wrapperLs(rec @s^eZdZUdZdZdZdZdZej ej e e ej ej ej ejdZeed<deefd d Zd S) CodeBlockz^ Directive for a code block with special highlighting or line numbering settings. TrrF)rlinenosr8 lineno-startemphasize-linesrQclassnamerrc s:|jj}d|j}|j|j}|jd}|rfz0t |jt |}t fdd|Dr?t j td|jdf|dfdd|D}Wntye}z|jj ||jd gWYd}~Sd}~wwd}d |jvr|j|j}|d}t||jd |d}d|}t||}d |jvsd |jvrd |d <|d|jdg7<d|jv|d<|jr|jd|d<n |jjd|jj|d<i} |d<|dur|| d<d |jvr|jd | d<|||jd} | rzt||| }Wnty} z|jj | |jd gWYd} ~ Sd} ~ ww|||gS)NrBric3|]}|kVqdSr;r)r<inlinesr)r*r?z CodeBlock.run..*line number spec is out of range(1-%d): %rrAcg|] }|kr|dqSrr)r<xror)r* z!CodeBlock.run..rLr8rgrhTrSrjrrr(rhighlight_argshl_lines linenostartrQ)rWdocumentrDcontent state_machineget_source_and_linelinenor r!lenrrFrGrHrr]reportersplitrNrrRrr$r%configrset_source_inforeadd_name) r'r}coder9linespecr{errr7literal extra_argsrQexcr)ror*r+vsf                  z CodeBlock.runN)r,r-r.r/r0r1r2r3r r4rintunchanged_required class_option unchangedrrr6rr r+r)r)r)r*rfas  rfc@seZdZgdZdedededdfddZd!d d Zd"ded e ee fde efd d Z d"d e ee fde ee ffddZ d"d e ee fde efddZd"de ed e ee fde efddZd"de ed e ee fde efddZd"de ed e ee fde efddZd"de ed e ee fde efddZd"de ed e ee fde efddZd"de ed e ee fde efddZd"de ed e ee fde efdd ZdS)#LiteralIncludeReader) ) lineno-matchrh)rrJ)rprepend) start-afterstart-at) end-beforeend-at)diffpyobject)rrh)rr)rr7)rr)rr)rr)rrfilenamer rrNcCs8||_||_|d|j|_|jdd|_|dS)Nencodingrhr)rr r!source_encodingr lineno_start parse_options)r'rr rr)r)r*__init__s  zLiteralIncludeReader.__init__cCs<|jD]\}}||jvr||jvrttd||fqdS)Nz%Cannot use both "%s" and "%s" options)INVALID_OPTIONS_PAIRr r]r)r'option1option2r)r)r*rsz"LiteralIncludeReader.parse_optionsr9c Csz2t||jdd}|}d|jvr||jd}|dWdWS1s+wYWdStyG}z ttd||d}~wty^}z ttd|j|f|d}~ww)Nstrict)rerrors tab-widthTz.Include file %r not found or reading it failedz`Encoding %r used for reading included file %r seems to be wrong, try giving an :encoding: option) openrreadr expandtabsrEOSErrorr UnicodeError)r'rr9ftextrr)r)r* read_files, (zLiteralIncludeReader.read_filecCsld|jvr |}n#|j|j|j|j|j|j|jg}|j |j |d}|D]}|||d}q$d |t |fS)NrrAr:) r show_diffpyobject_filter start_filter end_filter lines_filterprepend_filter append_filter dedent_filterrrrDr)r'r9r7filtersfuncr)r)r*rs  zLiteralIncludeReader.readcCs:||j}|jd}||}t||||j}t|S)Nr)rrr r!rlist)r'r9rK old_filename old_linesrr)r)r*rs   zLiteralIncludeReader.show_diffr7c Cs|jd}|rDddlm}||jd}|}||vr(ttd||jf||d}||d}||d|}d|jvrD||_ |S) Nrr)ModuleAnalyzerr:z,Object named %r not found in include file %rrr) r r! sphinx.pycoderfor_filer find_tagsr]rr) r'r7r9rranalyzertagsstartendr)r)r*rs     z$LiteralIncludeReader.pyobject_filtercs|jd}|rgt|t}tfdd|Dr)tjtdt|f|dd|jvrO|dtfddt |DrI|j |d7_ nt td fd d |Dgkrgt td ||j fS) Nr7c3s|] }|tkVqdSr;rrmr7r)r*r?sz4LiteralIncludeReader.lines_filter..rrrArrc3s |] \}}||kVqdSr;r))r<rnn)firstr)r*r?r@z8Cannot use "lineno-match" with a disjoint set of "lines"cs g|] }|tkr|qSr)r)r<rrr)r*rws z5LiteralIncludeReader.lines_filter..z2Line spec %r: no lines pulled from include file %r) r r!rrrFrGrHrall enumeraterr]r)r'r7r9rlinelistr))rr7r*r s(     z!LiteralIncludeReader.lines_filtercCsd|jvr|jd}d}nd|jvr|jd}d}nd}|rkt|D]6\}}||vrZ|rFd|jvr<|j|d7_||ddSd|jvrR|j|7_||dSq$|duretd|td||S) NrFrTrrz!start-after pattern not found: %szstart-at pattern not found: %s)r r!rrr])r'r7r9r inclusiverrLr)r)r*r%s,        z!LiteralIncludeReader.start_filtercCsd|jvr|jd}d}nd|jvr|jd}d}nd}|rVt|D]!\}}||vrE|r8|d|dS|dkr=q$|d|Sq$|durPtd|td||S) NrTrFrrzend-at pattern not found: %sz end-before pattern not found: %s)r r!rr])r'r7r9rrrrLr)r)r*rDs(      zLiteralIncludeReader.end_filtercCs$|jd}|r|d|d|S)NrrrB)r r!insert)r'r7r9rr)r)r*r_s z#LiteralIncludeReader.prepend_filtercCs"|jd}|r||d|S)NrJrB)r r!rJ)r'r7r9rJr)r)r*rfs z"LiteralIncludeReader.append_filtercCs$d|jvrt||jd|dS|S)Nr8rA)r rNr!)r'r7r9r)r)r*rms z"LiteralIncludeReader.dedent_filter)rNr;)r,r-r.rstrrrrrrrrrrrrrrrrrrr)r)r)r*rs $$ ((((((,rc@seZdZUdZdZdZdZdZidede j de d e j d e d e j d e j d e j de j de j de j de j de j de j de j de j de j e je je je j dZeed<deefddZdS)LiteralIncludez Like ``.. include:: :literal:``, but only warns if the include file is not found, and does not raise errors. Also has several options for selecting what to include. FrrTr8rgrhrrr(rrrr7rrrrrrJri)rQrjrkrrrc s(|jj}|jjs|jjd|jdgSd|jvr'|j |jd\}}||jd<z|j |j}|j |j d\}}|j |t||j|j}|j|d\}tj|||d} d|jv| d<|| |jdrod| d <n d |jvr{|jd | d <d |jvsd |jvsd |jvrd | d <| d|jdg7<i} | d<d|jvrt|jd} tfdd| Drtjtd|jdf|dfdd| D| d<|j| d<d|jvr|jdp|j d} t|| | } || | gWSty} z|jj| |jdgWYd} ~ Sd} ~ ww)NzFile insertion disabledryrrrArTrudiffr(rgrhrTrSrjrzric3rlr;r)rmrr)r*r?rqz%LiteralInclude.run..rrcrsrtr)rurr)r*rwrxz&LiteralInclude.run..r{r|rQ)rWr}settingsfile_insertion_enabledrrHrr r$ relfn2pathrrrnote_dependencyrrrrrRrr!rrFrGrrrer Exception)r'r}_pathr9 rel_filenamerreaderrretnoderr{rQrr)rr*r+sZ                 zLiteralInclude.runN)r,r-r.r/r0r1r2r3rr r4rrrrrrrr6rr r+r)r)r)r*rts^      rapprcCs<tdttdttdttdtddddS)N highlightz code-block sourcecodeliteralincludebuiltinT)versionparallel_read_safeparallel_write_safe)r register_directiverrfr)rr)r)r*setups    rr;)2r/r"rCdifflibrtypingrrrrrdocutilsrdocutils.nodesr r docutils.parsers.rstr docutils.statemachiner sphinxr sphinx.configrsphinx.directivesr sphinx.localer sphinx.utilrrsphinx.util.docutilsrsphinx.util.typingrsphinx.applicationr getLoggerr,rGrrrrNrVrerfrrrr)r)r)r*s6            ,SAW