Commits

whuss  committed cdeed59

Make the modules pygments and configobj optional. Now sws2tex also
works with a standard python install, only with syntax highlighting
and config file support disabled.

  • Participants
  • Parent commits 6452b28

Comments (0)

Files changed (1)

 import glob
 import textwrap
 
-from pygments import highlight
-from pygments.lexers import PythonLexer, TexLexer, HtmlLexer
-from pygments.formatters import LatexFormatter
-from pygments.styles import STYLE_MAP
-from pygments.styles import get_style_by_name
-from sphinx.highlighting import SphinxStyle
-
 from htmltable2latex import parse_table
 from html2latex import Html2Latex
 
-class Colorize:
-    def __init__(self, style=SphinxStyle):
-        self.pythonlexer = PythonLexer(encoding='utf8')
-        self.texlexer = TexLexer(encoding='utf8')
-        self.htmllexer = HtmlLexer(encoding='utf8')
-        self.formatter = LatexFormatter(noclasses=True, style=style)
+try:
+    from pygments import highlight
+    from pygments.lexers import PythonLexer, TexLexer, HtmlLexer
+    from pygments.formatters import LatexFormatter
+    from pygments.styles import STYLE_MAP
+    from pygments.styles import get_style_by_name
+    from sphinx.highlighting import SphinxStyle
 
-    def get_style_defs(self):
-        return self.formatter.get_style_defs()
+    class Colorize:
+        def __init__(self, style=SphinxStyle):
+            self.pythonlexer = PythonLexer(encoding='utf8')
+            self.texlexer = TexLexer(encoding='utf8')
+            self.htmllexer = HtmlLexer(encoding='utf8')
+            self.formatter = LatexFormatter(noclasses=True, style=style)
 
-    def __call__(self, s, format = 'python'):
-        if format == 'python' or format == 'sage' or format == 'sage interact' or format == 'cython':
-            return highlight(s, self.pythonlexer, self.formatter)
-        elif format == 'latex':
-            return highlight(s, self.texlexer, self.formatter)
-        elif format == 'html':
-            return highlight(s, self.htmllexer, self.formatter)
-        else:
-            return "\\begin{Verbatim}[commandchars=@\\[\\]]\n" + s + "\n\\end{Verbatim}\n"
+        def get_style_defs(self):
+            return self.formatter.get_style_defs()
 
-colorize = Colorize()
+        def __call__(self, s, format = 'python'):
+            if format == 'python' or format == 'sage' or format == 'sage interact' or format == 'cython':
+                return highlight(s, self.pythonlexer, self.formatter)
+            elif format == 'latex':
+                return highlight(s, self.texlexer, self.formatter)
+            elif format == 'html':
+                return highlight(s, self.htmllexer, self.formatter)
+            else:
+                return "\\begin{Verbatim}[commandchars=@\\[\\]]\n" + s + "\n\\end{Verbatim}\n"
+
+    colorize = Colorize()
+
+except ImportError:
+    print "Warning: Unable to load module pygments"
+    print "Warning: Syntax highlighting will be disabled"
+
+    def colorize(s, **kwds):
+        return "\\begin{Verbatim}[commandchars=@\\[\\]]\n" + s + "\n\\end{Verbatim}\n"
 
 def line_breaks(text, columns=85, options=None):
     r"""
     toc = options.toc
 
     latex_src = preamble
-    latex_src += colorize.get_style_defs() + "\n"
+    try:
+        # If pygments is loaded add color definitions to the preamble
+        latex_src += colorize.get_style_defs() + "\n"
+    except:
+        pass
+
     if title is not None:
         latex_src += "\\title{%s}" % title + "\n"
     if author is not None:
 cell_list = parse_worksheet(worksheet.readlines(), options = options)
 worksheet.close()
 
+try:
+    from IPython.external.configobj import ConfigObj
 
-from IPython.external.configobj import ConfigObj
-
-try:
     if os.path.exists('.sws2tex'):
         filename = '.sws2tex'
     elif os.path.exists(os.path.expanduser("~/.sws2tex")):
     if not options.quiet:
         print "Config file: %s" %filename
     config = ConfigObj(filename)
+except ImportError:
+    print "Warning: Unable to load module configobj"
+    print "Warning: Support for .sws2tex config file disabled"
+    config = {}
 except:
     config = {}