blob: e53ca1a8111a8522077e1d1070da4b7914384227 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
#!/usr/bin/env sh
exec guile -e '(@ (ppag) main)' -s "${0}" "${@}"
!#
(define-module (ppag))
(export main)
(use-modules (ice-9 match))
(use-modules (ice-9 pretty-print))
(use-modules (ice-9 rdelim))
(define (read-file filename)
"Read the entire contents of the provided file
Reads the file line by line, accumulating the result into a list. Stops when
reaching the EOF marker. `cons' will construct the list in reverse, reverse at
the end.
We assume the file provided exists and is readable."
(define (iter fh acc)
(let ((line (read-line fh)))
(if (eof-object? line)
acc
(iter fh (cons line acc)))))
(let ((fh (open-input-file filename)))
(reverse (iter fh '()))))
(define (json-encode-line line)
"JSON encode the provided line."
(string-join (append '("\"") (map json-encode-char (string->list line)) '("\"")) ""))
(define (json-encode-char char)
"JSON encode character
We need to take special care around quotes and backslack characters."
(cond ((eq? char #\") "\"")
((eq? char #\\) "\\")
(else
(list->string (list char)))))
(define (json-encode-file filename)
"JSON encode the file, line by line"
(map json-encode-line (read-file filename)))
(define (main args)
(match args
((_ input-file)
(display (string-join (json-encode-file input-file) ",\n"))
(newline))
(_ (error "Unrecognized usage."))))
|