Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply fixes from StyleCI #4

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ php:
- 5.4
- 5.5
- 5.6
- hhvm
- nightly
- 7.0
- 7.1
14 changes: 7 additions & 7 deletions examples/example.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@

use Ahc\HtmlUp;

require dirname(__DIR__).'/vendor/autoload.php';
require dirname(__DIR__) . '/vendor/autoload.php';

$markdown = file_get_contents(dirname(__DIR__).'/readme.md');
$markdown = file_get_contents(dirname(__DIR__) . '/readme.md');

/* You can use any of the three usage methods below */

# usage 1
// usage 1
// $h = new HtmlUp($markdown);
// echo $h->parse();
// echo $h->parse();

# usage 2
// usage 2
// $h = new HtmlUp($markdown);
// echo $h;
// echo $h;

# usage 3
// usage 3
echo new HtmlUp($markdown);
42 changes: 21 additions & 21 deletions src/HtmlUp.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ public function __construct($markdown)
{
// some normalisations
$this->Lines =
explode("\n", # the lines !
trim( # trim trailing \n
str_replace(array("\r\n", "\r"), "\n", # use standard newline
str_replace("\t", ' ', $markdown) # use 4 spaces for tab
explode("\n", // the lines !
trim( // trim trailing \n
str_replace(["\r\n", "\r"], "\n", // use standard newline
str_replace("\t", ' ', $markdown) // use 4 spaces for tab
), "\n"
)
);
Expand All @@ -51,14 +51,14 @@ public function parse()
return '';
}

$markup = '';
$nestLevel = $quoteLevel = 0;
$indent = $nextIndent = 0;
$stackList = $stackBlock = $stackTable = array();
$markup = '';
$nestLevel = $quoteLevel = 0;
$indent = $nextIndent = 0;
$stackList = $stackBlock = $stackTable = [];
$lastPointer = count($this->Lines) - 1;

while (isset($this->Lines[++$this->Pointer])) {
$line = $this->Lines[$this->Pointer];
$line = $this->Lines[$this->Pointer];
$trimmedLine = trim($line);

// flush stacks at the end of block
Expand All @@ -75,7 +75,7 @@ public function parse()

$markup .= "\n";

$inList = $inQuote = $inPara = $inHtml = null;
$inList = $inQuote = $inPara = $inHtml = null;
$nestLevel = $quoteLevel = 0;
continue;
}
Expand All @@ -86,7 +86,7 @@ public function parse()
) {
$markup .= "\n$line";
if (empty($inHtml) and
empty($this->Lines[$this->Pointer-1])
empty($this->Lines[$this->Pointer - 1])
) {
$inHtml = true;
}
Expand All @@ -98,15 +98,15 @@ public function parse()
: null;
$trimmedNextLine = $nextLine ? trim($nextLine) : null;

$indent = strlen($line) - strlen(ltrim($line));
$indent = strlen($line) - strlen(ltrim($line));
$nextIndent = $nextLine ? strlen($nextLine) - strlen(ltrim($nextLine)) : 0;

$nextMark1 = isset($trimmedNextLine[0]) ? $trimmedNextLine[0] : null;
$nextMark1 = isset($trimmedNextLine[0]) ? $trimmedNextLine[0] : null;
$nextMark12 = $trimmedNextLine ? substr($trimmedNextLine, 0, 2) : null;

// blockquote
if (preg_match('~^\s*(>+)\s+~', $line, $quoteMatch)) {
$line = substr($line, strlen($quoteMatch[0]));
$line = substr($line, strlen($quoteMatch[0]));
$trimmedLine = trim($line);
if (empty($inQuote) or $quoteLevel < strlen($quoteMatch[1])) {
$markup .= "\n<blockquote>";
Expand All @@ -116,14 +116,14 @@ public function parse()
$inQuote = true;
}

$mark1 = $trimmedLine[0];
$mark1 = $trimmedLine[0];
$mark12 = substr($trimmedLine, 0, 2);

// atx
if ($mark1 === '#') {
$level = strlen($trimmedLine) - strlen(ltrim($trimmedLine, '#'));
if ($level < 7) {
$markup .= "\n<h{$level}>".ltrim($trimmedLine, '# ')."</h{$level}>";
$markup .= "\n<h{$level}>" . ltrim($trimmedLine, '# ') . "</h{$level}>";
continue;
}
}
Expand Down Expand Up @@ -152,7 +152,7 @@ public function parse()
(($line = htmlspecialchars($this->Lines[$this->Pointer + 1])) or true) and
(($codeBlock and substr(ltrim($line), 0, 3) !== '```') or substr($line, 0, 4) === ' ')
) {
$markup .= "\n"; # @todo: donot use \n for first line
$markup .= "\n"; // @todo: donot use \n for first line
$markup .= $codeBlock ? $line : substr($line, 4);
++$this->Pointer;
}
Expand All @@ -171,7 +171,7 @@ public function parse()
}

// list
if ($ul = in_array($mark12, array('- ', '* ', '+ ')) or
if ($ul = in_array($mark12, ['- ', '* ', '+ ']) or
preg_match('/^\d+\. /', $trimmedLine)
) {
$wrapper = $ul ? 'ul' : 'ol';
Expand All @@ -182,9 +182,9 @@ public function parse()
++$nestLevel;
}

$markup .= '<li>'.ltrim($trimmedLine, '-*0123456789. ');
$markup .= '<li>' . ltrim($trimmedLine, '-*0123456789. ');

if ($ul = in_array($nextMark12, array('- ', '* ', '+ ')) or
if ($ul = in_array($nextMark12, ['- ', '* ', '+ ']) or
preg_match('/^\d+\. /', $trimmedNextLine)
) {
$wrapper = $ul ? 'ul' : 'ol';
Expand Down Expand Up @@ -249,7 +249,7 @@ public function parse()
if (empty($trimmedNextLine) or
!substr_count(trim($trimmedNextLine, '|'), '|')
) {
$inTable = null;
$inTable = null;
$stackTable[] = "</tbody>\n</table>";
}

Expand Down
10 changes: 5 additions & 5 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

error_reporting(E_ALL);

foreach (array(
dirname(__DIR__).'/vendor/autoload.php',
dirname(dirname(dirname(__DIR__))).'/autoload.php',
) as $loader) {
foreach ([
dirname(__DIR__) . '/vendor/autoload.php',
dirname(dirname(dirname(__DIR__))) . '/autoload.php',
] as $loader) {
if (is_file($loader)) {
return require $loader;
}
}

// As a last resort
require dirname(__DIR__).'/src/HtmlUp.php';
require dirname(__DIR__) . '/src/HtmlUp.php';
49 changes: 24 additions & 25 deletions tests/src/HtmlUpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ class HtmlUpTest extends PHPUnit_Framework_TestCase
{
/**
* @dataProvider dataSrc
*
*/
public function testAll()
{
list($testName, $markdown, $expectedMarkup) = func_get_args();
$actualMarkup = (string) new HtmlUp($markdown);
$actualMarkup = (string) new HtmlUp($markdown);

$this->assertion($testName, $expectedMarkup, $actualMarkup);
}
Expand All @@ -24,37 +23,37 @@ public function testAll()
* 'markdown text',
* 'expected markup text'
* )
* </code>
* </code>.
*/
public function dataSrc()
{
return array(
array(
return [
[
'Atx Header',
$this->assemble('# HelloH1', '## HelloH2'),
'<h1>HelloH1</h1><h2>HelloH2</h2>'
),
array(
'<h1>HelloH1</h1><h2>HelloH2</h2>',
],
[
'Unordered List',
$this->assemble('- Hello', '* HelloAgain'),
'<ul><li>Hello</li><li>HelloAgain</li></ul>'
),
array(
'<ul><li>Hello</li><li>HelloAgain</li></ul>',
],
[
'Ordered List',
$this->assemble('1. Hello', '2. HelloAgain'),
'<ol><li>Hello</li><li>HelloAgain</li></ol>'
),
array(
'<ol><li>Hello</li><li>HelloAgain</li></ol>',
],
[
'H8 is Paragraph',
'######## NoHeader',
'<p>######## NoHeader</p>'
),
array(
'<p>######## NoHeader</p>',
],
[
'Horizontal Rule',
$this->assemble('', '***', '', '___'),
'<hr /><hr />'
),
array(
'<hr /><hr />',
],
[
'Table',
$this->assemble('a | b', '---|---', '1 | 2', '4 | 5'),
'<table>
Expand All @@ -74,9 +73,9 @@ public function dataSrc()
<td>5</td>
</tr>
</tbody>
</table>'
),
);
</table>',
],
];
}

protected function assertion($testName, $expected, $actual)
Expand All @@ -91,8 +90,8 @@ protected function assertion($testName, $expected, $actual)
protected function normalizeMarkup($markup)
{
$markup = preg_replace(
array('/\>[^\S ]+/s', '/[^\S ]+\</s', ),
array('>', '<', ),
['/\>[^\S ]+/s', '/[^\S ]+\</s'],
['>', '<'],
$markup
);

Expand Down