免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3194 | 回复: 5
打印 上一主题 下一主题

perl写的一个转换pod文件成wordpress格式的html脚本 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-09-27 17:19 |只看该作者 |倒序浏览
本帖最后由 斯文牛氓 于 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

论坛徽章:
0
2 [报告]
发表于 2012-09-27 18:43 |只看该作者
perl的发行版不都自带了pod2html这个程序么

论坛徽章:
0
3 [报告]
发表于 2012-09-27 18:58 |只看该作者
回复 2# sjdy521


    仔细看看....

论坛徽章:
7
戌狗
日期:2013-12-15 20:43:38技术图书徽章
日期:2014-03-05 01:33:12技术图书徽章
日期:2014-03-15 20:31:17未羊
日期:2014-03-25 23:48:20丑牛
日期:2014-04-07 22:37:44巳蛇
日期:2014-04-11 21:58:0915-16赛季CBA联赛之青岛
日期:2016-03-17 20:36:13
4 [报告]
发表于 2012-09-28 07:04 |只看该作者
代码写de很斯文

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
5 [报告]
发表于 2012-09-28 07:26 |只看该作者
看标题我以为不是pod2html。。。
楼主竟然不知道pod2html...这个好像有了N年了。而且像pod这样的开源东西能被CPAN显示在网页上,想也能知道会有这样的工具。

楼主还可以看看pod2*

论坛徽章:
0
6 [报告]
发表于 2012-09-28 10:45 |只看该作者
回复 5# py


    只是针对wordpress的高亮插件改了下,pod2html我是很早就知道了
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP