使用Perl编辑HTML文件

时间:2010-12-21 12:40:35

标签: html perl

以下输出由第三方工具生成,该工具根据XML Schema验证XML文件。它将输出验证错误。好的,所以这只是一个小环境。

从上面的错误显示在HTML中,我希望能够使用perl执行“语法突出显示”。例如,我希望能够突出显示上述文本的某些部分。

具体来说,我想将任何符合“Line [0-9] *”的文本颜色为粗体和红色。我试图尝试使用正则表达式搜索和替换,但我没有那么成功。

任何指针/提示都会很棒。

谢谢!

Line 8: Element 'alphabet', attribute 'letters': The value 'XYZ' does not match the fixed value constraint 'ABC'.
Line 185: Element 'drink': The attribute 'coffee' is required but missing.
Line 254: Element 'timeout': This element is not expected.
Line 269: Element 'commands': This element is not expected. Expected is one of ( eat, drink, sleep ).
Line 812: Element 'software': The attribute 'version' is required but missing.
Line 876: Element 'windows-software': The attribute 'version' is required but missing.
Line 890: Element 'contact': The attribute 'telephone' is required but missing.
Line 890: Element 'operating': The attribute 'mode' is required but missing.

1 个答案:

答案 0 :(得分:1)

试试这个:

#!/usr/bin/perl 
use strict;
use warnings;

while(<DATA>) {
    s#(Line \d+)#<span style="font-weight:bold;color:red;">$1</span>#;
    s#(Element\s|attribute\s)('[^']*')#$1<span style="font-weight:bold;color:blue;">$2</span>#g;
    print
}

__DATA__
Line 8: Element 'alphabet', attribute 'letters': The value 'XYZ' does not match the fixed value constraint 'ABC'.
Line 185: Element 'drink': The attribute 'coffee' is required but missing.
Line 254: Element 'timeout': This element is not expected.

<强>输出

<span style="font-weight:bold;color:red;">Line 8</span>: Element <span style="font-weight:bold;color:blue;">'alphabet'</span>, attribute <span style="font-weight:bold;color:blue;">'letters'</span>: The value 'XYZ' does not match the fixed value constraint 'ABC'.
<span style="font-weight:bold;color:red;">Line 185</span>: Element <span style="font-weight:bold;color:blue;">'drink'</span>: The attribute <span style="font-weight:bold;color:blue;">'coffee'</span> is required but missing.
<span style="font-weight:bold;color:red;">Line 254</span>: Element <span style="font-weight:bold;color:blue;">'timeout'</span>: This element is not expected.

您可以使用css类而不是样式属性。

s#(Line \d+)#<span class="bold_red">$1</span>#;