Perl - 如何打开/读取/打印 - 目录和文件内容

时间:2014-04-24 04:30:39

标签: html perl

问题(2014年4月24日):我正在寻找一种方法来避免手动修改网页以包含指向目录中文件的链接,以便显示这些文件的内容作为使用Syntaxhighlighter的代码片段。如果没有使用文件管理器脚本(例如,* .cgi或* .php),是否有一种简单的方法来评估特定目录中的文件并将其内容包含在网页中?

换句话说,我希望能够从目录中手动添加/删除/修改代码片段文件,并让脚本在每次加载wepage时重新评估片段目录(重新)。就目前而言,如果我在服务器上重命名该文件,我需要手动编辑我的网页以更改license.txt的名称。我希望简单地指定一个目录(例如,/home/lawlist/public_html/code_snippets)并让脚本评估该目录的内容,以使用该目录中文件的内容填充该网页。该行为类似于* .cgi或* .php文件管理器可以执行的操作。

2 个答案:

答案 0 :(得分:1)

那是服务器技术。在服务器端JavaScript中,您可以使用nodejs'fs模块并输出内容。

“SyntaxHighlighting”是客户端技术,您可以使用任何您喜欢的库进行代码着色。

答案 1 :(得分:0)

Perl脚本 - test.cgi

  
#!/usr/bin/perl

use CGI qw(:standard);

print "Content-type: text/html\n\n";

print <<HTML;

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

<html>

<head>

<title>Document Title</title>

</head>

<body>

HTML

my $dir = '/home/lawlist/www';

my $query = new CGI;

my $name = $query->param('name');

my $file = $dir . "/" . $name;

opendir(DIR, $dir) or die $!;

while (my $file = readdir(DIR)) {

  next unless (-f "$dir/$file");

  next unless ($file =~ m/\.txt|\.el$/);

  print '<a href="/cgi-bin/test.cgi?name=' . $file . '">' . $file . "</a>" . "<br>" . "\n\n"; }

closedir(DIR);

if ($name) {

  open (DATA, $file) or return $self->print_json_error($self->language('ERR_CANNOT_OPEN', $file->{name}, $!));

  read (DATA, my $file, -s DATA);

  close DATA;

  print '<pre class="brush:  lisp">' . "\n\n" . $file . "\n" . '</pre>'; }

print <<HTML;

</body>

</html>

HTML

exit 0;

.htaccess配置

DirectoryIndex index.html index.htm index.php index.cgi

SSLOptions +StdEnvVars

# Customized server error messages:
# ErrorDocument 404 /page.not.found.basic.html

AddHandler cgi-script .pl .cgi
Options +Includes +ExecCGI

AddType text/html .shtml .shtm .html .htm
AddHandler server-parsed .shtml .shtm .html .htm

# if you want to see the list of files in a directory
# without an index.html file, then uncomment the following line:

# Options +Indexes

Options -Indexes

# Options +FollowSymLinks

# Options Indexes Includes FollowSymLinks ExecCGI

网页插入

<hr COLOR="#CCCCCC" size=1 NOSHADE>

<!--#exec cgi="/cgi-bin/test.cgi"-->

<hr COLOR="#CCCCCC" size=1 NOSHADE>
相关问题