由于CSDN这个博客不知道怎么搞,语法高亮,好像又不能够支持了,同时最近也学习正则,所以也就顺便写下了这个代码,练练手,同时也熟悉一下 Python,由于对正则还不熟悉,所以写的有点冗余,嘿嘿,有兴趣的参考参考修补修补,如有已经做好的,希望也能发给俺参考参考!
刚刚搞定,本博客中有几篇文章也用本代码转了一些Delphi代码的语法高亮格式过来。废话不多说了,
全部代码如下:
# -*- coding: gbk -*-
import re
class PaserDelphi():
"""Delphi代码生成HTML代码"""
#关键字定义
DelphiKeyWords = 'abs addr and ansichar ansistring array as asm begin boolean byte cardinal case char class comp const constructor currency destructor div do double downto else end except exports extended false file finalization finally for function goto if implementation in inherited int64 initialization integer interface is label library longint longword mod nil not object of on or packed pansichar pansistring pchar pcurrency pdatetime pextended pint64 pointer private procedure program property pshortstring pstring pvariant pwidechar pwidestring protected public published raise real real48 record repeat set shl shortint shortstring shr single smallint string then threadvar to true try type unit until uses val var varirnt while widechar widestring with word write writeln xor absolute abstract assembler automated cdecl contains default dispid dynamic export external far forward implements index message name near nodefault overload override package pascal readonly register reintroduce requires resident safecall stdcall stored virtual writeonly'
#正则
DelphiKeyWordList = r'/b'.join(['',DelphiKeyWords.replace(' ',r'/b|/b')]).join(['',r'/b'])
tempstr1 = r"'[/s/w]*".join(['',DelphiKeyWords.replace(' ',r"|'[/s/w]*")])
tempstr2 = r'/{[/s/w]*'.join(['',DelphiKeyWords.replace(' ',r'|/{[/s/w]*')])
tempstr3 = r"/(/*[/s/w]*".join(['',DelphiKey