Skip to content

Instantly share code, notes, and snippets.

@dietmarw
Created February 6, 2012 12:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dietmarw/1751797 to your computer and use it in GitHub Desktop.
Save dietmarw/1751797 to your computer and use it in GitHub Desktop.
#!/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