1
0
mirror of https://github.com/gryf/jekyll-rst.git synced 2025-12-19 04:20:21 +01:00

Clean up directives code

This commit is contained in:
2019-05-04 12:37:24 -06:00
parent 1f75eb9081
commit d76fb6835a

View File

@@ -7,30 +7,19 @@
# .. _Pygments: http://pygments.org/
# .. _Using Pygments in ReST documents: http://pygments.org/docs/rstdirective/
# .. _Octopress: http://octopress.org/
import re
import os
import hashlib
import __main__
# 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
import os
import re
from docutils import nodes
from docutils.parsers.rst import directives, Directive
from pygments import highlight
from pygments.formatters import HtmlFormatter
from pygments.lexers import get_lexer_by_name, TextLexer
class Pygments(Directive):
""" Source code syntax hightlighting.
"""
"""Source code syntax hightlighting."""
required_arguments = 1
optional_arguments = 0
final_argument_whitespace = True
@@ -52,8 +41,10 @@ class Pygments(Directive):
# Construct cache filename
cache_file = None
content_text = u'\n'.join(self.content).encode('utf-8')
cache_file_name = u'%s-%s.html' % (lexer_name, hashlib.md5(content_text).hexdigest())
cached_path = os.path.join(PYGMENTS_CACHE_DIR, cache_file_name)
cache_file_name = (u'%s-%s.html' %
(lexer_name,
hashlib.md5(content_text).hexdigest()))
cached_path = self._get_cached_path(cache_file_name)
# Look for cached version, otherwise parse
if os.path.exists(cached_path):
@@ -73,13 +64,22 @@ class Pygments(Directive):
# Add wrapper with optional caption and link
code = '<figure class="code">'
if self.options:
caption = ('<span>%s</span>' % self.options['caption']) if 'caption' in self.options else ''
title = self.options['title'] if 'title' in self.options else 'link'
link = ('<a href="%s">%s</a>' % (self.options['url'], title)) if 'url' in self.options else ''
caption = ''
title = 'link'
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:
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>'
# Write cache
@@ -90,5 +90,17 @@ class Pygments(Directive):
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('sourcecode', Pygments)