create-manual.py 2.39 KB
Newer Older
1 2 3
# Converts the project Wiki to html and adds it as manual.
#
# Adapted from https://gist.github.com/mrexmelle/659abc02ae1295d60647
4

5 6 7 8
import os
import subprocess
import sys
from bs4 import BeautifulSoup
9

10 11
wikiDir = '/tmp/PuRestJson.wiki/'
exportDir = 'manual/'
12

13 14
if len(sys.argv) > 1:
    exportDir = sys.argv[1]
15

16 17
if len(sys.argv) > 2:
    wikiDir = sys.argv[2]
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
print 'Input directory: ', wikiDir
print 'Output directory: ', exportDir

# rename all files containing '[' and ']' in names,
# because Windows does not like those.
for f in os.listdir(wikiDir):
    cleaned = f.replace('[', '').replace(']', '').replace(':', '')
    if f != cleaned:
        os.rename(wikiDir + f, wikiDir + cleaned)

# convert md files one-by-one.
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'
        subprocess.call(['grip', wikiDir + f, '--export', '--no-inline', 
            exportDir + htmlFile])
        # 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, 'w') as edited:
            edited.write(html)

os.rename(exportDir + "Home.html", exportDir + "index.html")