From fc58059cb4c14a5d10ab89d7bf48ff658aea7d99 Mon Sep 17 00:00:00 2001 From: Arun Isaac Date: Sun, 26 Jun 2022 23:29:54 +0530 Subject: document: Add object. * tissue/document.scm: New file. --- tissue/document.scm | 92 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 tissue/document.scm (limited to 'tissue/document.scm') diff --git a/tissue/document.scm b/tissue/document.scm new file mode 100644 index 0000000..202ea35 --- /dev/null +++ b/tissue/document.scm @@ -0,0 +1,92 @@ +;;; tissue --- Text based issue tracker +;;; Copyright © 2022 Arun Isaac +;;; +;;; 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 . + +(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 file title) + document? + (file document-file) + (title document-title)) + +(define (document->alist document) + "Convert DOCUMENT, a 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 object." + (document (assq-ref alist 'file) + (assq-ref alist 'title))) + +(define (print-document document) + "Print DOCUMENT, a 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 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 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))) -- cgit v1.2.3