diff --git a/library/Zepto/FileLoader/MarkdownLoader.php b/library/Zepto/FileLoader/MarkdownLoader.php index 5af5600..68e7b71 100644 --- a/library/Zepto/FileLoader/MarkdownLoader.php +++ b/library/Zepto/FileLoader/MarkdownLoader.php @@ -7,6 +7,7 @@ * @link http://https://github.com/hassankhan/Zepto * @license http://opensource.org/licenses/MIT * @version 0.2 + * @deprecated Use \Zepto\FileLoader\PageLoader instead */ namespace Zepto\FileLoader; @@ -15,32 +16,41 @@ class MarkdownLoader extends \Zepto\FileLoader { /** * An object which parses Markdown to HTML - * @var Michelf\MarkdownInterface + * + * @var \Michelf\MarkdownInterface */ protected $parser; - function __construct(\Michelf\MarkdownInterface $parser) { + /** + * Class constructor. Sets the base path, but also the Markdown parser + * + * @param string $base_path + * @param \Michelf\MarkdownInterface $parser + */ + public function __construct($base_path, \Michelf\MarkdownInterface $parser) + { + parent::__construct($base_path); $this->parser = $parser; } /** * Basically does the same job as the superclass, except this time we run * it through a post_process() method to work some magic on it - * @param string $file_path File path - * @param string $file_extension File extension - * @return array Loaded files + * + * @param string $file_path + * @return array */ - public function load($file_path, $file_extension) + public function load($file_path) { - $files = parent::load($file_path, $file_extension); - return $this->post_process(); + return $this->post_process(parent::load($file_path)); } /** * Returns the parser object - * @return Michelf\MarkdownInterface + * + * @return \Michelf\MarkdownInterface */ - public function get_parser() + public function parser() { return $this->parser; } @@ -48,35 +58,34 @@ public function get_parser() /** * Where the magic happens, ladies and gentlemen. An array with the keys set to the name of * the file and the values set to the processed Markdown text - * @codeCoverageIgnore + * * @return array + * @codeCoverageIgnore */ - private function post_process() + protected function post_process($content) { // Create array to store processed files $processed_files = array(); // Loop through files and process each one, adding them to the array - foreach ($this['file_cache'] as $file_name => $file) { + foreach ($content as $file_name => $file) { $processed_files[$file_name] = array( 'meta' => $this->parse_meta($file), 'content' => $this->parse_content($file) ); } - // Update local cache - $this['file_cache'] = $processed_files; - return $processed_files; } /** * Parses Markdown file headers for metadata - * @codeCoverageIgnore + * * @param string $file The loaded Markdown file as a string * @return array An array containing file metadata + * @codeCoverageIgnore */ - private function parse_meta($file) + protected function parse_meta($file) { // Grab meta section between '/* ... */' in the content file preg_match_all('#/\*(.*?)\*/#s', $file, $meta); @@ -93,11 +102,12 @@ private function parse_meta($file) /** * Parses Markdown file for content - * @codeCoverageIgnore + * * @param string $file The loaded Markdown file as a string * @return string The parsed string as HTML + * @codeCoverageIgnore */ - private function parse_content($file) + protected function parse_content($file) { $content = preg_replace('#/\*.+?\*/#s', '', $file); return $this->parser->defaultTransform($content); diff --git a/tests/Zepto/FileLoader/MarkdownLoaderTest.php b/tests/Zepto/FileLoader/MarkdownLoaderTest.php index 7027375..2ab9515 100644 --- a/tests/Zepto/FileLoader/MarkdownLoaderTest.php +++ b/tests/Zepto/FileLoader/MarkdownLoaderTest.php @@ -8,26 +8,26 @@ class MarkdownLoaderTest extends \PHPUnit_Framework_TestCase { /** * @covers Zepto\FileLoader\MarkdownLoader::__construct() - * @covers Zepto\FileLoader\MarkdownLoader::get_parser() + * @covers Zepto\FileLoader\MarkdownLoader::parser() */ public function testConstructWithMarkdown() { $parser = $this->getMock('Michelf\Markdown', array('defaultTransform')); - $loader = new MarkdownLoader($parser); + $loader = new MarkdownLoader(ROOT_DIR . 'content', $parser); - $this->assertInstanceOf('Michelf\Markdown', $loader->get_parser()); + $this->assertInstanceOf('Michelf\Markdown', $loader->parser()); } /** * @covers Zepto\FileLoader\MarkdownLoader::__construct() - * @covers Zepto\FileLoader\MarkdownLoader::get_parser() + * @covers Zepto\FileLoader\MarkdownLoader::parser() */ public function testConstructWithMarkdownExtra() { $parser = $this->getMock('Michelf\MarkdownExtra', array('defaultTransform')); - $loader = new MarkdownLoader($parser); + $loader = new MarkdownLoader(ROOT_DIR . 'content', $parser); - $this->assertInstanceOf('Michelf\MarkdownExtra', $loader->get_parser()); + $this->assertInstanceOf('Michelf\MarkdownExtra', $loader->parser()); } /** @@ -35,8 +35,11 @@ public function testConstructWithMarkdownExtra() */ public function testLoad() { - $parsed_text = "

