diff options
author | gbrandl <devnull@localhost> | 2007-02-14 17:50:02 +0100 |
---|---|---|
committer | gbrandl <devnull@localhost> | 2007-02-14 17:50:02 +0100 |
commit | 0dcd117d4e662c8a85aef4b312e2cb795a0e7d52 (patch) | |
tree | 0aad94cbc5bcd465f49a2f2d24d4e44e58c14516 | |
parent | 1c8ddc632f1eb5550f98b8185b1b820069cb8efb (diff) | |
download | pygments-0dcd117d4e662c8a85aef4b312e2cb795a0e7d52.tar.gz |
[svn] A test case for the external CSS function of the HTML formatter.
-rw-r--r-- | pygments/formatters/html.py | 25 | ||||
-rw-r--r-- | tests/test_html_formatter.py | 37 |
2 files changed, 44 insertions, 18 deletions
diff --git a/pygments/formatters/html.py b/pygments/formatters/html.py index 565f35b9..3b75e4e6 100644 --- a/pygments/formatters/html.py +++ b/pygments/formatters/html.py @@ -187,8 +187,10 @@ class HtmlFormatter(Formatter): `cssfile` If the `full` option is true and this option is given, it must be the - name of an external file. The stylesheet is then written to this file - instead of the HTML file. *New in Pygments 0.6.* + name of an external file. If the filename does not include an absolute + path, the file's path will be assumed to be relative to the main output + file's path, if the latter can be found. The stylesheet is then written + to this file instead of the HTML file. *New in Pygments 0.6.* `linenospecial` If set to a number n > 0, every nth line number is given the CSS @@ -345,13 +347,20 @@ class HtmlFormatter(Formatter): def _wrap_full(self, inner, outfile): if self.cssfile: - try: - filename = outfile.name - cssfilename = os.path.join(os.path.dirname(filename), self.cssfile) - except AttributeError: - print >>sys.stderr, 'Note: Cannot determine output file name, ' \ - 'using current directory as base for the CSS file name' + if os.path.isabs(self.cssfile): + # it's an absolute filename cssfilename = self.cssfile + else: + try: + filename = outfile.name + if not filename or filename[0] == '<': + # pseudo files, e.g. name == '<fdopen>' + raise AttributeError + cssfilename = os.path.join(os.path.dirname(filename), self.cssfile) + except AttributeError: + print >>sys.stderr, 'Note: Cannot determine output file name, ' \ + 'using current directory as base for the CSS file name' + cssfilename = self.cssfile # write CSS file try: cf = open(cssfilename, "w") diff --git a/tests/test_html_formatter.py b/tests/test_html_formatter.py index ba3ed69e..5297d1f0 100644 --- a/tests/test_html_formatter.py +++ b/tests/test_html_formatter.py @@ -12,21 +12,42 @@ import unittest import StringIO import random import tempfile +from os.path import join, dirname, isfile from pygments import lexers, formatters from pygments.token import _TokenType from pygments.formatters import HtmlFormatter from pygments.lexers import PythonLexer +tokensource = list(PythonLexer().get_tokens(file(os.path.join(testdir, testfile)).read())) + class HtmlFormatterTest(unittest.TestCase): -# TODO: write this test. -# def test_external_css(self): -# pass + def test_external_css(self): + # test correct behavior + # CSS should be in /tmp directory + fmt1 = HtmlFormatter(full=True, cssfile='fmt1.css') + # CSS should be in testdir (testdir is absolute) + fmt2 = HtmlFormatter(full=True, cssfile=join(testdir, 'fmt2.css')) + tfile = tempfile.NamedTemporaryFile(suffix='.html') + fmt1.format(tokensource, tfile) + try: + fmt2.format(tokensource, tfile) + except IOError: + # test directory not writable + pass + tfile.close() + + self.assert_(isfile(join(dirname(tfile.name), 'my.css'))) + + os.unlink(join(dirname(tfile.name), 'fmt1.css')) + try: + os.unlink(join(testdir, 'fmt2.css')) + except OSError: + pass + def test_all_options(self): - tokensource = list(PythonLexer().get_tokens(file(os.path.join(testdir, testfile)).read())) - for optdict in [dict(nowrap=True), dict(linenos=True), dict(linenos=True, full=True), @@ -37,13 +58,10 @@ class HtmlFormatterTest(unittest.TestCase): fmt.format(tokensource, outfile) def test_valid_output(self): - tokensource = list(PythonLexer().get_tokens(file(os.path.join(testdir, testfile)).read())) + # test all available wrappers fmt = HtmlFormatter(full=True, linenos=True, noclasses=True) handle, pathname = tempfile.mkstemp('.html') - # place all output files in /tmp too - old_wd = os.getcwd() - os.chdir(os.path.dirname(pathname)) tfile = os.fdopen(handle, 'w+b') fmt.format(tokensource, tfile) tfile.close() @@ -64,4 +82,3 @@ class HtmlFormatterTest(unittest.TestCase): self.failIf(ret, 'nsgmls run reported errors') os.unlink(pathname) - os.chdir(old_wd) |