summaryrefslogtreecommitdiff
path: root/journal-content.lisp
blob: 12ff5a3a7edd40904b8c47eaa2f667b018e6c1b6 (plain)
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
;;;; -*- coding: utf-8; mode: lisp -*-
;;;; Copyright 2007, Matthias Andreas Benkard.

;;;------------------------------------------------------------------------
;;; This file is part of The Mulkblog Project.
;;;
;;; The Mulkblog Project is free software.  You can redistribute it and/or
;;; modify it under the terms of the Affero General Public License as
;;; published by Affero, Inc.; either version 1 of the License, or
;;; (at your option) any later version.
;;;
;;; The Mulkblog Project 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
;;; Affero General Public License for more details.
;;;
;;; You should have received a copy of the Affero General Public
;;; License in the COPYING file that comes with The Mulkblog Project; if
;;; not, write to Affero, Inc., 510 Third Street, Suite 225, San
;;; Francisco, CA 94107 USA.
;;;------------------------------------------------------------------------

(in-package #:mulk.journal)


;;; (@* "Class definitions")
(defclass journal-entry ()
  ((id :type (integer 0)
       :accessor id-of
       :initarg :id)
   (uuid :type string
         :accessor uuid-of
         :initarg :uuid)
   (file :type (or null pathname)
         :accessor file-of
         :initarg :file)
   (title :type string
          :accessor title-of
          :initarg :title
          :initform "")
   (date :type (integer 0)
         :accessor date-of
         :initarg :date)
   (last-modification :type (or null (integer 0))
                      :accessor last-modification-of
                      :initarg :last-modification
                      :initform nil)
   (body :type string
         :accessor body-of
         :initarg :body
         :initform "")
   (categories :type list
               :accessor categories-of
               :initarg :categories
               :initform '())
   (comments :type list
             :accessor comments-about
             :initarg :comments
             :initform '())))


(defclass journal-comment ()
  ((id :type (integer 0)
       :accessor id-of
       :initarg :id)
   (uuid :type string
         :accessor uuid-of
         :initarg :uuid)
   (date :type (integer 0)
         :accessor date-of
         :initarg :date)
   (body :type string
         :accessor body-of
         :initarg :body
         :initform "")
   (author :type (or null string)
           :accessor author-of
           :initarg :author
           :initform nil)
   (email :type (or null string)
          :accessor email-of
          :initarg :email
          :initform nil)
   (website :type (or null string)
            :accessor website-of
            :initarg :website
            :initform nil)))


;; (@* "Journal entry operations")
(defmethod shared-initialize ((journal-entry journal-entry) slot-names
                              &key)
  (with-slots (id) journal-entry
    (when (or (eq slot-names t)
              (member 'id slot-names))
      (setf id (1+ (reduce #'max *journal-entries*
                           :key #'id-of
                           :initial-value -1)))))
  (call-next-method))


(defun find-entry (number)
  (find number *journal-entries* :key #'id-of))


(defun journal-markup->html (markup)
  (if (string= "" markup)
      markup
      (handler-bind
          ((error   ;; method-call-type-error or not
            ;; Work around a weird bug in cl-markdown or CLISP.  (I
            ;; don't know which.)
            #'(lambda (c)
                (declare (ignore c))
                #+nil (<:as-html
                       (with-output-to-string (s)
                         (system::pretty-print-condition c s)))
                (invoke-restart 'return nil))))
        (fixup-markdown-output
         (with-output-to-string (s)
           ;; Normally, we shouldn't need to create our own stream to
           ;; write into, but this is, of course, yet another
           ;; CLISP/Markdown hack, because Markdown's default
           ;; *OUTPUT-STREAM* seems to spontaneously close itself, making
           ;; everything break when Markdown tries to render more stuff.
           (markdown markup :stream s))))))


(defun read-journal-entry (filename)
  (with-open-file (file filename :direction :input
                                 :external-format #+clisp charset:utf-8
                                                  #+sbcl :utf-8)
    (let ((*read-eval* nil))
      (let ((data (read file)))
        (let ((comments (member :comments data)))
          (when comments
            (setf (second comments)
                  (mapcar #'(lambda (comment-record)
                              (apply #'make-instance
                                     'journal-comment
                                     comment-record))
                          (second comments)))))
        (apply #'make-instance 'journal-entry :file filename data)))))


(defun find-journal-entry-files ()
  (let ((journal-entry-files (list)))
    (when (file-exists-p *entry-dir*)
      (walk-directory *entry-dir*
                      #'(lambda (x)
                          (push x journal-entry-files))
                      :test (complement #'directory-pathname-p)))
    journal-entry-files))


(defun read-journal-entries ()
  (let ((journal-entry-files (find-journal-entry-files)))
    (sort (mapcar #'read-journal-entry journal-entry-files)
          #'>=
          :key #'id-of)))


(defun compute-journal-last-modified-date ()
  #-clisp (get-universal-time)
  #+clisp
  (max (compute-script-last-modified-date)
       (loop for file in (find-journal-entry-files)
              maximize (posix:file-stat-mtime (posix:file-stat file)))))


(defun write-out-entry (entry)
  (assert (file-of entry))
  (with-open-file (out (file-of entry) :direction :output
                                       :if-exists :supersede
                                       :external-format #+clisp charset:utf-8
                                                        #+sbcl :utf-8)
    (with-slots (id uuid date last-modification body title categories comments)
        entry
      (write `(:id ,id
               :uuid ,uuid
               :date ,date
               :last-modification ,last-modification
               :title ,title
               :categories ,categories
               :body ,body
               :comments ,(loop for comment in comments
                             collect
                               (with-slots (id uuid date author body email
                                            website)
                                   comment
                                 `(:id ,id
                                   :uuid ,uuid
                                   :date ,date
                                   :author ,author
                                   :email ,email
                                   :website ,website
                                   :body ,body))))
             :stream out))))