Browse Source

put updates to existing pages in rss feed

Thomas Buck 2 years ago
parent
commit
40ff4481a6
1 changed files with 41 additions and 5 deletions
  1. 41
    5
      macros.py

+ 41
- 5
macros.py View File

306
 # preconvert hooks
306
 # preconvert hooks
307
 # -----------------------------------------------------------------------------
307
 # -----------------------------------------------------------------------------
308
 
308
 
309
+# -----------------------------------------------------------------------------
310
+# multi language support
311
+# -----------------------------------------------------------------------------
312
+
309
 def hook_preconvert_anotherlang():
313
 def hook_preconvert_anotherlang():
310
     MKD_PATT = r'\.(?:md|mkd|mdown|markdown)$'
314
     MKD_PATT = r'\.(?:md|mkd|mdown|markdown)$'
311
     _re_lang = re.compile(r'^[\s+]?lang[\s+]?[:=]((?:.|\n )*)', re.MULTILINE)
315
     _re_lang = re.compile(r'^[\s+]?lang[\s+]?[:=]((?:.|\n )*)', re.MULTILINE)
356
 
360
 
357
     pages[:] = vpages
361
     pages[:] = vpages
358
 
362
 
359
-
363
+# -----------------------------------------------------------------------------
364
+# compatibility redirect for old website URLs
365
+# -----------------------------------------------------------------------------
360
 
366
 
361
 _COMPAT = """        case "%s":
367
 _COMPAT = """        case "%s":
362
             $loc = "%s/%s";
368
             $loc = "%s/%s";
399
     fp.write("?>")
405
     fp.write("?>")
400
     fp.close()
406
     fp.close()
401
 
407
 
402
-
408
+# -----------------------------------------------------------------------------
409
+# sitemap generation
410
+# -----------------------------------------------------------------------------
403
 
411
 
404
 _SITEMAP = """<?xml version="1.0" encoding="UTF-8"?>
412
 _SITEMAP = """<?xml version="1.0" encoding="UTF-8"?>
405
 <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
413
 <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
431
 # postconvert hooks
439
 # postconvert hooks
432
 # -----------------------------------------------------------------------------
440
 # -----------------------------------------------------------------------------
433
 
441
 
442
+# -----------------------------------------------------------------------------
443
+# rss feed generation
444
+# -----------------------------------------------------------------------------
445
+
434
 _RSS = """<?xml version="1.0"?>
446
 _RSS = """<?xml version="1.0"?>
435
 <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
447
 <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
436
 <channel>
448
 <channel>
443
 <lastBuildDate>%s</lastBuildDate>
455
 <lastBuildDate>%s</lastBuildDate>
444
 <docs>http://blogs.law.harvard.edu/tech/rss</docs>
456
 <docs>http://blogs.law.harvard.edu/tech/rss</docs>
445
 <generator>Poole</generator>
457
 <generator>Poole</generator>
458
+<ttl>720</ttl>
446
 %s
459
 %s
447
 </channel>
460
 </channel>
448
 </rss>
461
 </rss>
454
     <link>%s</link>
467
     <link>%s</link>
455
     <description>%s</description>
468
     <description>%s</description>
456
     <pubDate>%s</pubDate>
469
     <pubDate>%s</pubDate>
470
+    <atom:updated>%s</atom:updated>
457
     <guid>%s</guid>
471
     <guid>%s</guid>
458
 </item>
472
 </item>
459
 """
473
 """
460
 
474
 
461
 def hook_postconvert_rss():
475
 def hook_postconvert_rss():
462
     items = []
476
     items = []
477
+
478
+    # all pages with "date" get put into feed
463
     posts = [p for p in pages if "date" in p]
479
     posts = [p for p in pages if "date" in p]
464
-    posts.sort(key=lambda p: p.date, reverse=True)
465
-    posts = posts[:10]
480
+
481
+    # sort by update if available, date else
482
+    posts.sort(key=lambda p: p.get("update", p.date), reverse=True)
483
+
484
+    # only put 20 most recent items in feed
485
+    posts = posts[:20]
486
+
466
     for p in posts:
487
     for p in posts:
467
         title = p.title
488
         title = p.title
468
         if "post" in p:
489
         if "post" in p:
469
             title = p.post
490
             title = p.post
491
+
470
         link = "%s/%s" % (BASE_URL, p.url)
492
         link = "%s/%s" % (BASE_URL, p.url)
493
+
471
         desc = p.html.replace("href=\"img", "%s%s%s" % ("href=\"", BASE_URL, "/img"))
494
         desc = p.html.replace("href=\"img", "%s%s%s" % ("href=\"", BASE_URL, "/img"))
472
         desc = desc.replace("src=\"img", "%s%s%s" % ("src=\"", BASE_URL, "/img"))
495
         desc = desc.replace("src=\"img", "%s%s%s" % ("src=\"", BASE_URL, "/img"))
473
         desc = desc.replace("href=\"/img", "%s%s%s" % ("href=\"", BASE_URL, "/img"))
496
         desc = desc.replace("href=\"/img", "%s%s%s" % ("href=\"", BASE_URL, "/img"))
474
         desc = desc.replace("src=\"/img", "%s%s%s" % ("src=\"", BASE_URL, "/img"))
497
         desc = desc.replace("src=\"/img", "%s%s%s" % ("src=\"", BASE_URL, "/img"))
475
         desc = htmlspecialchars(desc)
498
         desc = htmlspecialchars(desc)
499
+
476
         date = time.mktime(time.strptime("%s 12" % p.date, "%Y-%m-%d %H"))
500
         date = time.mktime(time.strptime("%s 12" % p.date, "%Y-%m-%d %H"))
477
         date = email.utils.formatdate(date)
501
         date = email.utils.formatdate(date)
478
-        items.append(_RSS_ITEM % (title, link, desc, date, link))
502
+
503
+        update = time.mktime(time.strptime("%s 12" % p.get("update", p.date), "%Y-%m-%d %H"))
504
+        update = email.utils.formatdate(update)
505
+
506
+        items.append(_RSS_ITEM % (title, link, desc, date, update, link))
479
 
507
 
480
     items = "".join(items)
508
     items = "".join(items)
481
 
509
 
491
     fp.write(rss)
519
     fp.write(rss)
492
     fp.close()
520
     fp.close()
493
 
521
 
522
+# -----------------------------------------------------------------------------
523
+# compatibility redirect for old mobile pages
524
+# -----------------------------------------------------------------------------
525
+
494
 _COMPAT_MOB = """        case "%s":
526
 _COMPAT_MOB = """        case "%s":
495
             $loc = "%s/%s";
527
             $loc = "%s/%s";
496
             break;
528
             break;
535
     fp.write("?>")
567
     fp.write("?>")
536
     fp.close()
568
     fp.close()
537
 
569
 
570
+# -----------------------------------------------------------------------------
571
+# displaying filesize for download links
572
+# -----------------------------------------------------------------------------
573
+
538
 def hook_postconvert_size():
574
 def hook_postconvert_size():
539
     file_ext = '|'.join(['pdf', 'zip', 'rar', 'ods', 'odt', 'odp', 'doc', 'xls', 'ppt', 'docx', 'xlsx', 'pptx', 'exe', 'brd', 'plist'])
575
     file_ext = '|'.join(['pdf', 'zip', 'rar', 'ods', 'odt', 'odp', 'doc', 'xls', 'ppt', 'docx', 'xlsx', 'pptx', 'exe', 'brd', 'plist'])
540
     def matched_link(matchobj):
576
     def matched_link(matchobj):

Loading…
Cancel
Save