9.4 用Perl转换普通文本

再看看如何通过Perl语言为文本添加HTML标签。首先,和sed相似,我先逐一给出一系列命令;然后再在一个文件中展示同样的命令。

本书只介绍了Perl语言的初步知识及用法。本书不是Perl语言教程或手册,但我希望它能激发你对Perl语言的兴趣,因此想多展示几种可能的用法。学习Perl语言最好的起点是Learning Perl网站http://learn.perl.org/,上面还有Perl的安装指导方法。

如果当前行($.)就是第1行,将整行($_)赋值给变量$title,再将$title打印出来。

  1. perl -ne 'if ($. == 1) {chomp($title = $_); print "<h1>" . $title . "</h1>" . "\n";};'rime.txt

如果一切正常,结果应该是:

  1. <h1>THE RIME OF THE ANCYENT MARINERE, IN SEVEN PARTS.</h1>

以下是对这个Perl命令的详解。

  • 通过$.检查是否在第1行。
  • 选定整行($_)然后将这个字符串赋值给$title变量。当用chomp函数选定整行时,就会将结尾的换行符从字符串中剔除。
  • h1元素中打印$title,然后再打印换行符(\n)。

要了解关于Perl语言内置变量(如$.)的更多信息,请在提示符中输入命令perldoc -v $.(perldoc通常在安装Perl时就已经安装了)。如果该命令无效,请参见9.6节。

要在文件起始处添加标记(包括h1标签),使用以下命令:

  1. perl -ne 'if ($. == 1) {chomp($title = $_)};
  2. print "<!DOCTYPE html>\
  3. <html xmlns=\"http://www.w3.org/1999/xhtml\">\
  4. <head>\
  5. <title>$title</title>\
  6. <meta charset=\"utf-8\"/>\
  7. </head>\
  8. <body>\
  9. <h1>$title</h1>\n" if $. == 1; exit' rime.txt

结果如下:

  1. <!DOCTYPE html>
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <title>THE RIME OF THE ANCYENT MARINERE, IN SEVEN PARTS.</title>
  5. <meta charset="utf-8"/>
  6. </head>
  7. <body>
  8. <h1>THE RIME OF THE ANCYENT MARINERE, IN SEVEN PARTS.</h1>

打印函数print会打印随后的标签以及每一行,并在行尾加\,其中\会将换行符写入到输出内容中。$title变量的内容会在titleh1元素中展开显示。

9.4.1 用Perl处理罗马数字

要给标题和用于分隔内容的罗马数字添加标签,可使用:

  1. perl -ne 'print if s/^(ARGUMENT\.|I{0,3}V?I{0,2}\.)$/<h2>\1<\/h2>/;' rime.txt

输出如下所示:

  1. <h2>ARGUMENT.</h2>
  2. <h2>I.</h2>
  3. <h2>II.</h2>
  4. <h2>III.</h2>
  5. <h2>IV.</h2>
  6. <h2>V.</h2>
  7. <h2>VI.</h2>
  8. <h2>VII.</h2>

替换命令(s)会捕获ARGUMENT标题和七个大写罗马数字,其中每一个都在单独的一行且尾随一个句点,罗马数字范围为I至VII;然后将捕获的文本包括在h2标签中。

9.4.2 用Perl处理特定段落

请使用以下代码在行号为5时用p元素包括介绍段落:

  1. perl -ne 'if ($. == 5) {s/^([A-Z].*)$/<p>$1<\/p>/;print;}' rime.txt

可以看到如下内容:

  1. <p> How a Ship having passed the Line was driven by Storms to the cold Country towards
  2. the South Pole; and how from thence she made her course to the tropical Latitude
  3. of the Great Pacific Ocean; and of the strange things that befell; and in what
  4. manner the Ancyent Marinere came back to his own Country.</p>

9.4.3 用Perl处理多行诗文

以下命令会将一个p起始标签放在诗文第一行的起始处,并在该行的结尾添加一个br标签:

  1. perl -ne 'if ($. == 9) {s/^[ ]*(.*)/ <p>$1<br\/>/;print;}' rime.txt

