aboutsummaryrefslogtreecommitdiff
path: root/wiki-engine.el
blob: dd72ce44bc1a1947da5e9c8b966f2034abfadd41 (plain) (blame)
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
;;; 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)

;; TODO: make this a defcustom
(defvar wiki-default-engine 'mediawiki
  "The default wiki engine, when one is not supplied.")

(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-fetch-url (url title &optional dir callback)
  "Fetch URL asynchronously to a file in DIR.

Then call CALLBACK which is a closure taking no argument.

A non-nil TITLE overrides title inferred from the url."
  (let ((cb (lambda (status)
              (wiki-save-fetched-and-switch status title dir)
              (when callback (funcall callback)))))
    (url-retrieve url cb))
  )

(defun wiki-save-string-and-switch (to-insert title dir)
  "Insert string TO-INSERT to TITLE under DIR and switch to buffer."
  (let ((buffer (wiki-find-file title dir t))
        (coding-system-for-write 'utf-8))
    (with-current-buffer buffer
      (insert to-insert)
      (goto-char (point-min))
      (save-buffer)
      (revert-buffer t t))
    (switch-to-buffer buffer)))

(defun wiki-save-fetched-and-switch (status title dir)
  "If STATUS is ok, insert response payload to TITLE under DIR.

And switch to the corresponding buffer."
  (when (plist-get status :error)
    (error "Wiki fetch failed: %s" (plist-get status :error)))
  (wiki-delete-http-header)
  (let ((to-insert (buffer-string))
        (_ (kill-buffer)))
    (wiki-save-string-and-switch to-insert title dir)))

(defun wiki-compute-engine (site-info)
  "Return :engine of SITE-INFO, or the default engine."
  (or (plist-get site-info :engine) wiki-default-engine))

(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 (wiki-compute-engine site-info))
         (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-simple-fetch (site-id title)
  "A simple method to fetch TITLE from site with SITE-ID.

If the site has a `local' engine, \"fetch\" locally. Otherwise,
if `wiki-fetch-prefer-local' is non-nil, try fetching locally,
and if the title cannot be found locally, fetch remotely."
  (when (string-empty-p title) (setq title "Main Page"))
  (let* ((engine (wiki-compute-engine (alist-get site-id wiki-sites)))
         (found-local
          (when (or wiki-fetch-prefer-local (eq engine 'local))
            (wiki-find-file title (wiki-locate-dir site-id)
                            (eq engine 'local)))))
    (if found-local
        (switch-to-buffer found-local)
      (wiki-fetch-url
       (wiki-engine-wiki-url site-id title)
       title
       (wiki-locate-dir site-id)
       (lambda ()
         (wiki-mode)
         (setq-local wiki-site site-id
                     wiki-title title))))))

(defun wiki-engine-mediawiki-api-wiki (base-url title)
  (wiki-url-fetch-json
   (format
    "%sapi.php?action=query&format=json&titles=%s&prop=revisions&rvprop=content&rvslots=main"
    base-url title)))

(defun wiki-engine-mediawiki-api-fetch (site-id title)
  "Fetch TITLE from site with SITE-ID using mediawiki api."
  (when (string-empty-p title) (setq title "Main Page"))
  (let* ((engine (wiki-compute-engine (alist-get site-id wiki-sites)))
         (base-url (plist-get (alist-get site-id wiki-sites) :base-url))
         (found-local
          (when (or wiki-fetch-prefer-local (eq engine 'local))
            (wiki-find-file title (wiki-locate-dir site-id)
                            (eq engine 'local)))))
    (if found-local
        (switch-to-buffer found-local)
      (wiki-save-string-and-switch
       (alist-get
        '* (alist-get
            'main (alist-get
                   'slots (elt
                           (alist-get
                            'revisions
                            (cdr
                             (car
                              (alist-get
                               'pages (alist-get
                                       'query
                                       (wiki-url-fetch-json
                                        (wiki-engine-mediawiki-api-wiki
                                         base-url title)
                                        ))))))
                           0))))
       title
       (wiki-locate-dir site-id))
      (wiki-mode)
      (setq-local wiki-site site-id
                  wiki-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)
  "Find local TITLE in DIR. Do not switch to buffer.

Return the buffer if success, and nil otherwise. If
CREATE-IF-NOT-EXISTS is non-nil, creates the file is not found.
DIR defaults to `default-directory'."
  (interactive (list (read-file-name "Find wiki file: ")))
  (unless dir (setq dir default-directory))
  (let ((file-name (expand-file-name
                    (concat title wiki-extension)
                    dir)))
    (when (or (file-exists-p file-name) create-if-not-exists)
      (setq dir (file-name-directory file-name))
      (unless (file-exists-p dir) (make-directory dir t))
      (with-current-buffer (find-file-noselect file-name)
        (wiki-mode)
        (buffer-name)))))

(defmacro defun-wiki-fetchers ()
  "Defines all wiki fetcher functions."
  (cons 'progn
        (mapcar
         (lambda (pair)
           (pcase-let ((`(,id . ,info) pair))
             (let ((engine-fetcher
                    (or (plist-get info :fetcher)
                        'wiki-engine-simple-fetch)))
               `(defun ,(wiki-site-fetcher id) (title)
                  (interactive ,(format "sFetch title for %s: "
                                        (plist-get info :display-name)))
                  (,engine-fetcher ',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