1 ! Copyright (C) 2014 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays calendar calendar.format
4 io.encodings.utf8 io.files io.streams.string kernel math
5 math.parser sequences splitting ascii ;
8 ! http://en.wikipedia.org/wiki/SubRip
11 TUPLE: srt-chunk id begin-time end-time rect text ;
13 : read-srt-timestamp ( -- duration )
15 read-00 >>hour ":" expect
16 read-00 >>minute ":" expect
18 read-000 1000 /f + >>second ;
20 : parse-srt-timestamp ( string -- duration )
21 [ read-srt-timestamp ] with-string-reader ;
23 : parse-srt-chunk ( seq -- srt-chunk )
24 [ ?first string>number ]
27 [ "-->" split1 [ [ blank? ] trim parse-srt-timestamp ] bi@ ]
29 [ blank? ] trim " " split sift [
32 [ ":" split1 nip string>number ] map
33 first4 swapd [ 2array ] 2dip 2array 2array
37 [ 2 tail "\n" join ] tri srt-chunk boa ;
39 : parse-srt-lines ( seq -- seq' )
41 [ parse-srt-chunk ] { } map-as ;
43 : parse-srt-string ( seq -- seq' )
44 string-lines parse-srt-lines ;
46 : parse-srt-file ( path -- seq )
47 utf8 file-lines parse-srt-lines ;