Blame | Last modification | View Log | RSS feed
#!/usr/libexec/platform-python'''NAMEcode-filter - AsciiDoc filter to highlight language keywordsSYNOPSIScode-filter -b backend -l language [ -t tabsize ][ --help | -h ] [ --version | -v ]DESCRIPTIONThis filter reads source code from the standard input, highlights languagekeywords and comments and writes to the standard output.The purpose of this program is to demonstrate how to write an AsciiDocfilter -- it's much to simplistic to be passed off as a code syntaxhighlighter. Use the 'source-highlight-filter' instead.OPTIONS--help, -hPrint this documentation.-bBackend output file format: 'docbook', 'linuxdoc', 'html', 'css'.-lThe name of the source code language: 'python', 'ruby', 'c++', 'c'.-t tabsizeExpand source tabs to tabsize spaces.--version, -vPrint program version number.BUGS- Code on the same line as a block comment is treated as comment.Keywords inside literal strings are highlighted.- There doesn't appear to be an easy way to accomodate linuxdoc sojust pass it through without markup.AUTHORWritten by Stuart Rackham, <srackham@gmail.com>URLShttp://sourceforge.net/projects/asciidoc/http://asciidoc.org/COPYINGCopyright (C) 2002-2006 Stuart Rackham. Free use of this software isgranted under the terms of the GNU General Public License (GPL).'''import os, sys, reVERSION = '1.1.2'# Globals.language = Nonebackend = Nonetabsize = 8keywordtags = {'html':('<strong>','</strong>'),'css':('<strong>','</strong>'),'docbook':('<emphasis role="strong">','</emphasis>'),'linuxdoc':('','')}commenttags = {'html':('<i>','</i>'),'css':('<i>','</i>'),'docbook':('<emphasis>','</emphasis>'),'linuxdoc':('','')}keywords = {'python':('and', 'del', 'for', 'is', 'raise', 'assert', 'elif', 'from','lambda', 'return', 'break', 'else', 'global', 'not', 'try', 'class','except', 'if', 'or', 'while', 'continue', 'exec', 'import', 'pass','yield', 'def', 'finally', 'in', 'print'),'ruby':('__FILE__', 'and', 'def', 'end', 'in', 'or', 'self', 'unless','__LINE__', 'begin', 'defined?' 'ensure', 'module', 'redo', 'super','until', 'BEGIN', 'break', 'do', 'false', 'next', 'rescue', 'then','when', 'END', 'case', 'else', 'for', 'nil', 'retry', 'true', 'while','alias', 'class', 'elsif', 'if', 'not', 'return', 'undef', 'yield'),'c++':('asm', 'auto', 'bool', 'break', 'case', 'catch', 'char', 'class','const', 'const_cast', 'continue', 'default', 'delete', 'do', 'double','dynamic_cast', 'else', 'enum', 'explicit', 'export', 'extern','false', 'float', 'for', 'friend', 'goto', 'if', 'inline', 'int','long', 'mutable', 'namespace', 'new', 'operator', 'private','protected', 'public', 'register', 'reinterpret_cast', 'return','short', 'signed', 'sizeof', 'static', 'static_cast', 'struct','switch', 'template', 'this', 'throw', 'true', 'try', 'typedef','typeid', 'typename', 'union', 'unsigned', 'using', 'virtual', 'void','volatile', 'wchar_t', 'while')}block_comments = {'python': ("'''","'''"),'ruby': None,'c++': ('/*','*/')}inline_comments = {'python': '#','ruby': '#','c++': '//'}def print_stderr(line):sys.stderr.write(line+os.linesep)def sub_keyword(mo):'''re.subs() argument to tag keywords.'''word = mo.group('word')if word in keywords[language]:stag,etag = keywordtags[backend]return stag+word+etagelse:return worddef code_filter():'''This function does all the work.'''global language, backendinline_comment = inline_comments[language]blk_comment = block_comments[language]if blk_comment:blk_comment = (re.escape(block_comments[language][0]),re.escape(block_comments[language][1]))stag,etag = commenttags[backend]in_comment = 0 # True if we're inside a multi-line block comment.tag_comment = 0 # True if we should tag the current line as a comment.line = sys.stdin.readline()while line:line = line.rstrip()line = line.expandtabs(tabsize)# Escape special characters.line = line.replace('&','&')line = line.replace('<','<')line = line.replace('>','>')# Process block comment.if blk_comment:if in_comment:if re.match(r'.*'+blk_comment[1]+r'$',line):in_comment = 0else:if re.match(r'^\s*'+blk_comment[0]+r'.*'+blk_comment[1],line):# Single line block comment.tag_comment = 1elif re.match(r'^\s*'+blk_comment[0],line):# Start of multi-line block comment.tag_comment = 1in_comment = 1else:tag_comment = 0if tag_comment:if line: line = stag+line+etagelse:if inline_comment:pos = line.find(inline_comment)else:pos = -1if pos >= 0:# Process inline comment.line = re.sub(r'\b(?P<word>\w+)\b',sub_keyword,line[:pos]) \+ stag + line[pos:] + etagelse:line = re.sub(r'\b(?P<word>\w+)\b',sub_keyword,line)sys.stdout.write(line + os.linesep)line = sys.stdin.readline()def usage(msg=''):if msg:print_stderr(msg)print_stderr('Usage: code-filter -b backend -l language [ -t tabsize ]')print_stderr(' [ --help | -h ] [ --version | -v ]')def main():global language, backend, tabsize# Process command line options.import getoptopts,args = getopt.getopt(sys.argv[1:],'b:l:ht:v',['help','version'])if len(args) > 0:usage()sys.exit(1)for o,v in opts:if o in ('--help','-h'):print(__doc__)sys.exit(0)if o in ('--version','-v'):print('code-filter version %s' % (VERSION,))sys.exit(0)if o == '-b': backend = vif o == '-l':v = v.lower()if v == 'c': v = 'c++'language = vif o == '-t':try:tabsize = int(v)except:usage('illegal tabsize')sys.exit(1)if tabsize <= 0:usage('illegal tabsize')sys.exit(1)if backend is None:usage('backend option is mandatory')sys.exit(1)if backend not in keywordtags:usage('illegal backend option')sys.exit(1)if language is None:usage('language option is mandatory')sys.exit(1)if language not in keywords:usage('illegal language option')sys.exit(1)# Do the work.code_filter()if __name__ == "__main__":try:main()except (KeyboardInterrupt, SystemExit):passexcept:print_stderr("%s: unexpected exit status: %s" %(os.path.basename(sys.argv[0]), sys.exc_info()[1]))# Exit with previous sys.exit() status or zero if no sys.exit().sys.exit(sys.exc_info()[1])