def test_just_one_file_with_title(self): tupel = CodeLexerTuple(open('test/resources/Quicksort.hs').read(), HaskellLexer(stripnl=False), 'Haskell Quicksort'); result = highlightMultiSource((tupel,), MultiSourceFormatter( linenos=True, cssclass="source", noclasses=True, showtitles=True)) self.helper_just_one_file_title(result) self.assertFalse(re.search('<span>8</span>', result), "There must be only seven lines of code")
def test_just_one_file_without_title(self): tupel = CodeLexerTuple(open('test/resources/Quicksort.hs').read(), HaskellLexer(stripnl=False), 'Haskell Quicksort'); result = highlightMultiSource((tupel,), MultiSourceFormatter( linenos=True, cssclass="source", noclasses=True, showtitles=False)) self.assertFalse(re.search('<span>Haskell Quicksort</span>', result), "Result must contain title noting 'Haskell Quicksort'") self.assertTrue(re.search('<span>7</span>', result), "There must be seven lines of code") self.assertFalse(re.search('<span>8</span>', result), "There must be only seven lines of code") self.assertTrue(re.search('<span style="color: #0000FF">quicksort</span>', result), "There must be a function named quicksort highlighted")
def test_two_files_with_title(self): tupel = CodeLexerTuple(open('test/resources/Quicksort.hs').read(), HaskellLexer(stripnl=False), 'Haskell Quicksort'); tupel2 = CodeLexerTuple(open('test/resources/Quicksort.java').read(), JavaLexer(), 'Java Quicksort'); result = highlightMultiSource((tupel,tupel2), MultiSourceFormatter( linenos=True, cssclass="source", noclasses=True, showtitles=True)) self.helper_just_one_file_title(result) self.assertTrue(re.search('<span>Java Quicksort</span>', result, MULTILINE), "Result must contain title noting 'Haskell Quicksort'") self.assertTrue(re.search('<span style="color: #0000FF; font-weight: bold">java.util.ArrayList</span>', result), "There must be an import java.util.ArrayList")
def test_three_files_with_title(self): tupel = CodeLexerTuple(open('test/resources/Quicksort.hs').read(), HaskellLexer(stripnl=False), 'Haskell Quicksort'); tupel2 = CodeLexerTuple(open('test/resources/Quicksort.java').read(), JavaLexer(), 'Java Quicksort'); tupel3 = CodeLexerTuple(open('test/resources/Quicksort.py').read(), PythonLexer(), 'Python Quicksort'); result = highlightMultiSource((tupel,tupel2,tupel3), MultiSourceFormatter( linenos=True, cssclass="source", noclasses=True, showtitles=True)) #test invariant haskell source self.helper_just_one_file_title(result) #test java source self.assertTrue(re.search('<span>Java Quicksort</span>', result, MULTILINE), "Result must contain title noting 'Java Quicksort'") self.assertTrue(re.search('<span style="color: #0000FF; font-weight: bold">java.util.ArrayList</span>', result), "There must be an import java.util.ArrayList") #test python source self.assertTrue(re.search('<span>Python Quicksort</span>', result, MULTILINE), "Result must contain title noting 'Python Quicksort'") self.assertTrue(re.search('<span style="color: #008000; font-weight: bold">lambda</span>', result), "There must be an lambda keyword denoting an anonymous function")
''' This piece of source demonstrates the MultisourceHtmlFormatter that takes three quicksort implementations (python, haskell, java) into account and produces html output of all implementations side-by-side. The output gets writen to stdout and file output.html :copyright: Copyright 2013 by [email protected], see AUTHORS. :license: BSD, see LICENSE for details. ''' from MultisourceHtmlFormatter import CodeLexerTuple, highlightMultiSource,\ MultiSourceFormatter from pygments.lexers.agile import PythonLexer from pygments.lexers.functional import HaskellLexer from pygments.lexers.jvm import JavaLexer if __name__ == "__main__": python = open('test/resources/Quicksort.py') haskell = open('test/resources/Quicksort.hs') java = open('test/resources/Quicksort.java') tupel1 = CodeLexerTuple(python.read(), PythonLexer(), 'Python Quicksort'); tupel2 = CodeLexerTuple(haskell.read(), HaskellLexer(stripnl=False), 'Haskell Quicksort'); tupel3 = CodeLexerTuple(java.read(), JavaLexer(), 'Java Quicksort'); result = highlightMultiSource((tupel1,tupel2,tupel3), MultiSourceFormatter(linenos=True, cssclass="source", noclasses=True, showtitles=True)) print(result) open('output.html','w').write("<html><body>" + result + "</body></html>") java.close() haskell.close()
desc = """ Comparare et Pendere - multisource html formatter based on pygments. Refer to http://gixxi.github.com/comparareetpendere """ parser = OptionParser(usage = usage, description = desc, option_class = CustomOption); parser.add_option('-o', metavar='output filename', action='store', type="writefile", help='output filename. output is printed to stdout if not given.') parser.add_option('-i', metavar='input filename', action='append', type="readfile", help='input filename of the source code file. Repeat -i <FILENAME> for each file to be included in the formatting process.') parser.add_option('-t', metavar='title', action='append', type="string", help='title above the the input file included in the formatted output. Repreat -t <TITLE> for each file to be included in the formatting process. Use quotes \' to for titles containing spaces.') parser.add_option('-b', metavar='just body', action='store_true', default=False, help='iff true then only the content within body is contained in the output rather than an html document') (options, args) = parser.parse_args() #constraint: one input file must be given at least if not options.i: raise OptionValueError("option -i: at least one input file must be provided") #constraint: if titles are given at all then their number must match number of input files if options.t and len(options.t) != len(options.i): raise OptionValueError("option -t: when providing titles at all, the number of titles must be equal to the number of input files given by option -i") codeLexerTuples = tuple(yield_codelexertuples(options)) result = highlightMultiSource(codeLexerTuples, MultiSourceFormatter( linenos=True, cssclass="source", noclasses=True, showtitles=True if options.t else False)) if not options.b: result = '<html><body>' + result + '</body></html>' if options.o: options.o.write(result) else: print(result)