Python将Delphi代码生成语法高亮的HTML格式

本文介绍了一个使用Python编写的代码,该代码能够将Delphi源代码转换为带有语法高亮的HTML格式。通过正则表达式处理Delphi的关键字、字符串、注释和数字,生成相应的HTML标记以实现高亮显示。作者提到,这个程序适用于CSDN博客中无法支持语法高亮的情况,并且是作为学习正则表达式和Python的实践项目。
摘要由CSDN通过智能技术生成
 

由于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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值