Skip to content

Instantly share code, notes, and snippets.

@Etana
Created September 23, 2014 10:01
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 Etana/34b0032a14e10f5d5429 to your computer and use it in GitHub Desktop.
Save Etana/34b0032a14e10f5d5429 to your computer and use it in GitHub Desktop.
import sys, os, re
from subprocess import check_output
if len(sys.argv)<2:
print("USAGE: python put_on_one_page.py page_to_put_on_one_page")
sys.exit(0)
if not os.path.isfile(sys.argv[1]):
print("given path %s is not a file" % (sys.argv[1],))
sys.exit(0)
with open(sys.argv[1], "r") as f:
content = f.read()
# remove <?php...?>
content = re.sub(r'<\?php[\s\S]*?\?>','',content)
uploaded_image = {}
def upload_image(rel_path):
global uploaded_image
if rel_path not in uploaded_image:
try:
out = check_output(["bash", "/tmp/imgurbash.sh", rel_path])
uploaded_image[rel_path] = str(out, 'UTF-8')[:-1]
except:
uploaded_image[rel_path] = ""
return uploaded_image[rel_path]
# replace <link rel="stylesheet" /> by file content
for i in set(re.findall(r'<link [^>]*?\brel="stylesheet" type="text/css" href="([^:"]+)" />', content)):
if os.path.isfile(i):
with open(i) as s:
sub = s.read()
for img in re.findall(r'["\'\(]([^\'(:"]+\.(?:png|jpeg|jpg|gif))["\'\)]', sub):
realimg = os.path.normpath("./"+os.path.dirname(i)+'/'+img)
sub = sub.replace(img, realimg)
content = re.sub(r'<link [^>]*?\brel="stylesheet" type="text/css" href="'+i+'" />', '<style type="text/css">'+sub+'</style>', content)
# replace <script type="text/javascript" src="..." /> by file content
for i in set(re.findall(r'<script type="text/javascript" src="([^:"]+)"></script>', content)):
if os.path.isfile(i):
with open(i) as s:
sub = s.read()
content = content.replace('<script type="text/javascript" src="'+i+'"></script>', '<script type="text/javascript">'+sub+'</script>')
for img in re.findall(r'["\'\(]([^\'(:"]+\.(?:png|jpeg|jpg|gif))["\'\)]', content):
if os.path.isfile(img):
url = upload_image(img)
if url != "":
content = content.replace(img, url)
print(content)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment