Created
February 6, 2012 12:10
-
-
Save dietmarw/1751797 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from __future__ import with_statement | |
from BeautifulSoup import BeautifulSoup | |
import subprocess as sub | |
import re | |
import glob | |
import sys | |
repls = [ | |
(re.compile(r'^[Mm][Oo][Dd][Ee][Ll][Ii][Cc][Aa]://Modelica/'),'../../'), | |
(re.compile(r'^[Mm][Oo][Dd][Ee][Ll][Ii][Cc][Aa]://ModelicaReference/'),'../../'), | |
(re.compile(r'^[Mm][Oo][Dd][Ee][Ll][Ii][Cc][Aa]://ModelicaServices/'),'../../'), | |
(re.compile(r'.*/omlibrary/'), ''), | |
(re.compile(r'[Mm][Oo][Dd][Ee][Ll][Ii][Cc][Aa]://([A-Za-z0-9.\'()_]*#)'), r'\1.html#'), | |
(re.compile(r'[Mm][Oo][Dd][Ee][Ll][Ii][Cc][Aa]://([A-Za-z0-9.\'()_]*)'), r'\1.html'), | |
] | |
def linkreplace(link): | |
for (regex,repl) in repls: | |
link = regex.sub(repl,link) | |
return link | |
for filepath in sorted(glob.glob('/home/dietmarw/.workspace/Modelica.git/Modelica/Resources/help/*.html')): | |
tag = '[Checking file %s]:\n' % filepath | |
sys.stdout.write(tag) | |
sys.stderr.write(tag) | |
pid = sub.call(['tidy', '-modify', '-quiet', filepath]) | |
with open(filepath,'r') as html_file: | |
soup = BeautifulSoup(html_file) | |
for a in soup.findAll('a'): | |
try: | |
a['href'] = linkreplace(a['href']) | |
except: | |
pass | |
for img in soup.findAll('img'): | |
try: | |
img['src'] = linkreplace(img['src']) | |
except: | |
pass | |
with open(filepath,'w') as html_file: | |
html_file.write(soup.__str__()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment