「perl/script/MarkdownをHTMLに変換」の版間の差分
提供: dsk's note
< perl
細 |
細 |
||
(同じ利用者による、間の1版が非表示) | |||
16行目: | 16行目: | ||
my $html = markdown($data); | my $html = markdown($data); | ||
print $html; | print $html; | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | |||
+ | <syntaxhighlight lang="perl"> | ||
+ | #!/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; | ||
+ | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
[[Category: perl]] | [[Category: perl]] |
2015年5月4日 (月) 17:42時点における最新版
#!/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; }