1''' 2a script for python code highlighting is expected to partial html output. 3That's output content without the whole html tags. 4 5 6ex. 7<style> 8xxx 9xxx10xx11</style>12<table>13<div>14xx15xx16</div>17</table>1819usage: test.py inputfile outputfile2021'''222324frompygments.formattersimportHtmlFormatter25frompygmentsimporthighlight26frompygments.lexersimport*27frompygments.stylesimport*28importsys2930try:31readfile=open(sys.argv[1],'r')32except:33sys.exit()3435code=readfile.read()36readfile.close()3738file=open(sys.argv[1].split('.')[0]+'.html','w')3940st=get_style_by_name('native')41myformat=HtmlFormatter(style=st,linenos='inline')424344content='''<style>45.lineno {{46 color: {};47 margin-right: 5px;48}}49{}</style>'''.format('#'+str(int(st.background_color[1:])+303030),myformat.get_style_defs('.highlight'))50#rule the style5152content+=highlight(code,get_lexer_for_filename(sys.argv[1]),myformat)53#add the highlighted code545556file.write(content)57file.close()
網頁要搬家了!