summaryrefslogtreecommitdiff
path: root/tissue/document.scm
blob: b12ccbb1732b614c236b0f46d59e4b73825fee32 (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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
;;; tissue --- Text based issue tracker
;;; Copyright © 2022 Arun Isaac <arunisaac@systemreboot.net>
;;;
;;; This file is part of tissue.
;;;
;;; tissue is free software: you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation, either version 3 of the License, or
;;; (at your option) any later version.
;;;
;;; tissue 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
;;; General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with tissue.  If not, see <https://www.gnu.org/licenses/>.

(define-module (tissue document)
  #:use-module (rnrs hashtables)
  #:use-module (rnrs io ports)
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-19)
  #:use-module (srfi srfi-26)
  #:use-module (srfi srfi-171)
  #:use-module (ice-9 match)
  #:use-module (htmlprag)
  #:use-module (oop goops)
  #:use-module (term ansi-color)
  #:use-module (xapian xapian)
  #:use-module (tissue git)
  #:use-module (tissue utils)
  #:export (slot-set
            object->scm
            scm->object
            <document>
            document-title
            document-web-uri
            document-type
            document-id-term
            document-text
            document-term-generator
            document-snippet
            print
            document-sxml-snippet
            document->sxml
            <file-document>
            file-document-path
            read-gemtext-document))

(define (slot-set object slot-name value)
  "Set SLOT-NAME in OBJECT to VALUE. This is a purely functional setter
that operates on a copy of OBJECT. It does not mutate OBJECT."
  (let ((clone (shallow-clone object)))
    (slot-set! clone slot-name value)
    clone))

;; We override the default write to print object slots and values.
(define-method (write (object <object>) port)
  "Write OBJECT to PORT."
  (format port "#<~a ~a>"
          (class-name (class-of object))
          (string-join (filter-map (lambda (slot)
                                     (let ((slot-name (slot-definition-name slot)))
                                       (and (slot-bound? object slot-name)
                                            (call-with-output-string
                                              (cut format <> "~s: ~s"
                                                   slot-name
                                                   (slot-ref object slot-name))))))
                                   (class-slots (class-of object))))))

(define (date->iso-8601 date)
  "Convert DATE, an SRFI-19 date object, to an ISO-8601 date string."
  (date->string date "~4"))

(define (iso-8601->date str)
  "Convert STR, an ISO-8601 date string, to an SRFI-19 date object."
  (string->date str "~Y-~m-~dT~H:~M:~S~z"))

(define (date->alist date)
  "Convert DATE, an SRFI-19 date object, to an association list."
  `((type . <date>)
    (iso-8601 . ,(date->iso-8601 date))))

(define (alist->date alist)
  "Convert an association list to an SRFI-19 date object."
  (iso-8601->date (assq-ref alist 'iso-8601)))

(define (object->scm object)
  "Convert GOOPS OBJECT to a serializable object."
  (cond
   ((or (string? object)
        (number? object)
        (boolean? object))
    object)
   ((date? object)
    (date->alist object))
   ((list? object)
    (list->vector (map object->scm object)))
   (else
    (cons (cons 'type (class-name (class-of object)))
          (map (lambda (slot)
                 (let* ((slot-name (slot-definition-name slot))
                        (value (if (slot-bound? object slot-name)
                                   (slot-ref object slot-name)
                                   (goops-error "Unbound slot ~s in ~s" slot-name object))))
                   (cons slot-name (object->scm value))))
               (class-slots (class-of object)))))))

(define (scm->object scm)
  "Convert serializable object SCM to a GOOPS object."
  (cond
   ((or (string? scm)
        (number? scm)
        (boolean? scm))
    scm)
   ((vector? scm)
    (map scm->object (vector->list scm)))
   ;; Association list encoding date
   ((eq? (assq-ref scm 'type)
         '<date>)
    (alist->date scm))
   ;; Association list encoding arbitrary object
   (else
    (let* ((class (module-ref (current-module)
                              (assq-ref scm 'type)))
           (object (make class)))
      (for-each (match-lambda
                  ((slot-name . value)
                   (unless (eq? slot-name 'type)
                     (slot-set! object slot-name (scm->object value)))))
                scm)
      object))))

(define-class <document> ()
  (title #:accessor document-title #:init-keyword #:title)
  (web-uri #:accessor document-web-uri #:init-keyword #:web-uri))

(define-method (document-type (document <document>))
  (string-trim-both (symbol->string (class-name (class-of document)))
                    (char-set #\< #\>)))

(define-method (document-term-generator (document <document>))
  "Return a term generator for DOCUMENT. The returned term generator has
indexed the type and text of the document. If further free text is to
be indexed, to prevent phrase searches from spanning between this text
and further text, increase-termpos! must be called before indexing."
  (let ((term-generator
         (make-term-generator
          #:stem (make-stem "en")
          #:document (make-document
                      #:data (call-with-output-string
                               (cut write (object->scm document) <>))
                      #:terms `((,(document-id-term document) . 0))))))
    (index-text! term-generator (document-title document) #:prefix "S")
    (index-text! term-generator (document-text document))
    term-generator))

