diff --git a/directives.py b/directives.py index d854da0..0f3a866 100644 --- a/directives.py +++ b/directives.py @@ -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 = '
' if self.options: - caption = ('%s' % self.options['caption']) if 'caption' in self.options else '' - title = self.options['title'] if 'title' in self.options else 'link' - link = ('%s' % (self.options['url'], title)) if 'url' in self.options else '' + caption = '' + title = 'link' + link = '' + + if 'caption' in self.options: + caption = ('%s' % self.options['caption']) + if 'title' in self.options: + title = self.options['title'] + if 'url' in self.options: + link = ('%s' % (self.options['url'], title)) if caption or link: code += '
%s %s
' % (caption, link) - code += '
%s
' % (lexer_name, lined) + + code += ('
%s'
+                 '
' % (lexer_name, lined)) code += '
' # 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)