Add test suite.
[srt2vtt.git] / srt2vtt / ui.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 ui)
18   #:use-module (ice-9 format)
19   #:use-module (ice-9 match)
20   #:use-module (srfi srfi-1)
21   #:use-module (srfi srfi-37)
22   #:use-module (srt2vtt subrip)
23   #:use-module (srt2vtt webvtt)
24   #:export (main))
25
26 (define (show-help-and-exit)
27   (format #t "Usage: srt2vtt [OPTIONS]
28 Convert SubRip formatted subtitles to WebVTT format.~%")
29   (display "
30   -h, --help             display this help and exit")
31   (display "
32   -v, --version          display version and exit")
33   (display "
34   -i, --input=FILE-NAME  read input from FILE-NAME")
35   (display "
36   -o, --output=FILE-NAME write output to FILE-NAME")
37   (newline)
38   (exit 0))
39
40 (define (show-version-and-exit)
41   (format #t "srt2vtt 0.1~%")
42   (exit 0))
43
44 (define (show-usage-and-exit)
45   (format #t "Try `srt2vtt --help' for more information.~%")
46   (exit 1))
47
48 (define (show-wrong-args-and-exit)
49   (format #t "Invalid arguments~%")
50   (show-usage-and-exit))
51
52 (define %default-args
53   `((input . ,(current-input-port))
54     (output . ,(current-output-port))))
55
56 (define %options
57   (list (option '(#\h "help") #f #f
58                 (lambda (opt name arg args)
59                   (show-help-and-exit)))
60         (option '(#\v "version") #f #f
61                 (lambda (opt name arg args)
62                   (show-version-and-exit)))
63         (option '(#\i "input") #t #f
64                 (lambda (opt name arg args)
65                   (alist-cons 'input arg args)))
66         (option '(#\o "output") #t #f
67                 (lambda (opt name arg args)
68                   (alist-cons 'output arg args)))))
69
70 (define (make-call-with-port-or-file file-proc)
71   "Create a new procedure that accepts two arguments: a port or file,
72 and a procedure.  When the returned procedure is called with a port as
73 the first argument, the second argument is simply applied with that
74 port.  If the first argument is a string, the second argument is
75 applied with the port corresponding to the file name contained in the
76 string as opened by FILE-PROC."
77   (lambda (port-or-file proc)
78     (if (port? port-or-file)
79         (proc port-or-file)
80         (file-proc port-or-file proc))))
81
82 (define call-with-port-or-input-file
83   (make-call-with-port-or-file call-with-input-file))
84
85 (define call-with-port-or-output-file
86   (make-call-with-port-or-file call-with-output-file))
87
88 (define (parse-opts args)
89   "Parse the list of ARGS and return a list of option/value pairs.
90 When an option isn't specified in ARGS, it defaults to the value in
91 '%default-args'."
92   (args-fold args
93              %options
94              (lambda (opt name arg args)
95                (error "Unrecognized option '~a'" name))
96              (lambda (arg args)
97                (error "Extraneous argument '~a'" arg))
98              %default-args))
99
100 (define (convert input-port output-port)
101   "Read the SubRip formatted subtitles from INPUT-PORT and write the
102 WebVTT equivalents to OUTPUT-PORT."
103   (write-webvtts (read-subrips input-port) output-port))
104
105 (define (main args)
106   "srt2vtt entry point."
107   (let ((opts (parse-opts args)))
108     (call-with-port-or-input-file (assoc-ref opts 'input)
109       (lambda (input-port)
110         (call-with-port-or-output-file (assoc-ref opts 'output)
111           (lambda (output-port)
112             (convert input-port output-port)))))))