summaryrefslogtreecommitdiff
path: root/doc/skribilo.scm
blob: 75ac3a24dcba662b79a61b190c8a556de766a48f (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
;;; 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 (doc skribilo)
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-9)
  #:use-module (srfi srfi-26)
  #:use-module (srfi srfi-171)
  #:use-module (ice-9 match)
  #:use-module (texinfo)
  #:use-module (skribilo package base)
  #:export (docstring-function-documentation
            function-documentation))

(define-record-type <function>
  (function name arguments docstring)
  function?
  (name function-name)
  (arguments function-arguments)
  (docstring function-docstring))

(define (find-function-definition file name)
  "Return a @code{<function>} object describing a function named
@var{name} in @var{file}."
  (call-with-input-file file
    (cut port-transduce
         (tmap identity)
         (rany (match-lambda
                 (((or 'define 'define* 'define-lazy)
                   ((? (cut eq? name <>)) arguments ...)
                    docstring
                    body ...)
                  (function name arguments docstring))
                 (_ #f)))
         read
         <>)))

(define (stexi->skribe stexi)
  "Convert @var{stexi}, a stexinfo tree, to a skribe tree."
  (match stexi
    (('*fragment* children ...)
     (map stexi->skribe children))
    (('para children ...)
     (cons 'paragraph children))))

(define (quoted-write object port)
  "Write @var{object} to @var{port} printing quoted expressions using
the quote character."
  (match object
    (('quote child)
     (display "'" port)
     (quoted-write child port))
    ((parent children ...)
     (display "(" port)
     (quoted-write parent port)
     (unless (null? children)
       (display " " port))
     (for-each (cut quoted-write <> port)
               children)
     (display ")" port))
    (_ (write object port))))

(define (docstring-function-documentation file name)
  "Document function of @var{name} from @var{file} using its docstring."
  (let ((function (or (find-function-definition file name)
                      (error "Function not found in file:" name file))))
    (item #:key (code (list "("
                            (bold (symbol->string name))
                            (unless (null? (function-arguments function))
                              " ")
                            (string-join (map (lambda (element)
                                                (call-with-output-string
                                                  (cut quoted-write element <>)))
                                              (function-arguments function)))
                            ")"))
          (map (cut eval <> (current-module))
               (stexi->skribe
                (texi-fragment->stexi
                 (function-docstring function)))))))

(define (function-documentation name arguments . documentation)
  "Document function of @var{name} with @var{arguments} and
@var{documentation}."
  (apply item
         #:key (code (list "("
                            (bold (symbol->string name))
                            " "
                            arguments
                            ")"))
        documentation))