1 /*
2 * Copyright (c) 2009-2020 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
225 sexpr_for_each_noref(val, tmp, list) {
226 /* sanity check */
227 ASSERT3U(val->type, ==, VT_INT);
228
229 /* add the comment */
230 post_add_comment(post, val->i);
231 }
232
233 val_putref(list);
234 }
235
236 static int __do_load_post_body_fmt2(struct post *post, struct str *html)
237 {
238 str_putref(post->body); /* free the previous */
239 post->body = str_getref(html);
240 ASSERT(post->body);
241
242 return 0;
243 }
244
245 static int __do_load_post_body_fmt3(struct post *post, const struct str *input)
246 {
247 struct parser_output x;
248 int ret;
249
250 x.req = NULL;
251 x.post = post;
252 x.input = str_cstr(input);
253 x.len = str_len(input);
254 x.pos = 0;
255 x.lineno = 0;
256 x.table_nesting = 0;
257 x.texttt_nesting = 0;
258 x.sc_title = NULL;
259 x.sc_pub = NULL;
260 x.sc_tags = NULL;
261 x.sc_twitter_img = NULL;
262
263 fmt3_lex_init(&x.scanner);
264 fmt3_set_extra(&x, x.scanner);
287 if (x.sc_twitter_img) {
288 str_putref(post->twitter_img);
289 post->twitter_img = str_getref(x.sc_twitter_img);
290 }
291
292 post_add_tags(&post->tags, x.sc_tags);
293
294 str_putref(x.sc_title);
295 str_putref(x.sc_pub);
296 str_putref(x.sc_twitter_img);
297
298 str_putref(post->body); /* free the previous */
299 post->body = x.stroutput;
300 ASSERT(post->body);
301
302 return 0;
303 }
304
305 static int __load_post_body(struct post *post)
306 {
307 static const char *exts[4] = {
308 [2] = "html",
309 [3] = "tex",
310 };
311
312 char path[FILENAME_MAX];
313 struct str *raw;
314 int ret;
315
316 ASSERT3U(post->fmt, >=, 2);
317 ASSERT3U(post->fmt, <=, 3);
318
319 snprintf(path, FILENAME_MAX, "%s/posts/%d/post.%s",
320 str_cstr(config.data_dir), post->id, exts[post->fmt]);
321
322 raw = post_get_cached_file(post, path);
323 if (IS_ERR(raw))
324 return PTR_ERR(raw);
325
326 switch (post->fmt) {
327 case 2:
328 ret = __do_load_post_body_fmt2(post, raw);
329 break;
330 case 3:
331 ret = __do_load_post_body_fmt3(post, raw);
332 break;
333 }
334
335 str_putref(raw);
336
337 return ret;
338 }
339
340 static void __refresh_published_prop(struct post *post, struct val *lv)
341 {
342 /* update the time */
343 post->time = parse_time_str(sexpr_alist_lookup_str(lv, "time"));
344
345 /* update the title */
346 post->title = sexpr_alist_lookup_str(lv, "title");
347
348 /* update the format */
349 post->fmt = sexpr_alist_lookup_int(lv, "fmt", NULL);
350
351 /* update the listed bool */
352 post->listed = sexpr_alist_lookup_bool(lv, "listed", true, NULL);
|
1 /*
2 * Copyright (c) 2009-2020 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
3 * Copyright 2020, Kebe Software & Services
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
226 sexpr_for_each_noref(val, tmp, list) {
227 /* sanity check */
228 ASSERT3U(val->type, ==, VT_INT);
229
230 /* add the comment */
231 post_add_comment(post, val->i);
232 }
233
234 val_putref(list);
235 }
236
237 static int __do_load_post_body_fmt2(struct post *post, struct str *html)
238 {
239 str_putref(post->body); /* free the previous */
240 post->body = str_getref(html);
241 ASSERT(post->body);
242
243 return 0;
244 }
245
246 static int
247 __do_load_post_body_fmt4(struct post *post, const struct str *md)
248 {
249 /*
250 * Like HTML, assume the .lisp file contains all of the other post
251 * metadata. If that assumption changes, update here.
252 *
253 * Unlike HTML, we plan to provide a clean error string if MD
254 * parsing fails.
255 */
256 str_putref(post->body); /* Free the previous text. */
257 post->body = fmt4_md_to_html(md);
258 if (post->body == NULL) {
259 /*
260 * XXX KEBE SAYS do something clever here with a small HTML
261 * string. For now, panic.
262 */
263 ASSERT(post->body != NULL);
264 }
265
266 return (0);
267 }
268
269 static int __do_load_post_body_fmt3(struct post *post, const struct str *input)
270 {
271 struct parser_output x;
272 int ret;
273
274 x.req = NULL;
275 x.post = post;
276 x.input = str_cstr(input);
277 x.len = str_len(input);
278 x.pos = 0;
279 x.lineno = 0;
280 x.table_nesting = 0;
281 x.texttt_nesting = 0;
282 x.sc_title = NULL;
283 x.sc_pub = NULL;
284 x.sc_tags = NULL;
285 x.sc_twitter_img = NULL;
286
287 fmt3_lex_init(&x.scanner);
288 fmt3_set_extra(&x, x.scanner);
311 if (x.sc_twitter_img) {
312 str_putref(post->twitter_img);
313 post->twitter_img = str_getref(x.sc_twitter_img);
314 }
315
316 post_add_tags(&post->tags, x.sc_tags);
317
318 str_putref(x.sc_title);
319 str_putref(x.sc_pub);
320 str_putref(x.sc_twitter_img);
321
322 str_putref(post->body); /* free the previous */
323 post->body = x.stroutput;
324 ASSERT(post->body);
325
326 return 0;
327 }
328
329 static int __load_post_body(struct post *post)
330 {
331 static const char *exts[5] = {
332 [2] = "html",
333 [3] = "tex",
334 [4] = "md",
335 };
336
337 char path[FILENAME_MAX];
338 struct str *raw;
339 int ret;
340
341 ASSERT3U(post->fmt, >=, 2);
342 ASSERT3U(post->fmt, <=, 4);
343
344 snprintf(path, FILENAME_MAX, "%s/posts/%d/post.%s",
345 str_cstr(config.data_dir), post->id, exts[post->fmt]);
346
347 raw = post_get_cached_file(post, path);
348 if (IS_ERR(raw))
349 return PTR_ERR(raw);
350
351 switch (post->fmt) {
352 case 2:
353 ret = __do_load_post_body_fmt2(post, raw);
354 break;
355 case 3:
356 ret = __do_load_post_body_fmt3(post, raw);
357 break;
358 case 4:
359 ret = __do_load_post_body_fmt4(post, raw);
360 break;
361 }
362
363 str_putref(raw);
364
365 return ret;
366 }
367
368 static void __refresh_published_prop(struct post *post, struct val *lv)
369 {
370 /* update the time */
371 post->time = parse_time_str(sexpr_alist_lookup_str(lv, "time"));
372
373 /* update the title */
374 post->title = sexpr_alist_lookup_str(lv, "title");
375
376 /* update the format */
377 post->fmt = sexpr_alist_lookup_int(lv, "fmt", NULL);
378
379 /* update the listed bool */
380 post->listed = sexpr_alist_lookup_bool(lv, "listed", true, NULL);
|