From 9eacd79dfb10d5044ae2c6396167260e85aa5db1 Mon Sep 17 00:00:00 2001 From: Thomas ZILLIOX Date: Sun, 16 Mar 2014 01:33:08 +0100 Subject: [PATCH] Allow to display link reference in paragraph, without footnotes --- src/Markdownify/Converter.php | 39 +++++++++++++++++---- test/Test/Markdownify/ConverterTestCase.php | 19 ++++++++++ 2 files changed, 52 insertions(+), 6 deletions(-) diff --git a/src/Markdownify/Converter.php b/src/Markdownify/Converter.php index ece2347..e674275 100644 --- a/src/Markdownify/Converter.php +++ b/src/Markdownify/Converter.php @@ -443,13 +443,24 @@ protected function flushFootnotes() } else { $this->out("\n", true); } - $this->out(' [' . $tag['linkID'] . ']: ' . $tag['href'] . (isset($tag['title']) ? ' "' . $tag['title'] . '"' : ''), true); + $this->out(' [' . $tag['linkID'] . ']: ' . $this->getLinkReference($tag), true); $tag['unstacked'] = true; $this->footnotes[$k] = $tag; } } } + /** + * return formated link reference + * + * @param array $tag + * @return string link reference + */ + protected function getLinkReference($tag) + { + return $tag['href'] . (isset($tag['title']) ? ' "' . $tag['title'] . '"' : ''); + } + /** * flush enqued linebreaks * @@ -769,6 +780,11 @@ protected function handleTag_a_converter($tag, $buffer) # [1]: mailto:mail@example.com Title $tag['href'] = 'mailto:' . $bufferDecoded; } + + if ($this->linkPosition == self::LINK_IN_PARAGRAPH) { + return '[' . $buffer . '](' . $this->getLinkReference($tag) . ')'; + } + # [This link][id] foreach ($this->footnotes as $tag2) { if ($tag2['href'] == $tag['href'] && $tag2['title'] === $tag['title']) { @@ -780,7 +796,6 @@ protected function handleTag_a_converter($tag, $buffer) $tag['linkID'] = count($this->footnotes) + 1; array_push($this->footnotes, $tag); } - //var_dump($tag); return '[' . $buffer . '][' . $tag['linkID'] . ']'; } @@ -810,7 +825,7 @@ protected function handleTag_img() if (empty($this->parser->tagAttributes['src'])) { # support for "empty" images... dunno if this is really needed - # but there are some testcases which do that... + # but there are some test cases which do that... if (!empty($this->parser->tagAttributes['title'])) { $this->parser->tagAttributes['title'] = ' ' . $this->parser->tagAttributes['title'] . ' '; } @@ -821,7 +836,18 @@ protected function handleTag_img() $this->parser->tagAttributes['src'] = $this->decode($this->parser->tagAttributes['src']); } - # [This link][id] + $out = '![' . $this->parser->tagAttributes['alt'] . ']'; + if ($this->linkPosition == self::LINK_IN_PARAGRAPH) { + $out .= '(' . $this->parser->tagAttributes['src']; + if ($this->parser->tagAttributes['title']) { + $out .= ' "' . $this->parser->tagAttributes['title'] . '"'; + } + $out .= ')'; + $this->out($out, true); + return ; + } + + # ![This image][id] $link_id = false; if (!empty($this->footnotes)) { foreach ($this->footnotes as $tag) { @@ -842,8 +868,9 @@ protected function handleTag_img() ); array_push($this->footnotes, $tag); } - - $this->out('![' . $this->parser->tagAttributes['alt'] . '][' . $link_id . ']', true); + $out .= '[' . $link_id . ']'; + + $this->out($out, true); } /** diff --git a/test/Test/Markdownify/ConverterTestCase.php b/test/Test/Markdownify/ConverterTestCase.php index b8133d3..3bb1bbd 100644 --- a/test/Test/Markdownify/ConverterTestCase.php +++ b/test/Test/Markdownify/ConverterTestCase.php @@ -255,6 +255,15 @@ public function providerLinkConversion() [2]: http://example2.com/ "Title"'; $data['url-multiple-2']['linkPosition'] = Converter::LINK_AFTER_PARAGRAPH; + // Multiple paragraph link + $data['url-multiple-2']['html'] = + '

This is an example inline link.

+

This is another example inline link.

'; + $data['url-multiple-2']['md'] = 'This is [an example](http://example1.com/ "Title") inline link. + +This is [another example](http://example2.com/ "Title") inline link.'; + $data['url-multiple-2']['linkPosition'] = Converter::LINK_IN_PARAGRAPH; + // Direct link $data['url-direct']['html'] = '

http://example.com.

'; $data['url-direct']['md'] = '.'; @@ -290,12 +299,22 @@ public function providerLinkConversion() $data['image']['md'] = '![Alt text][1] [1]: /path/to/img.jpg'; + + // Image with src + alt attributes in content + $data['image--in']['html'] = 'Alt text'; + $data['image--in']['md'] = '![Alt text](/path/to/img.jpg)'; + $data['image--in']['linkPosition'] = Converter::LINK_IN_PARAGRAPH; // Image with src + alt + title attributes $data['image-title']['html'] = 'Alt text'; $data['image-title']['md'] = '![Alt text][1] [1]: /path/to/img.jpg "Optional title attribute"'; + + // Image with src + alt + title attributes in content + $data['image-title--in']['html'] = 'Alt text'; + $data['image-title--in']['md'] = '![Alt text](/path/to/img.jpg "Optional title attribute")'; + $data['image-title--in']['linkPosition'] = Converter::LINK_IN_PARAGRAPH; // Escaped image $data['image-escape']['html'] = '![This link](/path)';