o
    f)                     @   s   d dl Z d dlZd dlZd dlZd dlZd dlZejZejZej	Z	dddZ
dd Zdd Zd	Zd
diZg Zdd Zg dZG dd dZdd Zdd ZdS )    NTc                 C   sR   |D ]}t j|| }t j|r|  S q|r'ttd|  d  td dS )Nzfile not found: %s
    )ospathjoinexistserror_sysexit)nameinclude_pathZraise_errorifull r   ^/home/lily/lilypond-2.24.4/release/binaries/mingw/lilypond/lilypond-2.24.4/python/book_base.py	find_file'   s   
r   c                 C   s"   t ddt ddt dd| S )N>z&gt;<z&lt;&z&amp;)resub)sr   r   r   verbatim_html3   s
   r   c                 C   s   d|  d d|   S )Nz[^{}]*?(?:{z[^{}]*?z}[^{}]*?)*?r   )nr   r   r   brace_matcher:   s   r   z
line-widthZaltz[image of music]c                 C   s   t |  d S N)all_formatsappend)Zfmtr   r   r   register_formatV      r    )	Zmultiline_commentZverbatimZverbZlilypond_blockZsingleline_commentZlilypond_fileZincludeZlilypondZlilypondversionc                   @   s   e Z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d'd( Zd)d* Zd+d, Zd-S ).BookOutputFormatc                 C   s@   d | _ d | _i | _i | _g | _d| _i | _d| _t| _	d| _
d S )Nzps,pngr   z\s*,\s*)formatZdefault_extensionsnippet_resoutputhandled_extensionsZimage_formatsglobal_optionsdocument_languagedefault_snippet_optsdefault_snippet_optionssnippet_option_separator)selfr   r   r   __init__t   s   	
zBookOutputFormat.__init__c                    s2   t | j   fddtD dd  D  }|S )zList of snippet types (strings)c                    s   g | ]}| v r|qS r   r   .0xkeysr   r   
<listcomp>       z<BookOutputFormat.supported_snippet_types.<locals>.<listcomp>c                 S   s   g | ]}|t vr|qS r   )snippet_type_orderr.   r   r   r   r3      r4   )listr$   r2   r5   )r,   resr   r1   r   supported_snippet_types   s
   z(BookOutputFormat.supported_snippet_typesc                 C   s   | j |dS )z#return regex string for snippettypeN)r$   get)r,   Zsnippettyper   r   r   snippet_regexp   s   zBookOutputFormat.snippet_regexpc                 C   s
   || j kS r   )r#   )r,   r#   r   r   r   can_handle_format      
z"BookOutputFormat.can_handle_formatc                 C   s
   || j v S r   )r&   )r,   	extensionr   r   r   can_handle_extension   r<   z%BookOutputFormat.can_handle_extensionc                 C      d S r   r   )r,   Zoption_parserr   r   r   add_options      zBookOutputFormat.add_optionsc                 C   r?   r   r   )r,   r'   r   r   r   process_options   rA   z BookOutputFormat.process_optionsc                 C   s^   dg}|j r| jd7  _|d |jdkrd|_d|jvr-| jdd| 7  _d S d S )	NZepsz% -dinclude-eps-fonts -dgs-load-fonts ZpdfZlatexZpdflatexz-dseparate-page-formatsz -dseparate-page-formats=%s ,)Z
create_pdfZprocess_cmdr   Zlatex_programr   )r,   r'   Zformatsr   r   r   process_options_pdfnotdefault   s   


z.BookOutputFormat.process_options_pdfnotdefaultc                 C   s   t j|t jS r   )BookSnippetZsnippet_type_to_classr9   ZSnippet)r,   typer   r   r   snippet_class   s   zBookOutputFormat.snippet_classc                 C   s   dS Nr   r   r,   sourcer   r   r   get_document_language   rA   z&BookOutputFormat.get_document_languagec                 C   s:   |  || _t| jvr| |}|r|| jt< d S d S d S r   )rK   r(   
LINE_WIDTHr*   get_line_width)r,   rJ   Z
line_widthr   r   r   init_default_snippet_options   s   

