branch: externals/kiwix
commit 9774c6179a15973cc345ea99dfdf48679ae94ed2
Author: stardiviner <[email protected]>
Commit: stardiviner <[email protected]>
update elisp link matching regexp
---
kiwix.el | 84 +++++++++++++++++++++++++++++++++++++---------------------------
1 file changed, 49 insertions(+), 35 deletions(-)
diff --git a/kiwix.el b/kiwix.el
index f6135d3..52b7235 100644
--- a/kiwix.el
+++ b/kiwix.el
@@ -74,11 +74,13 @@
kiwix-libraries)))
(defvar kiwix-librarie-abbrev-list
- ;; TODO:
- '(("en" . (mapcar #'(lambda (var)
- (string-match-p "en" var))
- kiwix-libraries))
- ("zh" . ())
+ '(("default" . "en")
+ ;; TODO:
+ ;; (mapcar #'(lambda (var)
+ ;; (string-match-p "en" var))
+ ;; kiwix-libraries)
+ ("en" . "wikipedia_en_all_2016-02")
+ ("zh" . "wikipedia_zh_all_2015-11")
))
(defun kiwix-select-library-abbrev ()
@@ -86,6 +88,10 @@
(completing-read "Wikipedia library abbrev: "
(map-keys kiwix-librarie-abbrev-list)))
+(defun kiwix-select-library-fullname (abbr)
+ "Get Kiwix library full name which is associated with `ABBR'."
+ (cdr (assoc abbr kiwix-librarie-abbrev-list)))
+
;; launch Kiwix server
;;;###autoload
(defun kiwix-launch-server ()
@@ -129,8 +135,21 @@ for query string and library interactively."
;;; Support Org-mode
-;; [[wiki:(library):query]]
-;; elisp regexp: `\\(.*\\):(.*):.*'
+;;
+;; - [[wiki:(library):query]]
+;; - [[wiki:query]]
+;;
+;; links:
+;; - wiki:(zh):%E7%A6%85%E5%AE%97
+;; - wiki:(en):linux
+;; - wiki:linux
+;;
+;; - parameter `link' will be (en):linux" or linux".
+;;
+;; elisp regexp: "\\(?:(\\(.*\\)):\\)?\\([^] \n\t\r]*\\)"
+;; - non capturing group (\(?:...\)) for optional library
+;; - group 1: library (en or zh)
+;; - group 2: link? (match everything but ], space, tab, carriage return,
linefeed by using [^] \n\t\r]*)
;; for open wiki search query with local application database.
;; TODO: deprecated
@@ -138,42 +157,37 @@ for query string and library interactively."
(defun org-wiki-link-open (link)
"Open LINK in external wiki program."
- (cond ((string-match "\\(.*\\):(\\(.*\\)):\\(.*\\)" link)
- (let* ((type (match-string 1 link))
+ (cond ((string-match "\\(?:(\\(.*\\)):\\)?\\([^] \n\t\r]*\\)" link) ;
(library):query
+ (let* (
;; convert between libraries full name and abbrev.
- (library (cdr (assoc
- (match-string 2 link)
- kiwix-librarie-abbrev-list)))
- (query (match-string 3 link))
+ (library (kiwix-select-library-fullname (match-string 1 link)))
+ (query (match-string 2 link))
(url (concat kiwix-server-url library "/A/" (url-encode-url
(capitalize query)) ".html")))
+ ;; (prin1 (format "library: %s, query: %s" library query))
(browse-url url)))
- ((string-match "\\(.*\\):\\(.*\\)" link)
- (let* ((type (match-string 1 link))
- (query (match-string 2 link))
+ ((string-match "\\(?:(\\(.*\\)):\\)?\\([^] \n\t\r]*\\)" link) ; query
+ (let* ((query (match-string 2 link))
(url (concat kiwix-server-url kiwix-default-library "/A/"
(url-encode-url (capitalize query)) ".html")))
+ ;; (print1 (format "query: %s" query))
(browse-url url)))))
(defun org-wiki-link-export (link description format)
"Export the wiki LINK with DESCRIPTION for FORMAT from Org files."
- (let* ((type (when (string-match "\\(.+\\):(\\(.+\\)?):\\(.*\\)" link)
- (match-string 1 link)))
- (library (when (string-match "\\(.+\\):(\\(.+\\)?):\\(.*\\)" link)
- (match-string 2 link)))
- ;; query need to be convert to URL encoding: "禅宗"
https://zh.wikipedia.org/wiki/%E7%A6%85%E5%AE%97
- (query (url-encode-url
- (or description
- (when (string-match "\\(.+\\):(\\(.+\\)?):\\(.*\\)" link)
- (match-string 3 link)))))
- ;; "http://en.wikipedia.org/wiki/Linux"
- ;; --
- ;; ^- library: en, zh
- (path (concat "http://" library ".wikipedia.org/wiki/" query))
- (desc query))
- (when (stringp path)
- (cond
- ((eq format 'html) (format "<a href=\"%s\">%s</a>" path desc))
- ((eq format 'latex) (format "\\href{%s}{%s}" path desc))
- (t path)))))
+ (when (string-match "\\(?:(\\(.*\\)):\\)?\\([^] \n\t\r]*\\)" link)
+ (let* ((library (or (match-string 1 link)
+ (kiwix-select-library-fullname "default")))
+ ;; query need to be convert to URL encoding: "禅宗"
https://zh.wikipedia.org/wiki/%E7%A6%85%E5%AE%97
+ (query (url-encode-url (or (match-string 2 link) description)))
+ ;; "http://en.wikipedia.org/wiki/Linux"
+ ;; --
+ ;; ^- library: en, zh
+ (path (concat "http://" library ".wikipedia.org/wiki/" query))
+ (desc (or (match-string 2 link) description)))
+ (when (stringp path)
+ (cond
+ ((eq format 'html) (format "<a href=\"%s\">%s</a>" path desc))
+ ((eq format 'latex) (format "\\href{%s}{%s}" path desc))
+ (t path))))))
(defun org-wiki-store-link ()
"Store a link to a wiki link."