Repository: getnikola/nikola

Issue:
Gist URL scheme has changed
It appears that the Gist url structure has been changed since the plugin was written. They've added a username to it. The link to Gists seems to redirect properly, but the raw response url does not (GIST_RAW_URL = "https://gist.github.com/raw/{0}"). 

Also, the regex is out of date. They are no longer just digits. Mine appears to probably be a hex ID, but my sample size is 1 so far, so I'm not so confident in that :)



Discussion:
This applies to the Markdown gist plugin, the reST plugin works just fine, because it uses a directive and not crazy regexps to find links to gist.github.com.

We need to modify everything there to support usernames and hex IDs, and we might also manage to break old posts in the process.

I don't see how this is someone else's bug since mdx_gist.py is shipped with Nikola :-(

This was written by @mrabbitt maybe he can fix it?

I'm taking a look.  By the way the username is not required.  If you request a Gist URL with a hexidecimal ID and without a username, you'll get a 302 redirect to the URL with the username; e.g.
- https://gist.github.com/c4a43d6fdce612284ac0.js redirects to https://gist.github.com/mrabbitt/c4a43d6fdce612284ac0.js
- https://gist.githubusercontent.com/raw/c4a43d6fdce612284ac0/cow.txt redirects to https://gist.githubusercontent.com/mrabbitt/c4a43d6fdce612284ac0/raw/cow.txt

So I think the only issue is that the Markdown implementation assumes the Gist ID is numeric, and it needs to be loosened to allow hexidecimal IDs.


Resolving patch:
diff --git a/nikola/plugins/compile/markdown/mdx_gist.py b/nikola/plugins/compile/markdown/mdx_gist.py
--- a/nikola/plugins/compile/markdown/mdx_gist.py
+++ b/nikola/plugins/compile/markdown/mdx_gist.py
@@ -65,6 +65,42 @@
     </div>
     </p>
 
+Basic Example with hexidecimal id:
+
+    >>> import markdown
+    >>> text = """
+    ... Text of the gist:
+    ... [:gist: c4a43d6fdce612284ac0]
+    ... """
+    >>> html = markdown.markdown(text, [GistExtension()])
+    >>> print(html)
+    <p>Text of the gist:
+    <div class="gist">
+    <script src="https://gist.github.com/c4a43d6fdce612284ac0.js"></script>
+    <noscript>
+    <pre>Moo</pre>
+    </noscript>
+    </div>
+    </p>
+
+Example with hexidecimal id filename:
+
+    >>> import markdown
+    >>> text = """
+    ... Text of the gist:
+    ... [:gist: c4a43d6fdce612284ac0 cow.txt]
+    ... """
+    >>> html = markdown.markdown(text, [GistExtension()])
+    >>> print(html)
+    <p>Text of the gist:
+    <div class="gist">
+    <script src="https://gist.github.com/c4a43d6fdce612284ac0.js?file=cow.txt"></script>
+    <noscript>
+    <pre>Moo</pre>
+    </noscript>
+    </div>
+    </p>
+
 Example using reStructuredText syntax:
 
     >>> import markdown
@@ -83,6 +119,42 @@
     </div>
     </p>
 
+Example using hexidecimal ID with reStructuredText syntax:
+
+    >>> import markdown
+    >>> text = """
+    ... Text of the gist:
+    ... .. gist:: c4a43d6fdce612284ac0
+    ... """
+    >>> html = markdown.markdown(text, [GistExtension()])
+    >>> print(html)
+    <p>Text of the gist:
+    <div class="gist">
+    <script src="https://gist.github.com/c4a43d6fdce612284ac0.js"></script>
+    <noscript>
+    <pre>Moo</pre>
+    </noscript>
+    </div>
+    </p>
+
+Example using hexidecimal ID and filename with reStructuredText syntax:
+
+    >>> import markdown
+    >>> text = """
+    ... Text of the gist:
+    ... .. gist:: c4a43d6fdce612284ac0 cow.txt
+    ... """
+    >>> html = markdown.markdown(text, [GistExtension()])
+    >>> print(html)
+    <p>Text of the gist:
+    <div class="gist">
+    <script src="https://gist.github.com/c4a43d6fdce612284ac0.js?file=cow.txt"></script>
+    <noscript>
+    <pre>Moo</pre>
+    </noscript>
+    </div>
+    </p>
+
 Error Case: non-existent Gist ID:
 
     >>> import markdown
@@ -95,7 +167,8 @@
     <p>Text of the gist:
     <div class="gist">
     <script src="https://gist.github.com/0.js"></script>
-    <noscript><!-- WARNING: Received a 404 response from Gist URL: https://gist.github.com/raw/0 --></noscript>
+    <noscript><!-- WARNING: Received a 404 response from Gist URL: \
+https://gist.githubusercontent.com/raw/0 --></noscript>
     </div>
     </p>
 
@@ -111,7 +184,8 @@
     <p>Text of the gist:
     <div class="gist">
     <script src="https://gist.github.com/4747847.js?file=doesntexist.py"></script>
-    <noscript><!-- WARNING: Received a 404 response from Gist URL: https://gist.github.com/raw/4747847/doesntexist.py --></noscript>
+    <noscript><!-- WARNING: Received a 404 response from Gist URL: \
+https://gist.githubusercontent.com/raw/4747847/doesntexist.py --></noscript>
     </div>
     </p>
 
@@ -140,11 +214,11 @@
 
 GIST_JS_URL = "https://gist.github.com/{0}.js"
 GIST_FILE_JS_URL = "https://gist.github.com/{0}.js?file={1}"
-GIST_RAW_URL = "https://gist.github.com/raw/{0}"
-GIST_FILE_RAW_URL = "https://gist.github.com/raw/{0}/{1}"
+GIST_RAW_URL = "https://gist.githubusercontent.com/raw/{0}"
+GIST_FILE_RAW_URL = "https://gist.githubusercontent.com/raw/{0}/{1}"
 
-GIST_MD_RE = r'\[:gist:\s*(?P<gist_id>\d+)(?:\s*(?P<filename>.+?))?\s*\]'
-GIST_RST_RE = r'(?m)^\.\.\s*gist::\s*(?P<gist_id>\d+)(?:\s*(?P<filename>.+))\s*$'
+GIST_MD_RE = r'\[:gist:\s*(?P<gist_id>\S+)(?:\s*(?P<filename>.+?))?\s*\]'
+GIST_RST_RE = r'(?m)^\.\.\s*gist::\s*(?P<gist_id>[^\]\s]+)(?:\s*(?P<filename>.+?))?\s*$'
 
 
 class GistFetchException(Exception):
@@ -244,6 +318,5 @@ def makeExtension(configs=None):
 if __name__ == '__main__':
     import doctest
 
-    # Silence user warnings thrown by tests:
     doctest.testmod(optionflags=(doctest.NORMALIZE_WHITESPACE +
                                  doctest.REPORT_NDIFF))


Test patch: