From 7783971742e69fbd5ad26b0daf75bd4a3043aff0 Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Sat, 24 May 2014 13:59:49 +0200 Subject: more reformatting --- inc/parser/metadata.php | 286 ++++++++++++++++++++++++------------------------ 1 file changed, 146 insertions(+), 140 deletions(-) (limited to 'inc/parser/metadata.php') diff --git a/inc/parser/metadata.php b/inc/parser/metadata.php index 82a268fd6..e19aa602a 100644 --- a/inc/parser/metadata.php +++ b/inc/parser/metadata.php @@ -6,14 +6,14 @@ */ if(!defined('DOKU_INC')) die('meh.'); -if ( !defined('DOKU_LF') ) { +if(!defined('DOKU_LF')) { // Some whitespace to help View > Source - define ('DOKU_LF',"\n"); + define ('DOKU_LF', "\n"); } -if ( !defined('DOKU_TAB') ) { +if(!defined('DOKU_TAB')) { // Some whitespace to help View > Source - define ('DOKU_TAB',"\t"); + define ('DOKU_TAB', "\t"); } /** @@ -21,55 +21,55 @@ if ( !defined('DOKU_TAB') ) { */ class Doku_Renderer_metadata extends Doku_Renderer { - var $doc = ''; + var $doc = ''; var $meta = array(); var $persistent = array(); var $headers = array(); var $capture = true; - var $store = ''; + var $store = ''; var $firstimage = ''; - function getFormat(){ + function getFormat() { return 'metadata'; } - function document_start(){ + function document_start() { global $ID; $this->headers = array(); // external pages are missing create date - if(!$this->persistent['date']['created']){ + if(!$this->persistent['date']['created']) { $this->persistent['date']['created'] = filectime(wikiFN($ID)); } - if(!isset($this->persistent['user'])){ + if(!isset($this->persistent['user'])) { $this->persistent['user'] = ''; } - if(!isset($this->persistent['creator'])){ + if(!isset($this->persistent['creator'])) { $this->persistent['creator'] = ''; } // reset metadata to persistent values $this->meta = $this->persistent; } - function document_end(){ + function document_end() { global $ID; // store internal info in metadata (notoc,nocache) $this->meta['internal'] = $this->info; - if (!isset($this->meta['description']['abstract'])){ + if(!isset($this->meta['description']['abstract'])) { // cut off too long abstracts $this->doc = trim($this->doc); - if (strlen($this->doc) > 500) + if(strlen($this->doc) > 500) $this->doc = utf8_substr($this->doc, 0, 500).'…'; $this->meta['description']['abstract'] = $this->doc; } $this->meta['relation']['firstimage'] = $this->firstimage; - if(!isset($this->meta['date']['modified'])){ + if(!isset($this->meta['date']['modified'])) { $this->meta['date']['modified'] = filemtime(wikiFN($ID)); } @@ -79,54 +79,57 @@ class Doku_Renderer_metadata extends Doku_Renderer { global $conf; //only add items within configured levels - if($level >= $conf['toptoclevel'] && $level <= $conf['maxtoclevel']){ + if($level >= $conf['toptoclevel'] && $level <= $conf['maxtoclevel']) { // the TOC is one of our standard ul list arrays ;-) $this->meta['description']['tableofcontents'][] = array( - 'hid' => $id, - 'title' => $text, - 'type' => 'ul', - 'level' => $level-$conf['toptoclevel']+1 + 'hid' => $id, + 'title' => $text, + 'type' => 'ul', + 'level' => $level - $conf['toptoclevel'] + 1 ); } } function header($text, $level, $pos) { - if (!isset($this->meta['title'])) $this->meta['title'] = $text; + if(!isset($this->meta['title'])) $this->meta['title'] = $text; // add the header to the TOC - $hid = $this->_headerToLink($text,'true'); + $hid = $this->_headerToLink($text, 'true'); $this->toc_additem($hid, $text, $level); // add to summary - if ($this->capture && ($level > 1)) $this->doc .= DOKU_LF.$text.DOKU_LF; + if($this->capture && ($level > 1)) $this->doc .= DOKU_LF.$text.DOKU_LF; } - function section_open($level){} - function section_close(){} + function section_open($level) { + } + + function section_close() { + } - function cdata($text){ - if ($this->capture) $this->doc .= $text; + function cdata($text) { + if($this->capture) $this->doc .= $text; } - function p_open(){ - if ($this->capture) $this->doc .= DOKU_LF; + function p_open() { + if($this->capture) $this->doc .= DOKU_LF; } - function p_close(){ - if ($this->capture){ - if (strlen($this->doc) > 250) $this->capture = false; + function p_close() { + if($this->capture) { + if(strlen($this->doc) > 250) $this->capture = false; else $this->doc .= DOKU_LF; } } - function linebreak(){ - if ($this->capture) $this->doc .= DOKU_LF; + function linebreak() { + if($this->capture) $this->doc .= DOKU_LF; } - function hr(){ - if ($this->capture){ - if (strlen($this->doc) > 250) $this->capture = false; + function hr() { + if($this->capture) { + if(strlen($this->doc) > 250) $this->capture = false; else $this->doc .= DOKU_LF.'----------'.DOKU_LF; } } @@ -141,7 +144,7 @@ class Doku_Renderer_metadata extends Doku_Renderer { * @author Andreas Gohr */ function footnote_open() { - if ($this->capture){ + if($this->capture) { // move current content to store and record footnote $this->store = $this->doc; $this->doc = ''; @@ -157,141 +160,144 @@ class Doku_Renderer_metadata extends Doku_Renderer { * @author Andreas Gohr */ function footnote_close() { - if ($this->capture){ + if($this->capture) { // restore old content - $this->doc = $this->store; + $this->doc = $this->store; $this->store = ''; } } - function listu_open(){ - if ($this->capture) $this->doc .= DOKU_LF; + function listu_open() { + if($this->capture) $this->doc .= DOKU_LF; } - function listu_close(){ - if ($this->capture && (strlen($this->doc) > 250)) $this->capture = false; + function listu_close() { + if($this->capture && (strlen($this->doc) > 250)) $this->capture = false; } - function listo_open(){ - if ($this->capture) $this->doc .= DOKU_LF; + function listo_open() { + if($this->capture) $this->doc .= DOKU_LF; } - function listo_close(){ - if ($this->capture && (strlen($this->doc) > 250)) $this->capture = false; + function listo_close() { + if($this->capture && (strlen($this->doc) > 250)) $this->capture = false; } - function listitem_open($level){ - if ($this->capture) $this->doc .= str_repeat(DOKU_TAB, $level).'* '; + function listitem_open($level) { + if($this->capture) $this->doc .= str_repeat(DOKU_TAB, $level).'* '; } - function listitem_close(){ - if ($this->capture) $this->doc .= DOKU_LF; + function listitem_close() { + if($this->capture) $this->doc .= DOKU_LF; } - function listcontent_open(){} - function listcontent_close(){} + function listcontent_open() { + } - function unformatted($text){ - if ($this->capture) $this->doc .= $text; + function listcontent_close() { } - function preformatted($text){ - if ($this->capture) $this->doc .= $text; + function unformatted($text) { + if($this->capture) $this->doc .= $text; } - function file($text, $lang = null, $file = null){ - if ($this->capture){ + function preformatted($text) { + if($this->capture) $this->doc .= $text; + } + + function file($text, $lang = null, $file = null) { + if($this->capture) { $this->doc .= DOKU_LF.$text; - if (strlen($this->doc) > 250) $this->capture = false; + if(strlen($this->doc) > 250) $this->capture = false; else $this->doc .= DOKU_LF; } } - function quote_open(){ - if ($this->capture) $this->doc .= DOKU_LF.DOKU_TAB.'"'; + function quote_open() { + if($this->capture) $this->doc .= DOKU_LF.DOKU_TAB.'"'; } - function quote_close(){ - if ($this->capture){ + function quote_close() { + if($this->capture) { $this->doc .= '"'; - if (strlen($this->doc) > 250) $this->capture = false; + if(strlen($this->doc) > 250) $this->capture = false; else $this->doc .= DOKU_LF; } } - function code($text, $language = null, $file = null){ - if ($this->capture){ + function code($text, $language = null, $file = null) { + if($this->capture) { $this->doc .= DOKU_LF.$text; - if (strlen($this->doc) > 250) $this->capture = false; + if(strlen($this->doc) > 250) $this->capture = false; else $this->doc .= DOKU_LF; - } + } } - function acronym($acronym){ - if ($this->capture) $this->doc .= $acronym; + function acronym($acronym) { + if($this->capture) $this->doc .= $acronym; } - function smiley($smiley){ - if ($this->capture) $this->doc .= $smiley; + function smiley($smiley) { + if($this->capture) $this->doc .= $smiley; } - function entity($entity){ - if ($this->capture) $this->doc .= $entity; + function entity($entity) { + if($this->capture) $this->doc .= $entity; } - function multiplyentity($x, $y){ - if ($this->capture) $this->doc .= $x.'×'.$y; + function multiplyentity($x, $y) { + if($this->capture) $this->doc .= $x.'×'.$y; } - function singlequoteopening(){ + function singlequoteopening() { global $lang; - if ($this->capture) $this->doc .= $lang['singlequoteopening']; + if($this->capture) $this->doc .= $lang['singlequoteopening']; } - function singlequoteclosing(){ + function singlequoteclosing() { global $lang; - if ($this->capture) $this->doc .= $lang['singlequoteclosing']; + if($this->capture) $this->doc .= $lang['singlequoteclosing']; } function apostrophe() { global $lang; - if ($this->capture) $this->doc .= $lang['apostrophe']; + if($this->capture) $this->doc .= $lang['apostrophe']; } - function doublequoteopening(){ + function doublequoteopening() { global $lang; - if ($this->capture) $this->doc .= $lang['doublequoteopening']; + if($this->capture) $this->doc .= $lang['doublequoteopening']; } - function doublequoteclosing(){ + function doublequoteclosing() { global $lang; - if ($this->capture) $this->doc .= $lang['doublequoteclosing']; + if($this->capture) $this->doc .= $lang['doublequoteclosing']; } function camelcaselink($link) { $this->internallink($link, $link); } - function locallink($hash, $name = null){ + function locallink($hash, $name = null) { if(is_array($name)) { $this->_firstimage($name['src']); - if ($name['type'] == 'internalmedia') $this->_recordMediaUsage($name['src']); + if($name['type'] == 'internalmedia') $this->_recordMediaUsage($name['src']); } } /** * keep track of internal links in $this->meta['relation']['references'] */ - function internallink($id, $name = null){ + function internallink($id, $name = null) { global $ID; if(is_array($name)) { $this->_firstimage($name['src']); - if ($name['type'] == 'internalmedia') $this->_recordMediaUsage($name['src']); + if($name['type'] == 'internalmedia') $this->_recordMediaUsage($name['src']); } $parts = explode('?', $id, 2); - if (count($parts) === 2) { + if(count($parts) === 2) { $id = $parts[0]; } @@ -307,80 +313,80 @@ class Doku_Renderer_metadata extends Doku_Renderer { // p_set_metadata($id, $data); // add link title to summary - if ($this->capture){ + if($this->capture) { $name = $this->_getLinkTitle($name, $default, $id); $this->doc .= $name; } } - function externallink($url, $name = null){ + function externallink($url, $name = null) { if(is_array($name)) { $this->_firstimage($name['src']); - if ($name['type'] == 'internalmedia') $this->_recordMediaUsage($name['src']); + if($name['type'] == 'internalmedia') $this->_recordMediaUsage($name['src']); } - if ($this->capture){ - $this->doc .= $this->_getLinkTitle($name, '<' . $url . '>'); + if($this->capture) { + $this->doc .= $this->_getLinkTitle($name, '<'.$url.'>'); } } - function interwikilink($match, $name = null, $wikiName, $wikiUri){ + function interwikilink($match, $name = null, $wikiName, $wikiUri) { if(is_array($name)) { $this->_firstimage($name['src']); - if ($name['type'] == 'internalmedia') $this->_recordMediaUsage($name['src']); + if($name['type'] == 'internalmedia') $this->_recordMediaUsage($name['src']); } - if ($this->capture){ + if($this->capture) { list($wikiUri, $hash) = explode('#', $wikiUri, 2); $name = $this->_getLinkTitle($name, $wikiUri); $this->doc .= $name; } } - function windowssharelink($url, $name = null){ + function windowssharelink($url, $name = null) { if(is_array($name)) { $this->_firstimage($name['src']); - if ($name['type'] == 'internalmedia') $this->_recordMediaUsage($name['src']); + if($name['type'] == 'internalmedia') $this->_recordMediaUsage($name['src']); } - if ($this->capture){ - if ($name) $this->doc .= $name; + if($this->capture) { + if($name) $this->doc .= $name; else $this->doc .= '<'.$url.'>'; } } - function emaillink($address, $name = null){ + function emaillink($address, $name = null) { if(is_array($name)) { $this->_firstimage($name['src']); - if ($name['type'] == 'internalmedia') $this->_recordMediaUsage($name['src']); + if($name['type'] == 'internalmedia') $this->_recordMediaUsage($name['src']); } - if ($this->capture){ - if ($name) $this->doc .= $name; + if($this->capture) { + if($name) $this->doc .= $name; else $this->doc .= '<'.$address.'>'; } } - function internalmedia($src, $title=null, $align=null, $width=null, - $height=null, $cache=null, $linking=null){ - if ($this->capture && $title) $this->doc .= '['.$title.']'; + function internalmedia($src, $title = null, $align = null, $width = null, + $height = null, $cache = null, $linking = null) { + if($this->capture && $title) $this->doc .= '['.$title.']'; $this->_firstimage($src); $this->_recordMediaUsage($src); } - function externalmedia($src, $title=null, $align=null, $width=null, - $height=null, $cache=null, $linking=null){ - if ($this->capture && $title) $this->doc .= '['.$title.']'; + function externalmedia($src, $title = null, $align = null, $width = null, + $height = null, $cache = null, $linking = null) { + if($this->capture && $title) $this->doc .= '['.$title.']'; $this->_firstimage($src); } - function rss($url,$params) { + function rss($url, $params) { $this->meta['relation']['haspart'][$url] = true; $this->meta['date']['valid']['age'] = - isset($this->meta['date']['valid']['age']) ? - min($this->meta['date']['valid']['age'],$params['refresh']) : - $params['refresh']; + isset($this->meta['date']['valid']['age']) ? + min($this->meta['date']['valid']['age'], $params['refresh']) : + $params['refresh']; } //---------------------------------------------------------- @@ -392,19 +398,19 @@ class Doku_Renderer_metadata extends Doku_Renderer { * * @author Andreas Gohr */ - function _simpleTitle($name){ + function _simpleTitle($name) { global $conf; if(is_array($name)) return ''; - if($conf['useslash']){ + if($conf['useslash']) { $nssep = '[:;/]'; - }else{ + } else { $nssep = '[:;]'; } - $name = preg_replace('!.*'.$nssep.'!','',$name); + $name = preg_replace('!.*'.$nssep.'!', '', $name); //if there is a hash we use the anchor name only - $name = preg_replace('!.*#!','',$name); + $name = preg_replace('!.*#!', '', $name); return $name; } @@ -415,12 +421,12 @@ class Doku_Renderer_metadata extends Doku_Renderer { * @param boolean $create Create a new unique ID? * @author Andreas Gohr */ - function _headerToLink($title, $create=false) { - if($create){ - return sectionID($title,$this->headers); - }else{ + function _headerToLink($title, $create = false) { + if($create) { + return sectionID($title, $this->headers); + } else { $check = false; - return sectionID($title,$check); + return sectionID($title, $check); } } @@ -429,16 +435,16 @@ class Doku_Renderer_metadata extends Doku_Renderer { * * @author Harry Fuecks */ - function _getLinkTitle($title, $default, $id=null) { + function _getLinkTitle($title, $default, $id = null) { global $conf; $isImage = false; - if (is_array($title)){ + if(is_array($title)) { if($title['title']) return '['.$title['title'].']'; - } else if (is_null($title) || trim($title)==''){ - if (useHeading('content') && $id){ - $heading = p_get_first_heading($id,METADATA_DONT_RENDER); - if ($heading) return $heading; + } else if(is_null($title) || trim($title) == '') { + if(useHeading('content') && $id) { + $heading = p_get_first_heading($id, METADATA_DONT_RENDER); + if($heading) return $heading; } return $default; } else { @@ -446,15 +452,15 @@ class Doku_Renderer_metadata extends Doku_Renderer { } } - function _firstimage($src){ + function _firstimage($src) { if($this->firstimage) return; global $ID; - list($src,$hash) = explode('#',$src,2); - if(!media_isexternal($src)){ - resolve_mediaid(getNS($ID),$src, $exists); + list($src, $hash) = explode('#', $src, 2); + if(!media_isexternal($src)) { + resolve_mediaid(getNS($ID), $src, $exists); } - if(preg_match('/.(jpe?g|gif|png)$/i',$src)){ + if(preg_match('/.(jpe?g|gif|png)$/i', $src)) { $this->firstimage = $src; } } @@ -463,7 +469,7 @@ class Doku_Renderer_metadata extends Doku_Renderer { global $ID; list ($src, $hash) = explode('#', $src, 2); - if (media_isexternal($src)) return; + if(media_isexternal($src)) return; resolve_mediaid(getNS($ID), $src, $exists); $this->meta['relation']['media'][$src] = $exists; } -- cgit v1.2.3 From 93075b016d22b16e7d11cd93fac9a54e91e22269 Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Sat, 24 May 2014 15:44:08 +0200 Subject: updated doc blocks in metadata renderer --- inc/parser/metadata.php | 310 +++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 268 insertions(+), 42 deletions(-) (limited to 'inc/parser/metadata.php') diff --git a/inc/parser/metadata.php b/inc/parser/metadata.php index e19aa602a..e54c028f9 100644 --- a/inc/parser/metadata.php +++ b/inc/parser/metadata.php @@ -17,23 +17,48 @@ if(!defined('DOKU_TAB')) { } /** - * The Renderer + * The MetaData Renderer + * + * Metadata is additional information about a DokuWiki page that gets extracted mainly from the page's content + * but also it's own filesystem data (like the creation time). All metadata is stored in the fields $meta and + * $persistent. + * + * Some simplified rendering to $doc is done to gather the page's (text-only) abstract. */ class Doku_Renderer_metadata extends Doku_Renderer { - var $doc = ''; - var $meta = array(); - var $persistent = array(); + /** @var array transient meta data, will be reset on each rendering */ + public $meta = array(); + + /** @var array persistent meta data, will be kept until explicitly deleted */ + public $persistent = array(); + + /** @var array the list of headers used to create unique link ids */ + protected $headers = array(); + + /** @var bool determines if enough data for a page summary was collected, yet */ + protected $capture = true; - var $headers = array(); - var $capture = true; - var $store = ''; - var $firstimage = ''; + /** @var string temporary $doc store */ + protected $store = ''; + /** @var string keeps the first image reference */ + protected $firstimage = ''; + + /** + * Returns the format produced by this renderer. + * + * @return string always 'metadata' + */ function getFormat() { return 'metadata'; } + /** + * Initialize the document + * + * Sets up some of the persistent info about the page if it doesn't exist, yet. + */ function document_start() { global $ID; @@ -53,6 +78,11 @@ class Doku_Renderer_metadata extends Doku_Renderer { $this->meta = $this->persistent; } + /** + * Finalize the document + * + * Stores collected data in the metadata + */ function document_end() { global $ID; @@ -75,6 +105,13 @@ class Doku_Renderer_metadata extends Doku_Renderer { } + /** + * Add an item to the TOC + * + * @param string $id the hash link + * @param string $text the text to display + * @param int $level the nesting level + */ function toc_additem($id, $text, $level) { global $conf; @@ -91,6 +128,13 @@ class Doku_Renderer_metadata extends Doku_Renderer { } + /** + * Render a heading + * + * @param string $text the text to display + * @param int $level header level + * @param int $pos byte position in the original source + */ function header($text, $level, $pos) { if(!isset($this->meta['title'])) $this->meta['title'] = $text; @@ -102,20 +146,25 @@ class Doku_Renderer_metadata extends Doku_Renderer { if($this->capture && ($level > 1)) $this->doc .= DOKU_LF.$text.DOKU_LF; } - function section_open($level) { - } - - function section_close() { - } - + /** + * Render plain text data + * + * @param $text + */ function cdata($text) { if($this->capture) $this->doc .= $text; } + /** + * Open a paragraph + */ function p_open() { if($this->capture) $this->doc .= DOKU_LF; } + /** + * Close a paragraph + */ function p_close() { if($this->capture) { if(strlen($this->doc) > 250) $this->capture = false; @@ -123,10 +172,16 @@ class Doku_Renderer_metadata extends Doku_Renderer { } } + /** + * Create a line break + */ function linebreak() { if($this->capture) $this->doc .= DOKU_LF; } + /** + * Create a horizontal line + */ function hr() { if($this->capture) { if(strlen($this->doc) > 250) $this->capture = false; @@ -167,64 +222,99 @@ class Doku_Renderer_metadata extends Doku_Renderer { } } + /** + * Open an unordered list + */ function listu_open() { if($this->capture) $this->doc .= DOKU_LF; } + /** + * Close an unordered list + */ function listu_close() { if($this->capture && (strlen($this->doc) > 250)) $this->capture = false; } + /** + * Open an ordered list + */ function listo_open() { if($this->capture) $this->doc .= DOKU_LF; } + /** + * Close an ordered list + */ function listo_close() { if($this->capture && (strlen($this->doc) > 250)) $this->capture = false; } + /** + * Open a list item + * + * @param int $level the nesting level + */ function listitem_open($level) { if($this->capture) $this->doc .= str_repeat(DOKU_TAB, $level).'* '; } + /** + * Close a list item + */ function listitem_close() { if($this->capture) $this->doc .= DOKU_LF; } - function listcontent_open() { - } - - function listcontent_close() { - } - - function unformatted($text) { + /** + * Output preformatted text + * + * @param string $text + */ + function preformatted($text) { if($this->capture) $this->doc .= $text; } - function preformatted($text) { - if($this->capture) $this->doc .= $text; + /** + * Start a block quote + */ + function quote_open() { + if($this->capture) $this->doc .= DOKU_LF.DOKU_TAB.'"'; } - function file($text, $lang = null, $file = null) { + /** + * Stop a block quote + */ + function quote_close() { if($this->capture) { - $this->doc .= DOKU_LF.$text; + $this->doc .= '"'; if(strlen($this->doc) > 250) $this->capture = false; else $this->doc .= DOKU_LF; } } - function quote_open() { - if($this->capture) $this->doc .= DOKU_LF.DOKU_TAB.'"'; - } - - function quote_close() { + /** + * Display text as file content, optionally syntax highlighted + * + * @param string $text text to show + * @param string $lang programming language to use for syntax highlighting + * @param string $file file path label + */ + function file($text, $lang = null, $file = null) { if($this->capture) { - $this->doc .= '"'; + $this->doc .= DOKU_LF.$text; if(strlen($this->doc) > 250) $this->capture = false; else $this->doc .= DOKU_LF; } } + /** + * Display text as code content, optionally syntax highlighted + * + * @param string $text text to show + * @param string $language programming language to use for syntax highlighting + * @param string $file file path label + */ function code($text, $language = null, $file = null) { if($this->capture) { $this->doc .= DOKU_LF.$text; @@ -233,51 +323,109 @@ class Doku_Renderer_metadata extends Doku_Renderer { } } + /** + * Format an acronym + * + * Uses $this->acronyms + * + * @param string $acronym + */ function acronym($acronym) { if($this->capture) $this->doc .= $acronym; } + /** + * Format a smiley + * + * Uses $this->smiley + * + * @param string $smiley + */ function smiley($smiley) { if($this->capture) $this->doc .= $smiley; } + /** + * Format an entity + * + * Entities are basically small text replacements + * + * Uses $this->entities + * + * @param string $entity + */ function entity($entity) { if($this->capture) $this->doc .= $entity; } + /** + * Typographically format a multiply sign + * + * Example: ($x=640, $y=480) should result in "640×480" + * + * @param string|int $x first value + * @param string|int $y second value + */ function multiplyentity($x, $y) { if($this->capture) $this->doc .= $x.'×'.$y; } + /** + * Render an opening single quote char (language specific) + */ function singlequoteopening() { global $lang; if($this->capture) $this->doc .= $lang['singlequoteopening']; } + /** + * Render a closing single quote char (language specific) + */ function singlequoteclosing() { global $lang; if($this->capture) $this->doc .= $lang['singlequoteclosing']; } + /** + * Render an apostrophe char (language specific) + */ function apostrophe() { global $lang; if($this->capture) $this->doc .= $lang['apostrophe']; } + /** + * Render an opening double quote char (language specific) + */ function doublequoteopening() { global $lang; if($this->capture) $this->doc .= $lang['doublequoteopening']; } + /** + * Render an closinging double quote char (language specific) + */ function doublequoteclosing() { global $lang; if($this->capture) $this->doc .= $lang['doublequoteclosing']; } + /** + * Render a CamelCase link + * + * @param string $link The link name + * @see http://en.wikipedia.org/wiki/CamelCase + */ function camelcaselink($link) { $this->internallink($link, $link); } + /** + * Render a page local link + * + * @param string $hash hash link identifier + * @param string $name name for the link + */ function locallink($hash, $name = null) { if(is_array($name)) { $this->_firstimage($name['src']); @@ -287,6 +435,9 @@ class Doku_Renderer_metadata extends Doku_Renderer { /** * keep track of internal links in $this->meta['relation']['references'] + * + * @param string $id page ID to link to. eg. 'wiki:syntax' + * @param string|array $name name for the link, array for media file */ function internallink($id, $name = null) { global $ID; @@ -305,7 +456,7 @@ class Doku_Renderer_metadata extends Doku_Renderer { // first resolve and clean up the $id resolve_pageid(getNS($ID), $id, $exists); - @list($page, $hash) = explode('#', $id, 2); + @list($page) = explode('#', $id, 2); // set metadata $this->meta['relation']['references'][$page] = $exists; @@ -319,6 +470,12 @@ class Doku_Renderer_metadata extends Doku_Renderer { } } + /** + * Render an external link + * + * @param string $url full URL with scheme + * @param string|array $name name for the link, array for media file + */ function externallink($url, $name = null) { if(is_array($name)) { $this->_firstimage($name['src']); @@ -330,6 +487,16 @@ class Doku_Renderer_metadata extends Doku_Renderer { } } + /** + * Render an interwiki link + * + * You may want to use $this->_resolveInterWiki() here + * + * @param string $match original link - probably not much use + * @param string|array $name name for the link, array for media file + * @param string $wikiName indentifier (shortcut) for the remote wiki + * @param string $wikiUri the fragment parsed from the original link + */ function interwikilink($match, $name = null, $wikiName, $wikiUri) { if(is_array($name)) { $this->_firstimage($name['src']); @@ -337,12 +504,18 @@ class Doku_Renderer_metadata extends Doku_Renderer { } if($this->capture) { - list($wikiUri, $hash) = explode('#', $wikiUri, 2); + list($wikiUri) = explode('#', $wikiUri, 2); $name = $this->_getLinkTitle($name, $wikiUri); $this->doc .= $name; } } + /** + * Link to windows share + * + * @param string $url the link + * @param string|array $name name for the link, array for media file + */ function windowssharelink($url, $name = null) { if(is_array($name)) { $this->_firstimage($name['src']); @@ -355,6 +528,14 @@ class Doku_Renderer_metadata extends Doku_Renderer { } } + /** + * Render a linked E-Mail Address + * + * Should honor $conf['mailguard'] setting + * + * @param string $address Email-Address + * @param string|array $name name for the link, array for media file + */ function emaillink($address, $name = null) { if(is_array($name)) { $this->_firstimage($name['src']); @@ -367,6 +548,17 @@ class Doku_Renderer_metadata extends Doku_Renderer { } } + /** + * Render an internal media file + * + * @param string $src media ID + * @param string $title descriptive text + * @param string $align left|center|right + * @param int $width width of media in pixel + * @param int $height height of media in pixel + * @param string $cache cache|recache|nocache + * @param string $linking linkonly|detail|nolink + */ function internalmedia($src, $title = null, $align = null, $width = null, $height = null, $cache = null, $linking = null) { if($this->capture && $title) $this->doc .= '['.$title.']'; @@ -374,12 +566,29 @@ class Doku_Renderer_metadata extends Doku_Renderer { $this->_recordMediaUsage($src); } + /** + * Render an external media file + * + * @param string $src full media URL + * @param string $title descriptive text + * @param string $align left|center|right + * @param int $width width of media in pixel + * @param int $height height of media in pixel + * @param string $cache cache|recache|nocache + * @param string $linking linkonly|detail|nolink + */ function externalmedia($src, $title = null, $align = null, $width = null, $height = null, $cache = null, $linking = null) { if($this->capture && $title) $this->doc .= '['.$title.']'; $this->_firstimage($src); } + /** + * Render the output of an RSS feed + * + * @param string $url URL of the feed + * @param array $params Finetuning of the output + */ function rss($url, $params) { $this->meta['relation']['haspart'][$url] = true; @@ -389,8 +598,7 @@ class Doku_Renderer_metadata extends Doku_Renderer { $params['refresh']; } - //---------------------------------------------------------- - // Utils + #region Utils /** * Removes any Namespace from the given name but keeps @@ -417,9 +625,10 @@ class Doku_Renderer_metadata extends Doku_Renderer { /** * Creates a linkid from a headline * + * @author Andreas Gohr * @param string $title The headline title * @param boolean $create Create a new unique ID? - * @author Andreas Gohr + * @return string */ function _headerToLink($title, $create = false) { if($create) { @@ -434,13 +643,18 @@ class Doku_Renderer_metadata extends Doku_Renderer { * Construct a title and handle images in titles * * @author Harry Fuecks + * @param string|array $title either string title or media array + * @param string $default default title if nothing else is found + * @param null|string $id linked page id (used to extract title from first heading) + * @return string title text */ function _getLinkTitle($title, $default, $id = null) { - global $conf; - - $isImage = false; if(is_array($title)) { - if($title['title']) return '['.$title['title'].']'; + if($title['title']) { + return '['.$title['title'].']'; + } else { + return $default; + } } else if(is_null($title) || trim($title) == '') { if(useHeading('content') && $id) { $heading = p_get_first_heading($id, METADATA_DONT_RENDER); @@ -452,11 +666,16 @@ class Doku_Renderer_metadata extends Doku_Renderer { } } + /** + * Remember first image + * + * @param string $src image URL or ID + */ function _firstimage($src) { if($this->firstimage) return; global $ID; - list($src, $hash) = explode('#', $src, 2); + list($src) = explode('#', $src, 2); if(!media_isexternal($src)) { resolve_mediaid(getNS($ID), $src, $exists); } @@ -465,14 +684,21 @@ class Doku_Renderer_metadata extends Doku_Renderer { } } + /** + * Store list of used media files in metadata + * + * @param string $src media ID + */ function _recordMediaUsage($src) { global $ID; - list ($src, $hash) = explode('#', $src, 2); + list ($src) = explode('#', $src, 2); if(media_isexternal($src)) return; resolve_mediaid(getNS($ID), $src, $exists); $this->meta['relation']['media'][$src] = $exists; } + + #endregion } //Setup VIM: ex: et ts=4 : -- cgit v1.2.3 From 17076c2ad03d6de87b6c8f2ed3d6fcecb99ecf5b Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Sat, 24 May 2014 16:00:14 +0200 Subject: simplified abstract caoturing in metadata renderer --- inc/parser/metadata.php | 123 +++++++++++++++++++++--------------------------- 1 file changed, 54 insertions(+), 69 deletions(-) (limited to 'inc/parser/metadata.php') diff --git a/inc/parser/metadata.php b/inc/parser/metadata.php index e54c028f9..9552fedff 100644 --- a/inc/parser/metadata.php +++ b/inc/parser/metadata.php @@ -26,6 +26,11 @@ if(!defined('DOKU_TAB')) { * Some simplified rendering to $doc is done to gather the page's (text-only) abstract. */ class Doku_Renderer_metadata extends Doku_Renderer { + /** the approximate byte lenght to capture for the abstract */ + const ABSTRACT_LEN = 250; + + /** the maximum UTF8 character length for the abstract */ + const ABSTRACT_MAX = 500; /** @var array transient meta data, will be reset on each rendering */ public $meta = array(); @@ -36,15 +41,18 @@ class Doku_Renderer_metadata extends Doku_Renderer { /** @var array the list of headers used to create unique link ids */ protected $headers = array(); - /** @var bool determines if enough data for a page summary was collected, yet */ - protected $capture = true; - /** @var string temporary $doc store */ protected $store = ''; /** @var string keeps the first image reference */ protected $firstimage = ''; + /** @var bool determines if enough data for the abstract was collected, yet */ + protected $capture = true; + + /** @var int number of bytes captured for abstract */ + protected $captured = 0; + /** * Returns the format produced by this renderer. * @@ -92,8 +100,9 @@ class Doku_Renderer_metadata extends Doku_Renderer { if(!isset($this->meta['description']['abstract'])) { // cut off too long abstracts $this->doc = trim($this->doc); - if(strlen($this->doc) > 500) - $this->doc = utf8_substr($this->doc, 0, 500).'…'; + if(strlen($this->doc) > self::ABSTRACT_MAX) { + $this->doc = utf8_substr($this->doc, 0, self::ABSTRACT_MAX).'…'; + } $this->meta['description']['abstract'] = $this->doc; } @@ -105,6 +114,23 @@ class Doku_Renderer_metadata extends Doku_Renderer { } + /** + * Render plain text data + * + * This function takes care of the amount captured data and will stop capturing when + * enough abstract data is available + * + * @param $text + */ + function cdata($text) { + if(!$this->capture) return; + + $this->doc .= $text; + + $this->captured += strlen($text); + if($this->captured > self::ABSTRACT_LEN) $this->capture = false; + } + /** * Add an item to the TOC * @@ -143,50 +169,35 @@ class Doku_Renderer_metadata extends Doku_Renderer { $this->toc_additem($hid, $text, $level); // add to summary - if($this->capture && ($level > 1)) $this->doc .= DOKU_LF.$text.DOKU_LF; - } - - /** - * Render plain text data - * - * @param $text - */ - function cdata($text) { - if($this->capture) $this->doc .= $text; + $this->cdata(DOKU_LF.$text.DOKU_LF); } /** * Open a paragraph */ function p_open() { - if($this->capture) $this->doc .= DOKU_LF; + $this->cdata(DOKU_LF); } /** * Close a paragraph */ function p_close() { - if($this->capture) { - if(strlen($this->doc) > 250) $this->capture = false; - else $this->doc .= DOKU_LF; - } + $this->cdata(DOKU_LF); } /** * Create a line break */ function linebreak() { - if($this->capture) $this->doc .= DOKU_LF; + $this->cdata(DOKU_LF); } /** * Create a horizontal line */ function hr() { - if($this->capture) { - if(strlen($this->doc) > 250) $this->capture = false; - else $this->doc .= DOKU_LF.'----------'.DOKU_LF; - } + $this->cdata(DOKU_LF.'----------'.DOKU_LF); } /** @@ -226,28 +237,14 @@ class Doku_Renderer_metadata extends Doku_Renderer { * Open an unordered list */ function listu_open() { - if($this->capture) $this->doc .= DOKU_LF; - } - - /** - * Close an unordered list - */ - function listu_close() { - if($this->capture && (strlen($this->doc) > 250)) $this->capture = false; + $this->cdata(DOKU_LF); } /** * Open an ordered list */ function listo_open() { - if($this->capture) $this->doc .= DOKU_LF; - } - - /** - * Close an ordered list - */ - function listo_close() { - if($this->capture && (strlen($this->doc) > 250)) $this->capture = false; + $this->cdata(DOKU_LF); } /** @@ -256,14 +253,14 @@ class Doku_Renderer_metadata extends Doku_Renderer { * @param int $level the nesting level */ function listitem_open($level) { - if($this->capture) $this->doc .= str_repeat(DOKU_TAB, $level).'* '; + $this->cdata(str_repeat(DOKU_TAB, $level).'* '); } /** * Close a list item */ function listitem_close() { - if($this->capture) $this->doc .= DOKU_LF; + $this->cdata(DOKU_LF); } /** @@ -272,25 +269,21 @@ class Doku_Renderer_metadata extends Doku_Renderer { * @param string $text */ function preformatted($text) { - if($this->capture) $this->doc .= $text; + $this->cdata($text); } /** * Start a block quote */ function quote_open() { - if($this->capture) $this->doc .= DOKU_LF.DOKU_TAB.'"'; + $this->cdata(DOKU_LF.DOKU_TAB.'"'); } /** * Stop a block quote */ function quote_close() { - if($this->capture) { - $this->doc .= '"'; - if(strlen($this->doc) > 250) $this->capture = false; - else $this->doc .= DOKU_LF; - } + $this->cdata('"'.DOKU_LF); } /** @@ -301,11 +294,7 @@ class Doku_Renderer_metadata extends Doku_Renderer { * @param string $file file path label */ function file($text, $lang = null, $file = null) { - if($this->capture) { - $this->doc .= DOKU_LF.$text; - if(strlen($this->doc) > 250) $this->capture = false; - else $this->doc .= DOKU_LF; - } + $this->cdata(DOKU_LF.$text.DOKU_LF); } /** @@ -316,11 +305,7 @@ class Doku_Renderer_metadata extends Doku_Renderer { * @param string $file file path label */ function code($text, $language = null, $file = null) { - if($this->capture) { - $this->doc .= DOKU_LF.$text; - if(strlen($this->doc) > 250) $this->capture = false; - else $this->doc .= DOKU_LF; - } + $this->cdata(DOKU_LF.$text.DOKU_LF); } /** @@ -331,7 +316,7 @@ class Doku_Renderer_metadata extends Doku_Renderer { * @param string $acronym */ function acronym($acronym) { - if($this->capture) $this->doc .= $acronym; + $this->cdata($acronym); } /** @@ -342,7 +327,7 @@ class Doku_Renderer_metadata extends Doku_Renderer { * @param string $smiley */ function smiley($smiley) { - if($this->capture) $this->doc .= $smiley; + $this->cdata($smiley); } /** @@ -355,7 +340,7 @@ class Doku_Renderer_metadata extends Doku_Renderer { * @param string $entity */ function entity($entity) { - if($this->capture) $this->doc .= $entity; + $this->cdata($entity); } /** @@ -367,7 +352,7 @@ class Doku_Renderer_metadata extends Doku_Renderer { * @param string|int $y second value */ function multiplyentity($x, $y) { - if($this->capture) $this->doc .= $x.'×'.$y; + $this->cdata($x.'×'.$y); } /** @@ -375,7 +360,7 @@ class Doku_Renderer_metadata extends Doku_Renderer { */ function singlequoteopening() { global $lang; - if($this->capture) $this->doc .= $lang['singlequoteopening']; + $this->cdata($lang['singlequoteopening']); } /** @@ -383,7 +368,7 @@ class Doku_Renderer_metadata extends Doku_Renderer { */ function singlequoteclosing() { global $lang; - if($this->capture) $this->doc .= $lang['singlequoteclosing']; + $this->cdata($lang['singlequoteclosing']); } /** @@ -391,7 +376,7 @@ class Doku_Renderer_metadata extends Doku_Renderer { */ function apostrophe() { global $lang; - if($this->capture) $this->doc .= $lang['apostrophe']; + $this->cdata($lang['apostrophe']); } /** @@ -399,7 +384,7 @@ class Doku_Renderer_metadata extends Doku_Renderer { */ function doublequoteopening() { global $lang; - if($this->capture) $this->doc .= $lang['doublequoteopening']; + $this->cdata($lang['doublequoteopening']); } /** @@ -407,7 +392,7 @@ class Doku_Renderer_metadata extends Doku_Renderer { */ function doublequoteclosing() { global $lang; - if($this->capture) $this->doc .= $lang['doublequoteclosing']; + $this->cdata($lang['doublequoteclosing']); } /** -- cgit v1.2.3 From 8f2758f6b53b11db38d83ea9ad838ed802a6e635 Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Mon, 26 May 2014 10:44:20 +0200 Subject: made $capture public in metadata renderer some plugins (eg. the tag plugin) rely on it --- inc/parser/metadata.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'inc/parser/metadata.php') diff --git a/inc/parser/metadata.php b/inc/parser/metadata.php index 9552fedff..25bf3fe3d 100644 --- a/inc/parser/metadata.php +++ b/inc/parser/metadata.php @@ -48,7 +48,7 @@ class Doku_Renderer_metadata extends Doku_Renderer { protected $firstimage = ''; /** @var bool determines if enough data for the abstract was collected, yet */ - protected $capture = true; + public $capture = true; /** @var int number of bytes captured for abstract */ protected $captured = 0; -- cgit v1.2.3