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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
;;; wiki-engine.el -- client to wiki engines -*- lexical-binding: t -*-
;; Copyright (C) 2023 Free Software Foundation, Inc.
;; Author: Yuchen Pei <id@ypei.org>
;; This file is part of wiki.el.
;; wiki.el is free software: you can redistribute it and/or modify it under
;; the terms of the GNU Affero General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; wiki.el 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 Affero General
;; Public License for more details.
;; You should have received a copy of the GNU Affero General Public
;; License along with wiki.el. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; client to wiki engines, wiki server software. Each engine defines
;; how Emacs interacts with the remote server, including how to
;; construct a url to fetch from.
;;; Code:
(require 'wiki-utils)
(require 'wiki-markup)
(defun wiki-engine-html-url (site title)
"Return the url of the html webpage of TITLE on SITE."
(format "%s%s" (plist-get (alist-get site wiki-sites) :base-url)
title))
(defun wiki-current-html-url ()
"Return the url of the html webpage of the current wiki buffer."
(unless (and wiki-site wiki-title)
(error "Nil wiki-site or wiki-title!"))
(wiki-engine-html-url wiki-site wiki-title))
(defun wiki-engine-wiki-url (site title)
"Construct the url to fetch wiki of TITLE from SITE."
(let* ((site-info (alist-get site wiki-sites))
(engine (plist-get site-info :engine))
(base-url (plist-get site-info :base-url)))
(pcase engine
('mediawiki (format "%s%s?action=raw" base-url title))
('moinmoin (format "%s%s?action=raw" base-url title))
('oddmuse (format "%s?action=download;id=%s" base-url title))
(_ (error "Unknown engine: %s" engine)))))
(defun wiki-engine-mediawiki-fetch (site-id title)
"Fetch a mediawiki entry describing TITLE.
The site handle is passed as a symbol SITE-ID."
(let ((wiki-site-info (alist-get site-id wiki-sites)))
(cl-assert (eq (plist-get wiki-site-info :engine) 'mediawiki))
(when (string-empty-p title) (setq title "Main Page"))
(unless (and wiki-fetch-prefer-local
(wiki-find-file
title
(wiki-locate-dir site-id)))
(wiki-fetch-url
(format "%s%s?action=raw"
(plist-get wiki-site-info :base-url)
title)
(wiki-locate-dir site-id)
(lambda ()
(wiki-mode)
(setq-local wiki-site site-id
wiki-title title)
)
))))
(defun wiki-engine-oddmuse-fetch (site-id title)
"Fetch an oddmuse entry describing TITLE.
The site handle is passed as a symbol SITE-ID."
(let ((wiki-site-info (alist-get site-id wiki-sites)))
(cl-assert (eq (plist-get wiki-site-info :engine) 'oddmuse))
(unless (and wiki-fetch-prefer-local
(wiki-find-file
title
(wiki-locate-dir site-id)))
(wiki-fetch-url
(format "%s?action=download;id=%s"
(plist-get wiki-site-info :base-url)
title)
(wiki-locate-dir site-id)
(lambda ()
(wiki-mode)
(setq-local wiki-site site-id
wiki-title title)
)
title))))
(defun wiki-engine-moinmoin-fetch (site-id title)
"Fetch a moinmoin entry describing TITLE.
The site handle is passed as a symbol SITE-ID."
(let ((wiki-site-info (alist-get site-id wiki-sites)))
(cl-assert (eq (plist-get wiki-site-info :engine) 'moinmoin))
(unless (and wiki-fetch-prefer-local
(wiki-find-file
title
(wiki-locate-dir site-id)))
(wiki-fetch-url
(format "%s%s?action=raw"
(plist-get wiki-site-info :base-url)
title)
(wiki-locate-dir site-id)
(lambda ()
(wiki-mode)
(setq-local wiki-site site-id
wiki-title title))
title))))
(defun wiki-locate-dir (site-id)
"Locate the directory for a SITE-ID."
(expand-file-name (format "%s" site-id) wiki-local-dir))
(defun wiki-find-file (title &optional dir create-if-not-exists
extension)
"Find local TITLE in DIR.
Returns the file-name if success, and nil otherwise. If
CREATE-IF-NOT-EXISTS is non-nil, creates the file is not found.
DIR defaults to `default-directory'.
EXTENSION is the file extension."
(interactive (list (read-file-name "Find wiki file: ")))
(unless dir (setq dir default-directory))
(let ((file-name (expand-file-name
(if extension
(file-name-extension title extension)
title)
dir)))
(when (or (file-exists-p file-name) create-if-not-exists)
(find-file file-name)
(wiki-mode)
file-name)))
(defalias #'wiki-local-fetch #'wiki-find-file)
(defun wiki-engine-fetcher (wiki-site-info)
"Return the fetcher for the engine of WIKI-SITE-INFO."
(intern (format "wiki-engine-%s-fetch"
(plist-get wiki-site-info :engine))))
(defmacro defun-wiki-fetchers ()
"Defines all wiki fetcher functions."
(cons 'progn
(mapcar
(lambda (pair)
(pcase-let ((`(,id . ,info) pair))
`(defun ,(wiki-site-fetcher id) (title)
(interactive ,(format "sFetch title for %s: "
(plist-get info :display-name)))
(,(wiki-engine-fetcher info) ',id title))))
(seq-filter #'cdr
wiki-sites)
)))
(defun-wiki-fetchers)
(defun wiki-open-url (url)
"Open the raw wiki corresponding to the URL of a html wiki page.
If URL points to html title, open the corresponding raw title."
(interactive "sURL: ")
(when-let
((found-site
(seq-find
(lambda (site-pair)
(when-let ((base-url (plist-get (cdr site-pair) :base-url)))
(string-prefix-p base-url url)))
wiki-sites)))
(pcase-let ((`(,site-id . ,site-info) found-site))
(funcall (wiki-site-fetcher site-id)
(string-remove-prefix (plist-get site-info :base-url) url))))
)
(provide 'wiki-engine)
;;; wiki-engine.el ends here
|