summaryrefslogtreecommitdiffstats
path: root/nikola/plugins/compile/markdown/mdx_gist.py
diff options
context:
space:
mode:
Diffstat (limited to 'nikola/plugins/compile/markdown/mdx_gist.py')
-rw-r--r--nikola/plugins/compile/markdown/mdx_gist.py180
1 files changed, 32 insertions, 148 deletions
diff --git a/nikola/plugins/compile/markdown/mdx_gist.py b/nikola/plugins/compile/markdown/mdx_gist.py
index f439fa2..f6ce20a 100644
--- a/nikola/plugins/compile/markdown/mdx_gist.py
+++ b/nikola/plugins/compile/markdown/mdx_gist.py
@@ -22,7 +22,7 @@
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
# Warning: URL formats of "raw" gists are undocummented and subject to change.
-# See also: http://developer.github.com/v3/gists/
+# See also: https://developer.github.com/v3/gists/
#
# Inspired by "[Python] reStructuredText GitHub Gist directive"
# (https://gist.github.com/brianhsu/1407759), public domain by Brian Hsu
@@ -31,164 +31,54 @@ Extension to Python Markdown for Embedded Gists (gist.github.com).
Basic Example:
- >>> import markdown
- >>> text = '''
- ... Text of the gist:
- ... [:gist: 4747847]
- ... '''
- >>> html = markdown.markdown(text, [GistExtension()])
- >>> print(html)
- <p>Text of the gist:
- <div class="gist">
- <script src="https://gist.github.com/4747847.js"></script>
- <noscript>
- <pre>import this</pre>
- </noscript>
- </div>
- </p>
+ Text of the gist:
+ [:gist: 4747847]
Example with filename:
- >>> import markdown
- >>> text = '''
- ... Text of the gist:
- ... [:gist: 4747847 zen.py]
- ... '''
- >>> html = markdown.markdown(text, [GistExtension()])
- >>> print(html)
- <p>Text of the gist:
- <div class="gist">
- <script src="https://gist.github.com/4747847.js?file=zen.py"></script>
- <noscript>
- <pre>import this</pre>
- </noscript>
- </div>
- </p>
+ Text of the gist:
+ [:gist: 4747847 zen.py]
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>
+ Text of the gist:
+ [:gist: c4a43d6fdce612284ac0]
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>
+ Text of the gist:
+ [:gist: c4a43d6fdce612284ac0 cow.txt]
Example using reStructuredText syntax:
- >>> import markdown
- >>> text = '''
- ... Text of the gist:
- ... .. gist:: 4747847 zen.py
- ... '''
- >>> html = markdown.markdown(text, [GistExtension()])
- >>> print(html)
- <p>Text of the gist:
- <div class="gist">
- <script src="https://gist.github.com/4747847.js?file=zen.py"></script>
- <noscript>
- <pre>import this</pre>
- </noscript>
- </div>
- </p>
+ Text of the gist:
+ .. gist:: 4747847 zen.py
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>
+ Text of the gist:
+ .. gist:: c4a43d6fdce612284ac0
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>
+ Text of the gist:
+ .. gist:: c4a43d6fdce612284ac0 cow.txt
Error Case: non-existent Gist ID:
- >>> import markdown
- >>> text = '''
- ... Text of the gist:
- ... [:gist: 0]
- ... '''
- >>> html = markdown.markdown(text, [GistExtension()])
- >>> print(html)
- <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.githubusercontent.com/raw/0 --></noscript>
- </div>
- </p>
-
-Error Case: non-existent file:
-
- >>> import markdown
- >>> text = '''
- ... Text of the gist:
- ... [:gist: 4747847 doesntexist.py]
- ... '''
- >>> html = markdown.markdown(text, [GistExtension()])
- >>> print(html)
- <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.githubusercontent.com/raw/4747847/doesntexist.py --></noscript>
- </div>
- </p>
+ Text of the gist:
+ [:gist: 0]
+
+Error Case: non-existent file:
+
+ Text of the gist:
+ [:gist: 4747847 doesntexist.py]
"""
-from __future__ import unicode_literals, print_function
+import requests
+
+from nikola.plugin_categories import MarkdownExtension
+from nikola.utils import get_logger
try:
from markdown.extensions import Extension
@@ -200,12 +90,8 @@ except ImportError:
# the markdown compiler will fail first
Extension = Pattern = object
-from nikola.plugin_categories import MarkdownExtension
-from nikola.utils import get_logger, STDERR_HANDLER
-
-import requests
-LOGGER = get_logger('compile_markdown.mdx_gist', STDERR_HANDLER)
+LOGGER = get_logger('compile_markdown.mdx_gist')
GIST_JS_URL = "https://gist.github.com/{0}.js"
GIST_FILE_JS_URL = "https://gist.github.com/{0}.js?file={1}"
@@ -217,7 +103,6 @@ GIST_RST_RE = r'(?m)^\.\.\s*gist::\s*(?P<gist_id>[^\]\s]+)(?:\s*(?P<filename>.+?
class GistFetchException(Exception):
-
"""Raised when attempt to fetch content of a Gist from github.com fails."""
def __init__(self, url, status_code):
@@ -228,7 +113,6 @@ class GistFetchException(Exception):
class GistPattern(Pattern):
-
"""InlinePattern for footnote markers in a document's body text."""
def __init__(self, pattern, configs):
@@ -282,7 +166,7 @@ class GistPattern(Pattern):
pre_elem.text = AtomicString(raw_gist)
except GistFetchException as e:
- LOGGER.warn(e.message)
+ LOGGER.warning(e.message)
warning_comment = etree.Comment(' WARNING: {0} '.format(e.message))
noscript_elem.append(warning_comment)
@@ -290,7 +174,6 @@ class GistPattern(Pattern):
class GistExtension(MarkdownExtension, Extension):
-
"""Gist extension for Markdown."""
def __init__(self, configs={}):
@@ -302,15 +185,15 @@ class GistExtension(MarkdownExtension, Extension):
for key, value in configs:
self.setConfig(key, value)
- def extendMarkdown(self, md, md_globals):
+ def extendMarkdown(self, md, md_globals=None):
"""Extend Markdown."""
gist_md_pattern = GistPattern(GIST_MD_RE, self.getConfigs())
gist_md_pattern.md = md
- md.inlinePatterns.add('gist', gist_md_pattern, "<not_strong")
+ md.inlinePatterns.register(gist_md_pattern, 'gist', 175)
gist_rst_pattern = GistPattern(GIST_RST_RE, self.getConfigs())
gist_rst_pattern.md = md
- md.inlinePatterns.add('gist-rst', gist_rst_pattern, ">gist")
+ md.inlinePatterns.register(gist_rst_pattern, 'gist-rst', 176)
md.registerExtension(self)
@@ -319,6 +202,7 @@ def makeExtension(configs=None): # pragma: no cover
"""Make Markdown extension."""
return GistExtension(configs)
+
if __name__ == '__main__':
import doctest