Chinaunix

标题: perl写的一个转换pod文件成wordpress格式的html脚本 [打印本页]

作者: 斯文牛氓    时间: 2012-09-27 17:19
标题: perl写的一个转换pod文件成wordpress格式的html脚本
本帖最后由 斯文牛氓 于 2012-09-27 17:21 编辑

烂博客发文章不喜欢在线编辑,于是自己动手,丰衣足食,本菜鸟写了个pod2html的脚本,代码如下:
  1. use strict;
  2. use warnings;
  3. use Data::Dumper;
  4. use Pod::Html;
  5. use FileHandle;
  6. use Getopt::Long;
  7. use Carp;
  8. use HTML::Entities;
  9. use File::Basename;
  10. use File::Spec;
  11. use Cwd qw(abs_path);
  12. use open ':std', ':encoding(UTF-8)';
  13. use open ':encoding(utf8)';

  14. my $module_list;
  15. my $is_raw_zh;

  16. GetOptions( "m:s" => \$module_list, 'c:s' => \$is_raw_zh );

  17. if ( !$module_list ) {
  18.     Carp::croak(
  19.         "you must defined a module list like \n",
  20.         "$0 -m File::Spec,File::Basename\n"
  21.     );
  22. }

  23. #my $changed_pre = q{<pre class="brush: perl; gutter: true">};
  24. # wp-code-highlight prettyprint
  25. my $changed_pre = q{<pre class="wp-code-highlight prettyprint">};
  26. my $pod_path    = abs_path( dirname(__FILE__) ) . "/../pod";
  27. my $html_path   = abs_path( dirname(__FILE__) ) . "/../html";

  28. for my $module_name ( split( ",", $module_list ) ) {
  29.     $module_name =~ s{::}{-}g;
  30.     my $per_pod_file = File::Spec->catfile( $pod_path, $module_name );

  31.     if ( !-e $per_pod_file ) {
  32.         Carp::croak("this module ${module_name}'s pod is not exists\n");
  33.     }

  34.     # init file handle
  35.     #    my $readpod_r = new FileHandle($per_pod_file);
  36.     open( my $readpod_r, "<", $per_pod_file );
  37.     my $cpan_html_file =
  38.       File::Spec->catfile( $html_path, $module_name . ".html" );
  39.     my $zh_pod_file =
  40.       File::Spec->catfile( $pod_path, $module_name . ".zh.pod" );
  41.     my $wp_html_file = $cpan_html_file . "_wp.html";

  42.     #    my $zh_pod_w     = new FileHandle( ">" . $zh_pod_file )
  43.     #      or Carp::croak("creat zh pod file failed");
  44.     open( my $zh_pod_w, ">", $cpan_html_file );
  45.     goto RAW_ZH_POD if $is_raw_zh;

  46.   READ_TRANSFERED_FILE: {
  47.         while ( my $line = <$readpod_r> ) {

  48.             #chomp($line);
  49.             if ( $line =~ m/^=head1/xis ) {
  50.                 while ( $line = <$readpod_r> ) {
  51.                     if ( $line =~ m/^zh:(.*) # match zh line/xis ) {
  52.                         print $zh_pod_w $1;
  53.                     }
  54.                     elsif ( $line =~ m{^(?:\t|\s) # match code block}xis ) {
  55.                         print $zh_pod_w $line;
  56.                     }
  57.                 }
  58.             }
  59.         }
  60.     }

  61.   RAW_ZH_POD: {
  62.         last unless $is_raw_zh;
  63.         my $raw_pod = do { local $/; <$readpod_r> };
  64.         print $zh_pod_w $raw_pod;
  65.     }

  66.   CONVERT: {
  67.         if ( -s $zh_pod_file > 0 ) {
  68.             pod2html(
  69.                 "--infile=" . $zh_pod_file,
  70.                 "--outfile=" . $cpan_html_file,
  71.                 "--title=" . $module_name,
  72.             );

  73.             # substitute for wp format html document
  74.             my $wp_pre = q{<pre class="brush: perl; gutter: true">};
  75.             open( my $cpan_html_r, "<", $cpan_html_file )
  76.               or Carp::croak("open file failed $@");
  77.             open( my $wp_html_w, ">", $wp_html_file )
  78.               or Carp::croak("open wp html failed\n");

  79.             do {
  80.                 local $/ = undef;
  81.                 my $content = <$cpan_html_r>;
  82.                 $content =~ s{<span.*?>}{}sg;
  83.                 $content =~ s{</span>}{}sg;
  84.                 $content =~ s{</pre>[^<]*<pre>}{}sg;
  85.                 $content =~ s{<pre>}{$changed_pre}sg;
  86. #                $content =~ s{&lt;}{<}g;
  87. #                $content =~ s{&gt;}{>}g;
  88.                 $content=~ s{&amp;gt;}{>}g;
  89.                 print $wp_html_w $content, "\n";
  90.             };
  91.             close($wp_html_w);
  92.             close($cpan_html_r);

  93.             print "-------------------------", "\n";
  94.             print "All Convert are finished\n";
  95.             print "Convert Output File are:",        "\n";
  96.             print "cpan_html_file: $cpan_html_file", "\n";
  97.             print "wp_html_file  : $wp_html_file",   "\n";
  98.             print "zh_pod_file   : $zh_pod_file",    "\n";
  99.         }
  100.     }
  101.     close($readpod_r);
  102.     close($zh_pod_w);
  103. }

  104. =pod
  105. my $changed_pre = q{<pre class="brush: perl; gutter: true">};  
  106. my $content =do { open FH,$file;local $/;<FH> };
  107. $content =~ s{<span.*?>}{}sg;
  108. $content =~ s{</span>}{}sg;
  109. $content =~ s{</pre>[^<]*<pre>}{}sg;
  110. $content =~ s{<pre>}{$changed_pre}sg;

  111. print $content;
  112. =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