diff --git a/CHANGELOG.md b/CHANGELOG.md index 827d5e1..ac25532 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,10 @@ Version 0.9.4-dev * The pretty printer now prints namespaces in semicolon-style if possible (i.e. if the file does not contain a global namespace declaration). +* Added `prettyPrintFile(array $stmts)` method which will pretty print a file of statements including the opening + `` at the start and end + of files using inline HTML. + Version 0.9.3 (22.11.2012) -------------------------- diff --git a/lib/PHPParser/PrettyPrinter/Default.php b/lib/PHPParser/PrettyPrinter/Default.php index 6439c52..ad2b4b7 100644 --- a/lib/PHPParser/PrettyPrinter/Default.php +++ b/lib/PHPParser/PrettyPrinter/Default.php @@ -676,10 +676,7 @@ class PHPParser_PrettyPrinter_Default extends PHPParser_PrettyPrinterAbstract } public function pStmt_InlineHTML(PHPParser_Node_Stmt_InlineHTML $node) { - return '?>' . $this->pNoIndent( - ("\n" === $node->value[0] || "\r" === $node->value[0] ? "\n" : '') - . $node->value - ) . '' . $this->pNoIndent("\n" . $node->value) . 'preprocessNodes($nodes); + public function prettyPrint(array $stmts) { + $this->preprocessNodes($stmts); - return str_replace("\n" . $this->noIndentToken, "\n", $this->pStmts($nodes, false)); + return str_replace("\n" . $this->noIndentToken, "\n", $this->pStmts($stmts, false)); } /** @@ -93,6 +93,26 @@ abstract class PHPParser_PrettyPrinterAbstract return str_replace("\n" . $this->noIndentToken, "\n", $this->p($node)); } + /** + * Pretty prints a file of statements (includes the opening prettyPrint($stmts)); + + $p = preg_replace('/^\?>\n?/', '', $p, -1, $count); + $p = preg_replace('/<\?php$/', '', $p); + + if (!$count) { + $p = " - */ - public function testPrettyPrint($name, $code, $dump) { + protected function doTestPrettyPrintMethod($method, $name, $code, $dump) { $parser = new PHPParser_Parser(new PHPParser_Lexer_Emulative); $prettyPrinter = new PHPParser_PrettyPrinter_Default; $stmts = $parser->parse($code); $this->assertEquals( $this->canonicalize($dump), - $this->canonicalize($prettyPrinter->prettyPrint($stmts)), + $this->canonicalize($prettyPrinter->$method($stmts)), $name ); } + /** + * @dataProvider provideTestPrettyPrint + * @covers PHPParser_PrettyPrinter_Default + */ + public function testPrettyPrint($name, $code, $dump) { + $this->doTestPrettyPrintMethod('prettyPrint', $name, $code, $dump); + } + + /** + * @dataProvider provideTestPrettyPrintFile + * @covers PHPParser_PrettyPrinter_Default + */ + public function testPrettyPrintFile($name, $code, $dump) { + $this->doTestPrettyPrintMethod('prettyPrintFile', $name, $code, $dump); + } + public function provideTestPrettyPrint() { return $this->getTests(dirname(__FILE__) . '/../../code/prettyPrinter', 'test'); } + + public function provideTestPrettyPrintFile() { + return $this->getTests(dirname(__FILE__) . '/../../code/prettyPrinter', 'file-test'); + } } \ No newline at end of file diff --git a/test/code/prettyPrinter/inlineHTMLandPHPtest.file-test b/test/code/prettyPrinter/inlineHTMLandPHPtest.file-test new file mode 100644 index 0000000..74a2685 --- /dev/null +++ b/test/code/prettyPrinter/inlineHTMLandPHPtest.file-test @@ -0,0 +1,52 @@ +File containing both inline HTML and PHP +----- +HTML + +HTML +----- + +HTML +----- +HTML + +HTML +----- +HTML + +HTML +----- +HTML + +HTML + +HTML +----- +HTML + +HTML + +HTML \ No newline at end of file diff --git a/test/code/prettyPrinter/onlyInlineHTML.file-test b/test/code/prettyPrinter/onlyInlineHTML.file-test new file mode 100644 index 0000000..0e2d4f7 --- /dev/null +++ b/test/code/prettyPrinter/onlyInlineHTML.file-test @@ -0,0 +1,11 @@ +File containing only inline HTML +----- +Hallo World +Foo Bar +Bar Foo +World Hallo +----- +Hallo World +Foo Bar +Bar Foo +World Hallo \ No newline at end of file diff --git a/test/code/prettyPrinter/onlyPHP.file-test b/test/code/prettyPrinter/onlyPHP.file-test new file mode 100644 index 0000000..6b7ec49 --- /dev/null +++ b/test/code/prettyPrinter/onlyPHP.file-test @@ -0,0 +1,11 @@ +File containing only PHP +----- +