summaryrefslogtreecommitdiff
path: root/tissue/document.scm
blob: 202ea3512f31a3e73f898954fb7b066103bf89ed (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
;;; 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-9)
  #:use-module (srfi srfi-26)
  #:use-module (srfi srfi-171)
  #:use-module (term ansi-color)
  #:use-module (xapian xapian)
  #:use-module (tissue utils)
  #:export (document
            document?
            document-file
            document-title
            document->alist
            alist->document
            print-document
            read-gemtext-document
            index-document))

(define-record-type <document>
  (document file title)
  document?
  (file document-file)
  (title document-title))

(define (document->alist document)
  "Convert DOCUMENT, a <document> object, to an association list that
can be serialized."
  `((type . document)
    (file . ,(document-file document))
    (title . ,(document-title document))))

(define (alist->document alist)
  "Convert ALIST to a <document> object."
  (document (assq-ref alist 'file)
            (assq-ref alist 'title)))

(define (print-document document)
  "Print DOCUMENT, a <document> object, in search results."
  (display (colorize-string (document-title document) 'MAGENTA 'UNDERLINE))
  (newline)
  (display (colorize-string (document-file document) 'YELLOW))
  (newline)
  (newline))

(define (read-gemtext-document file)
  "Reade gemtext document from FILE. Return a <document> object."
  (document file
            (or (call-with-input-file 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)))

(define (index-document db document)
  "Index DOCUMENT, a <document> object, in writable xapian DB."
  (let* ((idterm (string-append "Q" (document-file document)))
         (body (call-with-input-file (document-file document)
                 get-string-all))
         (doc (make-document #:data (call-with-output-string
                                      (cut write (document->alist document) <>))
                             #:terms `((,idterm . 0))))
         (term-generator (make-term-generator #:stem (make-stem "en")
                                              #:document doc)))
    (index-text! term-generator "document" #:prefix "XT")
    (index-text! term-generator body)
    (replace-document! db idterm doc)))