This repository has been archived by the owner on Oct 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_toc.py
65 lines (52 loc) · 1.67 KB
/
generate_toc.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
import os
from html.parser import HTMLParser
class TitleExtractor(HTMLParser):
inHeading = False
inSubHeading = False
title = ''
subtitle = ''
def handle_starttag(self, tag, attrs):
if tag == 'h1':
self.inHeading = True
if tag == 'section':
for name, value in attrs:
if name == 'data-field' and value == 'subtitle':
self.inSubHeading = True
def handle_data(self, data):
if self.inHeading:
self.title = data
if self.inSubHeading:
self.subtitle = data
def handle_endtag(self, tag):
if tag == 'h1':
self.inHeading = False
if tag == 'section':
self.inSubHeading = False
def get_title(self):
return self.title
def get_subtitle(self):
if len(self.subtitle) > 100:
return ''
return self.subtitle
def cleanup(str):
if '|' in str:
parts = str.split('|')
return parts[0]
else:
return str
readme_file = open('README.md', 'w')
readme_file.write('''
# My Medium Blog
This is a backup of my Medium blog. [https://thebojda.medium.com](https://thebojda.medium.com), generated from: [https://github.com/TheBojda/my-medium-blog](https://github.com/TheBojda/my-medium-blog)
''')
files = os.listdir('posts')
files.sort(reverse=True)
for post in files:
file = open('posts/' + post)
html = file.read()
file.close()
parser = TitleExtractor()
parser.feed(html)
readme_file.write(
'[' + cleanup(parser.get_title() + ' ' + parser.get_subtitle()) + '](' + 'https://thebojda.myethmeta.org/posts/' + post + ')\n\n')
readme_file.close()