Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add a gist shortcode (Issue #2459). Would be nice to make rest_gist a…
…nd mdx_gist base on this. Nicer even to remove them all to the plugins repo
  • Loading branch information
ralsina committed Aug 28, 2016
1 parent 4766b81 commit 05aadc1
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES.txt
Expand Up @@ -4,6 +4,7 @@ New in master
Features
--------

* Exposed ``gist`` as a shortcode (Issue #2459)
* Always copy source files for listings (Issue #2473)
* Detect dependencies in template strings (Issue #2455)
* RSS feeds for sections (Issue #2068)
Expand Down
6 changes: 6 additions & 0 deletions docs/manual.txt
Expand Up @@ -936,6 +936,12 @@ chart
'Others', [14.2, 15.4, 15.3, 8.9, 9, 10.4]
{{% /chart %}}{{% /raw %}}

gist
Show GitHub gists. If you know the gist's ID, this will show it in your site:

{{% raw %}}{{% gist 2395294 %}} {{% /raw %}}


raw
Passes the content along, mostly used so I can write this damn section and you can see the shortcodes instead
of them being munged into shortcode **output**. I can't show an example because Inception.
Expand Down
13 changes: 13 additions & 0 deletions nikola/plugins/shortcode/gist.plugin
@@ -0,0 +1,13 @@
[Core]
name = gist
module = gist

[Nikola]
plugincategory = Shortcode

[Documentation]
author = Roberto Alsina
version = 0.1
website = https://getnikola.com/
description = Gist shortcode

58 changes: 58 additions & 0 deletions nikola/plugins/shortcode/gist.py
@@ -0,0 +1,58 @@
# -*- coding: utf-8 -*-
# This file is public domain according to its author, Brian Hsu

"""Gist directive for reStructuredText."""

import requests
from docutils.parsers.rst import Directive, directives
from docutils import nodes

from nikola.plugin_categories import ShortcodePlugin


class Plugin(ShortcodePlugin):
"""Plugin for gist directive."""

name = "gist"

def set_site(self, site):
"""Set Nikola site."""
self.site = site
site.register_shortcode('gist', self.handler)
return super(Plugin, self).set_site(site)

def get_raw_gist_with_filename(self, gistID, filename):
"""Get raw gist text for a filename."""
url = '/'.join(("https://gist.github.com/raw", gistID, filename))
return requests.get(url).text

def get_raw_gist(self, gistID):
"""Get raw gist text."""
url = "https://gist.github.com/raw/{0}".format(gistID)
try:
return requests.get(url).text
except requests.exceptions.RequestException:
raise self.error('Cannot get gist for url={0}'.format(url))

def handler(self, gistID, filename=None, site=None, data=None, lang=None, post=None):
"""Create HTML for gist."""
if 'https://' in gistID:
gistID = gistID.split('/')[-1].strip()
else:
gistID = gistID.strip()
embedHTML = ""
rawGist = ""

if filename 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:
rawGist = (self.get_raw_gist(gistID))
embedHTML = ('<script src="https://gist.github.com/{0}.js">'
'</script>').format(gistID)

output = '''{}
<noscript><pre>{}</pre></noscript>'''.format(embedHTML, rawGist)

return output, []

0 comments on commit 05aadc1

Please sign in to comment.