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

Commit

Permalink
Rebuild content loader, parser and converter.
Browse files Browse the repository at this point in the history
WIP
  • Loading branch information
ArnaudLigny committed Sep 5, 2016
1 parent ef2217b commit d502601
Show file tree
Hide file tree
Showing 8 changed files with 254 additions and 58 deletions.
44 changes: 37 additions & 7 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,34 @@ class Config
],
'content' => [
'dir' => 'content',
'ext' => ['md', 'markdown', 'mdown', 'mkdn', 'mkd', 'text', 'txt'],
],
'frontmatter' => [
'format' => 'yaml',
],
'body' => [
'format' => 'md',
'format' => [
'text' => [
'ext' => ['text', 'txt'],
'frontmatter' => 'yaml',
],
'markdown' => [
'ext' => ['md', 'markdown', 'mdown', 'mkdn', 'mkd'],
'parser' => 'PHPoole\Parser\Parsedown',
'frontmatter' => 'yaml',
],
'textile' => [
'ext' => ['textile'],
'parser' => 'PHPoole\Parser\Textile',
'frontmatter' => 'yaml',
],
'yaml' => [
'ext' => ['yaml', 'yaml'],
'parser' => 'PHPoole\Parser\SfYaml',
],
],
'frontmatter' => [
'yaml' => [
'parser' => 'PHPoole\Parser\SfYaml',
],
'ini' => [
'parser' => 'PHPoole\Parser\Ini',
],
],
],
'static' => [
'dir' => 'static',
Expand Down Expand Up @@ -156,6 +177,15 @@ public function get($key, $default = '')
return $this->data->get($key, $default);
}

/**
* @param $key
* @return Data
*/
public function getData($key)
{
return $this->data->getData($key);
}

/**
* Set source directory.
*
Expand Down
2 changes: 1 addition & 1 deletion src/PHPoole.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class PHPoole
* @see build()
*/
protected $steps = [
'PHPoole\Step\LocateContent',
'PHPoole\Step\LoadContent',
'PHPoole\Step\CreatePages',
'PHPoole\Step\ConvertPages',
'PHPoole\Step\GeneratePages',
Expand Down
25 changes: 25 additions & 0 deletions src/Parser/Ini.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
/*
* Copyright (c) Arnaud Ligny <arnaud@ligny.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace PHPoole\Parser;

/**
* Class Ini.
*/
class Ini implements ParserInterface
{
/**
* {@inheritdoc}
*
* @return array
*/
public static function parse($string)
{
return parse_ini_string($string);
}
}
27 changes: 27 additions & 0 deletions src/Parser/Parsedown.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
/*
* Copyright (c) Arnaud Ligny <arnaud@ligny.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace PHPoole\Parser;

use ParsedownExtra;

/**
* Class Parsedown.
*/
class Parsedown implements ParserInterface
{
/**
* {@inheritdoc}
*
* @return string
*/
public static function parse($string)
{
return ParsedownExtra::instance()->text($string);
}
}
22 changes: 22 additions & 0 deletions src/Parser/ParserInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
/*
* Copyright (c) Arnaud Ligny <arnaud@ligny.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace PHPoole\Parser;

/**
* Interface ParserInterface.
*/
interface ParserInterface
{
/**
* Parse string.
*
* @param $string
*/
public static function parse($string);
}
33 changes: 33 additions & 0 deletions src/Parser/SfYaml.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/*
* Copyright (c) Arnaud Ligny <arnaud@ligny.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace PHPoole\Parser;

use PHPoole\Exception\Exception;
use Symfony\Component\Yaml\Exception\ParseException;
use Symfony\Component\Yaml\Yaml;

/**
* Class SfYaml.
*/
class SfYaml implements ParserInterface
{
/**
* {@inheritdoc}
*
* @return array
*/
public static function parse($string)
{
try {
return Yaml::parse($string);
} catch (ParseException $e) {
//throw new Exception($e->getMessage());
}
}
}
109 changes: 109 additions & 0 deletions src/Step/LoadContent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php
/*
* Copyright (c) Arnaud Ligny <arnaud@ligny.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace PHPoole\Step;

use PHPoole\Exception\Exception;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;

/**
* Loads files in the 'content' directory.
*/
class LoadContent extends AbstractStep
{
// pattern of a file with a front matter
// https://regex101.com/r/xH7cL3/1
const PATTERN = '^\s*(?:<!--|---|\+++){1}[\n\r\s]*(.*?)[\n\r\s]*(?:-->|---|\+++){1}[\s\n\r]*(.*)$';
/**
* {@inheritdoc}
*
* @throws Exception
*/
public function init()
{
if (!is_dir($this->phpoole->getConfig()->getContentPath())) {
throw new Exception(sprintf('%s not found!', $this->phpoole->getConfig()->getContentPath()));
}
$this->process = true;
}

/**
* {@inheritdoc}
*/
public function internalProcess()
{
$content = [];
// collects files in each supported format
foreach ($this->phpoole->getConfig()->get('content.format') as $format => $data) {
$files = Finder::create()
->files()
->in($this->phpoole->getConfig()->getContentPath())
->name('/\.('.implode('|', $data['ext']).')$/');
/* @var $file SplFileInfo */
foreach ($files as $file) {
$index = $file->getRelativePathname();
$properties = $this->parse(
$file->getContents(),
$this->phpoole->getConfig()->get('content.frontmatter.'.$data['frontmatter'].'.parser')
);
$content[$index] = $properties;
$content[$index]['content'] = '';
$content[$index]['format'] = $format;
$content[$index]['lastmodified'] = $file->getMTime();
}
}

var_dump($content);

$this->phpoole->setContent($content);
}

/**
* Parse the contents of a file.
*
* Example:
* ```
* ---
* title: Title
* date: 2016-07-29
* ---
* Lorem Ipsum.
* ```
*
* @param string $content
*
* @param string $fmParser Class name
*
* @return array
*/
public function parse($content, $fmParser)
{
$properties = [];

// parse front matter
preg_match(
'/'.self::PATTERN.'/s',
$content,
$matches
);
// if not front matter, set 'content' property only
if (!$matches) {
$properties['raw'] = $content;

return $properties;
}
// parse front matter
/* @var $parser \PHPoole\Parser\ParserInterface */
$parser = new $fmParser();
$properties = $parser->parse(trim($matches[1]));
$properties['raw'] = trim($matches[2]);

return $properties;
}
}
50 changes: 0 additions & 50 deletions src/Step/LocateContent.php

This file was deleted.

0 comments on commit d502601

Please sign in to comment.