def test_filename(): optdict = dict(filename="test.py") outfile = StringIO() fmt = HtmlFormatter(**optdict) fmt.format(tokensource, outfile) html = outfile.getvalue() assert re.search("<span class=\"filename\">test.py</span><pre>", html)
def test_lineanchors_with_startnum(self): optdict = dict(lineanchors="foo", linenostart=5) outfile = StringIO() fmt = HtmlFormatter(**optdict) fmt.format(tokensource, outfile) html = outfile.getvalue() self.assertTrue(re.search("<pre><a name=\"foo-5\">", html))
def test_lineanchors(): optdict = dict(lineanchors="foo") outfile = StringIO() fmt = HtmlFormatter(**optdict) fmt.format(tokensource, outfile) html = outfile.getvalue() assert re.search("<pre><span></span><a name=\"foo-1\">", html)
def test_linenos(self): optdict = dict(linenos=True) outfile = StringIO() fmt = HtmlFormatter(**optdict) fmt.format(tokensource, outfile) html = outfile.getvalue() self.assertTrue(re.search("<pre>\s+1\s+2\s+3", html))
def test_external_css(self): # test correct behavior # CSS should be in /tmp directory fmt1 = HtmlFormatter(full=True, cssfile='fmt1.css', outencoding='utf-8') # CSS should be in TESTDIR (TESTDIR is absolute) fmt2 = HtmlFormatter(full=True, cssfile=join(TESTDIR, 'fmt2.css'), outencoding='utf-8') tfile = tempfile.NamedTemporaryFile(suffix='.html') fmt1.format(tokensource, tfile) try: fmt2.format(tokensource, tfile) self.assertTrue(isfile(join(TESTDIR, 'fmt2.css'))) except IOError: # test directory not writable pass tfile.close() self.assertTrue(isfile(join(dirname(tfile.name), 'fmt1.css'))) os.unlink(join(dirname(tfile.name), 'fmt1.css')) try: os.unlink(join(TESTDIR, 'fmt2.css')) except OSError: pass
def test_filename(self): optdict = dict(filename="test.py") outfile = StringIO() fmt = HtmlFormatter(**optdict) fmt.format(tokensource, outfile) html = outfile.getvalue() self.assertTrue(re.search("<span class=\"filename\">test.py</span><pre>", html))
def test_lineanchors_with_startnum(self): optdict = dict(lineanchors="foo", linenostart=5) outfile = StringIO() fmt = HtmlFormatter(**optdict) fmt.format(tokensource, outfile) html = outfile.getvalue() self.assertTrue(re.search("<pre><span></span><a name=\"foo-5\">", html))
def test_valid_output(self): # test all available wrappers fmt = HtmlFormatter(full=True, linenos=True, noclasses=True, outencoding='utf-8') handle, pathname = tempfile.mkstemp('.html') tfile = os.fdopen(handle, 'w+b') fmt.format(tokensource, tfile) tfile.close() catname = os.path.join(TESTDIR, 'dtds', 'HTML4.soc') try: import subprocess po = subprocess.Popen(['nsgmls', '-s', '-c', catname, pathname], stdout=subprocess.PIPE) ret = po.wait() output = po.stdout.read() po.stdout.close() except OSError: # nsgmls not available pass else: if ret: print(output) self.assertFalse(ret, 'nsgmls run reported errors') os.unlink(pathname)
def test_linenos_with_startnum(self): optdict = dict(linenos=True, linenostart=5) outfile = StringIO() fmt = HtmlFormatter(**optdict) fmt.format(tokensource, outfile) html = outfile.getvalue() self.assertTrue(re.search("<pre>\s+5\s+6\s+7", html))
def test_linenos_elements(linenos, noclasses, linenostep, linenostart, linenospecial, anchorlinenos, filename): options = dict( linenos=linenos, noclasses=noclasses, linenostep=linenostep, linenostart=linenostart, linenospecial=linenospecial, anchorlinenos=anchorlinenos, filename=filename, ) output = StringIO() fmt = HtmlFormatter(**options) fmt.format(CODE, output) html = output.getvalue() filename_parts = [] filename_parts.append(linenos) filename_parts.append("nocls" if noclasses == "True" else "cls") filename_parts.append("step_" + linenostep) filename_parts.append("start_" + linenostart) filename_parts.append("special_" + linenospecial) filename_parts.append("anchor" if anchorlinenos == "True" else "noanchor") filename_parts.append("filename" if filename else "nofilename") expected_html_filename = "_".join(filename_parts) + ".html" with open(os.path.join(EXPECTED_OUTPUT_DIR, expected_html_filename)) as f: expected_html = f.read() assert single_line(html) == single_line(expected_html)
def test_lineanchors(self): optdict = dict(lineanchors="foo") outfile = StringIO() fmt = HtmlFormatter(**optdict) fmt.format(tokensource, outfile) html = outfile.getvalue() self.assertTrue(re.search("<pre><a name=\"foo-1\">", html))
def test_valid_output(): # test all available wrappers fmt = HtmlFormatter(full=True, linenos=True, noclasses=True, outencoding='utf-8') handle, pathname = tempfile.mkstemp('.html') with os.fdopen(handle, 'w+b') as tfile: fmt.format(tokensource, tfile) catname = os.path.join(TESTDIR, 'dtds', 'HTML4.soc') try: import subprocess po = subprocess.Popen(['nsgmls', '-s', '-c', catname, pathname], stdout=subprocess.PIPE) ret = po.wait() output = po.stdout.read() po.stdout.close() except OSError: # nsgmls not available pass else: if ret: print(output) assert not ret, 'nsgmls run reported errors' os.unlink(pathname)
def test_valid_output(self): # test all available wrappers fmt = HtmlFormatter(full=True, linenos=True, noclasses=True, outencoding='utf-8') handle, pathname = tempfile.mkstemp('.html') tfile = os.fdopen(handle, 'w+b') fmt.format(tokensource, tfile) tfile.close() catname = os.path.join(TESTDIR, 'dtds', 'HTML4.soc') try: try: import subprocess ret = subprocess.Popen(['nsgmls', '-s', '-c', catname, pathname], stdout=subprocess.PIPE).wait() except ImportError: # Python 2.3 - no subprocess module ret = os.popen('nsgmls -s -c "%s" "%s"' % (catname, pathname)).close() if ret == 32512: raise OSError # not found except OSError: # nsgmls not available pass else: self.failIf(ret, 'nsgmls run reported errors') os.unlink(pathname)
def test_unicode_options(): fmt = HtmlFormatter(title='Föö', cssclass='bär', cssstyles='div:before { content: \'bäz\' }', encoding='utf-8') handle, pathname = tempfile.mkstemp('.html') with os.fdopen(handle, 'w+b') as tfile: fmt.format(tokensource, tfile)
def test_linespans(): outfile = StringIO() fmt = HtmlFormatter(linespans='L', anchorlinenos=True, linenos="inline") fmt.format(tokensource, outfile) html = outfile.getvalue() assert re.search( r"""<span id="L-1">\s*<a href="#L-1"><span\s*class="linenos">\s*1</span></a>""", html)
def test_unicode_options(self): fmt = HtmlFormatter(title=u'Föö', cssclass=u'bär', cssstyles=u'div:before { content: \'bäz\' }', encoding='utf-8') handle, pathname = tempfile.mkstemp('.html') tfile = os.fdopen(handle, 'w+b') fmt.format(tokensource, tfile) tfile.close()
def test_all_options(self): for optdict in [dict(nowrap=True), dict(linenos=True), dict(linenos=True, full=True), dict(linenos=True, full=True, noclasses=True)]: outfile = StringIO() fmt = HtmlFormatter(**optdict) fmt.format(tokensource, outfile)
def test_lineanchors_with_startnum(): optdict = dict(lineanchors="foo", linenostart=5) outfile = StringIO() fmt = HtmlFormatter(**optdict) fmt.format(tokensource, outfile) html = outfile.getvalue() assert re.search( "<pre>\\s*<span>\\s*</span>\\s*<a id=\"foo-5\" name=\"foo-5\" href=\"#foo-5\">", html)
def test_all_options(self): for optdict in [dict(nowrap=True), dict(linenos=True), dict(linenos=True, full=True), dict(linenos=True, full=True, noclasses=True)]: outfile = StringIO.StringIO() fmt = HtmlFormatter(**optdict) fmt.format(tokensource, outfile)
def test_debug_token_types(): fmt_nod_token_types = HtmlFormatter(debug_token_types=False) outfile_nod_token_types = StringIO() fmt_nod_token_types.format(tokensource, outfile_nod_token_types) html_nod_token_types = outfile_nod_token_types.getvalue() assert '<span class="n" title="Name">TESTDIR</span>' not in html_nod_token_types fmt_debug_token_types = HtmlFormatter(debug_token_types=True) outfile_debug_token_types = StringIO() fmt_debug_token_types.format(tokensource, outfile_debug_token_types) html_debug_token_types = outfile_debug_token_types.getvalue() assert '<span class="n" title="Name">TESTDIR</span>' in html_debug_token_types
def test_correct_output(self): hfmt = HtmlFormatter(nowrap=True) houtfile = StringIO.StringIO() hfmt.format(tokensource, houtfile) nfmt = NullFormatter() noutfile = StringIO.StringIO() nfmt.format(tokensource, noutfile) stripped_html = re.sub('<.*?>', '', houtfile.getvalue()) escaped_text = escape_html(noutfile.getvalue()) self.assertEquals(stripped_html, escaped_text)
def test_correct_output(): hfmt = HtmlFormatter(nowrap=True) houtfile = StringIO() hfmt.format(tokensource, houtfile) nfmt = NullFormatter() noutfile = StringIO() nfmt.format(tokensource, noutfile) stripped_html = re.sub('<.*?>', '', houtfile.getvalue()) escaped_text = escape_html(noutfile.getvalue()) assert stripped_html == escaped_text
def test_ctags(): try: import ctags except ImportError: # we can't check without the ctags module, but at least check the exception assert raises(RuntimeError, HtmlFormatter, tagsfile='support/tags') else: # this tagfile says that test_ctags() is on line 165, even if it isn't # anymore in the actual source fmt = HtmlFormatter(tagsfile='support/tags', lineanchors='L', tagurlformat='%(fname)s%(fext)s') outfile = StringIO() fmt.format(tokensource, outfile) assert '<a href="test_html_formatter.py#L-165">test_ctags</a>' \ in outfile.getvalue()
def test_ctags(self): try: import ctags except ImportError: # we can't check without the ctags module, but at least check the exception self.assertRaises(RuntimeError, HtmlFormatter, tagsfile='support/tags') else: # this tagfile says that test_ctags() is on line 165, even if it isn't # anymore in the actual source fmt = HtmlFormatter(tagsfile='support/tags', lineanchors='L', tagurlformat='%(fname)s%(fext)s') outfile = StringIO() fmt.format(tokensource, outfile) self.assertTrue('<a href="test_html_formatter.py#L-165">test_ctags</a>' in outfile.getvalue())
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) self.assert_(isfile(join(testdir, 'fmt2.css'))) except IOError: # test directory not writable pass tfile.close() self.assert_(isfile(join(dirname(tfile.name), 'fmt1.css'))) os.unlink(join(dirname(tfile.name), 'fmt1.css')) try: os.unlink(join(testdir, 'fmt2.css')) except OSError: pass
def test_valid_output(self): # test all available wrappers fmt = HtmlFormatter(full=True, linenos=True, noclasses=True, outencoding='utf-8') handle, pathname = tempfile.mkstemp('.html') tfile = os.fdopen(handle, 'w+b') fmt.format(tokensource, tfile) tfile.close() catname = os.path.join(TESTDIR, 'dtds', 'HTML4.soc') try: import subprocess ret = subprocess.Popen(['nsgmls', '-s', '-c', catname, pathname], stdout=subprocess.PIPE).wait() except OSError: # nsgmls not available pass else: self.failIf(ret, 'nsgmls run reported errors') os.unlink(pathname)
def test_linenos_elements( linenos, noclasses, linenostep, linenostart, linenospecial, anchorlinenos, filename ): options = dict( linenos=linenos, noclasses=noclasses, linenostep=linenostep, linenostart=linenostart, linenospecial=linenospecial, anchorlinenos=anchorlinenos, filename=filename, ) output = StringIO() fmt = HtmlFormatter(**options) fmt.format(CODE, output) html = output.getvalue() filename_parts = [] filename_parts.append(linenos) filename_parts.append("nocls" if noclasses == "True" else "cls") filename_parts.append("step_" + linenostep) filename_parts.append("start_" + linenostart) filename_parts.append("special_" + linenospecial) filename_parts.append("anchor" if anchorlinenos == "True" else "noanchor") filename_parts.append("filename" if filename else "nofilename") expected_html_filename = "_".join(filename_parts) + ".html" # with open(os.path.join(EXPECTED_OUTPUT_DIR, expected_html_filename), 'w') as f: # import bs4 as BeautifulSoup # f.write(str(BeautifulSoup.BeautifulSoup(html, 'html.parser'))) with open(os.path.join(EXPECTED_OUTPUT_DIR, expected_html_filename)) as f: expected_html = f.read() structural_diff.structural_diff(html, expected_html)
def format(self, tokensource, outfile): new_tokensource = self._my_format( tokensource, outfile ) #((ttype, '<span class="hll">%s</span>' % value) if value in self.matched_terms else (ttype,value) for ttype, value in tokensource ) HtmlFormatter.format(self, new_tokensource, outfile)
def check(optdict): outfile = StringIO() fmt = HtmlFormatter(**optdict) fmt.format(tokensource, outfile)