Skip to content

Commit

Permalink
Add test for footnotes after every paragraph or not
Browse files Browse the repository at this point in the history
  • Loading branch information
tzi committed Mar 16, 2014
1 parent 2935844 commit 5235b9f
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 11 deletions.
42 changes: 33 additions & 9 deletions src/Markdownify/Converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
/**
* default configuration
*/
define('MDFY_LINKS_EACH_PARAGRAPH', false);
define('MDFY_BODYWIDTH', false);
define('MDFY_KEEPHTML', true);

Expand Down Expand Up @@ -68,11 +67,15 @@ class Converter
protected $minBodyWidth = 25;

/**
* display links after each paragraph
* position where the link reference will be displayed
*
*
* @var bool
* @var int
*/
protected $linksAfterEachParagraph = false;
protected $linkPosition;
const LINK_AFTER_CONTENT = 0;
const LINK_AFTER_PARAGRAPH = 1;
const LINK_IN_PARAGRAPH = 2;

/**
* stores current buffers
Expand Down Expand Up @@ -220,17 +223,16 @@ class Converter
/**
* constructor, set options, setup parser
*
* @param bool $linksAfterEachParagraph wether or not to flush stacked links after each paragraph
* defaults to false
* @param int $linkPosition define the position of links
* @param int $bodyWidth wether or not to wrap the output to the given width
* defaults to false
* @param bool $keepHTML wether to keep non markdownable HTML or to discard it
* defaults to true (HTML will be kept)
* @return void
*/
public function __construct($linksAfterEachParagraph = MDFY_LINKS_EACH_PARAGRAPH, $bodyWidth = MDFY_BODYWIDTH, $keepHTML = MDFY_KEEPHTML)
public function __construct($linkPosition = self::LINK_AFTER_CONTENT, $bodyWidth = MDFY_BODYWIDTH, $keepHTML = MDFY_KEEPHTML)
{
$this->linksAfterEachParagraph = $linksAfterEachParagraph;
$this->linkPosition = $linkPosition;
$this->keepHTML = $keepHTML;

if ($bodyWidth > $this->minBodyWidth) {
Expand Down Expand Up @@ -269,6 +271,28 @@ public function parseString($html)
return $this->output;
}

/**
* set the position where the link reference will be displayed
*
* @param int $linkPosition
* @return void
*/
public function setLinkPosition($linkPosition)
{
$this->linkPosition = $linkPosition;
}

/**
* set keep HTML tags which cannot be converted to markdown
*
* @param bool $linkPosition
* @return void
*/
public function setKeepHTML($keepHTML)
{
$this->keepHTML = $keepHTMLl;
}

/**
* iterate through the nodes and decide what we
* shall do with the current node
Expand Down Expand Up @@ -325,7 +349,7 @@ protected function parse()
}
$func = 'handleTag_' . $this->parser->tagName;
$this->$func();
if ($this->linksAfterEachParagraph && $this->parser->isBlockElement && !$this->parser->isStartTag && empty($this->parser->openTags)) {
if ($this->linkPosition == self::LINK_AFTER_PARAGRAPH && $this->parser->isBlockElement && !$this->parser->isStartTag && empty($this->parser->openTags)) {
$this->flushFootnotes();
}
if (!$this->parser->isStartTag) {
Expand Down
2 changes: 1 addition & 1 deletion src/Markdownify/ConverterExtra.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class ConverterExtra extends Converter
/**
* constructor, see Markdownify::Markdownify() for more information
*/
public function __construct($linksAfterEachParagraph = MDFY_LINKS_EACH_PARAGRAPH, $bodyWidth = MDFY_BODYWIDTH, $keepHTML = MDFY_KEEPHTML)
public function __construct($linksAfterEachParagraph = self::LINK_AFTER_CONTENT, $bodyWidth = MDFY_BODYWIDTH, $keepHTML = MDFY_KEEPHTML)
{
parent::__construct($linksAfterEachParagraph, $bodyWidth, $keepHTML);

Expand Down
30 changes: 29 additions & 1 deletion test/Test/Markdownify/ConverterTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,11 @@ public function providerCodeConversion()
/**
* @dataProvider providerLinkConversion
*/
public function testLinkConversion($html, $md)
public function testLinkConversion($html, $md, $linkPosition = null)
{
if ($linkPosition !== null) {
$this->converter->setLinkPosition($linkPosition);
}
$this->assertEquals($md, $this->converter->parseString($html));
}

Expand All @@ -227,6 +230,31 @@ public function providerLinkConversion()
$data['url-empty']['html'] = '<p><a href="">This link</a>.</p>';
$data['url-empty']['md'] = '[This link]().';

// Multiple paragraph link
$data['url-multiple-1']['html'] =
'<p>This is <a href="http://example1.com/" title="Title">an example</a> inline link.</p>
<p>This is <a href="http://example2.com/" title="Title">another example</a> inline link.</p>';
$data['url-multiple-1']['md'] = 'This is [an example][1] inline link.
This is [another example][2] inline link.
[1]: http://example1.com/ "Title"
[2]: http://example2.com/ "Title"';
$data['url-multiple-1']['linkPosition'] = Converter::LINK_AFTER_CONTENT;

// Multiple paragraph link
$data['url-multiple-2']['html'] =
'<p>This is <a href="http://example1.com/" title="Title">an example</a> inline link.</p>
<p>This is <a href="http://example2.com/" title="Title">another example</a> inline link.</p>';
$data['url-multiple-2']['md'] = 'This is [an example][1] inline link.
[1]: http://example1.com/ "Title"
This is [another example][2] inline link.
[2]: http://example2.com/ "Title"';
$data['url-multiple-2']['linkPosition'] = Converter::LINK_AFTER_PARAGRAPH;

// Direct link
$data['url-direct']['html'] = '<p><a href="http://example.com">http://example.com</a>.</p>';
$data['url-direct']['md'] = '<http://example.com>.';
Expand Down

0 comments on commit 5235b9f

Please sign in to comment.