Perl CGI输出

Perl CGI输出,perl,cgi,Perl,Cgi,我有一个Perl CGI脚本,用于在不同类型的文本中查找关键字并生成排序输出 当前输出如下所示 对于关键字“the” 但我想在此输出中突出显示加粗的关键字“the” 这怎么可能呢 我尝试使用Term::ANSIColor但它以粗体打印整个句子,而不仅仅是关键字 我的CGI脚本如下所示: #!/usr/bin/perl # require use warnings; use diagnostics; use CGI; use File::Basename; my $q = new CGI;

我有一个Perl CGI脚本,用于在不同类型的文本中查找关键字并生成排序输出

当前输出如下所示

对于关键字“the”

但我想在此输出中突出显示加粗的关键字“the”

这怎么可能呢

我尝试使用
Term::ANSIColor
但它以粗体打印整个句子,而不仅仅是关键字

我的CGI脚本如下所示:

#!/usr/bin/perl

# require
use warnings;
use diagnostics;

use CGI;
use File::Basename;

my $q = new CGI;
print "Content-type: text/plain\n\n";

#initialize variables
my $target  = $q->param( "keyword" );
my $radius  = $q->param( "span" );
my $ordinal = $q->param( "ord" );
my $width   = 2 * $radius;

#print standard output
print "****************************\n";
print "*      SEARCH RESULTS      *\n";
print "****************************\n";
print "Your Search word is: $target\n\n";
print "Your Radius is     : $radius\n\n";
print "Your Ordinal is    : $ordinal\n\n";
print "-----------------------------------------------------------\n\n";

#normal concordance for abstract text
my @files = glob( 'ABS/*.txt' );
for my $file ( @files ) {

    my $path      = glob( 'ABS/*.txt' );
    my $file_name = basename( $path );

    # initialize
    my $count = 0;
    my @lines = ();
    $/ = "";    # Paragraph read mode

    # open the file, and process each line in it
    open( FILE, " < $file" ) or die( "Can not open $file ($!).\n" );
    while ( <FILE> ) {

        # re-initialize
        my $extract = '';

        # normalize the data
        chomp;
        s/\n/ /g;           # Replace new lines with spaces
        s/\b--\b/ -- /g;    # Add spaces around dashes

        # process each item if the target is found
        while ( $_ =~ /\b$target\b/gi ) {

            # find start position
            my $match = $1;
            my $pos   = pos;
            my $start = $pos - $radius - length( $match );

            # extract the snippets
            if ( $start < 0 ) {

                $extract = substr( $_, 0, $width + $start + length( $match ) );
                $extract = ( " " x -$start ) . $extract;
            }
            else {

                $extract = substr( $_, $start, $width + length( $match ) );
                my $deficit = $width + length( $match ) - length( $extract );

                if ( $deficit > 0 ) {
                    $extract .= ( " " x $deficit );
                }
            }

            # add the extracted text to the list of lines, and increment
            $lines[$count] = $extract;
            ++$count;

        }
    }

    sub removePunctuation {
        my $string = $_[0];

        $string = lc( $string );     # Convert to lowercase
        $string =~ s/[^-a-z ]//g;    # Remove non-aplhabetic characters
        $string =~ s/--+/ /g;        # Remove 2+ hyphens with a space
        $string =~ s/-//g;           # Remove hyphens
        $string =~ s/\s=/ /g;

        return ( $string );
    }

    sub onLeft {

        #USAGE: $word = onLeft($string, $radius, $ordinal);

        my $left = substr( $_[0], 0, $_[1] );
        $left = removePunctuation( $left );
        my @word = split( /\s+/, $left );

        return ( $word[ -$_[2] ] );
    }

    sub byLeftWords {

        my $left_a = onLeft( $a, $radius, $ordinal );
        my $left_b = onLeft( $b, $radius, $ordinal );

        lc( $left_a ) cmp lc( $left_b );
    }

    # process each line in the list of lines

    my $line_number = 0;
    print "File name: $file_name \n\n";

    foreach my $x ( sort byLeftWords @lines ) {
        ++$line_number;
        printf "%5d", $line_number;
        print " $x\n\n";

    }

    print "------------------------------------------------------------\n\n";
}

