aboutsummaryrefslogtreecommitdiff
path: root/wiki-engine.el
blob: fb48994973abaca37aa3fb5046727ad0b3d6ce9a (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
;;; wiki-engine.el -- client to wiki engines -*- lexical-binding: t -*-

;; Copyright (C) 2023 Free Software Foundation.

;; Author: Yuchen Pei <id@ypei.org>
;; Package-Requires: ((emacs "28.2"))

;; 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-mediawiki-fetch (wiki-site title)
  "Fetch a mediawiki entry describing TITLE.

The site name is passed as a symbol WIKI-SITE."
  (let ((wiki-site-info (alist-get wiki-site 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-local-find-file
                  title
                  (wiki-locate-dir wiki-site)))
      (wiki-fetch-url
       (format "%s%s?action=raw"
               (plist-get wiki-site-info :base-url)
               title)
       (wiki-locate-dir wiki-site)
       (lambda ()
         (wiki-mode)
         (setq-local wiki-site wiki-site)
         )
       ))))

(defun wiki-engine-oddmuse-fetch (wiki-site title)
  (let ((wiki-site-info (alist-get wiki-site wiki-sites)))
    (cl-assert (eq (plist-get wiki-site-info :engine) 'oddmuse))
    (wiki-fetch-url
     (format "%s?action=download;id=%s"
             (plist-get wiki-site-info :base-url)
             title)
     (wiki-locate-dir wiki-site)
     (lambda ()
       (wiki-mode)
       (setq-local wiki-site wiki-site)
       )
     title)))

(defun wiki-engine-moinmoin-fetch (wiki-site title)
  (let ((wiki-site-info (alist-get wiki-site wiki-sites)))
    (cl-assert (eq (plist-get wiki-site-info :engine) 'moinmoin))
    (wiki-fetch-url
     (format "%s%s?action=raw"
             (plist-get wiki-site-info :base-url)
             title)
     (wiki-locate-dir wiki-site)
     (lambda ()
       (wiki-mode)
       (setq-local wiki-site wiki-site)
       )
     title)))

(defun wiki-locate-dir (wiki-site)
  "Locate the directory for a WIKI-SITE."
  (expand-file-name (format "%s" wiki-site) wiki-local-dir))

(defun wiki-local-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'."
  (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)
      file-name)))

(defun wiki-engine-fetcher (wiki-site-info)
  (intern (format "wiki-engine-%s-fetch"
                  (plist-get wiki-site-info :engine))))

(defmacro defun-wiki-fetchers ()
  (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