This repository has been archived by the owner on Nov 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcreate-manual.py
67 lines (60 loc) · 2.45 KB
/
create-manual.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
# Converts the project Wiki to html and adds it as manual.
#
# Adapted from https://gist.github.com/mrexmelle/659abc02ae1295d60647
import os
import subprocess
import sys
from bs4 import BeautifulSoup
from grip import export
wikiDir = '/tmp/PuRestJson.wiki/'
exportDir = 'manual/'
if len(sys.argv) > 1:
exportDir = sys.argv[1]
if len(sys.argv) > 2:
wikiDir = sys.argv[2]
print ('Input directory: ', wikiDir)
print ('Output directory: ', exportDir)
for f in os.listdir(wikiDir):
if f.endswith('.md'):
print ('Converting: ', f)
baseFile = os.path.splitext(os.path.basename(f))[0]
htmlFile = baseFile + '.html'
export(wikiDir + f, render_inline=False,
out_filename=exportDir + htmlFile,
title=baseFile.replace('-', ' '))
# edit links to css, images and other pages.
htmlDoc = open(exportDir + htmlFile)
soup = BeautifulSoup(htmlDoc, 'lxml')
for s in soup.findAll('link'):
s.extract()
css = soup.new_tag('link')
css.attrs['rel'] = 'stylesheet'
css.attrs['href'] = 'style.css'
soup.head.append(css)
# internal links.
for a in soup.findAll('a'):
if a['href'].startswith('https://github.com/residuum/PuRestJson/wiki/'):
a['href'] = a['href']\
.replace('https://github.com/residuum/PuRestJson/wiki/', '')\
.replace('%5B', '')\
.replace('%5D', '')\
.replace(':', '') + '.html'
# images with link to itself.
for img in soup.findAll('img'):
if img['src'].startswith('https://camo.githubusercontent.com'):
img['src'] = img['data-canonical-src']\
.replace('https://raw.github.com/residuum/PuRestJson/master/manual/','')
del img['data-canonical-src']
a = img.parent
del a['href']
# write changes back to file
htmlDoc.close()
html = soup.prettify('utf-8')
with open(exportDir + htmlFile, 'wb') as edited:
edited.write(html)
# rename all files containing '[' and ']' in names,
# because Windows does not like those.
cleaned = htmlFile.replace('[', '').replace(']', '').replace(':', '')
if htmlFile != cleaned:
os.rename(exportDir + htmlFile, exportDir + cleaned)
os.rename(exportDir + "Home.html", exportDir + "index.html")