Chinaunix
标题:
perl写的一个转换pod文件成wordpress格式的html脚本
[打印本页]
作者:
斯文牛氓
时间:
2012-09-27 17:19
标题:
perl写的一个转换pod文件成wordpress格式的html脚本
本帖最后由 斯文牛氓 于 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
作者:
sjdy521
时间:
2012-09-27 18:43
perl的发行版不都自带了pod2html这个程序么
作者:
斯文牛氓
时间:
2012-09-27 18:58
回复
2#
sjdy521
仔细看看....
作者:
rubyish
时间:
2012-09-28 07:04
代码写de很斯文
作者:
py
时间:
2012-09-28 07:26
看标题我以为不是pod2html。。。
楼主竟然不知道pod2html...这个好像有了N年了。而且像pod这样的开源东西能被CPAN显示在网页上,想也能知道会有这样的工具。
楼主还可以看看pod2*
作者:
斯文牛氓
时间:
2012-09-28 10:45
回复
5#
py
只是针对wordpress的高亮插件改了下,pod2html我是很早就知道了
欢迎光临 Chinaunix (http://bbs.chinaunix.net/)
Powered by Discuz! X3.2