# done
exit;
#/usr/bin/perl
#要求
使用警告;
使用诊断;
使用CGI;
使用File::Basename;
my$q=新的CGI;
打印“内容类型:文本/纯文本\n\n”;
#初始化变量
我的$target=$q->param(“关键字”);
my$radius=$q->param(“span”);
my$ordinal=$q->param(“ord”);
my$宽度=2*$半径;
#打印标准输出
打印“************************************\n”;
打印“*搜索结果*\n”;
打印“************************************\n”;
打印“您的搜索词是:$target\n\n”;
打印“您的半径为:$Radius\n\n”;
打印“您的序号为:$Ordinal\n\n”;
打印“--------------------------------------\n\n”;
#抽象文本的规范一致性
my@files=glob('ABS/*.txt');
对于我的$file(@files){
my$path=glob('ABS/*.txt');
我的$file\u name=basename($path);
#初始化
我的$count=0;
我的@lines=();
$/=“”#段落读取模式
#打开文件,并处理其中的每一行
打开(文件“<$FILE”)或死亡(“无法打开$FILE($!)。\n”);
而(){
#重新初始化
我的$extract='';
#规范化数据
咀嚼;
s/\n//g;#用空格替换新行
s/\b--\b/--/g;#在破折号周围添加空格
#如果找到目标,则处理每个项目
而($\u=~/\b$target\b/gi){
#找到起始位置
我的$match=$1;
我的$pos=pos;
my$start=$pos-$radius-length($match);
#提取片段
如果($start<0){
$extract=substr($\ 0,$width+$start+length($match));
$extract=(“x-$start)。$extract;
}
否则{
$extract=substr($\$start,$width+length($match));
我的$detact=$width+length($match)-length($extract);
如果($赤字>0){
$extract.=(“x$detact”);
}
}
#将提取的文本添加到行列表中,并递增
$lines[$count]=$extract;
++$count;
}
}
次移除标点符号{
我的$string=$\u0];
$string=lc($string)#转换为小写
$string=~s/[^-a-z]//g;#删除非语法字符
$string=~s/--+//g;#用空格删除2个以上连字符
$string=~s/-//g;#删除连字符
$string=~s/\s=//g;
返回($string);
}
次上悬{
#用法:$word=onLeft($string,$radius,$ordinal);
my$left=substr($\u[0],0,$\u[1]);
$left=删除标点符号($left);
my@word=split(/\s+/,$left);
返回($word[-$\u2]]);
}
副词{
my$left_a=左($a,$radius,$ordinal);
my$left_b=左($b,$radius,$ordinal);
信用证($left_a)cmp信用证($left_b);
}
#处理行列表中的每一行
我的$line_编号=0;
打印“文件名:$File\u name\n\n”;
foreach my$x(按EFTwords@lines排序){
++$line_编号;
printf“%5d”,$line\u编号;
打印“$x\n\n”;
}
打印“---------------------------------------------------------------\n\n”;
}
#完成
出口

谢谢。

您无法控制
文本/普通
文档中显示的字符样式<代码>术语::ANSIColor
当然不起作用,因为浏览器无法识别ANSI终端转义序列,该序列只能在控制台上工作

如果您以HTML格式编写输出,那么您只需在单词周围添加
,就可以壮胆了

或者,更好的是,您可以编写这样的CSS样式

.bold{
字体大小:粗体;
}
并用

更新 要将文本文档转换为HTML,最简单的方法是用


听起来你好像是在控制台上运行这个程序,那么你为什么要使用CGI呢?请不要将文本作为图像发布,而且你必须始终在你编写的每个Perl程序的顶部使用严格的
,特别是如果你请求帮助的话。不,我不是在控制台上运行它。我正在Apache上通过本地主机对其进行测试。好吧,但是如果您看到
Term::ANSIColor
的任何效果,页面中可能会偶尔出现一些“胡说八道”字符。它不可能在浏览器中产生粗体字符。嘿,是的,我首先在终端中测试脚本,当我得到想要的结果时,我在Apache上本地托管它。我附加的输出图像是由Apache上的本地主机生成的。也许我没有解释清楚。对不起,谢谢你的建议。我已经尝试按照您的建议生成HTML输出,但无法获得所需的相同输出,因此我切换到纯文本输出。那么,我是否可以在不将输出更改为HTML的情况下执行此操作。?或者如何以HTML生成相同的输出。?我在用HTML生成相同类型的输出时遇到问题。非常感谢,非常感谢。我试试这个。