页面

2009-04-03

convert source code to kinds of document formats

There are several utilities on Debian to convert programming source code to text format, such as HTML, LaTex, and so forth. 'source-highlight' is the most powerful one from my view. Another two similar tools: code2html and highlight.

A python snippets:

#! /usr/bin/python

import os, re, fileinput

class subor:
def replace(self, line):
rv = line
mylist = ["enum"]
p = re.compile(r"\b(%s)\b" % '|'.join(mylist))
m = p.search(line)
if m:
print "Found %s" % m.group(1)
rv = line.replace("enum", "enm")
return rv
def do(self, root_dir):
for root, dirs, files in os.walk(root_dir):
for file in [f for f in files if f.endswith(".java")]:
file = root + "/" + file
print "processing " + file
# fh = open(file)
for lines in fileinput.FileInput(file, inplace="1"):
lines = lines.replace("enum", "enm")
print lines



# fh.close()

if __name__ == "__main__":
s = subor()
s.do("/src/caet/walend")

I use source-highlight to convert above source code into html:
$ source-highlight subor.py

没有评论: