aboutsummaryrefslogtreecommitdiff
path: root/blog/generate_feed.scm
blob: 8519f0ccd2366e558260301a5bbb1c5f43eddc99 (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
#! /usr/bin/guile \
-e main -s
!#

(use-modules (sxml simple))

(define site-url "https://shittyweb.org")

;; Generate description from article file
(define (get-description file)
  (call-with-input-file file
    (lambda (fp)
      (letrec ((read-characters
                (lambda (fp)
                  (let ((character (read-char fp)))
                    (unless (eof-object? character)
                      (display character)
                      (read-characters fp))))))
        (display "<![CDATA[\n")
        (read-characters fp)
        (display "]]>\n")))))

;; Generate rss item
(define (make-item article)
  (let ((title (cadr (list-ref article 0)))
        (name (cadr (list-ref article 2)))
        (pub-date (cadr (list-ref article 4)))
        (file (cadr (list-ref article 6))))
    (format #t "\n    <item>\n\
        <title>~a</title>\n\
        <link>~a/blog#~a</link>\n\
        <pubDate>~a</pubDate>\n\
        <description>\n" title site-url name pub-date)
    (get-description file)
    (display "        </description>\n")
    (display "    </item>\n")))
    
;; Generate rss feed
(define (generate-feed)
  ;; Display the rss header
  (format #t "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n\
<rss version=\"2.0\">\n\
  <channel>\n\
    <title>Nathan's shitty blog</title>\n\
    <description>The coffee powered blog of chaos</description>\n\
    <link>~a</link>\n\
    <image>\n\
      <url>~a/images/icon.png</url>\n\
      <title>Nathan's shitty blog</title>\n\
      <link>~a/blog</link>\n\
    </image>\n" site-url site-url site-url)

  (call-with-input-file "articles.xml"
    (lambda (fp)
      (let ((articles (xml->sxml fp)))
        (for-each (lambda (article)
                    (if (list? article)
                      (make-item (cddr article))))
                  (cdr (cadr articles))))))

  (display "  </channel>\n")
  (display "</rss>\n"))

(define (main args)
  (generate-feed))