emacs-wiki-discuss
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[emacs-wiki-discuss] emacs-wiki-journal permalink patch


From: Yamagata Yoriyuki
Subject: [emacs-wiki-discuss] emacs-wiki-journal permalink patch
Date: Sun, 27 Feb 2005 16:50:20 +0900 (JST)

Hi,

I made a experimental patch to emacs-wiki-journal.  New entries are
added to the recent MyJournal<number> file directly, and MyJournal
page is created by copying the newest MyJournal<number> file during
publishing.

It requires some modification to emacs-wiki, which introduces several
new hooks.  Some may not be necessary, but I'm too tired to think
clearly now....

Any ways, patches are attatched below.

--
Yamagata Yoriyuki

diff -u emacs-wiki-orig/emacs-wiki-publish.el emacs-wiki/emacs-wiki-publish.el
--- emacs-wiki-orig/emacs-wiki-publish.el       2005-02-27 16:20:27.000000000 
+0900
+++ emacs-wiki/emacs-wiki-publish.el    2005-02-27 16:13:19.000000000 +0900
@@ -151,6 +151,28 @@
   :type 'hook
   :group 'emacs-wiki-publish)
 
+(defcustom emacs-wiki-before-file-publish-hook nil
+  "Hook run before every file is published."
+  :type 'hook
+  :group 'emacs-wiki-publish)
+
+(defcustom emacs-wiki-after-file-publish-hook nil
+  "Hook run after every file is published."
+  :type 'hook
+  :group 'emacs-wiki-publish)
+
+(defcustom emacs-wiki-before-wiki-publish-hook nil
+  "Hook run before the files have been published through
+`emacs-wiki-publish'."
+  :type 'hook
+  :group 'emacs-wiki-publish)
+
+(defcustom emacs-wiki-after-wiki-publish-hook '(emacs-wiki-publish-index)
+  "Hook run after the files have been published through
+`emacs-wiki-publish'."
+  :type 'hook
+  :group 'emacs-wiki-publish)
+
 (defcustom emacs-wiki-publish-function 'emacs-wiki-publish-current
   "The function used to publish a Wiki page."
   :type 'function
@@ -941,6 +963,8 @@
            function))
   :group 'emacs-wiki-publish)
 
+;;; Functions
+
 (defun emacs-wiki-transform-content-type (content-type)
   "Using `emacs-wiki-coding-map', try and resolve an emacs coding
 system to an associated HTML coding system.
@@ -1032,6 +1056,7 @@
   (save-some-buffers nil (lambda ()
                            (derived-mode-p 'emacs-wiki-mode)))
   (let ((emacs-wiki-project emacs-wiki-current-project))
+    (run-hooks 'emacs-wiki-before-wiki-publish-hook)
     (emacs-wiki-refresh-file-alist)
     (let* ((names (emacs-wiki-file-alist))
            (files (list t))
@@ -1063,12 +1088,6 @@
   (interactive)
   (emacs-wiki-publish-files (list buffer-file-name) t))
 
-(defvar emacs-wiki-after-file-publish-hook nil
-  "Hook run after every file is published.")
-(defvar emacs-wiki-after-wiki-publish-hook '(emacs-wiki-publish-index)
-  "Hook run after the files have been published through
-`emacs-wiki-publish'.")
-
 (defun emacs-wiki-write-buffer (output-path)
   (let ((backup-inhibited t)
         (buffer-file-coding-system
@@ -1113,9 +1132,10 @@
           (make-directory publishing-directory 'parents)))
       (when (and (not (emacs-wiki-private-p page))
                (or force (file-newer-than-file-p file published)))
-        (let ((project emacs-wiki-current-project))
-          (with-emacs-wiki-project project
-            (funcall emacs-wiki-publish-function file published)))
+       (run-hook-with-args 'emacs-wiki-before-file-publishd-hook file)
+       (let ((project emacs-wiki-current-project))
+         (with-emacs-wiki-project project
+           (funcall emacs-wiki-publish-function file published)))
         (run-hook-with-args 'emacs-wiki-after-file-publish-hook file)
         (setq published-some t)))
     published-some))
diff -u emacs-wiki-journal-orig/emacs-wiki-journal.el 
emacs-wiki-journal/emacs-wiki-journal.el
--- emacs-wiki-journal-orig/emacs-wiki-journal.el       2005-02-27 
16:21:14.000000000 +0900
+++ emacs-wiki-journal/emacs-wiki-journal.el    2005-02-27 05:04:52.000000000 
+0900
@@ -1,9 +1,9 @@
 ;;; emacs-wiki-journal.el --- Maintain a weblog style journal with emacs-wiki
 
-;; Copyright (C) 2003, 2004 Gary V. Vaughan
-;; Copyright (C) 2004 Ole Arndt
-;; Copyright (C) 2004 Hoan Ton-That
-;; Copyright (C) 2004 Jose A. Ortega Ruiz
+;; Copyright (C) 2003, 2004 Gary V. Vaughan (gary AT gnu DOT org)
+;; Copyright (C) 2004 Ole Arndt (ole AT sugarshark DOT com)
+;; Copyright (C) 2004 Hoan Ton-That (hoan AT ton-that DOT org)
+;; Copyright (C) 2004 Jose A. Ortega Ruiz (jao AT gnu DOT org)
 ;; Copyright (C) 2004, 2005 Michael Olson
 ;; Copyright (C) 2004 Yamagata Yoriyuki
 ;; Copyright (C) 2004, 2005 Yu Li
@@ -120,7 +120,6 @@
 ;;;_* Prerequisites
 (require 'cl)
 (require 'emacs-wiki)
-(require 'parse-time)
 (defvar emacs-wiki-journal-loaded nil)
 
 ;;;_* Options
@@ -218,12 +217,18 @@
                  (const :tag "No title" nil))
   :group 'emacs-wiki-journal)
 
-(defcustom emacs-wiki-journal-old-title-prefix "Entries "
-  "Title prefix of the old journal pages.
-If `nil', no title is inserted."
+(defcustom emacs-wiki-journal-old-title-format-1 "Entries from %s to %s"
+  "Title of the old journal pages. If `nil', no title is inserted."
   :type 'string
   :group 'emacs-wiki-journal)
 
+(defcustom emacs-wiki-journal-old-title-format-2 "Entries of %s"
+  "Title of the old journal pages if it contains the entries of only
+one day. If `nil', no title is inserted."
+  :type 'string
+  :group 'emacs-wiki-journal)
+
+
 (defcustom emacs-wiki-journal-entries-per-page 4
   "Maximum number of journal entries per page."
   :type 'integer
@@ -234,8 +239,6 @@
   :type 'string
   :group 'emacs-wiki-journal)
 
-;;;_* Internal Functions
-
 (defcustom emacs-wiki-journal-index-title-threshold t
   "*If nil, filenames are always used in the index.
 This is faster, but less informative.
@@ -248,11 +251,6 @@
                  (const :tag "Do not scan for title" nil))
   :group 'emacs-wiki-journal)
 
-(defcustom emacs-wiki-journal-title-format "%d-%2d-%4d"
-  "The format of time string used to update the title if it exists."
-  :type 'string
-  :group 'emacs-wiki-journal)
-
 (defcustom emacs-wiki-journal-date-format "%4Y-%2m-%2e"
   "Format for the string used in the date tags.
 Unlike `emacs-wiki-journal-time-format', this should be
@@ -275,13 +273,41 @@
   :type 'string
   :group 'emacs-wiki-journal)
 
+(defcustom emacs-wiki-journal-prepare-welcome-page 
+  'emacs-wiki-journal-copy-current
+  "The method to create the welcome page."
+  :type 'function
+  :group 'emacs-wiki-journal)
+
+;;;_* Internal Variables
+
+(defvar emacs-wiki-journal-current-page nil
+  "The current journal page")
+
+(defvar emacs-wiki-journal-current-page-number nil
+  "The number of journal pages")
+
+;;;_* Internal Functions
+
 ;; From planner.el
+
+(defun emacs-wiki-journal-expand-file-name (page)
+  "Return the file name of the given EmacsWiki journal page"
+  (expand-file-name page emacs-wiki-journal-directory))
+
 (defun emacs-wiki-journal-find-file (page &optional command)
   "Open the emacs-wiki PAGE by name.
 If COMMAND is non-nil, it is the function used to visit the file."
   (make-directory emacs-wiki-journal-directory t)
   (funcall (or command 'find-file)
-           (expand-file-name page emacs-wiki-journal-directory)))
+           (emacs-wiki-journal-expand-file-name page)))
+
+(defun emacs-wiki-journal-find-file-noselect (page &optional command)
+  "Open the emacs-wiki PAGE by name, without selecting its buffer.
+If COMMAND is non-nil, it is the function used to visit the file."
+  (make-directory emacs-wiki-journal-directory t)
+  (funcall (or command 'find-file-noselect)
+           (emacs-wiki-journal-expand-file-name page)))
 
 (defun emacs-wiki-journal-category-alist (&optional no-check-p)
   "Return possible category index Wikis in `emacs-wiki-journal-directory'.
@@ -289,8 +315,8 @@
 directories to decide whether to re-read the cached alist, just
 re-read the disk."
   (let ((file-alist
-         (cadr (assoc emacs-wiki-journal-project
-                      emacs-wiki-file-alist)))
+         (cadr (assoc emacs-wiki-journal-project 
+                     emacs-wiki-file-alist)))
         (category-alist nil))
     (save-match-data
       (mapc
@@ -382,22 +408,47 @@
                      . ,emacs-wiki-journal-server-prefix)
                     (emacs-wiki-index-title-threshold
                      . ,emacs-wiki-journal-index-title-threshold))))
+  (add-to-list 'emacs-wiki-before-wiki-publish-hook 
+              emacs-wiki-journal-prepare-welcome-page)
   (emacs-wiki-update-project-interwikis))
 
 (defun emacs-wiki-journal-prepare-journal ()
   "Check if maximum number of journal entries reached
 and react accordingly."
+  (emacs-wiki-journal-set-current-page)
   (when (and (> emacs-wiki-journal-entries-per-page 0)
              (>= (emacs-wiki-journal-entries)
                 emacs-wiki-journal-entries-per-page))
-    (emacs-wiki-journal-store-journal-file)
-    (emacs-wiki-journal-update-anchors)))
+    (emacs-wiki-journal-update-old-journal-title)
+    (emacs-wiki-journal-create-new-page)))
+
+(defun emacs-wiki-journal-set-current-page ()
+  "set the current page and page number"
+  (unless (and emacs-wiki-journal-current-page 
+              emacs-wiki-journal-current-page-number
+              (or
+               (get-buffer emacs-wiki-journal-current-page)
+               (file-exists-p (emacs-wiki-journal-expand-file-name
+                              emacs-wiki-journal-current-page))))
+    (let ((last (emacs-wiki-journal-last-page)))
+      (setq emacs-wiki-journal-current-page (car last))
+      (setq emacs-wiki-journal-current-page-number (cdr last)))))
+
+(defun emacs-wiki-journal-create-new-page ()
+  "Create the new journal page"
+  (let* ((new-number (1+ emacs-wiki-journal-current-page-number))
+        (new-page (format "%s%d" emacs-wiki-journal-wiki new-number)))
+
+    (emacs-wiki-journal-find-file new-page)    
+    (setq emacs-wiki-journal-current-page-number new-number)
+    (setq emacs-wiki-journal-current-page new-page)))
 
 (defun emacs-wiki-journal-entries ()
-  "Return the number of journal entries in the main journal file."
-  (let ((jf (expand-file-name emacs-wiki-journal-wiki
-                              emacs-wiki-journal-directory)))
-    (if (file-exists-p jf)
+  "Return the number of journal entries in the current journal file."
+  (let ((jf (emacs-wiki-journal-expand-file-name 
+            emacs-wiki-journal-current-page)))
+    (if (or (file-exists-p jf) 
+           (get-buffer emacs-wiki-journal-current-page))
       (with-current-buffer (find-file-noselect jf)
         (save-excursion
           (labels
@@ -409,65 +460,52 @@
             (count-ent 0))))
       0)))
 
-(defun emacs-wiki-journal-store-journal-file ()
-  "Move current journal file to a numbered one."
-  (let* ((current (expand-file-name
-                   emacs-wiki-journal-wiki
-                   emacs-wiki-journal-directory))
-         (no (emacs-wiki-journal-pages))
-         (new (format "%s%d" current (1+ no))))
-    (when (file-exists-p current)
-      (copy-file current new)
-      (emacs-wiki-journal-update-old-journal-title new)
-      (with-current-buffer (find-file-noselect current)
-        (delete-region (point-min) (point-max))))))
+(defun emacs-wiki-journal-nextday()
+  (goto-char (point-min))
+  (and (re-search-forward
+       (format emacs-wiki-journal-date-tag-template
+               "\\(.+\\)")
+       nil t)
+       (match-string 1)))
+
+(defun emacs-wiki-journal-lastday()
+  (goto-char (point-max))
+  (and (re-search-backward
+       (format emacs-wiki-journal-date-tag-template
+               "\\(.+\\)")
+       nil t)
+       (match-string 1)))
+
+(defun emacs-wiki-journal-mktime(s)
+  (if s
+      (let* ((date (split-string s "-"))
+            (time (encode-time 0 0 0
+                               (string-to-int (nth 2 date))
+                               (string-to-int (nth 1 date))
+                               (string-to-int (nth 0 date)))))
+       (format-time-string emacs-wiki-journal-time-format time))))
 
-(defun emacs-wiki-journal-update-old-journal-title (file)
+(defun emacs-wiki-journal-update-old-journal-title (&optional page)
   "Update the title of an old journal page."
-  (labels
-      ((nextday ()
-               (goto-char (point-min))
-                (and (re-search-forward
-                      (format emacs-wiki-journal-date-tag-template
-                              "\\(.+\\)")
-                      nil t)
-                     (match-string 1)))
-       (lastday ()
-               (goto-char (point-max))
-               (and (re-search-backward
-                      (format emacs-wiki-journal-date-tag-template
-                              "\\(.+\\)")
-                      nil t)
-                    (match-string 1)))
-       (mktime (s)
-               (if s
-                   (let ((date (split-string s "-")))
-                     (format emacs-wiki-journal-title-format
-                             (string-to-int (nth 0 date))
-                             (string-to-int (nth 1 date))
-                             (string-to-int (nth 2 date))))
-                   s)))
-    (when emacs-wiki-journal-old-title-prefix
-      (with-current-buffer (find-file-noselect file)
-        (goto-char (point-min))
-        (when (re-search-forward "^#title " nil t)
-          (beginning-of-line)
-          (let ((kill-whole-line t)) (kill-line)))
-        (let* ((to (nextday))
-               (from (lastday)))
-          (goto-char (point-min))
-          (insert "#title " emacs-wiki-journal-old-title-prefix
-                  "\n\n\n"
-                  "date: "
-                  (if (not (string= from to))
-                      (concat (if from (concat (if to "from " " ")
-                                               (mktime from) " ")
-                                "")
-                              (if to (concat (if from "to " " ")
-                                             (mktime to) " ")
-                                ""))
-                    (mktime from))
-                  "\n\n")))
+  (when (and emacs-wiki-journal-old-title-format-1 
+            emacs-wiki-journal-old-title-format-2)
+    (with-current-buffer (emacs-wiki-journal-find-file-noselect 
+                         (if page page emacs-wiki-journal-current-page))
+      (goto-char (point-min))
+      (when (re-search-forward "^#title " nil t)
+       (beginning-of-line)
+       (let ((kill-whole-line t)) (kill-line)))
+      (let* ((to (emacs-wiki-journal-nextday))
+            (from (emacs-wiki-journal-lastday)))
+       (goto-char (point-min))
+       (insert "#title "
+               (if (not (string= from to))
+                   (format emacs-wiki-journal-old-title-format-1
+                           (emacs-wiki-journal-mktime from)
+                           (emacs-wiki-journal-mktime to))
+                 (format emacs-wiki-journal-old-title-format-2
+                         (emacs-wiki-journal-mktime from))))
+       (insert "\n"))
       (save-buffer (current-buffer))
       (kill-buffer (current-buffer)))))
 
@@ -477,44 +515,30 @@
 
 (defun emacs-wiki-journal-last-page ()
   "Return a pair with the last journal page name and number."
-  (let ((files
-        (directory-files emacs-wiki-journal-directory
-                         nil
-                         (concat emacs-wiki-journal-wiki
-                                 "[0-9]+\\'")
-                         'true))
-       (final 0))
-    (mapc (lambda (file)
-            (let* ((num-string 
-                    (substring file (length emacs-wiki-journal-wiki)))
-                   (num (string-to-number num-string)))
-              (setq final (max num final))))
-          files)
-    (cons (format "%s%d" emacs-wiki-journal-wiki final) final)))
-
-(defun emacs-wiki-journal-update-anchors ()
-  "Update anchors in category pages to journal wiki file to the
-last one stored."
-  (mapc
-   (lambda (f)
-     (let ((buffer (find-file-noselect f))
-           (from-string (concat emacs-wiki-journal-wiki "#"))
-           (replacement (format "%s%d#"
-                                emacs-wiki-journal-wiki
-                                (emacs-wiki-journal-pages))))
-       (when buffer
-         (with-current-buffer buffer
-           (goto-char (point-min))
-           (while (re-search-forward from-string nil t)
-             (replace-match replacement nil nil))
-           (save-buffer buffer)
-           (kill-buffer nil)))))
-   (mapcar 'cdr (emacs-wiki-journal-category-alist))))
+   (let ((files
+        (directory-files emacs-wiki-journal-directory
+                         nil
+                         (concat emacs-wiki-journal-wiki
+                                 "[0-9]+\\'")
+                         'true))
+       (final 1))
+     (mapc (lambda (file)
+             (let* ((num-string 
+                     (substring file (length emacs-wiki-journal-wiki)))
+                    (num (string-to-number num-string)))
+               (setq final (max num final))))
+           files)
+     (cons (format "%s%d" emacs-wiki-journal-wiki final) final)))
 
 (defun emacs-wiki-journal-more-entries-anchor ()
   "Return a wiki anchor to last stored journal page."
-  (emacs-wiki-journal-make-link (car (emacs-wiki-journal-last-page))
-                                emacs-wiki-journal-more-entries-link))
+  (if (> emacs-wiki-journal-current-page-number 1)
+      (emacs-wiki-journal-make-link
+       (format "%s%d"
+              emacs-wiki-journal-wiki
+              (1- emacs-wiki-journal-current-page-number))
+       emacs-wiki-journal-more-entries-link)))
+
 
 (defun emacs-wiki-journal-publish-index ()
   "Replacement for `emacs-wiki-publish-index'."
@@ -530,14 +554,17 @@
 (defun emacs-wiki-journal-add (category-wiki journal-entry-heading)
   "Add a journal entry under category with a heading"
   (interactive)
-  (let* ((category-anchor-base (downcase category-wiki))
+  (let* ((anchor-base "p")
          (anchor-regexp (concat "^#"
-                                (regexp-quote category-anchor-base)
+                                (regexp-quote anchor-base)
                                 "\\([0-9][0-9]*\\)"))
-         (anchor-ord "0"))
+         (anchor-ord "0")
+        (self-link nil))
+
+    (emacs-wiki-journal-set-current-page)
 
     (emacs-wiki-journal-find-file
-     emacs-wiki-journal-wiki
+     emacs-wiki-journal-current-page
      (when emacs-wiki-journal-use-other-window
        'find-file-other-window))
 
@@ -545,7 +572,7 @@
 
     (when emacs-wiki-journal-title
       (unless (re-search-forward "^#title " nil t)
-        (insert "#title " emacs-wiki-journal-title "\n\n")
+        (insert "#title " emacs-wiki-journal-title "\n")
         (forward-line)))
 
     (save-excursion
@@ -555,16 +582,18 @@
                  (buffer-substring (match-beginning 1)
                                    (match-end 1))))
         (when (and (> emacs-wiki-journal-entries-per-page 0)
-                   (> (emacs-wiki-journal-pages) 0)
+                   (> emacs-wiki-journal-current-page-number 1)
                    (zerop (emacs-wiki-journal-entries)))
           (insert "\n\n" (emacs-wiki-journal-more-entries-anchor)
                   "\n"))))
 
+    (setq self-link        
+         (concat emacs-wiki-journal-current-page
+                 "#" anchor-base anchor-ord))
     (save-excursion
       (emacs-wiki-journal-add-category-entry
        category-wiki
-       (concat emacs-wiki-journal-wiki
-               "#" category-anchor-base anchor-ord)
+       self-link
        journal-entry-heading))
 
     ;; skip # lines and blanks at the start of the buffer
@@ -588,12 +617,13 @@
                           (concat (emacs-wiki-journal-make-link
                             icon-file-name) " ")
                         nil)))
+
       ;; only add a new date if different from the top entry
       (if (not (looking-at (regexp-quote (concat "* " time-string))))
-          (insert (concat "* " time-string "\n\n"))
-        (forward-line 1))
-      ;; add the anchor first so that user is taken above entry
-      (insert (concat "#" category-anchor-base anchor-ord "\n"))
+           (insert (concat "* " time-string "\n\n"))
+          (forward-line 1))
+       ;; add the anchor first so that user is taken above entry
+       (insert (concat "#" anchor-base anchor-ord "\n"))
 
       (open-line 1)
       (insert
@@ -601,13 +631,24 @@
         "** " journal-entry-heading "\n"
         (if icon-link
             (concat icon-link "\n"))
-        "*** " category-wiki "\n"
+        "*** " category-wiki ", [[" self-link "][link this article]]\n"
         date-string "\n\n\n")))
 
-    (emacs-wiki-journal-find-file emacs-wiki-journal-wiki)
+    (emacs-wiki-journal-find-file emacs-wiki-journal-current-page)
     ;; move to insertion point
     (forward-line -1)))
 
+(defun emacs-wiki-journal-copy-current ()
+  "Copy the current page to the emacs-wiki-journal-wiki page"
+  (save-excursion
+    (emacs-wiki-journal-set-current-page)
+    (emacs-wiki-journal-find-file emacs-wiki-journal-current-page)
+    (write-region
+     (point-min)
+     (point-max)
+     (emacs-wiki-journal-expand-file-name 
+                emacs-wiki-journal-wiki))))
+
 ;;;###autoload
 (defun emacs-wiki-journal-add-entry ()
   "Find journal file and add an entry and category index for today."

reply via email to

[Prev in Thread] Current Thread [Next in Thread]