;; Copyright (C) 2024 Denis 'GNUtoo' Carikli ;; ;; 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 . (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)))))))