nani/website/tools/list.scm

list.scm

1
;;; Nani Project website
2
;;; Copyright © 2020 Julien Lepiller <julien@lepiller.eu>
3
;;;
4
;;; This file is part of the Nani Project website.
5
;;;
6
;;; The Nani Project website is free software; you can redistribute it and/or modify it
7
;;; under the terms of the GNU Affero General Public License as published by
8
;;; the Free Software Foundation; either version 3 of the License, or (at
9
;;; your option) any later version.
10
;;;
11
;;; The Nani Project website is distributed in the hope that it will be useful, but
12
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
;;; GNU Affero General Public License for more details.
15
;;;
16
;;; You should have received a copy of the GNU Affero General Public License
17
;;; along with the Nani Project website.  If not, see <http://www.gnu.org/licenses/>.
18
19
(use-modules (tools i18n))
20
(use-modules (nani radk))
21
(use-modules (nani jmdict serialize))
22
(use-modules (gcrypt hash))
23
(use-modules (ice-9 match))
24
(use-modules (ice-9 format))
25
(use-modules (ice-9 binary-ports))
26
27
(define* (description dico #:key (long? #f) (lang "en"))
28
  (define radk-synopsis
29
    `(_ "Radical to Kanji dictionary from the Electronic Dictionary Research and Development Group."))
30
  (define radk-description
31
    `(_ "This dictionary allows you to enter kanji by selecting some of its
32
    components.  Tap the water component button on the bottom of the screen to
33
    access the kanji selection by component view"))
34
35
  (define (jmdict-synopsis lang)
36
    (match lang
37
      ("e" `(_ "Japanese/English dictionary from the Electronic Dictionary Research and Development Group."))
38
      ("dut" `(_ "Japanese/Dutch dictionary from the Electronic Dictionary Research and Development Group."))
39
      ("fre" `(_ "Japanese/French dictionary from the Electronic Dictionary Research and Development Group."))
40
      ("ger" `(_ "Japanese/German dictionary from the Electronic Dictionary Research and Development Group."))
41
      ("hun" `(_ "Japanese/Hungarian dictionary from the Electronic Dictionary Research and Development Group."))
42
      ("rus" `(_ "Japanese/Russian dictionary from the Electronic Dictionary Research and Development Group."))
43
      ("slv" `(_ "Japanese/Slovenian dictionary from the Electronic Dictionary Research and Development Group."))
44
      ("spa" `(_ "Japanese/Spanish dictionary from the Electronic Dictionary Research and Development Group."))
45
      ("swe" `(_ "Japanese/Swedish dictionary from the Electronic Dictionary Research and Development Group."))))
46
  (define (jmdict-description lang)
47
    `(_ "This dictionary allows you to do searches on the main view of this app.
48
        Failing to download one of these dictionaries will make the app unusable
49
        as you can't search for anything.  This dictionary can be searched for by
50
        kanji, reading (kana) and by meaning in the languages you selected."))
51
52
  (let* ((english
53
          (cond
54
            ((equal? (dico-type dico) "radk")
55
             (if long?
56
                 radk-description
57
                 radk-synopsis))
58
            ((equal? (dico-type dico) "jmdict")
59
             (let ((dico-lang (substring dico 7)))
60
               (if long?
61
                   (jmdict-description dico-lang)
62
                   (jmdict-synopsis dico-lang))))))
63
         (translated (translate english lang)))
64
    (if (and (equal? english translated) (not (equal? lang "en")))
65
        #f
66
        translated)))
67
68
(define (filesize file)
69
  (stat:size (stat file)))
70
71
(define (sha256 file)
72
  (define hash (file-sha256 file))
73
  (apply
74
    string-append
75
    (map
76
      (lambda (n)
77
        (format #f "~2,'0x" n))
78
      (array->list hash))))
79
80
(define (dico-type file)
81
  (cond
82
    ((equal? file "radicals") "radk")
83
    ((and (> (string-length file) 6) (equal? (substring file 0 6) "JMdict"))
84
     "jmdict")))
85
86
(define (entries file)
87
  (cond
88
    ((equal? (dico-type (dico-name file)) "radk")
89
     (kanji-count file))
90
    ((equal? (dico-type (dico-name file)) "jmdict")
91
     (jmdict-entry-count file))))
92
93
(define (dico-name file)
94
  (basename file ".nani"))
95
96
(match (command-line)
97
  ((_ output dicos ...)
98
   (with-output-to-file output
99
     (lambda _
100
       (for-each
101
         (lambda (dico)
102
           (let* ((sha256 (sha256 dico))
103
                  (size (filesize dico))
104
                  (name (dico-name dico))
105
                  (type (dico-type name))
106
                  (entry-count (entries dico)))
107
             (format #t "[~a]~%" name)
108
             (for-each
109
               (lambda (lang)
110
                 (let ((synopsis (description name #:lang lang))
111
                       (description (description name #:lang lang #:long? #t)))
112
                   (when synopsis
113
                     (format #t "synopsis=~a=~a~%" lang synopsis))
114
                   (when description
115
                     (format #t "description=~a=~a~%" lang description))))
116
               (filter (lambda (lang) (not (equal? lang ""))) languages))
117
             (format #t "sha256=~a~%" sha256)
118
             (format #t "size=~a~%" size)
119
             (format #t "type=~a~%" type)
120
             (format #t "entries=~a~%" entry-count)
121
             (format #t "url=~a~%" (string-append "https://nani.lepiller.eu/" dico))
122
             (format #t "~%")))
123
         dicos)))))
124