# -*- 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 requests is None:
            msg = req_missing(['requests'], 'use the gist reST directive', optional=True)
            return [nodes.raw('', '<div class="text-error">{0}</div>'.format(msg), format='html')]
        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']
            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)

        return [nodes.raw('', embedHTML, format='html'),
                nodes.raw('', '<noscript>', format='html'),
                nodes.literal_block('', rawGist),
                nodes.raw('', '</noscript>', format='html')]

@@ -1201,226 +1201,8 @@
 f):%0A
-        if requests is None:%0A            msg = req_missing(%5B'requests'%5D, 'use the gist reST directive', optional=True)%0A            return %5Bnodes.raw('', '%3Cdiv class=%22text-error%22%3E%7B0%7D%3C/div%3E'.format(msg), format='html')%5D%0A
     
@@ -1241,16 +1241,16 @@
 nts%5B0%5D:%0A
+
         
@@ -1488,16 +1488,57 @@
 'file'%5D%0A
+            if requests is not None:%0A    
         
@@ -1595,32 +1595,32 @@
 tID, filename))%0A
-
             embe
@@ -1754,32 +1754,73 @@
 )%0A        else:%0A
+            if requests is not None:%0A    
             rawG
@@ -1853,16 +1853,16 @@
 istID))%0A
-
         
@@ -1980,16 +1980,283 @@
 istID)%0A%0A
+        if requests is None:%0A            reqnode = nodes.raw(%0A                '', req_missing('requests', 'have inline gist source',%0A                                optional=True), format='html')%0A        else:%0A            reqnode = nodes.literal_block('', rawGist)%0A%0A%0A
         
@@ -2384,40 +2384,15 @@
     
+req
 node
-s.literal_block('', rawGist)
 ,%0A  

fix #795 by making compile.rest.gist suggest requests instead of requiring them