Error 404

" . PHP_EOL. PHP_EOL - . "

Woops. Looks like this page doesn't exist.

". PHP_EOL; + $parsed_text = '

This is a Sub Page

' . PHP_EOL . PHP_EOL + . '

This is page.md in the "sub" folder.

' . PHP_EOL . PHP_EOL + . '

Lorem ipsum dolor sit amet, consectetur adipiscing elit.

' . PHP_EOL . PHP_EOL + . '

Donec ultricies tristique nulla et mattis.

' . PHP_EOL . PHP_EOL + . '

Phasellus id massa eget nisl congue blandit sit amet id ligula.

' . PHP_EOL; // Create a stub for the SomeClass class. $parser = $this->getMock('Michelf\MarkdownInterface', array('defaultTransform', 'transform')); @@ -45,68 +48,17 @@ public function testLoad() ->method('defaultTransform') ->will($this->returnValue($parsed_text)); - $loader = new MarkdownLoader($parser); + $loader = new MarkdownLoader(ROOT_DIR . 'content', $parser); - $files['404.md'] = array( + $expected['sub/page.md'] = array( 'meta' => array( - 'title' => 'Error 404', - 'robots' => 'noindex,nofollow' + 'title' => 'Sub Page', ), 'content' => $parsed_text ); - $result = $loader->load(ROOT_DIR . 'content/404.md', array('md')); - $this->assertEquals($files, $result); - } - - /** - * @covers Zepto\FileLoader\MarkdownLoader::load() - */ - public function testLoadMultipleFiles() - { - $index_content = "

This is a Sub Page Index

" . PHP_EOL . PHP_EOL - . "

This is index.md in the 'sub' folder.

" . PHP_EOL . PHP_EOL - . "

Lorem ipsum dolor sit amet, consectetur adipiscing elit.

" . PHP_EOL . PHP_EOL - . "

Donec ultricies tristique nulla et mattis.

" . PHP_EOL. PHP_EOL - . "

Phasellus id massa eget nisl congue blandit sit amet id ligula.

" . PHP_EOL; - - $sub_page_content = "

This is a Sub Page

" . PHP_EOL . PHP_EOL - . "

This is page.md in the 'sub' folder.

" . PHP_EOL . PHP_EOL - . "

Lorem ipsum dolor sit amet, consectetur adipiscing elit.

" . PHP_EOL . PHP_EOL - . "

Donec ultricies tristique nulla et mattis.

" . PHP_EOL . PHP_EOL - . "

Phasellus id massa eget nisl congue blandit sit amet id ligula.

". PHP_EOL; - - // Create a stub for the SomeClass class. - $parser = $this->getMock('Michelf\MarkdownInterface', array('defaultTransform', 'transform')); - - $parser::staticExpects($this->at(0)) - ->method('defaultTransform') - ->with($this->anything()) - ->will($this->returnValue($index_content)); - - $parser::staticExpects($this->at(1)) - ->method('defaultTransform') - ->with($this->anything()) - ->will($this->returnValue($sub_page_content)); - - $loader = new MarkdownLoader($parser); - - $files['sub/index.md'] = array( - 'meta' => array( - 'title' => 'Sub Page Index' - ), - 'content' => $index_content - ); - - $files['sub/page.md'] = array( - 'meta' => array( - 'title' => 'Sub Page' - ), - 'content' => $sub_page_content - ); - - $result = $loader->load(ROOT_DIR . 'content/sub', array('md')); - $this->assertEquals($files, $result); + $actual = $loader->load('sub/page.md'); + $this->assertEquals($expected, $actual); } }