- 论坛徽章:
- 0
|
本帖最后由 斯文牛氓 于 2012-09-27 17:21 编辑
烂博客发文章不喜欢在线编辑,于是自己动手,丰衣足食,本菜鸟写了个pod2html的脚本,代码如下:- use strict;
- use warnings;
- use Data::Dumper;
- use Pod::Html;
- use FileHandle;
- use Getopt::Long;
- use Carp;
- use HTML::Entities;
- use File::Basename;
- use File::Spec;
- use Cwd qw(abs_path);
- use open ':std', ':encoding(UTF-8)';
- use open ':encoding(utf8)';
- my $module_list;
- my $is_raw_zh;
- GetOptions( "m:s" => \$module_list, 'c:s' => \$is_raw_zh );
- if ( !$module_list ) {
- Carp::croak(
- "you must defined a module list like \n",
- "$0 -m File::Spec,File::Basename\n"
- );
- }
- #my $changed_pre = q{<pre class="brush: perl; gutter: true">};
- # wp-code-highlight prettyprint
- my $changed_pre = q{<pre class="wp-code-highlight prettyprint">};
- my $pod_path = abs_path( dirname(__FILE__) ) . "/../pod";
- my $html_path = abs_path( dirname(__FILE__) ) . "/../html";
- for my $module_name ( split( ",", $module_list ) ) {
- $module_name =~ s{::}{-}g;
- my $per_pod_file = File::Spec->catfile( $pod_path, $module_name );
- if ( !-e $per_pod_file ) {
- Carp::croak("this module ${module_name}'s pod is not exists\n");
- }
- # init file handle
- # my $readpod_r = new FileHandle($per_pod_file);
- open( my $readpod_r, "<", $per_pod_file );
- my $cpan_html_file =
- File::Spec->catfile( $html_path, $module_name . ".html" );
- my $zh_pod_file =
- File::Spec->catfile( $pod_path, $module_name . ".zh.pod" );
- my $wp_html_file = $cpan_html_file . "_wp.html";
- # my $zh_pod_w = new FileHandle( ">" . $zh_pod_file )
- # or Carp::croak("creat zh pod file failed");
- open( my $zh_pod_w, ">", $cpan_html_file );
- goto RAW_ZH_POD if $is_raw_zh;
- READ_TRANSFERED_FILE: {
- while ( my $line = <$readpod_r> ) {
- #chomp($line);
- if ( $line =~ m/^=head1/xis ) {
- while ( $line = <$readpod_r> ) {
- if ( $line =~ m/^zh:(.*) # match zh line/xis ) {
- print $zh_pod_w $1;
- }
- elsif ( $line =~ m{^(?:\t|\s) # match code block}xis ) {
- print $zh_pod_w $line;
- }
- }
- }
- }
- }
- RAW_ZH_POD: {
- last unless $is_raw_zh;
- my $raw_pod = do { local $/; <$readpod_r> };
- print $zh_pod_w $raw_pod;
- }
- CONVERT: {
- if ( -s $zh_pod_file > 0 ) {
- pod2html(
- "--infile=" . $zh_pod_file,
- "--outfile=" . $cpan_html_file,
- "--title=" . $module_name,
- );
- # substitute for wp format html document
- my $wp_pre = q{<pre class="brush: perl; gutter: true">};
- open( my $cpan_html_r, "<", $cpan_html_file )
- or Carp::croak("open file failed $@");
- open( my $wp_html_w, ">", $wp_html_file )
- or Carp::croak("open wp html failed\n");
- do {
- local $/ = undef;
- my $content = <$cpan_html_r>;
- $content =~ s{<span.*?>}{}sg;
- $content =~ s{</span>}{}sg;
- $content =~ s{</pre>[^<]*<pre>}{}sg;
- $content =~ s{<pre>}{$changed_pre}sg;
- # $content =~ s{<}{<}g;
- # $content =~ s{>}{>}g;
- $content=~ s{&gt;}{>}g;
- print $wp_html_w $content, "\n";
- };
- close($wp_html_w);
- close($cpan_html_r);
- print "-------------------------", "\n";
- print "All Convert are finished\n";
- print "Convert Output File are:", "\n";
- print "cpan_html_file: $cpan_html_file", "\n";
- print "wp_html_file : $wp_html_file", "\n";
- print "zh_pod_file : $zh_pod_file", "\n";
- }
- }
- close($readpod_r);
- close($zh_pod_w);
- }
- =pod
- my $changed_pre = q{<pre class="brush: perl; gutter: true">};
- my $content =do { open FH,$file;local $/;<FH> };
- $content =~ s{<span.*?>}{}sg;
- $content =~ s{</span>}{}sg;
- $content =~ s{</pre>[^<]*<pre>}{}sg;
- $content =~ s{<pre>}{$changed_pre}sg;
- print $content;
- =cut
复制代码 代码很糙,但是基本可以把pod转成的html发到wp博客了,适合懒人写wp博客,前提是文章是pod格式,效果可以参照:
http://perlfan.com/index.php/test-code/
关于自动post文章就没搞了.!
完整的脚本可以到git clone https://github.com/king-ming/POD_CN.git |
|