From 498e44f36cf25291063afe150d861f3113e406fc Mon Sep 17 00:00:00 2001 From: Joey Curtin Date: Thu, 28 Mar 2013 15:40:46 -0400 Subject: [PATCH] added loader for pygments formatter and stub for the testcase --- pelican/rstdirectives.py | 14 +++++++------- pelican/settings/base.py | 7 ++++++- pelican/tests/test_importedsettings.py | 1 + pelican/tests/test_pygments_formatter.py | 17 +++++++++++++++++ pelican/utils/importlib.py | 19 ++++++++++++++++++- 5 files changed, 49 insertions(+), 9 deletions(-) create mode 100644 pelican/tests/test_pygments_formatter.py diff --git a/pelican/rstdirectives.py b/pelican/rstdirectives.py index 98a610b5..ff3e6f8a 100644 --- a/pelican/rstdirectives.py +++ b/pelican/rstdirectives.py @@ -1,6 +1,8 @@ # -*- coding: utf-8 -*- from __future__ import unicode_literals, print_function +from pelican.settings import conf as pelican_settings +from pelican.utils.importlib import import_class from docutils import nodes, utils from docutils.parsers.rst import directives, roles, Directive from pygments.formatters import HtmlFormatter @@ -9,11 +11,7 @@ from pygments.lexers import get_lexer_by_name, TextLexer import re INLINESTYLES = False -DEFAULT = HtmlFormatter(noclasses=INLINESTYLES, linenos=True) -VARIANTS = { - 'linenos': HtmlFormatter(noclasses=INLINESTYLES, linenos=True), -} - +VARIANTS = {} class Pygments(Directive): """ Source code syntax hightlighting. @@ -32,8 +30,10 @@ class Pygments(Directive): # no lexer found - use the text one instead of an exception lexer = TextLexer() # take an arbitrary option if more than one is given - formatter = self.options and VARIANTS[self.options.keys()[0]] \ - or DEFAULT + formatter = import_class(pelican_settings.PYGMENTS_FORMATTER) + formatter = formatter(noclasses=INLINESTYLES, + linenos=pelican_settings.PYGMENTS_FORMATTER_SHOW_LINENO) + parsed = highlight('\n'.join(self.content), lexer, formatter) return [nodes.raw('', parsed, format='html')] diff --git a/pelican/settings/base.py b/pelican/settings/base.py index ad103d41..97d68a46 100644 --- a/pelican/settings/base.py +++ b/pelican/settings/base.py @@ -91,7 +91,12 @@ _DEFAULT_CONFIG = {'PATH': os.curdir, 'SUMMARY_MAX_LENGTH': 50, 'PLUGINS': [], 'TEMPLATE_PAGES': {}, - 'IGNORE_FILES': [] + 'IGNORE_FILES': [], + 'PYGMENTS_FORMATTER_SHOW_LINENO': False, + # This will allow users to import formatters form + # outside the pelican source. + # pelicanconf.py : PYGMENTS_FORMATTER = "my.custom.formatter" + 'PYGMENTS_FORMATTER': 'pygments.formatters.HtmlFormatter' } diff --git a/pelican/tests/test_importedsettings.py b/pelican/tests/test_importedsettings.py index cf0b40d2..fb8b76be 100644 --- a/pelican/tests/test_importedsettings.py +++ b/pelican/tests/test_importedsettings.py @@ -26,3 +26,4 @@ class TestImportLib(unittest.TestCase): for key in _DEFAULT_CONFIG.keys(): assert hasattr(conf, key) + diff --git a/pelican/tests/test_pygments_formatter.py b/pelican/tests/test_pygments_formatter.py new file mode 100644 index 00000000..59aea991 --- /dev/null +++ b/pelican/tests/test_pygments_formatter.py @@ -0,0 +1,17 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals, print_function +import os +import unittest +import sys +import locale + +from pelican.utils.importlib import import_module +from pelican.settings import read_settings +from pelican.settings.base import _DEFAULT_CONFIG + +# runtest with: +# cd ~/pelican/pelican/tests && python -m unittest discover . 'test_pygments*' && cd - +class TestPygments(unittest.TestCase): + + def test_formatter(self): + pass \ No newline at end of file diff --git a/pelican/utils/importlib.py b/pelican/utils/importlib.py index b626a3c5..451ee4c8 100644 --- a/pelican/utils/importlib.py +++ b/pelican/utils/importlib.py @@ -36,4 +36,21 @@ def import_module(name, package=None): level += 1 name = _resolve_name(name[level:], package, level) __import__(name) - return sys.modules[name] \ No newline at end of file + return sys.modules[name] + +def import_class(name, package=None): + """ + Imports just the class and not the module. + """ + if package is None: + package = '.'.join(name.split('.')[:-1]) + name = ''.join(name.split('.')[-1:]) + + if package == '': + package = None + + module = import_module(package) + if not hasattr(module, name): + raise AttributeError("%s is not part of %s" % (name, package)) + + return getattr(module, name)