diff options
Diffstat (limited to 'nikola/plugins/compile/markdown/mdx_gist.py')
| -rw-r--r-- | nikola/plugins/compile/markdown/mdx_gist.py | 68 |
1 files changed, 40 insertions, 28 deletions
diff --git a/nikola/plugins/compile/markdown/mdx_gist.py b/nikola/plugins/compile/markdown/mdx_gist.py index 70e7394..f439fa2 100644 --- a/nikola/plugins/compile/markdown/mdx_gist.py +++ b/nikola/plugins/compile/markdown/mdx_gist.py @@ -26,16 +26,16 @@ # # Inspired by "[Python] reStructuredText GitHub Gist directive" # (https://gist.github.com/brianhsu/1407759), public domain by Brian Hsu -''' -Extension to Python Markdown for Embedded Gists (gist.github.com) +""" +Extension to Python Markdown for Embedded Gists (gist.github.com). Basic Example: >>> import markdown - >>> text = """ + >>> text = ''' ... Text of the gist: ... [:gist: 4747847] - ... """ + ... ''' >>> html = markdown.markdown(text, [GistExtension()]) >>> print(html) <p>Text of the gist: @@ -50,10 +50,10 @@ Basic Example: Example with filename: >>> import markdown - >>> text = """ + >>> text = ''' ... Text of the gist: ... [:gist: 4747847 zen.py] - ... """ + ... ''' >>> html = markdown.markdown(text, [GistExtension()]) >>> print(html) <p>Text of the gist: @@ -68,10 +68,10 @@ Example with filename: Basic Example with hexidecimal id: >>> import markdown - >>> text = """ + >>> text = ''' ... Text of the gist: ... [:gist: c4a43d6fdce612284ac0] - ... """ + ... ''' >>> html = markdown.markdown(text, [GistExtension()]) >>> print(html) <p>Text of the gist: @@ -86,10 +86,10 @@ Basic Example with hexidecimal id: Example with hexidecimal id filename: >>> import markdown - >>> text = """ + >>> text = ''' ... Text of the gist: ... [:gist: c4a43d6fdce612284ac0 cow.txt] - ... """ + ... ''' >>> html = markdown.markdown(text, [GistExtension()]) >>> print(html) <p>Text of the gist: @@ -104,10 +104,10 @@ Example with hexidecimal id filename: Example using reStructuredText syntax: >>> import markdown - >>> text = """ + >>> text = ''' ... Text of the gist: ... .. gist:: 4747847 zen.py - ... """ + ... ''' >>> html = markdown.markdown(text, [GistExtension()]) >>> print(html) <p>Text of the gist: @@ -122,10 +122,10 @@ Example using reStructuredText syntax: Example using hexidecimal ID with reStructuredText syntax: >>> import markdown - >>> text = """ + >>> text = ''' ... Text of the gist: ... .. gist:: c4a43d6fdce612284ac0 - ... """ + ... ''' >>> html = markdown.markdown(text, [GistExtension()]) >>> print(html) <p>Text of the gist: @@ -140,10 +140,10 @@ Example using hexidecimal ID with reStructuredText syntax: Example using hexidecimal ID and filename with reStructuredText syntax: >>> import markdown - >>> text = """ + >>> text = ''' ... Text of the gist: ... .. gist:: c4a43d6fdce612284ac0 cow.txt - ... """ + ... ''' >>> html = markdown.markdown(text, [GistExtension()]) >>> print(html) <p>Text of the gist: @@ -158,38 +158,36 @@ Example using hexidecimal ID and filename with reStructuredText syntax: Error Case: non-existent Gist ID: >>> import markdown - >>> text = """ + >>> 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> + <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 = ''' ... 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> + <noscript><!-- WARNING: Received a 404 response from Gist URL: https://gist.githubusercontent.com/raw/4747847/doesntexist.py --></noscript> </div> </p> +""" -''' from __future__ import unicode_literals, print_function try: @@ -219,20 +217,26 @@ 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.''' + + """Raised when attempt to fetch content of a Gist from github.com fails.""" + def __init__(self, url, status_code): + """Initialize the exception.""" Exception.__init__(self) self.message = 'Received a {0} response from Gist URL: {1}'.format( status_code, url) class GistPattern(Pattern): - """ InlinePattern for footnote markers in a document's body text. """ + + """InlinePattern for footnote markers in a document's body text.""" def __init__(self, pattern, configs): + """Initialize the pattern.""" Pattern.__init__(self, pattern) def get_raw_gist_with_filename(self, gist_id, filename): + """Get raw gist text for a filename.""" url = GIST_FILE_RAW_URL.format(gist_id, filename) resp = requests.get(url) @@ -242,6 +246,7 @@ class GistPattern(Pattern): return resp.text def get_raw_gist(self, gist_id): + """Get raw gist text.""" url = GIST_RAW_URL.format(gist_id) resp = requests.get(url) @@ -251,6 +256,7 @@ class GistPattern(Pattern): return resp.text def handleMatch(self, m): + """Handle pattern match.""" gist_id = m.group('gist_id') gist_file = m.group('filename') @@ -284,7 +290,11 @@ class GistPattern(Pattern): class GistExtension(MarkdownExtension, Extension): + + """Gist extension for Markdown.""" + def __init__(self, configs={}): + """Initialize the extension.""" # set extension defaults self.config = {} @@ -293,6 +303,7 @@ class GistExtension(MarkdownExtension, Extension): self.setConfig(key, value) def extendMarkdown(self, md, md_globals): + """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") @@ -304,7 +315,8 @@ class GistExtension(MarkdownExtension, Extension): md.registerExtension(self) -def makeExtension(configs=None): +def makeExtension(configs=None): # pragma: no cover + """Make Markdown extension.""" return GistExtension(configs) if __name__ == '__main__': |
