/
makerss.py
executable file
·1077 lines (908 loc) · 32.3 KB
/
makerss.py
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
usage:
python makerss.py && python reverserss.py
I find this elisp useful:
(defun start-news-entry ()
(interactive)
(insert (format-time-string
" <a name=\"%Y-%m-%d\"></a><h3>%A %B %d %Y:</h3>"))
(newline)
(insert " <div class=\"pt\">") (newline) (newline)
(insert " <h3></h3>") (newline)
(insert " <h4>Tags: notyet</h4>") (newline) (newline) (newline)
(insert " </div>") (newline))
(require 'subr-x)
(defun process-sentinel (process event_type)
(message (format "%s: %s%s" process
(string-trim event_type)
(if (string= event_type "finished\n")
""
" (see buffer make-rss)"))))
(defun publish-news-entries ()
(interactive)
(message "publishing news entries in the background...")
(start-process "make-rss" "make-rss" "~/bin/makerss-and-reverserss.sh")
(set-process-sentinel (get-process "make-rss") 'process-sentinel))
"""
import sys, os, re, random, shutil, stat, subprocess
from lxml import etree
import lxml.html
from copy import deepcopy
from collections import defaultdict
import json
import urllib.parse
SHOW_ADS=False
def relative_to_absolute(pathname):
return os.path.join(os.path.dirname(__file__), pathname)
def load_snippet(snippet_name):
return open(relative_to_absolute(
os.path.join("snippets", snippet_name))).read().strip()
def load_html_js_snippet(snippet_name):
return '<script nonce="{{NONCE}}" type="text/javascript">%s</script>' % load_snippet(snippet_name)
class Configuration:
def __init__(self):
self.site_url = 'https://www.jefftk.com'
self.site_dir = '/home/jefftk/jtk'
self.in_html = 'news_raw.html'
self.front_page = "index.html"
self.out = 'news'
self.posts = 'p'
self.rss = 'news.rss'
self.rss_full = 'news_full.rss'
self.openring_filename = "current-openring.html"
self.rss_description = "Jeff Kaufman's Writing"
# How many posts to put in the rss feed.
self.rss_max = 30
# How many posts to put on the front page.
self.front_page_max = 6
# Author's UID on Google Plus
self.google_plus_uid = "103013777355236494008"
self.break_token = '~~break~~'
self.notyet_token = 'notyet'
self.nolw_token = 'nolw'
self.max_update_chars = 500
def full_url(self, leaf):
return os.path.join(self.site_url, leaf)
def full_filename(self, leaf):
if leaf.startswith('/'):
leaf = leaf[1:]
return os.path.join(self.site_dir, leaf)
def post_url(self, post):
return os.path.join(self.posts, post.name)
def relative_url(self, leaf):
return '/%s' % leaf
def front_page_fname(self):
return self.full_filename(self.front_page)
def new(self, fname):
return '%s-new' % fname
def prev(self, fname):
return '%s-prev' % fname
config = Configuration()
INTROS = json.load(open(relative_to_absolute("intros.json")))
BEST_POSTS = json.load(open(relative_to_absolute("best_posts.json")))
SNIPPETS = {
'rss_header': '''\
<?xml version="1.0" encoding="ISO-8859-1" ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<atom:link href="%s" rel="self" type="application/rss+xml" />
<title>%s</title>
<link>%s</link>
<description>%s</description>
<language>en-us</language>''' % (
config.full_url(config.rss),
config.rss_description,
config.full_url(config.posts),
config.rss_description),
'rss_footer': '\n</channel>\n</rss>',
}
def edit_front_page(front_page_list):
front_page = config.front_page_fname()
front_page_tmp = config.new(config.front_page_fname())
with open(front_page) as inf:
with open(front_page_tmp, 'w') as outf:
skipping = False
for line in inf:
if '<!-- end recent thoughts -->' in line:
for entry in front_page_list:
outf.write(entry)
outf.write('\n')
skipping = False
if not skipping:
outf.write(line)
if '<!-- begin recent thoughts -->' in line:
skipping = True
assert not skipping
os.rename(front_page_tmp, front_page)
def quote(s):
return s.replace('&', '&').replace('<','<').replace('>', '>').replace('"', '"')
def title_to_url_component(s):
s = s.lower()
s = s.replace("'", "").replace('"', "")
s = re.sub("[^A-Za-z0-9]", "-", s)
s = re.sub("-+", "-", s)
s = re.sub("^-*", "", s)
s = re.sub("-*$", "", s)
return s
def title_to_encoded(s):
if s.endswith("?"):
s = s[:-1]
# these won't work, but at least they won't mess with other directories
s = s.replace("/", "-")
return s
def dimensions(link_ondisk):
link_ondisk_dims = '%s.dimensions' % link_ondisk
dims = None
if os.path.exists(link_ondisk_dims):
with open(link_ondisk_dims) as inf:
dims = inf.read().strip()
if not dims:
identify_output = subprocess.check_output(['identify', link_ondisk])
dims = identify_output.split()[2].decode('utf-8')
with open(link_ondisk_dims, 'w') as outf:
outf.write(dims)
width, height = dims.split('x')
return int(width), int(height)
class Update:
def __init__(self, slug, post, element):
self.anchor = 'update-%s' % slug
self.slug = slug
self.year, self.numeric_month, self.day = slug.split('-')
self.short_month = {'01': 'Jan',
'02': 'Feb',
'03': 'Mar',
'04': 'Apr',
'05': 'May',
'06': 'Jun',
'07': 'Jul',
'08': 'Aug',
'09': 'Sep',
'10': 'Oct',
'11': 'Nov',
'12': 'Dec'}[self.numeric_month]
self.post = post
self.element = element
self.title = 'Update %s' % slug
def link(self):
return self.post.link() + '#%s' % self.anchor
def rss(self):
html = etree.tostring(
self.element, method='html', pretty_print=True).decode('utf-8')
html = re.sub('.*(<b>Update %s</b>)', '', html)
if len(html) > config.max_update_chars:
html = html[:500] + '...'
return '''
<item>
<guid>%s</guid>
<title>%s</title>
<link>%s</link>
<category>update_rss</category>
<pubDate>%s %s %s 08:00:00 EST</pubDate>
<description>%s</description>
</item>''' % (
config.relative_url(self.link()),
quote(self.title),
config.full_url(self.link()),
self.day, self.short_month, self.year,
quote(html))
def hidden_topic(tag):
return tag in ["all", "lwfeed"]
TAG_RENAMING = {
"giving": "ea",
}
class Post:
def __init__(self, slug, date, title, tags, element, openring):
self.slug = slug
self.date = date
self.title = title
self.published = config.notyet_token not in tags
yeslw = config.nolw_token not in tags
tags = [TAG_RENAMING.get(x, x) for x in tags
if x != config.notyet_token and x != config.nolw_token]
if yeslw:
tags.append("lwfeed")
self.name = title_to_url_component(title)
self.encoded_name = title_to_encoded(title)
self.element = element
self.openring = openring
self.updates = {}
# these are filled in externally for published posts
self.older_post = None
self.newer_post = None
# this is filled in externally for all posts
self.posts_by_slug = None
self.month, self.day, self.year = date.split()[1:4]
self.short_month = self.month[:3]
self.fb_link = None
services = []
for tag in tags:
if '/' not in tag:
continue
service, token = tag.split('/', 1)
if service == 'g+':
services.append((1, 'google plus', 'gp',
'https://plus.google.com/%s/posts/%s' % (
config.google_plus_uid, token), token))
elif service == 'fb':
if token.startswith('note/'):
token = token[len('note/'):]
fb_link = 'https://www.facebook.com/note.php?note_id=%s' % token
else:
token = token.split('_')[-1]
fb_link = 'https://www.facebook.com/jefftk/posts/%s' % token
self.fb_link = fb_link
services.append((2, 'facebook', 'fb', fb_link, token))
elif service == 'lw':
if not token.startswith("posts/") and not token.startswith("lw/"):
token = "posts/%s" % token
lw_link = 'https://lesswrong.com/%s' % token
services.append((3, 'lesswrong', 'lw', lw_link, token))
elif service == 'ea':
suffix = 'posts'
if token.startswith('old/'):
_, token = token.split('/', 1)
suffix = 'ea'
ea_link = 'https://forum.effectivealtruism.org/%s/%s' % (suffix, token)
services.append((4, 'the EA Forum', 'ea', ea_link, token))
elif service == 'r':
subreddit, post_id = token.split('/')
r_link = 'https://www.reddit.com/r/%s/comments/%s' % (
subreddit, post_id)
services.append((5, 'r/%s' % subreddit, 'r', r_link, token))
elif service == 'hn':
services.append((6, 'hacker news', 'hn',
'https://news.ycombinator.com/item?id=%s' % token,
token))
elif service == 'm1':
services.append((7, 'mastodon', service,
'https://schelling.pt/@jefftk/%s' % token,
token))
elif service == 'm':
services.append((8, 'mastodon', service,
'https://mastodon.mit.edu/@jefftk/%s' % token,
token))
if self.published and self.year > "2020":
if not any(service[2] == "lw" for service in services):
print("nolw", title)
# sort by and then strip off priorities
self.services = [x[1:] for x in sorted(services)]
self.tags = list(sorted(set(x for x in tags if '/' not in x)))
for possible_update in element.findall('.//b'):
update_text = possible_update.text
if update_text and update_text.startswith('Update '):
match = re.match(r'Update (\d\d\d\d-\d\d-\d\d)', update_text)
if match:
update_slug, = match.groups()
update = Update(update_slug, self, possible_update.getparent())
if update.anchor in self.updates:
raise Exception('Duplicate anchor %s on %s' % (
update.anchor, title))
self.updates[update.anchor] = update
# make it possible to link to this update
parent = possible_update.getparent()
parent.insert(parent.index(possible_update),
etree.Element('a', name=update.anchor))
for img_parent in element.findall('.//*[img]'):
img = img_parent.find('img')
src = img.get('src')
if src and src.startswith('/'):
src_ondisk = config.full_filename(src[1:])
width, height = dimensions(src_ondisk)
if bool(img.get('width')) != bool(img.get('height')):
raise Exception('bad image %s in %s' % (
etree.tostring(img, method='html'), title))
if not img.get('width'):
img.set('width', str(width))
img.set('height', str(height))
max_width = 100.0
max_height = max_width * height / width
img.set('class', 'mobile-fullwidth')
img.set('style', 'max-width:%.1fvw; max-height:%.1fvw;' %
(max_width, max_height))
img_parent.append(etree.Element(
'div', attrib={"class": "image-vertical-spacer"},
style="height:min(%.1fvw, %spx)" % (max_height, height)))
if not img.get('srcset'):
fname, ext = src.rsplit('.', 1)
if ext in ['jpg', 'png']:
srcsets = []
for sizing in ['2x', '3x', '4x']:
newsrc = '%s-%s.%s' % (fname, sizing, ext)
for suffix in ['-sm', '-small', '-tn', '-1x']:
if fname.endswith(suffix):
newsrc = '%s-%s.%s' % (fname[:-len(suffix)], sizing, ext)
newsrc_ondisk = config.full_filename(newsrc)
if os.path.exists(newsrc_ondisk):
newwidth, _ = dimensions(newsrc_ondisk)
srcsets.append('%s %sw' % (newsrc, newwidth))
if srcsets:
srcsets.insert(0, '%s %sw' % (src, width))
img.set('srcset', ','.join(srcsets))
def link(self):
return os.path.join(config.posts, self.name)
def bare_html(self, element):
# don't include the wrapping div
return '<p>' + '\n'.join(self.stringify_(x) for x in element.findall('*'))
def blog_entry_summary(self):
element = deepcopy(self.element)
removing = False
for child in element.findall('.//*'):
if child.text and config.break_token in child.text:
child.text, _ = child.text.split(config.break_token)
removing = True
child.tail = ''
elif child.tail and config.break_token in child.tail:
child.tail, _ = child.tail.split(config.break_token)
removing = True
elif removing:
child.getparent().remove(child)
link = config.relative_url(self.link())
append_more = "%s<a href='%s'>%s...</a>" % (
' ' if removing else '<p>', link, 'more' if removing else 'full post')
html = self.stringify_(element) + append_more
return '''\
<div class=blog-entry-summary>
<div class=blog-entry-date><a
class=invisible-link href='%s'>%s %s, %s</a></div>
<div class=blog-entry-title><a class=invisible-link href='%s'>%s</a></div>
<div class=blog-entry-beginning>
<p>%s
</div>
</div>''' % (link, self.month, tidy_day(self.day), self.year,
link, self.title, html)
def html(self):
element = deepcopy(self.element)
for tt in element.findall('.//tt'):
tt.tag = 'code'
if not self.published:
element.insert(0, parse('<p><i>draft post</i></p>'))
# make youtube responsive
for iframe in element.findall('.//iframe'):
if 'youtube' in iframe.get('src'):
width, height = iframe.attrib['width'], iframe.attrib['height']
del iframe.attrib['width']
del iframe.attrib['height']
div = etree.Element('div')
div.attrib['style'] = (
'position: relative;'
'padding-bottom: %.2f%%;'
'height: 0;'
'overflow: hidden;'
'max-width: 100%%;') % (100 * float(height) / float(width))
iframe.attrib['style'] = (
'position: absolute;'
'top: 0;'
'left: 0;'
'width: 100%;'
'height: 100%;')
parent = iframe.getparent()
iframe_index = list(parent).index(iframe)
parent.remove(iframe);
parent.insert(iframe_index, div)
div.append(iframe)
no_tags_no_ws = re.sub('<[^>]*>', '',
re.sub('\s+',' ',
re.sub('<style>[^<]*</style>', '',
self.bare_html(element)))).strip()
head = etree.Element('head')
description = content=no_tags_no_ws[:400]
head.append(etree.Element('meta', name='description', content=description))
head.append(etree.Element(
'meta', name='keywords', content=', '.join(
tag for tag in self.tags if not hidden_topic(tag))))
head.append(etree.Element(
'meta', name='viewport', content=load_snippet('meta_viewport.html')))
head.append(etree.Element('meta', charset='utf-8'))
if SHOW_ADS:
head.append(parse('''\
<script nonce="{{NONCE}}" async='async' src='https://www.googletagservices.com/tag/js/gpt.js'></script>'''))
head.append(parse('''\
<script nonce="{{NONCE}}">
var googletag = googletag || {};
googletag.cmd = googletag.cmd || [];
</script>'''))
head.append(parse('''\
<script nonce="{{NONCE}}">
googletag.cmd.push(function() {
googletag.pubads().setForceSafeFrame(true).setSafeFrameConfig({useUniqueDomain: true});
var maxAdWidth = 550;
if (window.innerWidth >= 1030 || window.innerWidth < 850) {
maxAdWidth = window.innerWidth - 16;
}
googletag.defineSlot('/21707489405/post_bottom_square', {
'fixed': [300, 250],
'max': [maxAdWidth, 400],
}, 'div-gpt-ad-1524882696974-0').addService(googletag.pubads());
googletag.pubads().enableSingleRequest();
googletag.enableServices();
});
</script>'''))
title = etree.Element('title')
title.text = self.title
head.append(title)
body = etree.Element('body')
head.append(parse(load_html_js_snippet('google_analytics.js')))
head.append(parse(load_html_js_snippet('hover_preview.js')))
head.append(parse('<style>%s</style>' % load_snippet('post.css')))
wrapper = etree.Element('div', id='wrapper')
wrapper.append(parse(links_partial()))
wrapper.append(etree.Element('hr'))
content = etree.Element('div')
content.set('class', 'content')
fancy_date = '%s %s, %s' % (self.month, tidy_day(self.day), self.year)
tag_block = ''
if self.tags:
tag_block = '<span>%s</span>' % (
', '.join(
'<i><a href="/news/%s">%s</a></i>' % (tag, tag)
for tag in self.tags
if not hidden_topic(tag)))
content.append(parse('''<table id="title-date-tags">
<tr><td valign="top" rowspan="2"><h3><a href="%s">%s</a></h3></td>
<td align="right" valign="top">%s</td></tr>
<tr><td align="right" valign="top">%s</td></tr></table>''' % (
config.relative_url(self.link()), self.title, fancy_date, tag_block)))
content.append(element)
if self.newer_post:
newer_section='''\
<a id=newer href="%s">
<div class=arr>
←
</div>
%s
</a>''' % (config.relative_url(self.newer_post.link()),
self.newer_post.title)
else:
newer_section='<div id=newer-blank></div>'
older_section = ''
if self.older_post:
older_section = '''\
<a id=older href="%s">
%s
<div class=arr>
→
</div>
</a>''' % (config.relative_url(self.older_post.link()),
self.older_post.title)
content.append(parse('<div id=newer-older>%s%s</div>' % (
newer_section, older_section)))
if self.services:
content.append(parse('<p>Comment via: %s</p>\n' % (
', '.join('<a href="%s">%s</a>' % (service_link, service_name)
for service_name, _, service_link, _ in self.services))))
content.append(parse('''\
<div id="comments">
%s
<script nonce="{{NONCE}}" type="text/javascript">
%s
</script>
</div>''' % (
load_html_js_snippet('comment_script.js'),
'\n'.join(
"pullComments('/wsgi/json-comments/%s/%s', '%s');\n" % (
service_abbr, service_tag, service_name)
for service_name, service_abbr, service_link, service_tag
in self.services))))
wrapper.append(content)
wrapper.append(self.openring)
wrapper.append(parse('<p>'))
if SHOW_ADS:
wrapper.append(parse('''\
<div id="ad-wrapper">
<div id='div-gpt-ad-1524882696974-0'>
<script nonce="{{NONCE}}">
googletag.cmd.push(function() { googletag.display('div-gpt-ad-1524882696974-0'); });
</script>
</div>
</div>
'''))
best_post_slugs = []
while len(best_post_slugs) < 5:
slug, = random.choices(
population=([slug for (slug, _, _) in BEST_POSTS]),
weights=([weight for (_, _, weight) in BEST_POSTS]),
k=1)
if slug not in best_post_slugs:
best_post_slugs.append(slug)
best_posts = [self.posts_by_slug[slug] for slug in best_post_slugs]
best_posts_html = '<p><b>More Posts</b></p><ul>%s</ul>' % (
''.join('<li><p><a href="%s">%s</a></p></li>' % (
config.relative_url(other.link()),
other.title) for other in best_posts))
preview_iframe_html = (
'<div id=preview><iframe id=preview-iframe scrolling=no '
'sandbox="allow-same-origin"></iframe>'
'<button id=preview-open>open</button></div>')
wrapper.append(parse(
'<div id="right-column"><div id="top-posts">%s</div>%s</div>' % (
best_posts_html,
preview_iframe_html)))
wrapper.append(etree.Element('hr'))
wrapper.append(parse(links_partial()))
body.append(wrapper)
image_link = None
for a_img in element.findall('.//a[img]'):
if not image_link or 'highlight' in a_img.attrib:
image_link = "%s%s" % (config.site_url, a_img.get('href'))
if image_link is None:
image_link = "%s/jefftk-glyph-framed.jpg" % config.site_url
head.append(etree.Element('meta', property="og:title", content=self.title))
head.append(etree.Element('meta', property="og:type", content="article"))
head.append(etree.Element('meta', property="og:image", content=image_link))
head.append(etree.Element('meta', property="og:url", content="%s/%s" % (
config.site_url, self.link())))
head.append(etree.Element('meta', property="og:description", content=description))
page = etree.Element('html', lang='en', prefix="og: https://ogp.me/ns#")
page.append(head)
page.append(body)
return '<!doctype html>\n%s' % etree.tostring(
page, method='html', pretty_print=True).decode('utf-8').replace(
config.break_token, '').replace('dontescapethis', 'href')
def stringify_(self, element):
return etree.tostring(
element, method='html', pretty_print=True).decode(
'utf-8').replace(config.break_token, '')
def rss(self):
element = deepcopy(self.element)
if self.services:
comments_links = ', '.join(
'<a href="%s">%s</a>' % (
service_link, service_name)
for (service_name, _, service_link, _) in self.services)
element.append(parse('<p><i>Comment via: %s</i>' %
comments_links))
html = self.bare_html(element)
# Temporary work-around for LW not handling relative URLs in RSS import
# https://github.com/LessWrong2/Lesswrong2/issues/2434
for prefix in ['href="', 'src="', 'srcset="', ' 550w,', '1100w,']:
html = html.replace("%s/" % prefix, '%s%s/' % (prefix, config.site_url))
return '''
<item>
<guid>%s</guid>
<title>%s</title>
<link>%s</link>
%s
<pubDate>%s %s %s 08:00:00 EST</pubDate>
<description>%s</description>
</item>''' % (
config.relative_url(self.link()),
quote(self.title),
config.full_url(self.link()),
'\n '.join('<category>%s</category>' % tag
for tag in sorted(self.tags) if not hidden_topic(tag)),
self.day, self.short_month, self.year,
quote(html))
def parse(s):
try:
return lxml.html.fragment_fromstring(s, create_parent=False)
except:
print(s)
raise
def parsePosts():
posts = []
published_posts = []
posts_by_name = {}
posts_by_slug = {}
raw_fname = config.full_filename(config.in_html)
cleaned_fname = raw_fname + '.tmp'
with open(raw_fname) as inf:
with open(cleaned_fname, 'w') as outf:
raw_text = inf.read()
for f, r in [('“', '"'),
('”', '"'),
("’", "'")]:
raw_text = raw_text.replace(f, r)
outf.write(raw_text)
with open(config.full_filename(config.openring_filename)) as inf:
openring = parse(inf.read())
with open(cleaned_fname) as inf:
tree = etree.parse(inf, etree.HTMLParser())
body = tree.find('body')
prev = None
prev_prev = None
for element in body.findall('*'):
# Posts are divs
# Post metadata is right before each div, in first an a and then an h3
# Yes, this is silly, not changing it now.
if element.tag == 'div':
assert prev.tag == 'h3'
assert prev_prev.tag == 'a', element.findall('*')[0].text
slug = prev_prev.get('name')
date = prev.text[:-1]
post_elements = element.findall('*')
title_h3 = post_elements[0]
try:
tags_h4 = post_elements[1]
except Exception:
print(post_elements)
print(title_h3.text)
print("Prev: %s" % posts[-1].title)
raise
assert title_h3.tag == 'h3'
title = title_h3.text
assert tags_h4.tag == 'h4'
tags_raw = tags_h4.text
assert tags_raw.startswith('Tags:')
tags = [x.strip() for x in tags_raw[len('Tags:'):].split(',')]
# Remove the title and tags now that we're done with them.
element.remove(title_h3)
element.remove(tags_h4)
if tags_h4.tail:
# don't lose this text
first = etree.Element('span')
first.text = tags_h4.tail
element.insert(0, first)
if not title:
print(post_elements)
post = Post(slug, date, title, tags, element, openring)
posts.append(post)
if post.published:
published_posts.append(post)
if post.name in posts_by_name:
raise Exception('Duplicate post title: %r and %r' % (
posts_by_name[post.name].title, post.title))
posts_by_name[post.name] = post
if post.slug in posts_by_slug:
raise Exception('Duplicate post slug: %r for %r and %r' % (
post.slug, posts_by_slug[post.slug].title, post.title))
posts_by_slug[post.slug] = post
post.posts_by_slug = posts_by_slug
prev_prev = prev
prev = element
# Now that we've loaded all the posts, update links to use pretty
# names and not slugs.
#for post in posts:
# for anchor in post.element.findall('.//a'):
# href = anchor.get('href')
# if href:
# match = re.match(r'(\d\d\d\d-\d\d-\d\d)', href)
# if match:
# slug, = match.groups()
# new_href = config.relative_url(posts_by_slug[slug].link())
# anchor.set('href', new_href)
for published_post_earlier, published_post_later in zip(
published_posts[1:], published_posts):
published_post_earlier.newer_post = published_post_later
published_post_later.older_post = published_post_earlier
return posts
def links_partial():
return '''
<div class="headfoot">
<ul>
<li><a href="/" rel="author">Jeff Kaufman</a></li>
<li><a href="/p/index">Posts</a></li>
<li><a href="/news.rss">RSS</a></li>
<li><a href="__REVERSE_RSS__">◂◂RSS</a></li>
<li><a href="/contact">Contact</a></li>
</ul>
</div>
'''
def delete_old_staging():
for modifier in [config.new, config.prev]:
for directory in [config.out, config.posts]:
full_directory = config.full_filename(modifier(directory))
try:
shutil.rmtree(full_directory)
except FileNotFoundError:
pass
for fname in [config.rss, config.rss_full]:
try:
full_fname = config.full_filename(modifier(fname))
except FileNotFoundError:
pass
def make_new_staging():
for directory in [config.out, config.posts]:
os.mkdir(config.full_filename(config.new(directory)))
def start():
delete_old_staging()
make_new_staging()
front_page_list = []
tag_to_posts = defaultdict(list)
rss_entries = {} # (slug | update_slug-original_slug) -> entry
fb_links = []
for post in parsePosts():
fname_base = config.full_filename(
os.path.join(config.new(config.posts), post.name))
fname_slug = config.full_filename(
os.path.join(config.new(config.out), post.slug)) + '.html'
with open(fname_base + '.html', 'w') as outf:
outf.write(post.html())
shutil.copy(fname_base + '.html', fname_slug)
with open(config.full_filename(
os.path.join(config.new(config.posts), post.encoded_name)) + ".html",
"w") as outf:
outf.write(
'<meta http-equiv=refresh content="0;URL=\'%s\'">\n' % (
post.name))
st = os.stat(fname_slug)
os.chmod(fname_slug, st.st_mode & (stat.S_IRUSR | stat.S_IWUSR))
if post.published:
if len(front_page_list) < config.front_page_max:
front_page_list.append(post.blog_entry_summary())
tags = set(['all']).union(post.tags)
for tag in tags:
tag_to_posts[tag].append(post)
rss_entries[post.slug] = post.rss()
for update in post.updates.values():
rss_entries['%s-%s' % (update.slug, post.slug)] = update.rss()
if post.fb_link:
fb_links.append([post.title, post.date, post.fb_link])
with open(config.full_filename("fblinks.html"), 'w') as outf:
outf.write("<ul>\n")
for title, date, fb_link in fb_links:
outf.write('<li>%s <a href="%s">%s</a>\n' % (
date, fb_link, title))
outf.write("</ul>\n")
rss_entries = list(reversed([
entry for _, entry in sorted(rss_entries.items())]))
for rss_file in [config.rss, config.rss_full]:
with open(config.full_filename(
config.new(rss_file)), 'w') as outf:
outf.write(SNIPPETS['rss_header'])
for n, rss_entry in enumerate(rss_entries):
if rss_file == config.rss_full or n < config.rss_max:
outf.write(rss_entry)
outf.write(SNIPPETS['rss_footer'])
tag_json = {}
for tag, tag_posts in tag_to_posts.items():
if tag == 'all':
rss_link = '/news.rss'
else:
rss_link = '/news/%s.rss' % tag
tag_json[tag] = [post.link() for post in tag_posts]
entries = '\n'.join('''\
<li><a href="%s">
<div class=title>%s</div>
<div class=date>%s %s, %s</div></a></li>''' % (
config.relative_url(post.link()), post.title, post.month,
post.day, post.year) for post in tag_posts)
intro = wrap_intro(INTROS.get(tag, ""))
if tag == "all":
intro = "<dl>%s</dl><h3>All %s posts going back to 2004:</h3>" % (
"\n".join(
"<dt><a href='/news/%s'>%s</a></dt><dd>%s</dd>" % (k, k, v)
for (k, v) in sorted(INTROS.items(), key=lambda _: random.random())),
format(len(tag_posts), ","))
with open(config.full_filename(os.path.join(
config.new(config.out), '%s.html' % tag)), 'w') as outf:
outf.write('''\
<html>
<head>
<title>%s</title>
<meta name=viewport content="%s">
<link rel=alternate type="application/rss+xml" title="RSS Feed" href="%s">
%s
</head>
<body>
<style>
h2 { margin: .5em }
body {
margin: 0;
padding: 0;
}
#content {
max-width: 550px;
margin-left: auto;
margin-right: auto;
}
#content p {
margin: 5px;
}
#content li { list-style-type: none; margin: 0 }
#content li a { display: block; padding: .75em }
#content li a:link { text-decoration: none }
.title:hover { text-decoration: underline }
#content ul { margin: 0; padding: 0 }
#content li:nth-child(odd) {
background: #EEE;
}
dd {
margin-bottom: 0.5em;
}
.date { font-size: 85%% ; color: black }
.headfoot { margin: 3px }
.headfoot ul {
list-style-type: none;
margin: 0;
padding: 0;
}
.headfoot li {
display: inline;
margin-left: 5px;
margin-right: 5px;
}
</style>
%s<hr>
<div id=content>
<h2>Posts :: %s (<a href="%s">rss</a>)</h2>
%s
<ul>
%s
</ul>
</div>
<hr>%s
</body>
</html>''' % (
'Blog Posts' if tag == 'all' else 'Posts tagged %s' % tag,
load_snippet('meta_viewport.html'),
rss_link,
load_html_js_snippet('google_analytics.js'),
links_partial(),
tag,
rss_link,
intro,
entries,
links_partial()))
edit_front_page(front_page_list)
for x in [
config.posts, config.out, config.rss, config.rss_full]:
x = config.full_filename(x)