结果如下:

  1. <p>It is an ancyent Marinere,<br/>

接下来的Perl命令会在第10行到第832行之间的每一行诗文的结尾处添加一个br标签:

  1. perl -ne 'if (10..832) { s/^([ ]{5,7}.*)/$1<br\/>/; print;}' rime.txt

下面是你可以看到的结果的一部分:

  1. Farewell, farewell! but this I tell<br/>
  2. To thee, thou wedding-guest!<br/>
  3. He prayeth well who loveth well<br/>
  4. Both man and bird and beast.<br/>

在诗文最后一行结尾处添加一个p结束标签。

  1. perl -ne 'if ($. == 833) {s/^(.*)/$1<\/p>/; print;}' rime.txt

这就会显示:

  1. He rose the morrow morn.</p>

将每一行结尾处的空行替换为br标签:

  1. perl -ne 'if (9..eof) {s/^$/<br\/>/; print;}' rime.txt

结果为:

  1. <br/>
  2. He prayeth best who loveth best,
  3. All things both great and small:
  4. For the dear God, who loveth us,
  5. He made and loveth all.
  6. <br/>
  7. The Marinere, whose eye is bright,
  8. Whose beard with age is hoar,
  9. Is gone; and now the wedding-guest
  10. Turn'd from the bridegroom's door.
  11. <br/>

最后,找到文件结尾时,就打印出几个结束标签:

  1. perl -ne 'if (eof) {print "</body>\n</html>\n"};' rime.txt

将所有这些代码放在一个文件中运行起来更简单,下一节就会看到了。

9.4.4 使用Perl命令文件

下面列出的是html.pl文件,它用Perl语言将rime.txt转换为HTML。该示例中的编号将指导你理解这个Perl脚本的内容。

  1. #!/usr/bin/perl -p ①
  2. if ($. == 1) {
  3. chomp($title = $_);
  4. }
  5. print "<!DOCTYPE html>\ ③
  6. <html xmlns=\"http://www.w3.org/1999/xhtml\">\
  7. <head>\
  8. <title>$title</title>\
  9. <meta charset=\"utf-8\"/>\
  10. </head>\
  11. <body>\
  12. <h1>$title</h1>\n" if $. == 1;
  13. s/^(ARGUMENT|I{0,3}V?I{0,2})\.$/<h2>$1<\/h2>/;
  14. if ($. == 5) {
  15. s/^([A-Z].*)$/<p>$1<\/p>/;
  16. }
  17. if ($. == 9) {
  18. s/^[ ]*(.*)/ <p>$1<br\/>/;
  19. }
  20. if (10..832) {
  21. s/^([ ]{5,7}.*)/$1<br\/>/;
  22. }
  23. if (9..eof) {
  24. s/^$/<br\/>/;
  25. }
  26. if ($. == 833) {
  27. s/^(.*)$/$1<\/p>\n <\/body>\n<\/html>\n/;
  28. }

① 这一行是shebang命令,它提示shell你要运行的程序的位置。

② 如果当前行($.)是第1行,则将整行($_)赋值给变量$title,同时使用chomp函数将字符串最后一个字符(换行符)剔除。

③ 在文档顶部的第1行打印文档类型以及一些HTML标签,然后在几个地方重用变量$title的值。

④ 用h2标签包括ARGUMENT标题和罗马数字。

⑤ 用p标签包括介绍段落。

⑥ 在诗文开头添加p起始标签,在行尾添加br标签。

⑦ 在每行诗文结尾处添加一个br标签,最后一行除外。

⑧ 在第9行后,每一个空行都用换行标签(br)代替。

⑨ 在最后一行附加pbody以及html结束标签。

要运行该文件,只需输入:

  1. perl html.pl rime.txt

还可以用>将输出内容重定向到文件中。在下一章也就是最后一章中,我们来总结本书讲解的正则表达式知识。