branch: externals/pyim commit d7dcc568cc77e6c9b94c2aa3ccf97e3be55759f7 Author: Feng Shu <tuma...@163.com> Commit: Feng Shu <tuma...@163.com>
让 pyim-cloudim 不依赖 json-parse-buffer * tests/pyim-tests.el (pyim-tests-pyim-cloudim): * pyim-cloudim.el (json, pyim-cloudim:baidu) (pyim-cloudim-parse-baidu-buffer, pyim-cloudim:google) (pyim-cloudim-parse-google-buffer): * README.org (使用云输入法): --- README.org | 8 -------- pyim-cloudim.el | 38 +++++++++++++++++--------------------- tests/pyim-tests.el | 35 +++++++++++++++++------------------ 3 files changed, 34 insertions(+), 47 deletions(-) diff --git a/README.org b/README.org index 002610c981..a886cf23b8 100644 --- a/README.org +++ b/README.org @@ -170,14 +170,6 @@ pyim 可以使用搜索引擎提供的云输入法服务,比如: ;; (setq pyim-cloudim 'google) #+end_example -不过,由于 json 解析库的原因,Emacs 旧版本可能无法使用云输入法,可以通过下面的函 -数测试一下。 - -#+begin_example -(pyim-cloudim:baidu "nihao" 'pinyin) -#+end_example - - ** 使用双拼模式 pyim 支持双拼输入模式,用户可以通过变量 `pyim-default-scheme' 来设定: diff --git a/pyim-cloudim.el b/pyim-cloudim.el index 9334865f7c..32af111b98 100644 --- a/pyim-cloudim.el +++ b/pyim-cloudim.el @@ -29,7 +29,6 @@ ;; * 代码 :code: (require 'cl-lib) (require 'url) -(require 'json) (require 'pyim-candidates) (defgroup pyim-cloudim nil @@ -55,8 +54,7 @@ (defun pyim-cloudim:baidu (string scheme-name) "使用 baidu 云输入法引擎搜索 STRING, 获取词条列表。" - (when (and (equal scheme-name 'quanpin) - (functionp 'json-parse-buffer)) + (when (equal scheme-name 'quanpin) (with-current-buffer (pyim-cloudim-url-retrieve-sync (format "https://olime.baidu.com/py?py=%s" string) t nil 0.1) @@ -91,14 +89,14 @@ (float-time (time-since start-time))) (throw 'done 'timeout)) (url-debug 'retrieval - "Spinning in url-retrieve-synchronously: nil (%S)" + "Spinning in pyim-cloudim-url-retrieve-sync: nil (%S)" proc-buffer) (when-let ((redirect-buffer (buffer-local-value 'url-redirect-buffer proc-buffer))) (unless (eq redirect-buffer proc-buffer) (url-debug - 'retrieval "Redirect in url-retrieve-synchronously: %S -> %S" + 'retrieval "Redirect in pyim-cloudim-url-retrieve-sync: %S -> %S" proc-buffer redirect-buffer) (let (kill-buffer-query-functions) (kill-buffer proc-buffer)) @@ -121,31 +119,29 @@ data-buffer))) (defun pyim-cloudim-parse-baidu-buffer () - "解析 `url-retrieve-synchronously' 返回的 baidu buffer." - (goto-char (point-min)) - (search-forward "\n\n" nil t) - (delete-region (point-min) (point)) - (let ((data (funcall 'json-parse-buffer))) - (kill-buffer) - (list (elt (elt (elt (gethash "0" data) 0) 0) 0)))) + "解析 `pyim-cloudim-url-retrieve-sync' 返回的 baidu buffer." + ;; NOTE: 以前这个函数使用 `json-parse-buffer' 来处理返回的结果,但因为旧版本 + ;; Emacs 没有 `json-parse-buffer' 函数,所以现在改用这种简单粗暴的方式,虽然没 + ;; 有使用 json 得到的结果精确,但应该适用于大多数情况,同时也减少了一个包依赖。 + (let ((word (replace-regexp-in-string + "\\CC" "" + (decode-coding-string + (buffer-string) + 'utf-8)))) + (when (> (length word) 0) + (list word)))) (defun pyim-cloudim:google (string scheme-name) "使用 google 云输入法引擎搜索 STRING, 获取词条列表。" - (when (and (eq scheme-name 'quanpin) - (functionp 'json-parse-buffer)) + (when (eq scheme-name 'quanpin) (with-current-buffer (pyim-cloudim-url-retrieve-sync (format "https://www.google.cn/inputtools/request?ime=pinyin&text=%s" string) t nil 0.1) (pyim-cloudim-parse-google-buffer)))) (defun pyim-cloudim-parse-google-buffer () - "解析 `url-retrieve-synchronously' 返回的 google buffer." - (goto-char (point-min)) - (search-forward "\n\n" nil t) - (delete-region (point-min) (point)) - (let ((data (funcall 'json-parse-buffer))) - (kill-buffer) - (list (elt (elt (elt (elt data 1) 0) 1) 0)))) + "解析 `pyim-cloudim-url-retrieve-sync' 返回的 google buffer." + (pyim-cloudim-parse-baidu-buffer)) ;; * Footer (provide 'pyim-cloudim) diff --git a/tests/pyim-tests.el b/tests/pyim-tests.el index 6bb1f114d3..fcfc1a75dc 100644 --- a/tests/pyim-tests.el +++ b/tests/pyim-tests.el @@ -939,18 +939,17 @@ yin-xing 因行 (should (eq (length words) 51)))) (ert-deftest pyim-tests-pyim-cloudim () - (when (functionp 'json-parse-buffer) - (with-temp-buffer - (insert "HTTP/1.1 200 OK + (with-temp-buffer + (insert "HTTP/1.1 200 OK Content-Length: 88 Content-Type: text/plain; charset=utf-8 Date: Sun, 08 May 2022 00:56:13 GMT {\"0\":[[[\"你好\",5,{\"pinyin\":\"ni'hao\",\"type\":\"IMEDICT\"}]]],\"1\":\"ni'hao\",\"result\":[null]}") - (should (equal (pyim-cloudim-parse-baidu-buffer) '("你好")))) + (should (equal (pyim-cloudim-parse-baidu-buffer) '("你好")))) - (with-temp-buffer - (insert "HTTP/1.1 200 OK + (with-temp-buffer + (insert "HTTP/1.1 200 OK Date: Sun, 08 May 2022 03:33:56 GMT Pragma: no-cache Expires: -1 @@ -966,23 +965,23 @@ Alt-Svc: h3=\":443\"; ma=2592000,h3-29=\":443\"; ma=2592000,h3-Q050=\":443\"; ma Transfer-Encoding: chunked [\"SUCCESS\",[[\"nihao\",[\"你好\"],[],{\"annotation\":[\"ni hao\"],\"candidate_type\":[0],\"lc\":[\"16 16\"]}]]]") - (should (equal (pyim-cloudim-parse-google-buffer) '("你好")))) + (should (equal (pyim-cloudim-parse-google-buffer) '("你好")))) - (when (not noninteractive) - (should (equal (pyim-cloudim:baidu "nihao" 'quanpin) '("你好"))) - (should (equal (pyim-cloudim:google "nihao" 'quanpin) '("你好"))) + (when (not noninteractive) + (should (equal (pyim-cloudim:baidu "nihao" 'quanpin) '("你好"))) + (should (equal (pyim-cloudim:google "nihao" 'quanpin) '("你好"))) - (let ((pyim-cloudim 'baidu)) - (should (equal (pyim-cloudim "nihao" 'quanpin) '("你好")))) + (let ((pyim-cloudim 'baidu)) + (should (equal (pyim-cloudim "nihao" 'quanpin) '("你好")))) - (let ((pyim-cloudim 'google)) - (should (equal (pyim-cloudim "nihao" 'quanpin) '("你好")))) + (let ((pyim-cloudim 'google)) + (should (equal (pyim-cloudim "nihao" 'quanpin) '("你好")))) - (let ((pyim-cloudim 'xxx)) - (should (not (pyim-cloudim "nihao" 'quanpin)))) + (let ((pyim-cloudim 'xxx)) + (should (not (pyim-cloudim "nihao" 'quanpin)))) - (let ((pyim-cloudim nil)) - (should (not (pyim-cloudim "nihao" 'quanpin))))))) + (let ((pyim-cloudim nil)) + (should (not (pyim-cloudim "nihao" 'quanpin)))))) (ert-run-tests-batch-and-exit) ;; * Footer