]> gitweb.factorcode.org Git - factor.git/blob - extra/subrip-subtitles/subrip-subtitles.factor
core: Add words/unwords/unwords-as and use them.
[factor.git] / extra / subrip-subtitles / subrip-subtitles.factor
1 ! Copyright (C) 2014 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays calendar calendar.parser
4 io.encodings.utf8 io.files io.streams.string kernel math
5 math.parser sequences splitting ascii ;
6 IN: subrip-subtitles
7
8 ! http://en.wikipedia.org/wiki/SubRip
9 ! .srt
10
11 TUPLE: srt-chunk id begin-time end-time rect text ;
12
13 : read-srt-timestamp ( -- duration )
14     instant
15     read-00 >>hour ":" expect
16     read-00 >>minute ":" expect
17     read-00 "," expect
18     read-000 1000 /f + >>second ;
19
20 : parse-srt-timestamp ( string -- duration )
21     [ read-srt-timestamp ] with-string-reader ;
22
23 : parse-srt-chunk ( seq -- srt-chunk )
24     [ ?first string>number ]
25     [
26         ?second "  " split1
27         [ "-->" split1 [ [ blank? ] trim parse-srt-timestamp ] bi@ ]
28         [
29             [ blank? ] trim words sift [
30                 f
31             ] [
32                 [ ":" split1 nip string>number ] map
33                 first4 swapd [ 2array ] 2dip 2array 2array
34             ] if-empty
35         ] bi*
36     ]
37     [ 2 tail unlines ] tri srt-chunk boa ;
38
39 : parse-srt-lines ( seq -- seq' )
40     { "" } split harvest
41     [ parse-srt-chunk ] { } map-as ;
42
43 : parse-srt-string ( seq -- seq' )
44     lines parse-srt-lines ;
45
46 : parse-srt-file ( path -- seq )
47     utf8 file-lines parse-srt-lines ;