z-BookOutputFormat.init_default_snippet_optionsc                 C   r?   r   r   rI   r   r   r   rM      rA   zBookOutputFormat.get_line_widthc                 C   s   |r	t | j|S g S r   )r   splitr+   )r,   Zoption_stringr   r   r   split_snippet_options   s   z&BookOutputFormat.split_snippet_optionsc                 C   s   t || jjS r   )r   r'   r   )r,   Zinput_filenamer   r   r   input_fullname   r!   zBookOutputFormat.input_fullnamec                 C      |S r   r   )r,   cmdr   r   r   adjust_snippet_command   rA   z'BookOutputFormat.adjust_snippet_commandc                 C   rR   r   r   )r,   Zchunksr   r   r   process_chunks   rA   zBookOutputFormat.process_chunksc                 C   s   t td dS )NzOutput function not implementedr   )warningr
   )r,   basenamesnippetr   r   r   snippet_output   s   zBookOutputFormat.snippet_outputc                 C   s   | j |d|  S rH   )r%   r9   get_replacements)r,   rF   rX   r   r   r   output_simple   s   zBookOutputFormat.output_simplec                 C   s   | j |d| S rH   )r%   r9   )r,   rF   Z	variablesr   r   r   output_simple_replacements   s   z+BookOutputFormat.output_simple_replacementsc                 C   sP   d}|  }tj|jv r&||d< tj|j|d< |j|d< | j	tj | }|S )Nr   basefilenameext)
rZ   book_snippetsZPRINTFILENAMEZoption_dictr   r   rW   r^   r_   r%   )r,   rW   rX   r   Zrepr   r   r   output_print_filename   s   
z&BookOutputFormat.output_print_filenamec                 C   s   g S r   r   )r,   rX   r]   r   required_filesr   r   r   rb      rA   zBookOutputFormat.required_filesc                 C   sj   g }|d |v r3|j js3t|d }|dkr ||d  |S td|d D ]}||d|   q'|S )Nz.epsr   z.pngz-page%d.png)r'   Zskip_png_checkrE   Zps_page_countr   range)r,   rX   r]   r   rb   r7   Z
page_countZpager   r   r   required_files_png   s   z#BookOutputFormat.required_files_pngN)__name__
__module____qualname__r-   r8   r:   r;   r>   r@   rB   rD   rG   rK   rN   rM   rP   rQ   rT   rU   rY   r[   r\   ra   rb   rd   r   r   r   r   r"   s   s.    
r"   c                 C   sT   dg}d}t | }	 | d|}|dk rn|d }|| |}q
|t |  |S )z=Return a list of indices indicating the first char of a line.r   Tr   r   )lenfindr   )r   Znlsstartendr   r   r   r   find_linestarts   s   
	rl   c              	   C   s  i }|  }|D ]}t||||< qg }d}tdd |D }t| }	d}
	 d }d}|D ]f}|| r=|| d |k r{d ||< || | ||}|sMq/||}|d}|
}|	| |k ri|d7 }|	| |k s_|d7 }||||||}||f||< || r|r|| d || d k r|}|| d }q/|s|	t
| |t| |
 	 |S ||	|
d  kr|
d7 }
||	|
d  ks|| \}}|	t
| |||
d  |	| d ||< |t|jd }q))Nr   c                 S   s   g | ]}|d fqS r   r   )r/   tr   r   r   r3   
  s    z*find_toplevel_snippets.<locals>.<listcomp>Ti   @matchr   )r8   r   compiler:   dictrl   searchrG   rj   r   rE   Z	Substringrh   rn   group)Zinput_stringZ	formatterr'   r7   typesrm   ZsnippetsindexfoundZline_startsZline_start_idxfirstZendexrF   mklassrj   Zline_numberZsnipr   r   r   find_toplevel_snippets  sf   


ry   )T)r   r   r   r`   rE   ZlilylibZlyZprogressrV   r	   r   r   r   rL   r)   r   r    r5   r"   rl   ry   r   r   r   r   <module>   s(   
}