我可以在自动Perl构建中使用perltidy的HTML格式化程序吗?

时间:2009-05-07 15:59:10

标签: html perl build-process build perl-tidy

我正在使用Module :: Build来执行build,test,testpod,html和&在我正在开发的Perl模块上安装操作。生成的HTML文件是可以的,但如果我能以某种方式配置Module :: Build以使用perltidy -html格式化实用程序而不是自己的HTML格式化程序,我会更高兴。

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

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

以下是我现在在我编写的构建脚本中使用它的方法,但它完全是一个黑客...堕入命令行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');

2 个答案:

答案 0 :(得分:1)

嗯,行动在

中实施
htmlify_pods
<{3>}中的

应该可以覆盖该方法。

很久......

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

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;

答案 1 :(得分:1)

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

sub ACTION_htmltidy
    {
    my( $self ) = @_;

    $self->depends_on( ...other targets... );

    require Perl::Tidy;

    ...do your damage...
    }

如果您希望使用其他操作,可以对其进行扩展,以便您可以进行依赖:

sub ACTION_install
    {
    my( $self ) = @_;

    $self->depends_on( 'htmltidy' );

    $self->SUPER::install;
    }