Skip to content

Instantly share code, notes, and snippets.

@ethernick
Last active August 29, 2015 13:58
Show Gist options
  • Save ethernick/10013593 to your computer and use it in GitHub Desktop.
Save ethernick/10013593 to your computer and use it in GitHub Desktop.
Extend PHP Fat-Free (F3) with MetaMarkdown. Setting a global 'meta' var that can be used later on.
<?php
class MetaMarkdown extends Markdown {
protected function meta($txt) {
$pattern = '/^([A-Za-z]+)\:\s*\t*(.+)$/m';
//$pattern = '/^(.*)\:(?\s*)(?\t*)(.+)(\r\n|\n)/';
//echo $txt;
$txt = preg_replace_callback(
'/^([A-Za-z]+)\:\s*\t*(.+)$/m',
function($expr) {
F3::instance()->set('meta.'.strtolower($expr[1]),$expr[2]);
return "";
},
$txt
);
return $txt;
}
function convert($txt) {
$txt = $this->meta($txt);
$txt=preg_replace_callback(
'/(<code.*?>.+?<\/code>|'.
'<[^>\n]+>|\([^\n\)]+\)|"[^"\n]+")|'.
'\\\\(.)/s',
function($expr) {
// Process escaped characters
return empty($expr[1])?$expr[2]:$expr[1];
},
$this->build(preg_replace('/\r\n|\r/',"\n",$txt))
);
return $this->snip($txt);
}
}
?>

Title: Meta Markdown Gist Author: Nick Kempinski Date: April, 6 2014

Meta Markdown Gist

Once upon a time, there was Nick!

<?php
$file = F3::instance()->read('my-markdown.md');
# Old Markdown
$html = Markdown::instance()->convert($file);
# Look for 'meta'
var_dump(F3::instance()->get('meta'));
# = NULL
# New & Improved Markdown
$meta_html = MetaMarkdown::instance()->convert($file);
# $html == $meta_html
# but now with added 'meta'
var_dump(F3::instance()->get('meta'));
# = { ["title"]=> string(18) "Meta Markdown Gist" ["author"]=> string(14) "Nick Kempinski" ["date"]=> string(13) "April, 6 2014" }
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment