Add test suite.
[srt2vtt.git] / srt2vtt / webvtt.scm
1 ;;; srt2vtt --- SRT to WebVTT converter
2 ;;; Copyright © 2015 David Thompson <davet@gnu.org>
3 ;;;
4 ;;; srt2vtt is free software; you can redistribute it and/or modify it
5 ;;; under the terms of the GNU General Public License as published by
6 ;;; the Free Software Foundation; either version 3 of the License, or
7 ;;; (at your option) any later version.
8 ;;;
9 ;;; srt2vtt is distributed in the hope that it will be useful, but
10 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
11 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 ;;; General Public License for more details.
13 ;;;
14 ;;; You should have received a copy of the GNU General Public License
15 ;;; along with srt2vtt.  If not, see <http://www.gnu.org/licenses/>.
16
17 (define-module (srt2vtt webvtt)
18   #:use-module (ice-9 format)
19   #:use-module (ice-9 match)
20   #:use-module (srfi srfi-1)
21   #:use-module (srfi srfi-26)
22   #:use-module (srt2vtt)
23   #:export (write-webvtt
24             write-webvtts))
25
26 (define (write-time time port)
27   "Write TIME as a WebVTT formatted timestamp to PORT."
28   (match time
29    ((h m s ms)
30     (format port "~2,'0d:~2,'0d:~2,'0d.~3,'0d" h m s ms))))
31
32 (define (write-webvtt subtitle port)
33   "Write SUBTITLE as a WebVTT formatted subtitle to PORT."
34   (format port "~a~%" (subtitle-id subtitle))
35   (write-time (subtitle-start subtitle) port)
36   (display " --> " port)
37   (write-time (subtitle-end subtitle) port)
38   (newline port)
39   (format port "~a~%" (string-join (subtitle-text subtitle) "\n"))
40   (newline port))
41
42 (define (write-webvtts subtitles port)
43   "Write all SUBTITLES as WebVTT formatted subtitles to PORT."
44   (format port "WEBVTT~%~%")
45   (for-each (cut write-webvtt <> port) subtitles))