aboutsummaryrefslogtreecommitdiff
path: root/src/main.lisp
blob: f03bd1a771b1e02419c39cfe6fba0d9a8732204b (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
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
; Helpers

(defun lines (str)
  (split str #\linefeed))

(defun split (str c)
  (loop
    for i = 0 then (1+ j)
    as j = (position c str :start i)
    collect (subseq str i j)
    while j))

(defun write-file (filename s)
  (ensure-directories-exist filename)
  (with-open-file (str filename
                       :direction :output
                       :if-exists :supersede
                       :if-does-not-exist :create)
    (format str "~A" s)))

; Data

(defun song-key (key song)
  (let ((tuple (car song)))
    (cond
      ((eql tuple nil) nil)
      ((eql (car tuple) key) (cdr tuple))
      (t (song-key key (cdr song))))))

; HTML

(defun page (title body)
    (format 
      nil
      "<!doctype html><html lang=\"fr\"><meta charset=\"utf-8\"><meta name=\"viewport\" content=\"width=device-width\"><title>~A</title><link rel=\"stylesheet\" href=\"/main.css\"><link rel=\"icon\" href=\"/icon.png\">~A<script src=\"/main.js\"></script>"
      title
      body))

(defun h (node attrs children)
  (format
    nil
    "<~A ~A>~A</~A>"
    node
    (apply #'concatenate 'string (mapcar (lambda (x) (format nil " ~A=\"~A\"" (car x) (car (cdr x)))) attrs))
    (apply #'concatenate 'string children)
    node))

; Title

(defun title-tags (title from)
  (h "section" nil
     (list
       (h "h1" '(("class" "g-Title")) (list title))
       (h "div" '(("class" "g-Author")) (list from)))))

; Part

(defun part-name (key)
  (ecase key
    ('intro "Intro")
    ('verse "Couplet")
    ('chorus "Refrain")
    ('interlude "Interlude")
    ('outro "Outro")))

(defun part-tags (key children)
  (h "div" 
     '(("class" "g-Part"))
     (list
       (if
         (eql key 'all)
         nil
         (h "h3" nil (list (part-name key))))
       children)))

; Chords

(defun chord-div (chord)
  (h "span" '(("class" "g-Chords__Chord")) (list (string chord))))

(defun chord-cell (chords)
  (h "td" '(("class" "g-Chords__Cell"))
    (if
      (listp chords)
      (mapcar #'chord-div chords)
      (list (chord-div chords)))))

(defun chord-row (chords)
  (h "tr" nil (mapcar #'chord-cell chords)))

(defun chord-rows (xs)
  (if
    (eql xs nil)
    nil
    (cons (chord-row (car xs)) (chord-rows (cdr xs)))))

(defun chord-table (key row)
  (part-tags 
    key 
    (h "table" '(("class" "g-Chords__Table")) (chord-rows row))))

(defun chord-tables (xs)
  (if
    (eql xs nil)
    nil
    (let ((key (car (car xs)))
          (rows (cdr (car xs))))
      (cons (chord-table key rows) (chord-tables (cdr xs))))))

(defun chord-tags (chords tonality)
  (h "section" nil
     (list
       (h "h2" '(("class" "g-Subtitle")) '("Accords"))
       (h "div" '(("class" "g-Parts"))
          (cons
              (h "div" (list (list "data-tonality" tonality)) nil)
              (chord-tables chords))))))

; Lyrics

(defun emph (str cs)
  (apply #'concatenate 'string
    (loop for c across str collect
      (if (member c cs) (h "emph" nil (list (make-string 1 :initial-element c))) (make-string 1 :initial-element c)))))

(defun lyrics-line (line)
  (h "div" nil (list (emph line (list #\, #\. #\? #\!)))))

(defun lyrics-section (s)
  (let ((p (car (cdr s))))
    (part-tags 
      (car s)
      (if p 
        (h "div" '(("class" "g-Lyrics__Paragraph")) (mapcar #'lyrics-line (lines p)))
        nil))))

(defun lyrics-tags (lyrics)
  (h "section" nil
     (list
       (h "h2" '(("class" "g-Subtitle")) '("Paroles"))
       (h "div" '(("class" "g-Parts")) (mapcar #'lyrics-section lyrics)))))

; Main

(defun export-song (path)
  (let ((data (with-open-file (in path) (read in))))
    (let ((output (concatenate 'string "public/" (car (split path #\.)) ".html"))
          (title (car (song-key 'title (cdr data))))
          (from (car (song-key 'from (cdr data))))
          (tonality (car (song-key 'tonality (cdr data))))
          (chords (song-key 'chords (cdr data)))
          (lyrics (song-key 'lyrics (cdr data))))
      (write-file output (page
        (format nil "~A – ~A" title from)
        (h
          "body"
          nil
          (list
            (h "a" '(("class" "g-Back") ("href" "/")) '("Retour à l’accueil"))
            (title-tags title from)
            (chord-tags chords tonality)
            (lyrics-tags lyrics))))))))

(export-song "songs/graeme-allwright/petit-garcon.lisp")
(export-song "songs/ben-e-king/stand-by-me.lisp")
(export-song "songs/tri-yann/pelot-d-hennebont.lisp")