root/generate-svg-french-revolutionary.scm

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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
(define-module (generate-svg-french-revolutionary)
  #:use-module (srfi srfi-19)
  #:use-module (sxml simple)
  #:export (generate-svg-french-revolutionary gdate->fyear))

;;; TODO haven't really checked on the different mappings to gregorian

(define day-w 11)
(define day-h day-w)
(define day-p 2)

(define month-w (* 10 (+ day-p day-w)))
(define month-h (* 3 (+ day-p day-h)))
(define month-p (* 2 day-h))

(define (is-leap-year fyear)
  (let ((year (+ fyear 1793)))
    (and (= 0 (remainder year 4))
         (or (not (= 0 (remainder year 100)))
             (=  0 (remainder year 400))))))

(define (month-max year) 12)

(define (days-in-month year month)
  (if (= 12 month)
      (if (is-leap-year year) 6 5)
      30))

(define (translate-str x y)
  (string-append "translate("
                 (number->string x) ","
                 (number->string y) ")"))

(define (season-name n)
  (cond ((= 0 n) "Autumn")
        ((= 1 n) "Winter")
        ((= 2 n) "Spring")
        ((= 3 n) "Summer")
        ((= 4 n) "Festive")))

(define (month-pos n)
  (define season (floor (/ n 3)))
  (define season-month (remainder n 3))
  (if (= 12 n)
      (cons (* (+ month-w day-p day-p) 1) (* (+ month-h month-p) season))
      (cons (* (+ month-w day-p day-p) season-month) (* (+ month-h month-p) season))))

(define (month-week-offset n)
  0)

(define (generate-month month)
  (let ((x (car (month-pos month)))
        (y (cdr (month-pos month))))
    `(g (@ (transform ,(translate-str x y)) (class "month"))
        (text (@ ;; (fill "#ffffbb")
               (x ,(exact->inexact (/ month-w 2)))
               (y ,-3)
               (text-anchor "middle")
               (font-family "Courier")
               (text-decoration "")
               (font-weight "bold")
               (fill "#ffffff")
               (font-size 9))
              ,(month-name month)))))

(define dayd (* 60 60 24))
(define (add-leap day season year)
  (if (and (is-leap-year year)
           (or (and (= season 0) (> day 59))
               (> season 0)))
      dayd
      0))

(define sept-21 264)

(define (frdate->gregorian day month year)
  ;; (define before-gregorian-year-end (or (< month 3)
  ;;                                       (and (= month 3)
  ;;                                            (<= day 12 ))))

  (time-monotonic->date
   (make-time 'time-monotonic 0
              (+ ;; (add-leap day month year)
                 ;; (if (and (= month 12) (= day 6))
                 ;;     (* 60 dayd)
                 ;;     (* day dayd))
                 (* 30 dayd month)
                 (* dayd day)
                 (* dayd sept-21)
                 (time-second
                  (date->time-monotonic
                       (make-date 0 0 0 0 0 1 (+ year 1792) 0)))
                 ;; (if before-gregorian-year-end
                 ;;     (* dayd sept-21)
                 ;;     (* -1 dayd (+ (* 3 30) 12)))
                 ;; (* 1 dayd)
                 ;; (if before-gregorian-year-end
                 ;;     (time-second
                 ;;      (date->time-monotonic
                 ;;       (make-date 0 0 0 0 0 1 (+ year 1792) 0)))
                 ;;     (time-second
                 ;;      (date->time-monotonic
                 ;;       (make-date 0 0 0 0 0 1 (+ year 1793) 0))))
                 )
              ))) ;; 2lazy2doit→

(define (generate-day day month year day-details-lam)
  (let* ((mo (month-week-offset month))
         (in-week (remainder (- (+ day mo) 1) 10))
         (x (* in-week (+ day-w day-p)))
         (y (* (floor (/ (- (+ day mo) 1) 10)) (+ day-h day-p)))
         (custom (day-details-lam (frdate->gregorian day month year))))
    ;; (warn year month day "is" (frdate->gregorian day month year))
    `(g (@ (transform ,(translate-str x y)) ,(assoc 'class custom))
        ,(assoc 'title custom)
        (rect (@ (height ,day-h)
                 (width ,day-w)
                 ,(assoc 'fill custom)))
        (text (@ (y ,(- day-h 1))
                 (x ,(/ day-w 2.0))
                 (font-size ,(- day-h 1)) (font-weight "bold")
                 (font-family "Courier") (text-anchor "middle") (fill "#ffaaaa"))
              ,day))))

(define (generate-days month-n year day-details-lam)
  (let ((day-tags '()) ;; this code is a shit
        (days-in-m (days-in-month year month-n)))
    (do ((day-n 1 (1+ day-n))) ((> day-n days-in-m))
      (let ((day-tag (generate-day day-n month-n year day-details-lam)))
        (set! day-tags (cons day-tag day-tags))
        day-tags))
    day-tags))

;; Chaos, Discord, Confusion, Bureaucracy, and The Aftermath
(define (month-name n)
  (cond ((= 0 n) "Vendémiaire")
        ((= 1 n) "Brumaire")
        ((= 2 n) "Frimaire")
        ((= 3 n) "Nivôse")
        ((= 4 n) "Pluviôse")
        ((= 5 n) "Ventôse")
        ((= 6 n) "Germinal")
        ((= 7 n) "Floréal")
        ((= 8 n) "Prairial")
        ((= 9 n) "Messidor")
        ((= 10 n) "Thermidor")
        ((= 11 n) "Fructidor")
        ((= 12 n) "Sansculottides")))

(define (generate-months year day-details-lam)
  (let ((month-tags '())) ;; this code is a shit
    (do ((month-n 0 (1+ month-n))) ((> month-n (month-max year)))
      (let* ((month-tag (generate-month month-n)))
        (set! month-tag (append month-tag (generate-days month-n year day-details-lam)))
        (set! month-tags (cons month-tag month-tags))
        month-tags))
    month-tags))

(define (year-legend year)
  `(g (@ (transform ,(string-append (translate-str (* 0.25 month-w) (* (+ month-h month-p) 4.4))
                                    " ")))
      (text (@ (text-anchor "center")
               (font-family "Courier")
               (font-weight "bold")
               (fill "#ffffff")
               (text-decoration "underline")
               (font-size 10))
            ,(string-append "Year: " (number->string year)))))

(define (gdate->fyear date)
  (if (> (date-year-day date) sept-21 )
      (- (date-year date) 1792)
      (- (date-year date) 1793)))

(define (generate-svg-french-revolutionary gyear-date day-details-lam)
  (let* ((year (gdate->fyear gyear-date))
         (month-tags (generate-months year day-details-lam))
         (total-height (+ (* (+ month-p month-h) 5) (* day-w 4)))
         (total-width (+ 15 (* 3 (+ day-p day-p day-p month-w)) (* day-w 4))))
    `(svg (@ (width ,total-width) (height ,total-height)
             (viewBox ,(string-append
                        "0 0 "
                        (number->string total-width) " "
                        (number->string total-height)))
             (xmlns "http://www.w3.org/2000/svg"))
          (rect (@ (height ,total-width)
                   (width ,total-width)
                   (fill "#332233")))
          (rect (@ (transform ,(translate-str day-w day-w))
                   (height ,(- total-height (* day-w 2)))
                   (width ,(- total-width (* day-w 2)))
                   (fill "#aa99aa")))
          (rect (@ (transform ,(translate-str (* day-w 2) (* day-w 2)))
                   (height ,(- total-height (* day-w 4)))
                   (width ,(- total-width (* day-w 4)))
                   (fill "#332233")))
          (g (@ (transform ,(translate-str (* day-w 3) (* day-w 4))))
             ,(year-legend year)
             (title ,(string-append "git activity french-revolutionary calendar " (number->string year)))
             ,month-tags
             ))))