MEDIUM: remove metadata from the current page

This commit is contained in:
martimiz 2013-05-22 21:35:03 +02:00
parent 5400ba2afc
commit 227289575a
4 changed files with 27 additions and 18 deletions

View File

@ -44,7 +44,7 @@ class DocumentationParser {
public static function parse(DocumentationPage $page, $baselink = null) { public static function parse(DocumentationPage $page, $baselink = null) {
if(!$page || (!$page instanceof DocumentationPage)) return false; if(!$page || (!$page instanceof DocumentationPage)) return false;
$md = $page->getMarkdown(); $md = $page->getMarkdown(true);
// Pre-processing // Pre-processing
$md = self::rewrite_image_links($md, $page); $md = self::rewrite_image_links($md, $page);

View File

@ -178,7 +178,7 @@ class DocumentationService {
* @param bool $allow * @param bool $allow
*/ */
public static function enable_meta_comments($allow = true) { public static function enable_meta_comments($allow = true) {
self::$meta_comments_enabled = ($allow)? true: false; self::$meta_comments_enabled = (bool) $allow;
} }
/** /**

View File

@ -255,7 +255,7 @@ class DocumentationPage extends ViewableData {
* *
* @return string * @return string
*/ */
function getMarkdown() { function getMarkdown($removeMetaData = false) {
try { try {
$path = $this->getPath(true); $path = $this->getPath(true);
@ -264,7 +264,7 @@ class DocumentationPage extends ViewableData {
if(empty($ext) || DocumentationService::is_valid_extension($ext)) { if(empty($ext) || DocumentationService::is_valid_extension($ext)) {
if ($md = file_get_contents($path)) { if ($md = file_get_contents($path)) {
if ($this->title != 'Index') $this->getMetadataFromComments($md); if ($this->title != 'Index') $this->getMetadataFromComments($md, $removeMetaData);
} }
return $md; return $md;
} }
@ -287,25 +287,39 @@ class DocumentationPage extends ViewableData {
} }
/** /**
* get metadata from the first html comments block in the page * get metadata from the first html block in the page, then remove the
* block on request
* *
* @param DocumentationPage $md * @param DocumentationPage $md
* @param bool $remove
*/ */
public function getMetadataFromComments($md = '') { public function getMetadataFromComments(&$md, $removeMetaData = false) {
if($md && DocumentationService::meta_comments_enabled()) { if($md && DocumentationService::meta_comments_enabled()) {
//$pattern = '/^<!--(.*?)-->/Uis';
$pattern = "/^(.+)\n(\r)*\n/Uis"; // get the text up to the first whiteline
$matches = preg_match($pattern, $md, $block); $extPattern = "/^(.+)\n(\r)*\n/Uis";
$matches = preg_match($extPattern, $md, $block);
if($matches && $block[1]) { if($matches && $block[1]) {
$pattern = '/(?<key>[A-Za-z][A-Za-z0-9_-]+)[\t]*:[\t]*(?<value>[^:\n\r\/]+)/x'; $metaDataFound = false;
$matches = preg_match_all($pattern, $block[1], $meta);
// find the key/value pairs
$intPattern = '/(?<key>[A-Za-z][A-Za-z0-9_-]+)[\t]*:[\t]*(?<value>[^:\n\r\/]+)/x';
$matches = preg_match_all($intPattern, $block[1], $meta);
foreach($meta['key'] as $index => $key) { foreach($meta['key'] as $index => $key) {
if(isset($meta['value'][$index])) { if(isset($meta['value'][$index])) {
// check if a property exists for this key
if (property_exists(get_class(), $key)) { if (property_exists(get_class(), $key)) {
$this->setMetaData($key, $meta['value'][$index]); $this->setMetaData($key, $meta['value'][$index]);
$metaDataFound = true;
} }
} }
} }
// optionally remove the metadata block (only on the page that is displayed)
if ($metaDataFound && $removeMetaData) {
$md = preg_replace($extPattern, '', $md);
}
} }
} }
} }

View File

@ -63,12 +63,7 @@ Custom metadata can be added to the head of the MarkDown file like this:
Make sure to add an empty line to separate the metadata from the content of Make sure to add an empty line to separate the metadata from the content of
the file. To make the metadata invisible on the page, you can use a comment tag: the file.
<!--
pagenumber: 1
title: A custom title
-->
You now need to explicitly enable the use of metadata by adding the following to You now need to explicitly enable the use of metadata by adding the following to
your _config.php: your _config.php: