1

我正在使用 Module::Build 在我正在开发的 Perl 模块上执行构建、测试、testpod、html 和安装操作。生成的 HTML 文件没问题,但如果我能以某种方式将 Module::Build 配置为使用 perltidy -html 格式化实用程序而不是它自己的 HTML 格式化程序,我会更高兴。

任何人都知道我可以用更漂亮的 perltidy HTML 格式化程序替换 Module::Build 附带的 HTML 格式化程序吗?

附录: 当我在上面说“替换”时,这可能具有误导性。我真的不想编写代码来替换 Module::Build 附带的 html 格式化程序。我真的很想知道 Module::Build 是否有任何其他HTML 格式化程序选项。它生成的 HTML 看起来非常简单和通用。太无聊了。我非常喜欢 perltidy 的输出。

这是我现在如何在我编写的构建脚本中让它工作的方法,但这完全是一个 hack ......落入命令行 perltidy 脚本:

use strict;
use warnings;

# get list of files in directory
my $libLocation = "lib/EDF";
opendir( DIR, $libLocation );
my @filenameArray = readdir(DIR);

# iterate over all files to find *.pm set
for my $file (@filenameArray) {
    if ( $file =~ m/      # matching regex
                      \.  # literal period character
                      pm  # the pm file extenstion
                  /x      # end of regex
       )
    {

        my $return = `perl D:/Perl/site/bin/perltidy -q --indent-columns=4 --maximum-line-length=80 -html -opath blib/libhtml2 -toc $libLocation/$file`;

        if ($return eq "") {
            print "HTMLized " . $file . "\n";
        }
        else {
            print "Error: " . $return . "\n";
        }

    }

}

但我真的希望有一种方法可以使用 Module::Build 并用一个标志或一个参数告诉它,或者告诉它使用不同的 HTML 格式化程序。不过,我想这是一个白日梦:

use strict;
use warnings;
use Module::Build;

my $build = Module::Build->resume (
  properties => {
    config_dir => '_build',
  },
);

$build->dispatch('build');
$build->dispatch('html', engine => 'perltidy');

或许:

$build->dispatch('htmltidy');
4

2 回答 2

1

好吧,该动作在

htmlify_pods

Module::Build::Base中。

应该可以覆盖该方法。

很久以后 ...

这是我的尝试(仅测试一次):

package My::Builder;

use strict;
use warnings;

use base 'Module::Build';

sub htmlify_pods {
  my $self = shift;
  my $type = shift;
  my $htmldir = shift || File::Spec->catdir($self->blib, "${type}html");

  require Module::Build::Base;
  require Module::Build::PodParser;
  require Perl::Tidy;

  $self->add_to_cleanup('pod2htm*');

  my $pods = $self->_find_pods( 
      $self->{properties}{"${type}doc_dirs"},
      exclude => [ Module::Build::Base::file_qr('\.(?:bat|com|html)$') ] );
  return unless %$pods;  # nothing to do

  unless ( -d $htmldir ) {
    File::Path::mkpath($htmldir, 0, oct(755))
      or die "Couldn't mkdir $htmldir: $!";
  }

  my @rootdirs = ($type eq 'bin') ? qw(bin) :
      $self->installdirs eq 'core' ? qw(lib) : qw(site lib);

  my $podpath = join ':',
                map  $_->[1],
                grep -e $_->[0],
                map  [File::Spec->catdir($self->blib, $_), $_],
                qw( script lib );

  foreach my $pod ( keys %$pods ) {

    my ($name, $path) = File::Basename::fileparse($pods->{$pod},
        Module::Build::Base::file_qr('\.(?:pm|plx?|pod)$'));
    my @dirs = File::Spec->splitdir( File::Spec->canonpath( $path ) );
    pop( @dirs ) if $dirs[-1] eq File::Spec->curdir;

    my $fulldir = File::Spec->catfile($htmldir, @rootdirs, @dirs);
    my $outfile = File::Spec->catfile($fulldir, "${name}.html");
    my $infile  = File::Spec->abs2rel($pod);

    next if $self->up_to_date($infile, $outfile);

    unless ( -d $fulldir ){
      File::Path::mkpath($fulldir, 0, oct(755))
        or die "Couldn't mkdir $fulldir: $!";
    }

    my $path2root = join( '/', ('..') x (@rootdirs+@dirs) );
    my $htmlroot = join( '/',
             ($path2root,
              $self->installdirs eq 'core' ? () : qw(site) ) );

    my $fh = IO::File->new($infile) or die "Can't read $infile: $!";
    my $abstract = Module::Build::PodParser->new(fh => $fh)->get_abstract();

    my $title = join( '::', (@dirs, $name) );
    $title .= " - $abstract" if $abstract;

    my %opts = (
        argv => join(" ", 
            qw( -html --podflush ),
            "--title=$title",
            '--podroot='.$self->blib,
            "--htmlroot=$htmlroot",
            "--podpath=$podpath",
        ),
        source => $infile,
        destination => $outfile,
    );

    if ( eval{Pod::Html->VERSION(1.03)} ) {
      $opts{argv} .= ' --podheader';
      $opts{argv} .= ' --backlink=Back to Top';
      if ( $self->html_css ) {
          $opts{argv} .= " --css=$path2root/" . $self->html_css;
      }
    }

    $self->log_info("HTMLifying $infile -> $outfile\n");
    $self->log_verbose("perltidy %opts\n");
    Perl::Tidy::perltidy(%opts);    # or warn "pod2html @opts failed: $!";
  }
}
1;

** 使用它.. **

#!/usr/bin/perl

use strict;
use warnings;

use My::Builder;

my $builder = My::Builder->new(
    module_name => 'My::Test',
    license     => 'perl',
);

$builder->create_build_script;
于 2009-05-07T16:27:09.283 回答
1

定义可以通过 dispatch 调用的新 Module::Build 操作非常容易,Module::Build 文档中有很多示例。定义一个动作来处理你的新步骤:

子 ACTION_htmltidy
    {
    我的( $self ) = @_;

    $self->depends_on( ...其他目标... );

    需要 Perl::Tidy;

    ...做你的伤害...
    }

如果您希望其他操作使用您的操作,您可以扩展它以便建立依赖关系:

子 ACTION_install
    {
    我的( $self ) = @_;

    $self->depends_on('htmltidy');

    $self->超级::安装;
    }
于 2009-05-09T19:21:37.590 回答