mirror of
https://github.com/gryf/jekyll-rst.git
synced 2025-12-19 20:38:06 +01:00
Clean up directives code
This commit is contained in:
@@ -7,30 +7,19 @@
|
|||||||
# .. _Pygments: http://pygments.org/
|
# .. _Pygments: http://pygments.org/
|
||||||
# .. _Using Pygments in ReST documents: http://pygments.org/docs/rstdirective/
|
# .. _Using Pygments in ReST documents: http://pygments.org/docs/rstdirective/
|
||||||
# .. _Octopress: http://octopress.org/
|
# .. _Octopress: http://octopress.org/
|
||||||
|
|
||||||
import re
|
|
||||||
import os
|
|
||||||
import hashlib
|
import hashlib
|
||||||
import __main__
|
import os
|
||||||
|
import re
|
||||||
# Absolute path to pygments cache dir
|
|
||||||
PYGMENTS_CACHE_DIR = os.path.abspath(os.path.join(os.path.dirname(__main__.__file__), '../../.pygments-cache'))
|
|
||||||
|
|
||||||
# Ensure cache dir exists
|
|
||||||
if not os.path.exists(PYGMENTS_CACHE_DIR):
|
|
||||||
os.makedirs(PYGMENTS_CACHE_DIR)
|
|
||||||
|
|
||||||
from pygments.formatters import HtmlFormatter
|
|
||||||
|
|
||||||
from docutils import nodes
|
from docutils import nodes
|
||||||
from docutils.parsers.rst import directives, Directive
|
from docutils.parsers.rst import directives, Directive
|
||||||
|
|
||||||
from pygments import highlight
|
from pygments import highlight
|
||||||
|
from pygments.formatters import HtmlFormatter
|
||||||
from pygments.lexers import get_lexer_by_name, TextLexer
|
from pygments.lexers import get_lexer_by_name, TextLexer
|
||||||
|
|
||||||
|
|
||||||
class Pygments(Directive):
|
class Pygments(Directive):
|
||||||
""" Source code syntax hightlighting.
|
"""Source code syntax hightlighting."""
|
||||||
"""
|
|
||||||
required_arguments = 1
|
required_arguments = 1
|
||||||
optional_arguments = 0
|
optional_arguments = 0
|
||||||
final_argument_whitespace = True
|
final_argument_whitespace = True
|
||||||
@@ -52,8 +41,10 @@ class Pygments(Directive):
|
|||||||
# Construct cache filename
|
# Construct cache filename
|
||||||
cache_file = None
|
cache_file = None
|
||||||
content_text = u'\n'.join(self.content).encode('utf-8')
|
content_text = u'\n'.join(self.content).encode('utf-8')
|
||||||
cache_file_name = u'%s-%s.html' % (lexer_name, hashlib.md5(content_text).hexdigest())
|
cache_file_name = (u'%s-%s.html' %
|
||||||
cached_path = os.path.join(PYGMENTS_CACHE_DIR, cache_file_name)
|
(lexer_name,
|
||||||
|
hashlib.md5(content_text).hexdigest()))
|
||||||
|
cached_path = self._get_cached_path(cache_file_name)
|
||||||
|
|
||||||
# Look for cached version, otherwise parse
|
# Look for cached version, otherwise parse
|
||||||
if os.path.exists(cached_path):
|
if os.path.exists(cached_path):
|
||||||
@@ -73,13 +64,22 @@ class Pygments(Directive):
|
|||||||
# Add wrapper with optional caption and link
|
# Add wrapper with optional caption and link
|
||||||
code = '<figure class="code">'
|
code = '<figure class="code">'
|
||||||
if self.options:
|
if self.options:
|
||||||
caption = ('<span>%s</span>' % self.options['caption']) if 'caption' in self.options else ''
|
caption = ''
|
||||||
title = self.options['title'] if 'title' in self.options else 'link'
|
title = 'link'
|
||||||
link = ('<a href="%s">%s</a>' % (self.options['url'], title)) if 'url' in self.options else ''
|
link = ''
|
||||||
|
|
||||||
|
if 'caption' in self.options:
|
||||||
|
caption = ('<span>%s</span>' % self.options['caption'])
|
||||||
|
if 'title' in self.options:
|
||||||
|
title = self.options['title']
|
||||||
|
if 'url' in self.options:
|
||||||
|
link = ('<a href="%s">%s</a>' % (self.options['url'], title))
|
||||||
|
|
||||||
if caption or link:
|
if caption or link:
|
||||||
code += '<figcaption>%s %s</figcaption>' % (caption, link)
|
code += '<figcaption>%s %s</figcaption>' % (caption, link)
|
||||||
code += '<div class="highlight"><pre><code class="%s">%s</code></pre></div>' % (lexer_name, lined)
|
|
||||||
|
code += ('<div class="highlight"><pre><code class="%s">%s</code>'
|
||||||
|
'</pre></div>' % (lexer_name, lined))
|
||||||
code += '</figure>'
|
code += '</figure>'
|
||||||
|
|
||||||
# Write cache
|
# Write cache
|
||||||
@@ -90,5 +90,17 @@ class Pygments(Directive):
|
|||||||
|
|
||||||
return [nodes.raw('', code, format='html')]
|
return [nodes.raw('', code, format='html')]
|
||||||
|
|
||||||
|
def _get_cached_path(self, cache_file_name):
|
||||||
|
# TODO(gryf): Here is an assumption, that .pygments-cache directory
|
||||||
|
# will lay on top direcotry of jekyll project. Make it *real* tmp, by
|
||||||
|
# using tempfile module.
|
||||||
|
cached_path = os.path.abspath(os.path.join(os.path.dirname(__file__),
|
||||||
|
'../../.pygments-cache'))
|
||||||
|
if not os.path.exists(cached_path):
|
||||||
|
os.makedirs(cached_path)
|
||||||
|
|
||||||
|
return os.path.join(cached_path, cache_file_name)
|
||||||
|
|
||||||
|
|
||||||
directives.register_directive('code-block', Pygments)
|
directives.register_directive('code-block', Pygments)
|
||||||
directives.register_directive('sourcecode', Pygments)
|
directives.register_directive('sourcecode', Pygments)
|
||||||
|
|||||||
Reference in New Issue
Block a user