Skip to content

Commit

Permalink
#120 のPHP Markdown Extra footnote記法に対応
Browse files Browse the repository at this point in the history
  • Loading branch information
takahashim committed Jun 29, 2016
1 parent e4f311a commit d8cd096
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
43 changes: 43 additions & 0 deletions lib/Text/Md2Inao.pm
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use Text::Markdown::Hoedown;

use Text::Md2Inao::Director;
use Text::Md2Inao::Builder::Inao;
use Text::Md2Inao::Logger;

# デフォルトのリストスタイル
# disc: 黒丸
Expand Down Expand Up @@ -64,9 +65,51 @@ sub prepare_text_for_markdown {
## Work Around: リストの後にコードブロックが続くとだめな問題 (issue #6)
$text =~ s!([-*+] .*?)\n\n !$1\n\n \n\n !g;

## PHP Markdown Extraスタイルのfootnoteの変換 (issue #120)
$text = replace_markdown_extra_footnote($text);

return $text;
}

sub replace_markdown_extra_footnote {
my $text = shift;
my %footnotes;
my %footnotes_used;
my @lines = split /\n/, $text;

## 脚注の注釈を収集
for (@lines) {
if (m/^\[\^(\w+)\]:(.+)$/) {
my ($k, $v) = ($1, $2);
$v =~ s/^\s+//;
$v =~ s/\s+$//;
$footnotes{lc $k} = $v;
$footnotes_used{lc $k} = 0;
s/^.+$//;
}
}

## 脚注をreplace
for (@lines) {
if (m/\[\^(\w+)\]/) {
my ($k) = ($1);
my $v = $footnotes{lc $k};
if ($v) {
s/\[\^$k\]/(注:$v)/g;
$footnotes_used{lc $k} = 1;
} else {
## !!! error
log warn => "脚注'" . $k . "'が見つかりません";
}
}
}
my @unused = grep { !$footnotes_used{$_} } keys %footnotes_used;
if (@unused) {
log warn => "脚注'" . join(', ', @unused) . "'が使われていません";
}
return join "\n", @lines;
}

sub prepare_html_for_inao {
my $html = shift;
## Work Around: リスト周りと inao 記法の相性が悪い
Expand Down
23 changes: 23 additions & 0 deletions t/07_footnote.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use utf8;
use Test::Base;
use Text::Md2Inao::TestHelper;

plan tests => 1 * blocks;
run_is in => 'expected';

__END__
=== PHP Markdown Extra style footnote
--- in md2inao
通常の本文[^1]通常の本文
[^1]: 注釈ですよ。_イタリック_
--- expected
通常の本文◆注/◆注釈ですよ。◆i/◆イタリック◆/i◆◆/注◆通常の本文
===
--- in md2inao
通常の本文[^1]通常の本文
[^1]: 注釈ですよ。
--- expected
通常の本文◆注/◆注釈ですよ。◆/注◆通常の本文

0 comments on commit d8cd096

Please sign in to comment.