I'm writing an extension to open a custom link type in Org-mode with Kiwix URL. (Kiwix is an offline Wikipedia program). Here is my kiwix.el source code.
I created a wiki:
link type for Org-mode.
Here is the link formats:
[[wiki:(library):query]]
[[wiki:query]]
links:
wiki:(zh):%E7%A6%85%E5%AE%97
wiki:(en):linux
wiki:linux
And here is the elisp regexp:
"\\(wiki\\):\\((\\(.*\\)):\\)?\\(.*\\)"
Here is my current function to handle the link:
(defun org-wiki-link-open (link)
"Open LINK in external wiki program."
(cond ((string-match "\\(wiki\\):\\((\\(.*\\)):\\)?\\(.*\\)" link) ; [[wiki:(library):query]]
(let* ((type (match-string 1 link))
;; convert between libraries full name and abbrev.
(library (cdr (assoc
(match-string 3 link)
kiwix-librarie-abbrev-list)))
(query (match-string 4 link))
(url (concat kiwix-server-url library "/A/" (url-encode-url (capitalize query)) ".html")))
(browse-url url)))
((string-match "\\(wiki\\):\\(.*\\)" link) ; [[wiki:query]]
(let* ((type (match-string 1 link))
(query (match-string 4 link))
(url (concat kiwix-server-url kiwix-default-library "/A/" (url-encode-url (capitalize query)) ".html")))
(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)))))
(org-add-link-type "wiki" 'org-wiki-link-open 'org-wiki-link-export)
But this regexp is wrong, can't open those example links correctly.
- [[wiki:(en):Linux][Linux (wiki)]]
- [[wiki:Linux][Linux]]
- [[wiki:(zh):%E7%A6%85%E5%AE%97][禅宗]]
So I hope someone can help me to improve this regexp pattern and my function code.