Create a gist now

Instantly share code, notes, and snippets.

Create static HTML from a Gist ID. See http://jlarocco.com/2011/12/22/static-gist-creator/ for more info.
#!/usr/bin/env python3
import urllib.request
import sys
from PyQt4 import QtGui
from PyQt4 import QtCore
from PyQt4.QtScript import QScriptEngine, QScriptValue
def doc_write(context, eng):
if context.argumentCount() != 0:
na = context.argumentCount()
args = list()
for i in range(na):
args.append( str(context.argument(i).toVariant()))
temp = ' '.join(args)
# if not 'embed.css' in temp: broke when running the script on itself!
if not temp.startswith('<link rel="stylesheet"'):
temp = temp.replace("\n", '')
print(temp)
return QScriptValue('')
def main(args):
gist_id = None
if len(sys.argv) > 1:
gist_id = args[0]
else:
print("No Gist ID given!")
exit(0)
url = "https://gist.github.com/{gist_id}.js".format(gist_id=gist_id)
app = QtCore.QCoreApplication(args)
qse = QScriptEngine()
dw = qse.newFunction(doc_write)
qse.globalObject().setProperty('doc_write', dw)
fh = urllib.request.urlopen(url)
gist_script = fh.read().decode('utf-8')
gist_script = gist_script.replace('document.write', 'doc_write')
res = qse.evaluate(gist_script)
if __name__ == "__main__":
main(sys.argv[1:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment