◊(Local Yarn Code "Check-in [9cf832ae]")

Overview
Comment:Dynamic footer text for articles. More comments in code. Clean up anchor links to notes.
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 9cf832aec142b2ec30fc1cda39bda7470395b6ab5ba04533fc4dd29ff6cc6926
User & Date: joel on 2018-09-20 12:38:04
Other Links: manifest | tags
Context
2018-09-21
21:54
Fix series info missing from footer check-in: 5b4bc26c user: joel tags: trunk
2018-09-20
12:38
Dynamic footer text for articles. More comments in code. Clean up anchor links to notes. check-in: 9cf832ae user: joel tags: trunk
02:19
Fix title in template function check-in: 7b7da3d7 user: joel tags: trunk
Changes

Modified crystalize.rkt from [d75fdc63] to [3a59963c].

93
94
95
96
97
98
99

100
101
102
103
104
105
106
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107







+







    noun_plural
    noun_singular))

(define table_articles (make-table-schema "articles" table_articles-fields))
(define table_notes (make-table-schema "notes" table_notes-fields #:primary-key-cols '(pagenode note_id)))
(define table_series (make-table-schema "series" table_series-fields))

;; Split all ◊note tags out of the Pollen doc
(define (doc->body/notes doc)
  (define (is-note? tx) (and (txexpr? tx) (equal? 'note (get-tag tx))))
  (splitf-txexpr doc is-note?))

;; ~~~ Provided functions: Initializing; Saving posts and notes

;; Initialize the database connection, creating the database if it doesn’t
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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228







+

+
+
-
+












-
+













+
+
+
+











-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+









+
+
+







  (define-values (body-txpr note-txprs) (doc->body/notes doc))
  (define doc-html (->html (cdr body-txpr)))
  
  (define-values (disposition disp-note-id)
    (notes->last-disposition-values note-txprs))
  (define-values (title-plain title-html-flow)
    (make-article-titles (maybe-meta 'title (default-title pubdate)) disposition))
  (define series-node (maybe-meta 'series))
  (define header (html$-article-open title-html-flow pubdate))

  (define footertext (make-article-footertext pagenode series-node disposition disp-note-id (length note-txprs)))
  (define footer (html$-article-close))
  (define footer (html$-article-close footertext))
  
  (define notes-section-html (crystalize-notes! pagenode title-plain note-txprs))

  ;; Values must come in the order defined in table_article_fields
  (define article-record
    (list (symbol->string pagenode)
          title-plain
          title-html-flow
          pubdate
          (maybe-meta 'updated)
          (maybe-meta 'author default-authorname)
          (maybe-meta 'conceal)
          (maybe-meta 'series)
          series-node
          (maybe-meta 'noun (series-noun))
          (length note-txprs)
          doc-html
          disposition
          disp-note-id
          (string-append header doc-html footer)
          "" ; listing_excerpt_html: Not yet used
          "")) ; listing_short_html: Not yet used

  (apply query! (make-insert/replace-query 'articles table_articles-fields) article-record)
          
  ◊string-append{◊header ◊doc-html ◊notes-section-html ◊footer})

;; ~~~ Article-related helper functions ~~~
;;

;; Given a disposition and title, return both a plain-text and HTML version of the title
(define (make-article-titles title-val disposition)
  (define disposition-part
    (cond [(non-empty-string? disposition)
           (define-values (mark _) (disposition-values disposition))
           (format "<span class=\"disposition-mark\">~a</span>" mark)]
          [else ""]))
  
  (cond [(txexpr? title-val)
         (values (apply string-append (tx-strs title-val))
                 (string-append (->html title-val) disposition-part))]
        [else (values title-val (string-append title-val disposition-part))]))
  

;; Convert a bunch of information about an article into some nice English and links.
(define (make-article-footertext pagenode series disposition disp-note-id note-count)
  (define s-title (series-title))
  (define s-noun (series-noun))
  (define series-part
    (cond [(non-empty-string? series-title)
           (format "This is ~a, part of <a href=\"/~a\">‘~a’</a>."
                   s-noun
                   series
                   s-title)]
          [else ""]))
  (define disp-part
    (cond [(non-empty-string? disposition)
           (define-values (mark verb) (disposition-values disposition))
           (format "Now considered <a href=\"/~a#~a\">~a&nbsp;~a</a>."
                   pagenode
                   disp-note-id
                   mark
                   verb)]
          [else ""]))
  (define notes-part
    (cond [(note-count . > . 1)
           (format "There are <a href=\"/~a#furthernotes\">~a notes</a> appended."
                   pagenode
                   note-count)]
          [(and (note-count . > . 0) (string=? disposition ""))
           (format "There is <a href=\"/~a#furthernotes\">a note</a> appended."
                   pagenode)]
          [else ""]))
  
  (format "~a ~a ~a" series-part disp-part notes-part))
    

;; ~~~ Notes ~~~

;; Save a collection of ◊note tags to the DB, and return the HTML of the complete
;; “Further Notes” section at the end
;;
(define (crystalize-notes! pagenode parent-title note-txprs)
  (define (crystalizer note-tx)
    (crystalize-note! note-tx (symbol->string pagenode) parent-title))
  
  (cond [((length note-txprs) . > . 0)
         (define notes-html (map crystalizer note-txprs))
         (html$-notes-section notes-html)]
        [else ""]))

;; Save an individual note to the DB and return the HTML of the complete note as
;; it should appear on an individual article page
;;
(define (crystalize-note! note-tx pagenode parent-title-plain)
  (define-values (_ attrs elems) (txexpr->values note-tx))
  (define disposition-attr (maybe-attr 'disposition attrs))
  (define note-date (maybe-attr 'date attrs))
  
  ;; Check required attributes
  (unless (non-empty-string? note-date)

Modified dust.rkt from [a49e4077] to [9b28a908].

30
31
32
33
34
35
36

37
38
39
40
41
42
43
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44







+







         racket/string)

;; Provides common helper functions used throughout the project

(provide maybe-meta     ; Select from (current-metas) or default value ("") if not available
         maybe-attr     ; Return an attribute’s value or a default ("") if not available
         series-noun    ; Retrieve noun-singular from current 'series meta, or ""
         series-title   ; Retrieve title of series in current 'series meta, or ""
         attr-present?  ; Test if an attribute is present
         disposition-values
         ymd->english
         ymd->dateformat
         default-authorname
         default-title
         tx-strs
55
56
57
58
59
60
61






62
63
64
65
66
67
68
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75







+
+
+
+
+
+








(define (series-noun)
  (define series-pagenode (->pagenode (or (select-from-metas 'series (current-metas)) "")))
  (case series-pagenode
    ['|| ""] ; no series specified
    [else (or (select-from-metas 'noun-singular series-pagenode) "")]))

(define (series-title)
  (define series-pagenode (->pagenode (or (select-from-metas 'series (current-metas)) "")))
  (case series-pagenode
    ['|| ""] ; no series specified
    [else (or (select-from-metas 'title series-pagenode) "")]))

(define (attr-present? name attrs)
  (for/or ([attr-pair (in-list attrs)])
          (equal? name (car attr-pair))))

(define (maybe-attr name attrs [missing ""])
  (define result (assoc name attrs))
  (cond
96
97
98
99
100
101
102
103
104

105
106
107
108
109
110
111
103
104
105
106
107
108
109


110
111
112
113
114
115
116
117







-
-
+







  (cond [(string=? "" str) (values "" "")]
        [else (let ([splut (string-split str)])
                (values (car splut) (string-join (cdr splut))))]))

;; The format of a note’s ID is “HTML-driven” (used as an anchor link) but is included
;; here since it also serves as a primary key in the DB.
(define (build-note-id txpr)
  (string-append "#"
                 (maybe-attr 'date (get-attrs txpr))
  (string-append (maybe-attr 'date (get-attrs txpr))
                 "_"
                 (uri-encode (maybe-attr 'author (get-attrs txpr) default-authorname))))

;; Extract the last disposition (if any), and the ID of the disposing note, out of a list of notes
(define (notes->last-disposition-values txprs)
  (define (contains-disposition? tx) (attr-present? 'disposition (get-attrs tx)))
  (define disp-notes (filter contains-disposition? txprs))

Modified template-html.rkt from [325f21c3] to [fdb84ec5].

69
70
71
72
73
74
75
76

77
78

79
80
81
82
83
84
85
86
87
88
69
70
71
72
73
74
75

76
77

78
79
80

81
82
83
84
85
86
87







-
+

-
+


-







    [else
     ◊string-append{<article class="no-title hentry">
      <h1><a href="#" class="rel-bookmark">
      <time datetime="◊published" class="entry-title">◊ymd->english[published]</time>
      </a></h1>
      <section class="entry-content">}]))

(define (html$-article-close)
(define (html$-article-close footertext)
  ◊string-append{</section>
 <footer class="article-info"><span class="x">(</span>Part of ‘Talking About Poetry’. Once I threw a mudball at a birdhouse. I’m not exactly proud of it, though.<span class="x">)</span></footer>
 <footer class="article-info"><span class="x">(</span>◊|footertext|<span class="x">)</span></footer>
 </article>})
 

(define (html$-page-body-close)
  ◊string-append{<footer>By Joel Dueck</footer>
 </main></body>})

;; Notes
;;
(define (html$-note-title author pagenode parent-title)
134
135
136
137
138
139
140
141

142
143
144
145
146
147
148
149
150
151
152

153
154
155
133
134
135
136
137
138
139

140
141
142
143
144
145
146
147
148
149
150

151
152
153
154







-
+










-
+



 ◊author-part
 </section>
 </article>})

(define (html$-note-in-article id date author author-url contents)
  ◊string-append{
 <div class="note u-comment" id="◊|id|">
 <h3><a href="◊|id|"><time class="dt-published" datetime="◊date">◊ymd->english[date]</time>
 <h3><a href="#◊|id|"><time class="dt-published" datetime="◊date">◊ymd->english[date]</time>
 </a></h3>
 <div class="p-content p-name">
 ◊contents
 </div>
 <div class="note-meta">
 &mdash;<a class="u-author h-card" href="◊|author-url|">◊|author|</a>
 </div>
 </div>})

(define (html$-notes-section note-htmls)
  ◊string-append{<div class="further-notes">
  ◊string-append{<div class="further-notes" id="furthernotes">
 <h2>Further Notes</h2>
 ◊(apply string-append note-htmls)
 </div>})