Skip to content

Instantly share code, notes, and snippets.

@brianhsu
Created November 30, 2011 02:49
Show Gist options
  • Save brianhsu/1407759 to your computer and use it in GitHub Desktop.
Save brianhsu/1407759 to your computer and use it in GitHub Desktop.
[Python] reStructuredText GitHub Gist directive.
class GitHubGist(Directive):
""" Embed GitHub Gist.
Usage:
.. gist:: GIST_ID
"""
required_arguments = 1
optional_arguments = 1
option_spec = {'file': directives.unchanged}
final_argument_whitespace = True
has_content = False
def get_raw_gist_with_filename(self, gistID, filename):
url = "https://raw.github.com/gist/%s/%s" % (gistID, filename)
return urlopen(url).read()
def get_raw_gist(self, gistID):
url = "https://raw.github.com/gist/%s/" % (gistID)
return urlopen(url).read()
def run(self):
gistID = self.arguments[0].strip()
embedHTML = ""
rawGist = ""
if 'file' in self.options:
filename = self.options['file']
rawGist = (self.get_raw_gist_with_filename(gistID, filename))
embedHTML = '<script src="https://gist.github.com/%s.js?file=%s"></script>' % \
(gistID, filename)
else:
rawGist = (self.get_raw_gist(gistID))
embedHTML = '<script src="https://gist.github.com/%s.js"></script>' % gistID
return [nodes.raw('', embedHTML, format='html'),
nodes.raw('', '<noscript>', format='html'),
nodes.literal_block('', rawGist),
nodes.raw('', '</noscript>', format='html')]
directives.register_directive('gist', GitHubGist)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment