This repository has been archived by the owner on Oct 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
insertions-and-slideshows.py
77 lines (67 loc) · 3.02 KB
/
insertions-and-slideshows.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
import sys
import os.path
import time
from HTMLParser import HTMLParser
gfImporting = False
gstrClassname = None
gInsertSought = False
def S3BinaryHostReplace( str ):
str = str.replace("(img/", "(http://b00p.s3-us-west-2.amazonaws.com/img/")
str = str.replace("(doc/", "(http://b00p.s3-us-west-2.amazonaws.com/doc/")
return str ;
class MyHTMLParser(HTMLParser):
def handle_starttag(self, tag, attrs):
global gfImporting
global gstrClassname
if tag == 'div': # is apparently lower-ized caseically speaking
for attr in attrs:
if attr[0] == 'id':
if attr[1] == gstrClassname: # name that class
gfImporting = True
def handle_endtag(self, tag):
global gfImporting
if tag == 'div': ## Div fails. case sensitive html!
gfImporting = False
gInsertSought = False
def handle_data(self, data):
global gfImporting
if gfImporting==True:
mainoutfile.write(S3BinaryHostReplace(data)) # show contents when we're importing
fname = sys.argv[1] # arg1 is source pathspec
fname = fname.replace("ref/", "") # remove /ref source prefix, if found
fname = fname.replace(".md", "") # remove .md suffix, so I can add S1, S2 to core fname before .md
mainoutfile = open("tmp/" + fname + ".md", "w")
slide = 1
parser = MyHTMLParser()
for line in sys.stdin:
# if we want a slice, start with # see, and get a hyperlink of See Foobat, and new file desty Foobat
if line.lower().startswith('# see '):
slide = slide + 1
mainoutfile.write('**Next: [' + line[5:].strip() + '](' + fname + 'S' + str(slide) + '.html)** \r\n\r\n')
mainoutfile.close()
mainoutfile = open("tmp/" + fname + "S" + str(slide) + ".md", "w")
line = '<title>' + line[6:] + '</title>\r\n# ' + line[6:]
# The top title is # See Foobat minus the # See portion
if line.lower().startswith('insert:'):
gInsertSought = True
params = line[len('insert:'):].split()
# filename ='src/' + params[0]
gstrClassname = params[1] # set this global
insertfilename = 'src/' + params[0]
if False == os.path.isfile(insertfilename):
insertfilename = 'src/ref/' + params[0] # sometimes i forget to prepend the ref/
if False == os.path.isfile(insertfilename):
insertfilename = 'src/ref/_' + params[0] #refs have the underscore but i can omit it (should be sure there's no same name in root!)
if False == os.path.isfile(insertfilename):
print("insert: source file not found, so ctrl-c and fix: " + insertfilename)
time.sleep(99999)
sys.exit() # won't stop this but worthy effort
f = open(insertfilename, "r")
parser.feed(f.read())
parser.close()
else:
mainoutfile.write(S3BinaryHostReplace(line)),
if True == gInsertSought:
print("insert id not found, so ctrl-c and fix. but this is broken")
# time.sleep(99999)
mainoutfile.close()