Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/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 {
# 本文埋め込みは、特集の場合55文字まで、連載の場合58文字まで
if ($max > 55) {
warn "本文埋め込みリストは55文字まで!(現在$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";
}
}
elsif ($elem->tag eq 'table') {
$inao .= "◆table/◆\n";
if ($elem->find('caption')) {
$inao .= "";
$inao .= $elem->find('caption')->as_trimmed_text;
$inao .= "\n"
}
for my $table ($elem->find('tr')) {
if ($table->find('th')) {
$inao .= "◆table-title◆";
for my $item ($table->find('th')){
$inao .= $item->as_trimmed_text;
$inao .= "\t";
}
}
if ($table->find('td')) {
for my $item ($table->find('td')){
$inao .= $item->as_trimmed_text;
$inao .= "\t";
}
}
chop($inao);
$inao .= "\n"
}
$inao .= "◆/table◆\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