
    Sh                         d dl Z d dlZd dlmZmZmZmZmZmZ ddl	m
Z
 ddlmZ ddlmZ ddlmZ dd	lmZmZmZmZ dd
lmZ  ed          dz
  Z G d de
          ZdS )    N)ListSequenceOptionalTextIOAnyTuple   )
FormatBase   )SSAEvent)SSAStyle)
parse_tags)ms_to_times	make_time	TIMESTAMPtimestamp_to_ms)SSAFiled   )hc                   .   e Zd ZdZeZededefd            Zede	e         defd            Z
ededee         fd            Ze	 	 dd
ddedededededdfd            Ze	 	 dd
ddedededededdfd            Zed
ddee         fd            ZdS )SubripFormatz0SubRip Text (SRT) subtitle format implementationmsreturnc                     | dk     rd} | t           k    r!t          j        dt                     t           } t	          |           \  }}}} |dd|dd|dd| dS )zConvert ms to 'HH:MM:SS,mmm'r   z@Overflow in SubRip timestamp, clamping to MAX_REPRESENTABLE_TIME02d:,03d)MAX_REPRESENTABLE_TIMEwarningswarnRuntimeWarningr   )r   r   mss       \/var/www/html/movieo_spanner_bot/venv/lib/python3.11/site-packages/pysubs2/formats/subrip.pyms_to_timestampzSubripFormat.ms_to_timestamp   sz     66B&&&M\^lmmm'B!"oo1a222!222!222"2222    groupsc                      t          |           S N)r   )r(   s    r%   r   zSubripFormat.timestamp_to_ms    s    v&&&r'   textc                    d|v sd|v rdS |                                                     d          rdS d|v rdS |                                D ]0}t          | j                            |                    dk    r dS 1dS )z5See :meth:`pysubs2.formats.FormatBase.guess_format()`z[Script Info]z[V4+ Styles]NWEBVTTzhttp://www.w3.org/ns/ttmlr   srt)lstrip
startswith
splitlineslenr   findall)clsr+   lines      r%   guess_formatzSubripFormat.guess_format$   s     d""n&<&<4;;==##H-- 	4&$..4OO%% 	 	D3=((..//144uu 5 tr'   Fsubsr   fpformat_keep_html_tagskeep_unknown_html_tagskwargsNc                    g }g }|D ]}	| j                             |	          }
t          |
          dk    rEt          | j        |
          \  }}|                    ||f           |                    g            t|r|d                             |	           dt          t                   dt          ffd}t          ||          D ]8\  \  }}}t          || ||                    }|                    |           9dS )a~  
        See :meth:`pysubs2.formats.FormatBase.from_file()`

        Supported tags:

          - ``<i>``
          - ``<u>``
          - ``<s>``
          - ``<b>``

        Keyword args:
            keep_html_tags: If True, all HTML tags will be kept as-is instead of being
                converted to SubStation tags (eg. you will get ``<i>example</i>`` instead of ``{\i1}example{\i0}``).
                Setting this to True overrides the ``keep_unknown_html_tags`` option.
            keep_unknown_html_tags: If True, supported HTML tags will be converted
                to SubStation tags and any other HTML tags will be kept as-is
                (eg. you would get ``<blink>example {\i1}text{\i0}</blink>``).
                If False, these other HTML tags will be stripped from output
                (in the previous example, you would get only ``example {\i1}text{\i0}``).
        r   linesr   c                    t          |           dk    r>t          d | d d         D                       rt          j        d| d                   rdS d                    |                                           }t          j        dd|          }st          j        dd|          }t          j        d	d
|          }t          j        dd|          }t          j        dd|          }t          j        dd|          }t          j        dd|          }t          j        dd|          }t          j        dd|          }sst          j        dd|          }t          j        dd|          }|S )Nr   c              3   @   K   | ]}t          j        d |          V  dS )z\s*$N)rematch).0r5   s     r%   	<genexpr>z?SubripFormat.from_file.<locals>.prepare_text.<locals>.<genexpr>a   s.      KKBHWd33KKKKKKr'   r>   z
\s*\d+\s*$ z\n+ *\d+ *$z< *i *>z{\\i1}z
< */ *i *>z{\\i0}z< *s *>z{\\s1}z
< */ *s *>z{\\s0}z< *u *>z{\\u1}z
< */ *u *>z{\\u0}z< *b *>z{\\b1}z
< */ *b *>z{\\b0}z< */? *[a-zA-Z][^>]*>\nz\\N)r2   allrB   rC   joinstripsub)r?   r$   r:   r;   s     r%   prepare_textz,SubripFormat.from_file.<locals>.prepare_text]   sf    E

aKKcrc
KKKKK  b	::   r $$&&A~r1--A! 8F:y!44F=)Q77F:y!44F=)Q77F:y!44F=)Q77F:y!44F=)Q77" <&< <F3R;;ufa((AHr'   )startendr+   N)
r   r3   r2   mapr   appendr   strzipr   )r4   r7   r8   r9   r:   r;   r<   
timestampsfollowing_linesr5   stampsrM   rN   rL   r?   es       ``          r%   	from_filezSubripFormat.from_file9   s>   . -/
+- 	5 	5D]**400F6{{a !4f==
s!!5#,///&&r**** 5#B'..t444	S	 	c 	 	 	 	 	 	 	2 $'z?#C#C 	 	LUC%u#LL4G4GHHHAKKNNNN	 	r'   Tapply_styleskeep_ssa_tagsc                    dt           dt          dt           ffd}t          |                               d          D ]\  }}	|                     |	j                  }
|                     |	j                  } ||	j        j        	                    |	j
        t          j                            }t          ||           t          |
d||           t          |d|	           |dz  }d
S )a  
        See :meth:`pysubs2.formats.FormatBase.to_file()`

        Italic, underline and strikeout styling is supported.

        Keyword args:
            apply_styles: If False, do not write any styling (ignore line style
                and override tags).
            keep_ssa_tags: If True, instead of trying to convert inline override
                tags to HTML (as supported by SRT), any inline tags will be passed
                to output (eg. ``{\an7}``, which would be otherwise stripped;
                or ``{\b1}`` instead of ``<b>``). Whitespace tags ``\h``, ``\n``
                and ``\N`` will always be converted to whitespace regardless of
                this option. In the current implementation, enabling this option
                disables processing of line styles - you will get inline tags but
                if for example line's style is italic you will not get ``{\i1}``
                at the beginning of the line. (Since this option is mostly useful
                for dealing with non-standard SRT files, ie. both input and output
                is SRT which doesn't use line styles - this shouldn't be much
                of an issue in practice.)
        r+   styler   c                    |                      dd          } |                      dd          } |                      dd          } g }r|                    |            nZt          | |j                  D ]C\  }}r'|j        rd| d}|j        rd| d	}|j        rd
| d}|                    |           Dt          j        ddd	                    |          
                                          S )Nz\h rG   
z\Nz<i>z</i>z<u>z</u>z<s>z</s>z
+rF   )replacerP   r   stylesitalic	underline	strikeoutrB   rK   rI   rJ   )r+   r[   bodyfragmentstyrX   rY   r7   s        r%   rL   z*SubripFormat.to_file.<locals>.prepare_text   s   <<s++D<<t,,D<<t,,DD *D!!!!%/eT[%I%I * *MHc# <: <';X';';';H= <';X';';';H= <';X';';';HKK))))6%rwwt}}':':'<'<===r'   r	   )filez-->z

)rN   rg   N)rQ   r   	enumerate_get_visible_linesr&   rM   rN   r+   r`   getr[   DEFAULT_STYLEprint)r4   r7   r8   r9   rX   rY   r<   rL   linenor5   rM   rN   r+   s    `  ``       r%   to_filezSubripFormat.to_filez   s   0	>s 	>8 	> 	> 	> 	> 	> 	> 	> 	> 	>* &c&<&<T&B&BAFF 	 	LFD''
33E%%dh//C<	4;??4:xG]+^+^__D&r""""%"----$F,,,,aKFF	 	r'   c                 *    |                                 S r*   )get_text_events)r4   r7   s     r%   ri   zSubripFormat._get_visible_lines   s    ##%%%r'   )FF)TF)__name__
__module____qualname____doc__r   staticmethodintrQ   r&   r   r   classmethodr   r6   r   boolr   rW   rn   r   r   ri    r'   r%   r   r      s       ::I3C 3C 3 3 3 \3 ' '# ' ' ' \'      [( Y^16> >Y >F >S >RV >*.>BE>JN> > > [>@ UY&+4 49 4& 43 4d 4#47:4?C4 4 4 [4l &i &DN & & & [& & &r'   r   )rB   r    typingr   r   r   r   r   r   baser
   ssaeventr   ssastyler   
substationr   timer   r   r   r   ssafiler   r   r   ry   r'   r%   <module>r      s   				  ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?                   " " " " " " E E E E E E E E E E E E       #S)))A- b& b& b& b& b&: b& b& b& b& b&r'   