perl/script/MarkdownをHTMLに変換

提供: dsk's note
< perl
2015年5月3日 (日) 14:18時点におけるDsk (トーク | 投稿記録)による版

移動: 案内検索
#!/usr/bin/perl
use strict;
use warnings;
use lib '/home/dsktnk/local/lib/perl5';
use Text::Markdown qw/markdown/;
use LWP::UserAgent;
 
print qq|Content-type: text/html\n\n|;
my $url="http://dsktnk.sakura.ne.jp/test.md";
my $ua = LWP::UserAgent->new;
$ua->agent('Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)');
my $request = HTTP::Request->new('GET' , "$url");
my $response = $ua->request($request);
my $data = $response->content;
my $html = markdown($data);
print $html;


#!/usr/bin/perl
use strict;
use warnings;
use CGI;
use lib '/home/dsktnk/local/lib/perl5';
use Text::Markdown qw/markdown/;
use LWP::UserAgent;
 
my $obj = new CGI;
my $url = $obj->param('url');
 
 
print qq|Content-type: text/html\n\n|;
my $ua = LWP::UserAgent->new;
$ua->agent('Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)');
 
my $request = HTTP::Request->new('GET' , "$url");
my $response = $ua->request($request);
my $data = $response->content;
my ($frontmatter,$md,$i);
 
 
# YAML frontmatter
if ($data =~/^-{3}${1}(.*?)-{3}${1}(.*?)/s){
    $frontmatter = $1;
}
 
# Markdown
my @line = split ( /\n/ , $data );
my ($ln,$line);
my $i=0;
my $item ="---";
 
foreach $ln(@line){
    if ($ln =~ /$item/){
        $i++;
    }
    elsif ($i>1){
        $line .= "$ln\n";
    }
}
 
 
my $html = markdown($line);
 
print &header_html;
print $html;
print $frontmatter;
 
 
sub header_html{
my $printlines = <<"__EOF__";
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
        "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja">
<head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <meta http-equiv="Content-Style-Type" content="text/css" />
        <meta name="author" content="dsk" />
        <link rel="stylesheet" href="index.css" type="text/css" />
</head>
<body>
__EOF__
return $printlines;
}