Skip to content

Instantly share code, notes, and snippets.

@inao
Forked from typester/markdown2inao.pl
Created June 24, 2010 07:16
Show Gist options
  • Save inao/451106 to your computer and use it in GitHub Desktop.
Save inao/451106 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
use strict;
use warnings;
use Pod::Usage;
use Text::Markdown 'markdown';
use HTML::TreeBuilder;
use List::Util 'max';
my $infile = $ARGV[0]
or pod2usage(-1);
open my $fh, '<', $infile or die $!;
my $text = do { local $/; <$fh> };
close $fh;
my $html = markdown($text);
my $tree = HTML::TreeBuilder->new;
$tree->parse_content(\$html);
my $inao = q[];
my $body = $tree->find('body');
for my $elem ($body->content_list) {
if ($elem->tag =~ /^h(\d+)/) {
my $level = $1;
$inao .= '■' x $level;
$inao .= $elem->as_trimmed_text;
$inao .= "\n";
}
elsif ($elem->tag eq 'p') {
my $p = q[];
for my $inline ($elem->content_list) {
if (ref $inline eq '') {
# (注:)は脚注としてあつかう
$inline =~ s!\(注:(.+?)\)!◆注/◆$1◆/注◆!gs;
$p .= $inline;
}
elsif ($inline->tag eq 'code') {
$p .= '◆cmd/◆';
$p .= $inline->as_trimmed_text;
$p .= '◆/cmd◆';
}
elsif ($inline->tag eq 'strong') {
$p .= '◆b/◆';
$p .= $inline->as_trimmed_text;
$p .= '◆/b◆';
}
elsif ($inline->tag eq 'em') {
$p .= '◆i/◆';
$p .= $inline->as_trimmed_text;
$p .= '◆/i◆';
}
}
if ($p !~ /^[\s ]+$/) {
$inao .= "$p\n";
}
}
elsif ($elem->tag eq 'pre') {
my $code = $elem->find('code');
my $text = $code->as_text;
# asciiのみカウント。日本語は目視でがんばる
my $max = max map { length } grep /^[\x00-\x7f]*$/, split /\r?\n/, $text;
if ($text =~ /^●/) {
# キャプション付きリスト63文字まで
if ($max > 63) {
warn "リストは63文字まで!(現在$max使用):\n$text\n\n";
}
}
else {
# 本文埋め込みは58文字まで
if ($max > 58) {
warn "本文埋め込みリストは58文字まで!(現在$max使用):\n$text\n\n";
}
}
$inao .= "◆list/◆\n";
$inao .= $text;
$inao .= "◆/list◆\n";
}
elsif ($elem->tag eq 'ul') {
for my $list ($elem->find('li')) {
$inao .= '・' . $list->as_trimmed_text . "\n";
}
}
elsif ($elem->tag eq 'ol') {
my $i = 0;
for my $list ($elem->find('li')) {
$inao .= '(' . ++$i .')' . $list->as_trimmed_text . "\n";
}
}
}
print $inao;
__END__
=head1 NAME
markdown2inao.pl - markdown to inao converter
=head1 SYNOPSIS
markdown2inao.pl input.markdown.txt
=cut
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment