aboutsummaryrefslogtreecommitdiffstats
path: root/build/update-makefile.scm
blob: 4eea4f3dedc1aabf81a0a0625041f036e94ee49d (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
;; Copyright (C) 2024 Denis 'GNUtoo' Carikli <GNUtoo@cyberdimension.org>
;;
;; This program 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.
;;
;; This program 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 this program.  If not, see <https://www.gnu.org/licenses/>.

(define-module (build update-makefile)
  #:use-module (ice-9 textual-ports)
  #:use-module (rnrs base))

;; Input https://blog.replicant.us/2023/12/replicant-37c3/
;; Ouptut: posts/2023_12_replicant-37c3.md
(define-public (html-to-markdown url)
  """Convert an URL like https://blog.replicant.us/2023/12/replicant-37c3/
     to the corresponding markdown file path like
     posts/2023_12_replicant-37c3.md"""

  ;; Output: replicant-37c3
  (define page
    (list-ref (string-split url #\/) 5))

  ;; Output: 2023
  (define year
    (list-ref (string-split url #\/) 3))

  ;; Output: 12
  (define month
    (list-ref (string-split url #\/) 4))

  (string-append "posts/" year "_" month "_" page ".md"))

(define-public (get-markdown-article-lines port)
  """Generate the list of lines like
     '\tposts/2023_12_replicant-37c3.md \\' from links.txt."""
  (define lines '())
  (define (func port lines)
    (let ((line (get-line port)))
      (if (eof-object? line)
          (reverse lines)
          ((lambda _
             (set!
              lines
              (cons
               (string-append "\t" (html-to-markdown line) " \\\n")
               lines))
             (func port lines))))))
  (func port lines))

(define-public (generate-new-makefile generated-text)
  """Generate a Makefile with the generated-text instead to replace the
     'POSTS = [...]' part of the Makefile."""
  (define lines '())
  (define (process-makefile port)
    (define print? #t)
    (define (func port print?)
      (let ((line (get-line port)))
        (if (eof-object? line)
            (reverse lines)
            ((lambda _
               (if (string=? line "POSTS = \\")
                   (set! print? #f))
               (if print?
                   (set! lines (cons (string-append line "\n") lines)))
               (if (and (not print?) (string=? line "\t$(SENTINEL)"))
                   ((lambda _
                      (set! print? #t)
                      (set! lines (cons generated-text lines)))))
               (func port print?))))))
    (func port print?))
  process-makefile)

(define-public new-markdown-articles-text
  (generate-new-makefile
   (string-append
    "POSTS = \\\n"
    (string-join
     (call-with-input-file "links.txt" get-markdown-article-lines)
     "")
    "\t$(SENTINEL)\n")))

(define-public new-makefile-text
  (string-join
   (call-with-input-file "Makefile" new-markdown-articles-text)
   ""))

;;;;;;;;;;;
;; Tests ;;
;;;;;;;;;;;
(define-public update-makefile-tests
  (list
   (list
    "html-to-markdown"
    (lambda _
      (assert
       (string=?
        (html-to-markdown "https://blog.replicant.us/2023/12/replicant-37c3/")
        "posts/2023_12_replicant-37c3.md"))))

   (list
    "get-markdown-article-lines"
    (lambda _
      (assert
       (string=?
        (string-join
         (call-with-input-file "tests/links.txt" get-markdown-article-lines)
         "")
        (call-with-input-file "tests/markdown-article-lines.txt"
          get-string-all)))))

   (list
    "generate-new-makefile"
    (lambda _
      (assert
       (string=?
	(string-join
	 (call-with-input-file "tests/Makefile" (generate-new-makefile ""))
	 "")
	(call-with-input-file "tests/Makefile.empty" get-string-all)))))))