-
Notifications
You must be signed in to change notification settings - Fork 4
/
unidecode-convert.el
83 lines (74 loc) · 3.14 KB
/
unidecode-convert.el
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
;;; unidecode-convert.el --- Convert character tables -*- lexical-binding: t -*-
;;
;; Copyright (C) 2018 John Mastro
;;
;; Author: John Mastro <[email protected]>
;; Version: 0.2
;;
;; This file is not part of GNU Emacs.
;;
;; This program is free software: you can redistribute it and/or modify it under
;; the terms of the GNU General Public License as published by the Free Software
;; Foundation; either version 2 of the License, or (at your option) any later
;; version.
;;
;; This program is distributed in the hope that it will be useful, but WITHOUT
;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
;; FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
;; details.
;;
;; You should have received a copy of the GNU General Public License along with
;; this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;;
;; Convert the character tables in the Python Unidecode package to Emacs Lisp
;; data.
;;
;; See README.org for additional information.
;;; Code:
(require 'json)
(defvar unidecode-convert-python-program "python"
"The Python executable to use.")
(defvar unidecode-convert-python-script
(let* ((file (or load-file-name buffer-file-name))
(here (file-name-directory (file-chase-links file))))
(expand-file-name "unidecode_convert.py" here))
"The Python script to use.")
(defun unidecode-convert-json-file (source destination)
"Convert JSON file in SOURCE to Lisp data in DESTINATION."
(let ((print-circle t)
(data (json-read-file source)))
(with-temp-buffer
(insert ";; -*- mode: emacs-lisp -*-\n")
(prin1 data (current-buffer))
(insert "\n")
(write-region nil nil destination))))
(defun unidecode-convert-json-files (source destination)
"Convert JSON files in SOURCE to Lisp data in DESTINATION."
(let ((files (directory-files source t "\\.json\\'")))
(dolist (file files)
(let* ((base-name (file-name-nondirectory file))
(sans-ext (file-name-sans-extension base-name))
(out-file (concat "unidecode-" sans-ext ".eld"))
(out-file (expand-file-name out-file destination)))
(unidecode-convert-json-file file out-file)))))
(defun unidecode-convert (source destination)
"Convert Unidecode modules in SOURCE to Lisp data.
Save the Lisp data files in DESTINATION."
(interactive (list (read-directory-name "Source: ")
(read-directory-name "Destination: ")))
(let ((tmp (make-temp-file "unidecode-tmp-" t)))
(unwind-protect
(with-current-buffer (get-buffer-create "*unidecode-convert*")
(erase-buffer)
(let ((ret (call-process unidecode-convert-python-program
nil t nil
unidecode-convert-python-script source tmp)))
(if (zerop ret)
(progn (unidecode-convert-json-files tmp destination)
(kill-buffer (current-buffer)))
(pop-to-buffer (current-buffer))
(message "unidecode_convert.py error"))))
(delete-directory tmp t))))
(provide 'unidecode-convert)
;;; unidecode-convert.el ends here