summaryrefslogtreecommitdiffstats
path: root/nikola/plugins/compile/rest/gist.py
diff options
context:
space:
mode:
authorLibravatarAgustin Henze <tin@sluc.org.ar>2013-11-20 16:58:50 -0300
committerLibravatarAgustin Henze <tin@sluc.org.ar>2013-11-20 16:58:50 -0300
commitca94afc07df55cb7fc6fe3b4f3011877b7881195 (patch)
treed81e1f275aa77545f33740723f307a83dde2e0b4 /nikola/plugins/compile/rest/gist.py
parentf794eee787e9cde54e6b8f53e45d69c9ddc9936a (diff)
Imported Upstream version 6.2.1upstream/6.2.1
Diffstat (limited to 'nikola/plugins/compile/rest/gist.py')
-rw-r--r--nikola/plugins/compile/rest/gist.py84
1 files changed, 84 insertions, 0 deletions
diff --git a/nikola/plugins/compile/rest/gist.py b/nikola/plugins/compile/rest/gist.py
new file mode 100644
index 0000000..e09ed76
--- /dev/null
+++ b/nikola/plugins/compile/rest/gist.py
@@ -0,0 +1,84 @@
+# -*- coding: utf-8 -*-
+# This file is public domain according to its author, Brian Hsu
+
+from docutils.parsers.rst import Directive, directives
+from docutils import nodes
+
+try:
+ import requests
+except ImportError:
+ requests = None # NOQA
+
+from nikola.plugin_categories import RestExtension
+from nikola.utils import req_missing
+
+
+class Plugin(RestExtension):
+
+ name = "rest_gist"
+
+ def set_site(self, site):
+ self.site = site
+ directives.register_directive('gist', GitHubGist)
+ return super(Plugin, self).set_site(site)
+
+
+class GitHubGist(Directive):
+ """ Embed GitHub Gist.
+
+ Usage:
+
+ .. gist:: GIST_ID
+
+ or
+
+ .. gist:: GIST_URL
+
+
+ """
+
+ 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 = '/'.join(("https://gist.github.com/raw", gistID, filename))
+ return requests.get(url).text
+
+ def get_raw_gist(self, gistID):
+ url = "https://gist.github.com/raw/{0}".format(gistID)
+ return requests.get(url).text
+
+ def run(self):
+ if 'https://' in self.arguments[0]:
+ gistID = self.arguments[0].split('/')[-1].strip()
+ else:
+ gistID = self.arguments[0].strip()
+ embedHTML = ""
+ rawGist = ""
+
+ if 'file' in self.options:
+ filename = self.options['file']
+ if requests is not None:
+ rawGist = (self.get_raw_gist_with_filename(gistID, filename))
+ embedHTML = ('<script src="https://gist.github.com/{0}.js'
+ '?file={1}"></script>').format(gistID, filename)
+ else:
+ if requests is not None:
+ rawGist = (self.get_raw_gist(gistID))
+ embedHTML = ('<script src="https://gist.github.com/{0}.js">'
+ '</script>').format(gistID)
+
+ if requests is None:
+ reqnode = nodes.raw(
+ '', req_missing('requests', 'have inline gist source',
+ optional=True), format='html')
+ else:
+ reqnode = nodes.literal_block('', rawGist)
+
+ return [nodes.raw('', embedHTML, format='html'),
+ nodes.raw('', '<noscript>', format='html'),
+ reqnode,
+ nodes.raw('', '</noscript>', format='html')]