用Mathematica实现,功能比较简单,利用正则表达式对相应位置加上html标签< span >即可,写这个的初衷是csdn的代码渲染看起来不是很好看,而且之前刷PAT来着,如果用import爬取问题描述,然后连接成一篇article也比较方便(难道不是因为懒吗(逃~))
code = Import["C:\\Users\\gl486\\Desktop\\nb\\cpp.txt"];(*读入代码*)
incl = "(#include)";(*include*)
hder = "( <)([a-z]+)(>)(\n)";(*头文件*)
keyword = "(int|long|double|float|struct|class|void|bool)";(*关键字*)
op = "(<|>|=|\+|-|\*|&";
con = "|if|else|while|do|break|return|for|using namespace)";
num = "((0x[0-9a-f]+)|([0-9]+(.[0-9]+)?))";
str = "(\"(.*?)+\")";(*字符串*)
note = "(//.+\n)";(*注释*)
rp = {RegularExpression[incl] ->
"<span style='color:#C71585'>$1</span>",
RegularExpression[hder] ->
"<span style='color:#EEB422'> <$2></span>$4",
RegularExpression[keyword] ->
"<span style='color:#43CD80;font-style:italic;'>$1</span>",
RegularExpression[op <> con] ->
"<span style='color:#C71585'>$1</span>",
RegularExpression[num] -> "<span style='color:#9B30FF'>$1</span>",
RegularExpression[str] -> "<span style='color:#CD950C'>$1</span>",
RegularExpression[note] -> "<span style='color:#CD2626'>$1</span>"};
code = StringReplace[code, rp];
code = "<pre style=\"overflow:auto; \
font-family:Menlo,Monaco,Consolas,"Courier New",monospace; \
font-size:13px; padding:9.5px; margin-top:0px; margin-bottom:10px; \
line-height:1.42857; color:rgb(207,207,207); word-break:break-all; \
word-wrap:break-word; background-color:rgb(30,30,30); border:1px \
solid rgb(204,204,204)\">
" <> code <> "</pre>";(*加入<pre>标签*)
code;
Export["C:\\Users\\gl486\\Desktop\\nb\\cppRe.txt", code](*输出到文件*)