aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYuchen Pei <id@ypei.org>2025-01-04 12:21:15 +1100
committerYuchen Pei <id@ypei.org>2025-01-04 12:21:15 +1100
commit3bb836e703480e23b3eee8fdb369dacb294dc46a (patch)
tree4768445f97bc128970890932ccb14147970a088b
parent4bfc032aa411552271100301b341b764b5967ce1 (diff)
Factor out wiki-parse-title that separates entry and section by #HEADmaster
-rw-r--r--wiki-engine.el28
1 files changed, 15 insertions, 13 deletions
diff --git a/wiki-engine.el b/wiki-engine.el
index c10de6e..79aede0 100644
--- a/wiki-engine.el
+++ b/wiki-engine.el
@@ -162,9 +162,10 @@ and if the title cannot be found locally, fetch remotely."
(wiki-find-file title (wiki-locate-dir site-id)
(eq engine 'local))))
(post-proc (plist-get (alist-get site-id wiki-sites) :fetch-post-proc))
- (`(,entry ,section) (split-string title "#")))
+ (`(,entry . ,section) (wiki-parse-title title)))
(if found-local
- (switch-to-buffer found-local)
+ (progn (switch-to-buffer found-local)
+ (when section (wiki-goto-heading section)))
(wiki-fetch-url
(wiki-engine-wiki-url site-id entry)
entry
@@ -223,6 +224,11 @@ API-BASE-URL is the base url for the api request."
"Locate the directory for a SITE-ID."
(expand-file-name (format "%s" site-id) wiki-local-dir))
+(defun wiki-parse-title (title)
+ (pcase-let ((`(,entry ,section) (split-string title "#")))
+ (cons (string-remove-suffix "/" entry)
+ section)))
+
(defun wiki-find-file (title &optional dir create-if-not-exists)
"Find local TITLE in DIR. Do not switch to buffer.
@@ -231,17 +237,15 @@ CREATE-IF-NOT-EXISTS is non-nil, creates the file if not found.
DIR defaults to `default-directory'."
(interactive (list (read-file-name "Find wiki file: ")))
(unless dir (setq dir default-directory))
- (pcase-let*
- ((`(,entry ,section) (split-string title "#"))
- (file-name (expand-file-name
- (concat entry wiki-extension)
- dir)))
+ (let* ((entry (car (wiki-parse-title title)))
+ (file-name (expand-file-name
+ (concat entry 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)
- (when section (wiki-goto-heading section))
(buffer-name)))))
(defmacro wiki-define-site-commands ()
@@ -346,11 +350,9 @@ If URL points to html title, open the corresponding raw title."
wiki-sites)))
(pcase-let ((`(,site-id . ,site-info) found-site))
(funcall (wiki-site-fetcher site-id)
- (string-remove-suffix
- "/"
- (string-remove-prefix
- (format "%s/" (wiki-engine-compute-base-url site-info))
- url)))))
+ (string-remove-prefix
+ (format "%s/" (wiki-engine-compute-base-url site-info))
+ url))))
)
(provide 'wiki-engine)