summaryrefslogtreecommitdiff
path: root/src/gizmo/wasp-newspaper.el
blob: 4bdb9caf7f78a8475e40b8f85c4e9431dc86c8be (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
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
;;; wasp-newspaper --- The Effort Post -*- lexical-binding: t; -*-
;;; Commentary:
;;; Code:

(require 'dash)
(require 's)
(require 'f)
(require 'ht)
(require 'wasp-utils)
(require 'wasp-db)
(require 'wasp-glossary)

(defvar w/newspaper-todays-articles nil)

(defconst w/newspaper-slogans
  (list
   "hello computer"
   "only on !discord IRC"
   "GoMoCo HaThPl"
   "good morning computer"
   "hack the planet"
   "!oomfie"
   "All the news that's fit to prin1"
   "I use arch by the way"
   "play void stranger (2023)"
   "[i](this was sent from godot)[i]"
   "LCOLONQ Lies in LaTeX"
   "Super idol's smile / Is not as sweet as yours / The sunlight at noon in August / Does not shine like you / Love the 105 °C you / Distilled water that is pure every drop"
   "this is where we read about the computer"
   "brought to you by viewers like you. thank you!"
   ))

(defconst w/newspaper-prices
  (list
   "1 COLON"
   "3 to 5"
   "501 Internal Server Error"
   "$3.50"
   "206 bpm"
   "1 boost"
   "a snack for friend"
   "59 frames per second"))

(defun w/newspaper-screenshot (k)
  "Take a screenshot and pass the path of that screenshot to K."
  (let ((path (s-concat (make-temp-name "/tmp/wasp-newspaper-screenshot") ".png")))
    (make-process
     :name "*wasp-newspaper-screenshot*"
     :buffer nil
     :command `("newspaper-screenshot" ,path)
     :sentinel
     (lambda (_ _)
       (funcall k path)))))

(w/defstruct
 w/newspaper-article
 headline
 author
 content
 image)

(defun w/newspaper-wrap-emoji (s)
  "Wrap emoji with appropriate TeX in S."
  (s-replace-regexp "[^[:ascii:]]" (lambda (c) (format "{\\\\figemote %s}" c)) s))

(defun w/newspaper-escape (s)
  "Apply appropriate subsitutions to S."
  (s-replace-regexp
   (rx "\"" (one-or-more (not "\"")) "\"")
   (lambda (x)
     (s-concat "``" (s-chop-suffix "\"" (s-chop-prefix "\"" x)) "''"))
   (s-replace-all
    '(("&" . "\\&")
      ("%" . "\\%")
      ("$" . "\\$")
      ("#" . "\\#")
      ("_" . "\\_")
      ("{" . "\\{")
      ("}" . "\\}")
      ("~" . "\\textasciitilde")
      ("^" . "\\textasciicircum")
      ("\\" . "\\textbackslash"))
    s)
   nil
   t))

(defun w/newspaper-article-tex (a)
  "Convert an article A to TeX source."
  (s-concat
   "\\byline{"
   (w/newspaper-wrap-emoji (w/newspaper-escape (w/newspaper-article-headline a)))
   "}{"
   (w/newspaper-wrap-emoji (w/newspaper-escape (w/newspaper-article-author a)))
   "}\n"
   (if (w/newspaper-article-image a)
       (s-concat
        "\\includegraphics[width=0.8\\linewidth]{"
        (w/newspaper-article-image a)
        "}\\\\"
        )
     "")
   (w/newspaper-wrap-emoji (w/newspaper-escape (w/newspaper-article-content a)))
   "\n\\closearticle\n"))

(w/defstruct
 w/newspaper
 slogan
 price
 articles
 (edition 1))

(defun w/newspaper-tex (np)
  "Convert a newspaper NP to TeX source."
  (s-replace-all
   (list
    (cons "FIG_EDITION" (number-to-string (w/newspaper-edition np)))
    (cons "FIG_SLOGAN" (w/newspaper-slogan np))
    (cons "FIG_PRICE" (w/newspaper-price np))
    (cons "FIG_ARTICLES" (apply #'s-concat (-map #'w/newspaper-article-tex (w/newspaper-articles np))))
    )
   (w/slurp (w/asset "newspaper/template.tex"))))

(defun w/newspaper-pdf (src k)
  "Build TeX SRC to PDF.
Pass the path of the generated PDF to K."
  (when (get-buffer "*wasp-newspaper-pdf*")
    (with-current-buffer "*wasp-newspaper-pdf*"
      (erase-buffer)))
  (let ((dir (make-temp-file "wasp-newspaper" t))
        (srcfile (w/tempfile "wasp-newspaper-src" src ".tex")))
    (make-process
     :name "wasp-newspaper-pdf"
     :buffer "*wasp-newspaper-pdf*"
     :command (list "print-newspaper" srcfile dir)
     :sentinel
     (lambda (_ _)
       (funcall k (f-join dir "newspaper.pdf"))))))

(defvar w/newspaper-test-issue
  (w/make-newspaper
   :slogan "hello computer" :price "3 to 5"
   :articles
   (list
    (w/make-newspaper-article
     :headline "omg hi oomfie"
     :author "Joel"
     :content "\\lipsum[1]")
    (w/make-newspaper-article
     :headline "omg hi oomfie"
     :author "Joel"
     :content "\\lipsum[1]"
     :image "/home/llll/tmp/mrgreen.png")
    (w/make-newspaper-article
     :headline "omg hi oomfie"
     :author "Joel"
     :content "\\lipsum[1]")
    (w/make-newspaper-article
     :headline "omg hi oomfie"
     :author "Joel"
     :content "\\lipsum[1]")
    )))

(defun w/newspaper ()
  "Generate and open today's work-in-progress newspaper."
  (interactive)
  (w/db-get
   "newspaper:edition"
   (lambda (edition)
     (w/newspaper-pdf
      (w/newspaper-tex
       (w/make-newspaper
        :slogan (w/pick-random w/newspaper-slogans) :price (w/pick-random w/newspaper-prices)
        :edition (string-to-number edition)
        :articles
        w/newspaper-todays-articles))
      #'find-file))))

(defun w/newspaper-publish ()
  "Finalize and publish today's work-in-progress newspaper."
  (interactive)
  (w/glossary-save)
  (w/db-get
   "newspaper:edition"
   (lambda (edstr)
     (let ((edition (string-to-number edstr)))
       (w/newspaper-pdf
        (w/newspaper-tex
         (w/make-newspaper
          :slogan (w/pick-random w/newspaper-slogans) :price (w/pick-random w/newspaper-prices)
          :edition edition
          :articles
          w/newspaper-todays-articles))
        (lambda (path)
          (make-process
           :name "fig-newspaper-publish"
           :command (list "scp" path (format "llll@pub.colonq.computer:~/public_html/news/%03d.pdf" edition))
           :sentinel
           (lambda (_ _)
             (w/db-set "newspaper:edition" (number-to-string (1+ edition)))
             (browse-url (format "https://pub.colonq.computer/~llll/news/%03d.pdf" edition))
             ))))))))

(provide 'wasp-newspaper)
;;; wasp-newspaper.el ends here