Skip to content
This repository has been archived by the owner on Nov 2, 2018. It is now read-only.

Commit

Permalink
Rewritten Zepto\FileLoader\MarkdownLoader, updated test classes. …
Browse files Browse the repository at this point in the history
…Marked as deprecated, hopefully can use pages in the future.
  • Loading branch information
hassankhan committed Feb 10, 2014
1 parent 2892e3d commit f50a8ee
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 84 deletions.
50 changes: 30 additions & 20 deletions library/Zepto/FileLoader/MarkdownLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -15,68 +16,76 @@ 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;
}

/**
* 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);
Expand All @@ -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);
Expand Down
80 changes: 16 additions & 64 deletions tests/Zepto/FileLoader/MarkdownLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,38 @@ 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());
}

/**
* @covers Zepto\FileLoader\MarkdownLoader::load
*/
public function testLoad()
{
$parsed_text = "<h1>Error 404</h1>" . PHP_EOL. PHP_EOL
. "<p>Woops. Looks like this page doesn't exist.</p>". PHP_EOL;
$parsed_text = '<h2>This is a Sub Page</h2>' . PHP_EOL . PHP_EOL
. '<p>This is page.md in the "sub" folder.</p>' . PHP_EOL . PHP_EOL
. '<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>' . PHP_EOL . PHP_EOL
. '<p>Donec ultricies tristique nulla et mattis.</p>' . PHP_EOL . PHP_EOL
. '<p>Phasellus id massa eget nisl congue blandit sit amet id ligula.</p>' . PHP_EOL;

// Create a stub for the SomeClass class.
$parser = $this->getMock('Michelf\MarkdownInterface', array('defaultTransform', 'transform'));
Expand All @@ -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 = "<h2>This is a Sub Page Index</h2>" . PHP_EOL . PHP_EOL
. "<p>This is index.md in the 'sub' folder.</p>" . PHP_EOL . PHP_EOL
. "<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>" . PHP_EOL . PHP_EOL
. "<p>Donec ultricies tristique nulla et mattis.</p>" . PHP_EOL. PHP_EOL
. "<p>Phasellus id massa eget nisl congue blandit sit amet id ligula.</p>" . PHP_EOL;

$sub_page_content = "<h2>This is a Sub Page</h2>" . PHP_EOL . PHP_EOL
. "<p>This is page.md in the 'sub' folder.</p>" . PHP_EOL . PHP_EOL
. "<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>" . PHP_EOL . PHP_EOL
. "<p>Donec ultricies tristique nulla et mattis.</p>" . PHP_EOL . PHP_EOL
. "<p>Phasellus id massa eget nisl congue blandit sit amet id ligula.</p>". 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);
}

}

0 comments on commit f50a8ee

Please sign in to comment.