Skip to content

Commit

Permalink
Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
modbot committed Apr 5, 2018
1 parent 60b6544 commit e6e0400
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 52 deletions.
21 changes: 0 additions & 21 deletions .github/CONTRIBUTING.md

This file was deleted.

54 changes: 23 additions & 31 deletions src/frontmatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
* PHP YAML FrontMatter Class
* An easy to use class for handling YAML frontmatter in PHP.
*
* @author Blaxus
* @package Modularr/YAML-FrontMatter
*/
class FrontMatter
Expand All @@ -14,7 +13,7 @@ class FrontMatter
* @param array $data metadata & content
*/
public $data;

/**
* Constructor method, checks a file and then puts the contents into custom strings for usage
* @param string $file The input file
Expand All @@ -24,13 +23,12 @@ public function __construct($file)
$file = (file_exists($file)) ? $this->Read($file) : str_replace(array("\r\n", "\r", "\n"), "\n", $file);
$this->yaml_separator = "---\n";
$fm = $this->FrontMatter($file);

foreach($fm as $key => $value)
{

foreach($fm as $key => $value) {
$this->data[$key] = $value;
}
}

/**
* fetch method returns the value of a given key
* @return string $value The value for a given key
Expand All @@ -39,7 +37,7 @@ public function fetch($key)
{
return $this->data[$key];
}

/**
* keyExists method Checks to see if a key exists
* @return bool
Expand All @@ -49,7 +47,7 @@ public function keyExists($key)
#return (isset($this->data[$key])) ? true : false; # Isset Version
return array_key_exists($key, $this->data); # array_key_exists version
}

/**
* fetchKeys method returns an array of all meta data without the content
* @return [array] collection of all meta keys provided to FrontMatter
Expand All @@ -58,22 +56,21 @@ public function fetchKeys()
{
# Cache the keys so we don't edit the native object data
$keys = $this->data;

# Remove $data[content] from the keys so we only have the meta data
array_pop($keys);

return $keys;
}

/**
* FrontMatter method, rturns all the variables from a YAML Frontmatter input
* @param string $input The input string
* @return array $final returns all variables in an array
*/
function FrontMatter($input)
{
if (!$this->startsWith($input, $this->yaml_separator))
{
if (!$this->startsWith($input, $this->yaml_separator)) {
# No front matter
# Store Content in Final array
$final['content'] = $input;
Expand All @@ -83,9 +80,8 @@ function FrontMatter($input)

# Explode Seperators. At most, make three pieces out of the input file
$document = explode($this->yaml_separator,$input, 3);

switch( sizeof($document) )
{

switch( sizeof($document) ) {
case 0:
case 1:
// Empty document
Expand All @@ -102,21 +98,21 @@ function FrontMatter($input)
$front_matter = $document[1];
$content = $document[2];
}

# Parse YAML
try {
$final = Yaml::parse($front_matter);
} catch (ParseException $e) {
printf("Unable to parse the YAML string: %s", $e->getMessage());
}

# Store Content in Final array
$final['content'] = $content;

# Return Final array
return $final;
}

/**
* A convenience wrapper around strpos to check the start of a string
* From http://stackoverflow.com/a/860509/270334
Expand All @@ -128,7 +124,7 @@ private function startsWith($haystack,$needle,$case=true)
return strpos($haystack, $needle, 0) === 0;
return stripos($haystack, $needle, 0) === 0;
}

/**
* Read Method, Read file and returns it's contents
* @return string $data returned data
Expand All @@ -137,25 +133,21 @@ protected function Read($file)
{
# Open File
$fh = fopen($file, 'r');

$fileSize = filesize($file);

if(!empty($fileSize))
{

if(!empty($fileSize)) {
# Read Data
$data = fread($fh, $fileSize);

# Fix Data Stream to be the exact same format as PHP's strings
$data = str_replace(array("\r\n", "\r", "\n"), "\n", $data);
}
else
{
} else {
$data = '';
}

# Close File
fclose($fh);

# Return Data
return $data;
}
Expand Down

0 comments on commit e6e0400

Please sign in to comment.