(define-class <file-document> (<document>)
  (path #:accessor file-document-path #:init-keyword #:path))

(define-method (document-type (document <file-document>))
  (next-method))

(define-method (document-id-term (document <file-document>))
  "Return the ID term for DOCUMENT."
  (string-append "Q" (file-document-path document)))

(define-method (document-text (document <file-document>))
  "Return the full text of DOCUMENT."
  (call-with-file-in-git (current-git-repository) (file-document-path document)
    get-string-all))

(define-method (document-term-generator (document <file-document>))
  "Return a term generator indexing DOCUMENT."
  (let ((term-generator (next-method)))
    (increase-termpos! term-generator)
    (index-text! term-generator (file-document-path document))
    term-generator))

(define (document-html-snippet document mset)
  "Return snippet for DOCUMENT. MSET is the xapian MSet object
representing a list of search results."
  (mset-snippet mset
                (string-join
                 (remove (cut string-every char-set:whitespace <>)
                         (string-split (document-text document)
                                       #\newline))
                 "\n")
                #:length 200
                #:highlight-start "<b>"
                #:highlight-end "</b>"
                #:stemmer (make-stem "en")))

(define (document-snippet document mset)
  "Return snippet for DOCUMENT. MSET is the xapian MSet object
representing a list of search results."
  ;; mset-snippet, and thus document-html-snippet, returns serialized
  ;; HTML. So, we reverse it with html->sxml.
  (match (html->sxml (document-html-snippet document mset))
    (('*TOP* children ...)
     (string-join
      (map (match-lambda
             ;; Colorize string instead of HTML bold.
             (('b str) (colorize-string str 'BOLD 'ON-RED))
             ;; Else, return verbatim.
             (str str))
           children)
      ""))))

(define-method (print (document <file-document>) mset port)
  "Print DOCUMENT in command-line search results. MSET is the xapian
MSet object representing a list of search results."
  (display (colorize-string (document-title document) 'MAGENTA 'UNDERLINE)
           port)
  (newline port)
  (display (colorize-string "DOCUMENT" 'BOLD 'YELLOW) port)
  (display " " port)
  (display (colorize-string (file-document-path document) 'YELLOW)
           port)
  (newline port)
  (let ((snippet (document-snippet document mset)))
    (unless (string-null? snippet)
      (display snippet port)
      (newline port)
      (newline port))))

(define (document-sxml-snippet document mset)
  "Return snippet in SXML form for DOCUMENT. MSET is the xapian MSet
object representing a list of search results."
  ;; mset-snippet, and thus document-html-snippet, returns serialized
  ;; HTML. So, we reverse it with html->sxml.
  (match (html->sxml (document-html-snippet document mset))
    (('*TOP* children ...)
     (append-map (lambda (child)
                   (cond
                    ;; Add (br) if end of line.
                    ((and (string? child)
                          (string-suffix? "\n" child))
                     (list (string-trim-right child #\newline)
                           '(br)))
                    ;; Else, return verbatim.
                    (else
                     (list child))))
                 children))))

(define-method (document->sxml (document <file-document>) mset)
  "Render DOCUMENT to SXML. MSET is the xapian MSet object representing
a list of search results."
  `(li (@ (class "search-result search-result-document"))
       (a (@ (href ,(document-web-uri document))
             (class "search-result-title"))
          ,(document-title document))
       ,@(let ((snippet (document-sxml-snippet document mset)))
           (if snippet
               (list `(div (@ (class "search-result-snippet"))
                           ,@snippet))
               (list)))))

(define (read-gemtext-document file)
  "Reade gemtext document from FILE. Return a <file-document> object."
  (make <file-document>
    #:title (or (call-with-file-in-git (current-git-repository) file
                  (lambda (port)
                    (port-transduce (tfilter-map (lambda (line)
                                                   ;; The first level one
                                                   ;; heading is the title.
                                                   (and (string-prefix? "# " line)
                                                        (string-remove-prefix "# " line))))
                                    (rany identity)
                                    get-line-dos-or-unix
                                    port)))
                ;; Fallback to filename if document has no title.
                file)
    #:path file))