-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
184 lines (154 loc) · 5.28 KB
/
test.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
#%%
from frontmatter import Frontmatter
import markdown
fp = 'testtemplate.md'
# %%
head_body = Frontmatter.read_file(fp)
type(head_body['body'])
# %%
bodyhtml = markdown.markdown(head_body['body'], extensions=['toc', 'tables','fenced_code'])
# bodyhtml = markdown.markdown(head_body['body'], extensions=['toc', 'tables','fenced_code', 'codehilite'])
bodyhtml
# %%
ofp = 'test.html'
of = open(ofp,'w',encoding='utf-8',errors='xmlcharrefreplace')
of.write(bodyhtml)
of.close()
# %%
md = markdown.Markdown(extensions=['toc', 'tables','fenced_code'])
# need fenced_code here too
# %%
bodytoc = md.convert(head_body['body'])
# bodytoc
bodyhtml == bodytoc
# %%
md.toc
# %%
with open('test.html','r+',encoding='utf-8',errors='xmlcharrefreplace') as f:
old = f.read()
f.seek(0)
f.write(md.toc)
f.write(old)
f.close()
#%%
from bs4 import BeautifulSoup
htmlfp = '../saturn-drmtest.github.io/layout/articletest.html'
soup = BeautifulSoup(open(htmlfp).read(), "html.parser")
soup.title
# %%
type(soup.title.string)
# %%
soup.title.string = 'new title'
soup.title
# %%
soup = BeautifulSoup('<div id="content"></div>', "html.parser")
targetdiv = soup.find(id='content')
targetdiv.insert(0, tempcontent[1])
targetdiv
# %%
html = '''
<div id="offsetheader">
<img src="/assets/img/covers/codingcover.jpg"/>
</div>
'''
headImgSrc = '/assests/img/covers/architecturecover.jpg'
soup = BeautifulSoup(html, "html.parser")
targetDiv = soup.find(id='offsetheader')
targetDiv.img['src'] = headImgSrc
targetDiv
# %%
import os
class filepaths():
def __init__(self, orifp, desfolder):
self.path = orifp
self.fileList = []
self.validFileList = []
self.desFileDict = {}
self.desfolder = desfolder
def getFiles(self):
for root, subFolders, files in os.walk(self.path):
for fileName in files:
self.fileList.append(os.path.join(root, fileName))
def validFiles(self):
for fileName in self.fileList:
clearFileName = os.path.basename(fileName)
subfolderFileName = '/'.join(fileName.split('/')[3:])
htmlBaseName = os.path.splitext(subfolderFileName)[0] + '.html'
if clearFileName == '.DS_Store':
pass
elif os.path.exists(os.path.join(self.desfolder, htmlBaseName)):
pass
else:
self.validFileList.append(fileName)
self.desFileDict[fileName] = os.path.join(self.desfolder, htmlBaseName)
def getFilePaths(self):
return self.fileList
def getValidFileNames(self):
return self.validFileList
filepathclass = filepaths('../saturn-drmtest.github.io/posts', '../saturn-drmtest.github.io/postshtml')
filepathclass.getFiles()
filepathclass.validFiles()
# filepathclass.validFileList
dic = filepathclass.desFileDict
# %%
clearFileName = os.path.basename('../saturn-drmtest.github.io/posts/01blog/01digest/2020-01-26-资治通鉴.md')
clearFileName
# %%
subfolderFileName = '/'.join('../saturn-drmtest.github.io/posts/01blog/01digest/2020-01-26-资治通鉴.md'.split('/')[3:])
subfolderFileName
# %%
htmlBaseName = os.path.splitext(clearFileName)[0] + '.html'
htmlBaseName
# %%
os.path.exists(os.path.join('../saturn-drmtest.github.io/postshtml', htmlBaseName))
# %%
print('Converting %s' % os.path.basename('../saturn-drmtest.github.io/posts/01blog/01digest/2020-01-26-资治通鉴.md'))
# %%
def insertDiv(modifiedSoup, id=''):
targetDiv = soup.find(id=id)
targetDiv.clear()
targetDiv.insert(0, modifiedSoup)
htmltxt = '''
<h1 class="anchor" id="head1">head1</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et
dolore
magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea
commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu
fugiat
nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt
mollit
anim id est laborum.</p>
<h2 id="subhead1">subhead1</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et
dolore
magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea
commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu
fugiat
nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt
mollit
anim id est laborum.</p>
'''
htmlfp = '../saturn-drmtest.github.io/layout/article.html'
soup = BeautifulSoup(open(htmlfp).read(), "html.parser")
insertDiv(BeautifulSoup(htmltxt, 'html.parser'), id='content')
soup
# %%
from bs4 import BeautifulSoup
soup = BeautifulSoup("<b>stop</b>")
tag = soup.new_tag('h1')
tag.string = "Don't"
soup.find('b').string.insert_before(tag)
soup.b
# %%
html = '''
<div id="content">
<div id="post"><h1>title</h1></div>
</div>
'''
soup = BeautifulSoup(html, 'html.parser')
tag = soup.new_tag('h1')
tag.string = 'title2'
targettag = soup.find(id='post')
targettag.insert(0, tag)
soup
# %%