* @author Andreas Gohr */ if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/'); if ( !defined('DOKU_LF') ) { // Some whitespace to help View > Source define ('DOKU_LF',"\n"); } if ( !defined('DOKU_TAB') ) { // Some whitespace to help View > Source define ('DOKU_TAB',"\t"); } require_once DOKU_INC . 'inc/parser/renderer.php'; require_once DOKU_INC . 'inc/html.php'; /** * The Renderer */ class Doku_Renderer_xhtml extends Doku_Renderer { // @access public var $doc = ''; // will contain the whole document var $toc = array(); // will contain the Table of Contents var $headers = array(); var $footnotes = array(); var $acronyms = array(); var $smileys = array(); var $badwords = array(); var $entities = array(); var $interwiki = array(); var $lastsec = 0; var $store = ''; function document_start() { //reset some internals $this->toc = array(); $this->headers = array(); } function document_end() { if ( count ($this->footnotes) > 0 ) { $this->doc .= '
'.DOKU_LF; $id = 0; foreach ( $this->footnotes as $footnote ) { $id++; // the number of the current footnote // check its not a placeholder that indicates actual footnote text is elsewhere if (substr($footnote, 0, 5) != "@@FNT") { // open the footnote and set the anchor and backlink $this->doc .= '
'; $this->doc .= ''; $this->doc .= $id.') '.DOKU_LF; // get any other footnotes that use the same markup $alt = array_keys($this->footnotes, "@@FNT$id"); if (count($alt)) { foreach ($alt as $ref) { // set anchor and backlink for the other footnotes $this->doc .= ', '; $this->doc .= ($ref+1).') '.DOKU_LF; } } // add footnote markup and close this footnote $this->doc .= $footnote; $this->doc .= '
' . DOKU_LF; } } $this->doc .= '
'.DOKU_LF; } // prepend the TOC if($this->info['toc']){ $this->doc = $this->render_TOC().$this->doc; } // make sure there are no empty paragraphs $this->doc = preg_replace('#

\s*

#','',$this->doc); } /** * Return the TOC rendered to XHTML * * @author Andreas Gohr */ function render_TOC(){ if(count($this->toc) < 3) return ''; global $lang; $out = '
'.DOKU_LF; $out .= '
'; $out .= $lang['toc']; $out .= '
'.DOKU_LF; $out .= '
'.DOKU_LF; $out .= html_buildlist($this->toc,'toc',array($this,'_tocitem')); $out .= '
'.DOKU_LF.'
'.DOKU_LF; return $out; } /** * Callback for html_buildlist */ function _tocitem($item){ return ''. $this->_xmlEntities($item['title']).''; } function header($text, $level, $pos) { global $conf; // create a unique header id $hid = $this->_headerToLink($text,'true'); //handle TOC if($level >= $conf['toptoclevel'] && $level <= $conf['maxtoclevel']){ // the TOC is one of our standard ul list arrays ;-) $this->toc[] = array( 'hid' => $hid, 'title' => $text, 'type' => 'ul', 'level' => $level-$conf['toptoclevel']+1); } // write the header $this->doc .= DOKU_LF.''.$this->_xmlEntities($text)."".DOKU_LF; } /** * Section edit marker is replaced by an edit button when * the page is editable. Replacement done in 'inc/html.php#html_secedit' * * @author Andreas Gohr * @author Ben Coburn */ function section_edit($start, $end, $level, $name) { global $conf; if ($start!=-1 && $level<=$conf['maxseclevel']) { $name = str_replace('"', '', $name); $this->doc .= ''; } } function section_open($level) { $this->doc .= "
".DOKU_LF; } function section_close() { $this->doc .= DOKU_LF.'
'.DOKU_LF; } function cdata($text) { $this->doc .= $this->_xmlEntities($text); } function p_open() { $this->doc .= DOKU_LF.'

'.DOKU_LF; } function p_close() { $this->doc .= DOKU_LF.'

'.DOKU_LF; } function linebreak() { $this->doc .= '
'.DOKU_LF; } function hr() { $this->doc .= '
'.DOKU_LF; } function strong_open() { $this->doc .= ''; } function strong_close() { $this->doc .= ''; } function emphasis_open() { $this->doc .= ''; } function emphasis_close() { $this->doc .= ''; } function underline_open() { $this->doc .= ''; } function underline_close() { $this->doc .= ''; } function monospace_open() { $this->doc .= ''; } function monospace_close() { $this->doc .= ''; } function subscript_open() { $this->doc .= ''; } function subscript_close() { $this->doc .= ''; } function superscript_open() { $this->doc .= ''; } function superscript_close() { $this->doc .= ''; } function deleted_open() { $this->doc .= ''; } function deleted_close() { $this->doc .= ''; } /** * Callback for footnote start syntax * * All following content will go to the footnote instead of * the document. To achieve this the previous rendered content * is moved to $store and $doc is cleared * * @author Andreas Gohr */ function footnote_open() { // move current content to store and record footnote $this->store = $this->doc; $this->doc = ''; } /** * Callback for footnote end syntax * * All rendered content is moved to the $footnotes array and the old * content is restored from $store again * * @author Andreas Gohr */ function footnote_close() { // recover footnote into the stack and restore old content $footnote = $this->doc; $this->doc = $this->store; $this->store = ''; // check to see if this footnote has been seen before $i = array_search($footnote, $this->footnotes); if ($i === false) { // its a new footnote, add it to the $footnotes array $id = count($this->footnotes)+1; $this->footnotes[count($this->footnotes)] = $footnote; } else { // seen this one before, translate the index to an id and save a placeholder $i++; $id = count($this->footnotes)+1; $this->footnotes[count($this->footnotes)] = "@@FNT".($i); } // output the footnote reference and link, incl. onmouseover for insitu footnote popup $this->doc .= ''.$id.')'; } function listu_open() { $this->doc .= '
    '.DOKU_LF; } function listu_close() { $this->doc .= '
'.DOKU_LF; } function listo_open() { $this->doc .= '
    '.DOKU_LF; } function listo_close() { $this->doc .= '
'.DOKU_LF; } function listitem_open($level) { $this->doc .= '
  • '; } function listitem_close() { $this->doc .= '
  • '.DOKU_LF; } function listcontent_open() { $this->doc .= '
    '; } function listcontent_close() { $this->doc .= '
    '.DOKU_LF; } function unformatted($text) { $this->doc .= $this->_xmlEntities($text); } /** * Execute PHP code if allowed * * @author Andreas Gohr */ function php($text) { global $conf; if($conf['phpok']){ ob_start(); eval($text); $this->doc .= ob_get_contents(); ob_end_clean(); }else{ $this->file($text); } } /** * Insert HTML if allowed * * @author Andreas Gohr */ function html($text) { global $conf; if($conf['htmlok']){ $this->doc .= $text; }else{ $this->file($text); } } function preformatted($text) { $this->doc .= '
    ' . $this->_xmlEntities($text) . '
    '. DOKU_LF; } function file($text) { $this->doc .= '
    ' . $this->_xmlEntities($text). '
    '. DOKU_LF; } function quote_open() { $this->doc .= '
    '.DOKU_LF; } function quote_close() { $this->doc .= '
    '.DOKU_LF; } /** * Callback for code text * * Uses GeSHi to highlight language syntax * * @author Andreas Gohr */ function code($text, $language = NULL) { global $conf; if ( is_null($language) ) { $this->preformatted($text); } else { //strip leading and trailing blank line $text = preg_replace('/^\s*?\n/','',$text); $text = preg_replace('/\s*?\n$/','',$text); $this->doc .= p_xhtml_cached_geshi($text, $language); } } function geshi_cached($text = '') { $this->doc .= $text; } function acronym($acronym) { if ( array_key_exists($acronym, $this->acronyms) ) { $title = $this->_xmlEntities($this->acronyms[$acronym]); $this->doc .= ''.$this->_xmlEntities($acronym).''; } else { $this->doc .= $this->_xmlEntities($acronym); } } function smiley($smiley) { if ( array_key_exists($smiley, $this->smileys) ) { $title = $this->_xmlEntities($this->smileys[$smiley]); $this->doc .= ''.
                    $this->_xmlEntities($smiley).''; } else { $this->doc .= $this->_xmlEntities($smiley); } } /* * not used function wordblock($word) { if ( array_key_exists($word, $this->badwords) ) { $this->doc .= '** BLEEP **'; } else { $this->doc .= $this->_xmlEntities($word); } } */ function entity($entity) { if ( array_key_exists($entity, $this->entities) ) { $this->doc .= $this->entities[$entity]; } else { $this->doc .= $this->_xmlEntities($entity); } } function multiplyentity($x, $y) { $this->doc .= "$x×$y"; } function singlequoteopening() { $this->doc .= "‘"; } function singlequoteclosing() { $this->doc .= "’"; } function doublequoteopening() { $this->doc .= "“"; } function doublequoteclosing() { $this->doc .= "”"; } /** */ function camelcaselink($link) { $this->internallink($link,$link); } function locallink($hash, $name = NULL){ global $ID; $name = $this->_getLinkTitle($name, $hash, $isImage); $hash = $this->_headerToLink($hash); $title = $ID.' ↵'; $this->doc .= ''; $this->doc .= $name; $this->doc .= ''; } /** * Render an internal Wiki Link * * $search and $returnonly are not for the renderer but are used * elsewhere - no need to implement them in other renderers * * @author Andreas Gohr */ function internallink($id, $name = NULL, $search=NULL,$returnonly=false) { global $conf; global $ID; // default name is based on $id as given $default = $this->_simpleTitle($id); // now first resolve and clean up the $id resolve_pageid(getNS($ID),$id,$exists); $name = $this->_getLinkTitle($name, $default, $isImage, $id); if ( !$isImage ) { if ( $exists ) { $class='wikilink1'; } else { $class='wikilink2'; } } else { $class='media'; } //keep hash anchor list($id,$hash) = explode('#',$id,2); //prepare for formating $link['target'] = $conf['target']['wiki']; $link['style'] = ''; $link['pre'] = ''; $link['suf'] = ''; // highlight link to current page if ($id == $ID) { $link['pre'] = ''; $link['suf'] = ''; } $link['more'] = ''; $link['class'] = $class; $link['url'] = wl($id); $link['name'] = $name; $link['title'] = $id; //add search string if($search){ ($conf['userewrite']) ? $link['url'].='?s=' : $link['url'].='&s='; $link['url'] .= rawurlencode($search); } //keep hash if($hash) $link['url'].='#'.$hash; //output formatted if($returnonly){ return $this->_formatLink($link); }else{ $this->doc .= $this->_formatLink($link); } } function externallink($url, $name = NULL) { global $conf; $name = $this->_getLinkTitle($name, $url, $isImage); // add protocol on simple short URLs if(substr($url,0,3) == 'ftp' && (substr($url,0,6) != 'ftp://')) $url = 'ftp://'.$url; if(substr($url,0,3) == 'www') $url = 'http://'.$url; if ( !$isImage ) { $class='urlextern'; } else { $class='media'; } //prepare for formating $link['target'] = $conf['target']['extern']; $link['style'] = ''; $link['pre'] = ''; $link['suf'] = ''; $link['more'] = ''; $link['class'] = $class; $link['url'] = $url; $link['name'] = $name; $link['title'] = $this->_xmlEntities($url); if($conf['relnofollow']) $link['more'] .= ' rel="nofollow"'; //output formatted $this->doc .= $this->_formatLink($link); } /** */ function interwikilink($match, $name = NULL, $wikiName, $wikiUri) { global $conf; $link = array(); $link['target'] = $conf['target']['interwiki']; $link['pre'] = ''; $link['suf'] = ''; $link['more'] = ''; $link['name'] = $this->_getLinkTitle($name, $wikiUri, $isImage); //get interwiki URL if ( isset($this->interwiki[$wikiName]) ) { $url = $this->interwiki[$wikiName]; } else { // Default to Google I'm feeling lucky $url = 'http://www.google.com/search?q={URL}&btnI=lucky'; $wikiName = 'go'; } if ( !$isImage ) { $class = preg_replace('/[^_\-a-z0-9]+/i','_',$wikiName); $link['class'] = "interwiki iw_$class"; } else { $link['class'] = 'media'; } //do we stay at the same server? Use local target if( strpos($url,DOKU_URL) === 0 ){ $link['target'] = $conf['target']['wiki']; } //split into hash and url part list($wikiUri,$hash) = explode('#',$wikiUri,2); //replace placeholder if(preg_match('#\{(URL|NAME|SCHEME|HOST|PORT|PATH|QUERY)\}#',$url)){ //use placeholders $url = str_replace('{URL}',rawurlencode($wikiUri),$url); $url = str_replace('{NAME}',$wikiUri,$url); $parsed = parse_url($wikiUri); if(!$parsed['port']) $parsed['port'] = 80; $url = str_replace('{SCHEME}',$parsed['scheme'],$url); $url = str_replace('{HOST}',$parsed['host'],$url); $url = str_replace('{PORT}',$parsed['port'],$url); $url = str_replace('{PATH}',$parsed['path'],$url); $url = str_replace('{QUERY}',$parsed['query'],$url); $link['url'] = $url; }else{ //default $link['url'] = $url.rawurlencode($wikiUri); } if($hash) $link['url'] .= '#'.rawurlencode($hash); $link['title'] = htmlspecialchars($link['url']); //output formatted $this->doc .= $this->_formatLink($link); } /** */ function windowssharelink($url, $name = NULL) { global $conf; global $lang; //simple setup $link['target'] = $conf['target']['windows']; $link['pre'] = ''; $link['suf'] = ''; $link['style'] = ''; //Display error on browsers other than IE $link['more'] = 'onclick="if(document.all == null){alert(\''. $this->_xmlEntities($lang['nosmblinks'],ENT_QUOTES). '\');}" onkeypress="if(document.all == null){alert(\''. $this->_xmlEntities($lang['nosmblinks'],ENT_QUOTES).'\');}"'; $link['name'] = $this->_getLinkTitle($name, $url, $isImage); if ( !$isImage ) { $link['class'] = 'windows'; } else { $link['class'] = 'media'; } $link['title'] = $this->_xmlEntities($url); $url = str_replace('\\','/',$url); $url = 'file:///'.$url; $link['url'] = $url; //output formatted $this->doc .= $this->_formatLink($link); } function emaillink($address, $name = NULL) { global $conf; //simple setup $link = array(); $link['target'] = ''; $link['pre'] = ''; $link['suf'] = ''; $link['style'] = ''; $link['more'] = ''; $name = $this->_getLinkTitle($name, $address, $isImage); if ( !$isImage ) { $link['class']='mail JSnocheck'; } else { $link['class']='media JSnocheck'; } $address = $this->_xmlEntities($address); $address = obfuscate($address); $title = $address; if(empty($name)){ $name = $address; } #elseif($isImage{ # $name = $this->_xmlEntities($name); # } if($conf['mailguard'] == 'visible') $address = rawurlencode($address); $link['url'] = 'mailto:'.$address; $link['name'] = $name; $link['title'] = $title; //output formatted $this->doc .= $this->_formatLink($link); } function internalmedia ($src, $title=NULL, $align=NULL, $width=NULL, $height=NULL, $cache=NULL, $linking=NULL) { global $conf; global $ID; resolve_mediaid(getNS($ID),$src, $exists); $link = array(); $link['class'] = 'media'; $link['style'] = ''; $link['pre'] = ''; $link['suf'] = ''; $link['more'] = ''; $link['target'] = $conf['target']['media']; $noLink = false; $link['title'] = $this->_xmlEntities($src); list($ext,$mime) = mimetype($src); if(substr($mime,0,5) == 'image'){ $link['url'] = ml($src,array('id'=>$ID,'cache'=>$cache),($linking=='direct')); }elseif($mime == 'application/x-shockwave-flash'){ // don't link flash movies $noLink = TRUE; }else{ // add file icons $class = preg_replace('/[^_\-a-z0-9]+/i','_',$ext); $link['class'] .= ' mediafile mf_'.$class; $link['url'] = ml($src,array('id'=>$ID,'cache'=>$cache),true); } $link['name'] = $this->_media ($src, $title, $align, $width, $height, $cache); //output formatted if ($linking == 'nolink' || $noLink) $this->doc .= $link['name']; else $this->doc .= $this->_formatLink($link); } /** * @todo don't add link for flash */ function externalmedia ($src, $title=NULL, $align=NULL, $width=NULL, $height=NULL, $cache=NULL, $linking=NULL) { global $conf; $link = array(); $link['class'] = 'media'; $link['style'] = ''; $link['pre'] = ''; $link['suf'] = ''; $link['more'] = ''; $link['target'] = $conf['target']['media']; $link['title'] = $this->_xmlEntities($src); $link['url'] = ml($src,array('cache'=>$cache)); $link['name'] = $this->_media ($src, $title, $align, $width, $height, $cache); $noLink = false; list($ext,$mime) = mimetype($src); if(substr($mime,0,5) == 'image'){ // link only jpeg images // if ($ext != 'jpg' && $ext != 'jpeg') $noLink = TRUE; }elseif($mime == 'application/x-shockwave-flash'){ // don't link flash movies $noLink = TRUE; }else{ // add file icons $link['class'] .= ' mediafile mf_'.$ext; } //output formatted if ($linking == 'nolink' || $noLink) $this->doc .= $link['name']; else $this->doc .= $this->_formatLink($link); } /** * Renders an RSS feed * * @author Andreas Gohr */ function rss ($url,$params){ global $lang; global $conf; require_once(DOKU_INC.'inc/FeedParser.php'); $feed = new FeedParser(); $feed->feed_url($url); //disable warning while fetching if (!defined('DOKU_E_LEVEL')) { $elvl = error_reporting(E_ERROR); } $rc = $feed->init(); if (!defined('DOKU_E_LEVEL')) { error_reporting($elvl); } //decide on start and end if($params['reverse']){ $mod = -1; $start = $feed->get_item_quantity()-1; $end = $start - ($params['max']); $end = ($end < -1) ? -1 : $end; }else{ $mod = 1; $start = 0; $end = $feed->get_item_quantity(); $end = ($end > $params['max']) ? $params['max'] : $end;; } $this->doc .= '
      '; if($rc){ for ($x = $start; $x != $end; $x += $mod) { $item = $feed->get_item($x); $this->doc .= '
    • '; $this->externallink($item->get_permalink(), $item->get_title()); if($params['author']){ $author = $item->get_author(0); if($author){ $name = $author->get_name(); if(!$name) $name = $author->get_email(); if($name) $this->doc .= ' '.$lang['by'].' '.$name; } } if($params['date']){ $this->doc .= ' ('.$item->get_date($conf['dformat']).')'; } if($params['details']){ $this->doc .= '
      '; if($htmlok){ $this->doc .= $item->get_description(); }else{ $this->doc .= strip_tags($item->get_description()); } $this->doc .= '
      '; } $this->doc .= '
    • '; } }else{ $this->doc .= '
    • '; $this->doc .= ''.$lang['rssfailed'].''; $this->externallink($url); $this->doc .= '
    • '; } $this->doc .= '
    '; } // $numrows not yet implemented function table_open($maxcols = NULL, $numrows = NULL){ $this->doc .= ''.DOKU_LF; } function table_close(){ $this->doc .= '
    '.DOKU_LF; } function tablerow_open(){ $this->doc .= DOKU_TAB . '' . DOKU_LF . DOKU_TAB . DOKU_TAB; } function tablerow_close(){ $this->doc .= DOKU_LF . DOKU_TAB . '' . DOKU_LF; } function tableheader_open($colspan = 1, $align = NULL){ $this->doc .= 'doc .= ' class="'.$align.'align"'; } if ( $colspan > 1 ) { $this->doc .= ' colspan="'.$colspan.'"'; } $this->doc .= '>'; } function tableheader_close(){ $this->doc .= ''; } function tablecell_open($colspan = 1, $align = NULL){ $this->doc .= 'doc .= ' class="'.$align.'align"'; } if ( $colspan > 1 ) { $this->doc .= ' colspan="'.$colspan.'"'; } $this->doc .= '>'; } function tablecell_close(){ $this->doc .= ''; } //---------------------------------------------------------- // Utils /** * Build a link * * Assembles all parts defined in $link returns HTML for the link * * @author Andreas Gohr */ function _formatLink($link){ //make sure the url is XHTML compliant (skip mailto) if(substr($link['url'],0,7) != 'mailto:'){ $link['url'] = str_replace('&','&',$link['url']); $link['url'] = str_replace('&amp;','&',$link['url']); } //remove double encodings in titles $link['title'] = str_replace('&amp;','&',$link['title']); // be sure there are no bad chars in url or title // (we can't do this for name because it can contain an img tag) $link['url'] = strtr($link['url'],array('>'=>'%3E','<'=>'%3C','"'=>'%22')); $link['title'] = strtr($link['title'],array('>'=>'>','<'=>'<','"'=>'"')); $ret = ''; $ret .= $link['pre']; $ret .= ' */ function _simpleTitle($name){ global $conf; //if there is a hash we use the ancor name only list($name,$hash) = explode('#',$name,2); if($hash) return $hash; //trim colons of a namespace link $name = rtrim($name,':'); if($conf['useslash']){ $nssep = '[:;/]'; }else{ $nssep = '[:;]'; } $name = preg_replace('!.*'.$nssep.'!','',$name); if(!$name) return $this->_simpleTitle($conf['start']); return $name; } /** * Renders internal and external media * * @author Andreas Gohr */ function _media ($src, $title=NULL, $align=NULL, $width=NULL, $height=NULL, $cache=NULL) { $ret = ''; list($ext,$mime) = mimetype($src); if(substr($mime,0,5) == 'image'){ //add image tag $ret .= '_xmlEntities($title).'"'; $ret .= ' alt="'.$this->_xmlEntities($title).'"'; }elseif($ext == 'jpg' || $ext == 'jpeg'){ //try to use the caption from IPTC/EXIF require_once(DOKU_INC.'inc/JpegMeta.php'); $jpeg = new JpegMeta(mediaFN($src)); if($jpeg !== false) $cap = $jpeg->getTitle(); if($cap){ $ret .= ' title="'.$this->_xmlEntities($cap).'"'; $ret .= ' alt="'.$this->_xmlEntities($cap).'"'; } }else{ $ret .= ' alt=""'; } if ( !is_null($width) ) $ret .= ' width="'.$this->_xmlEntities($width).'"'; if ( !is_null($height) ) $ret .= ' height="'.$this->_xmlEntities($height).'"'; $ret .= ' />'; }elseif($mime == 'application/x-shockwave-flash'){ $ret .= '_xmlEntities($width).'"'; if ( !is_null($height) ) $ret .= ' height="'.$this->_xmlEntities($height).'"'; $ret .= '>'.DOKU_LF; $ret .= ''.DOKU_LF; $ret .= ''.DOKU_LF; $ret .= '_xmlEntities($width).'"'; if ( !is_null($height) ) $ret .= ' height="'.$this->_xmlEntities($height).'"'; $ret .= ' type="application/x-shockwave-flash"'. ' pluginspage="http://www.macromedia.com/go/getflashplayer">'.DOKU_LF; $ret .= ''.DOKU_LF; }elseif(!is_null($title)){ // well at least we have a title to display $ret .= $this->_xmlEntities($title); }else{ // just show the sourcename $ret .= $this->_xmlEntities(noNS($src)); } return $ret; } function _xmlEntities($string) { return htmlspecialchars($string); } /** * Creates a linkid from a headline * * @param string $title The headline title * @param boolean $create Create a new unique ID? * @author Andreas Gohr */ function _headerToLink($title,$create=false) { $title = str_replace(':','',cleanID($title)); $title = ltrim($title,'0123456789._-'); if(empty($title)) $title='section'; if($create){ // make sure tiles are unique $num = ''; while(in_array($title.$num,$this->headers)){ ($num) ? $num++ : $num = 1; } $title = $title.$num; $this->headers[] = $title; } return $title; } /** * Construct a title and handle images in titles * * @author Harry Fuecks */ function _getLinkTitle($title, $default, & $isImage, $id=NULL) { global $conf; $isImage = FALSE; if ( is_null($title) ) { if ($conf['useheading'] && $id) { $heading = p_get_first_heading($id); if ($heading) { return $this->_xmlEntities($heading); } } return $this->_xmlEntities($default); } else if ( is_string($title) ) { return $this->_xmlEntities($title); } else if ( is_array($title) ) { $isImage = TRUE; return $this->_imageTitle($title); } } /** * Returns an HTML code for images used in link titles * * @todo Resolve namespace on internal images * @author Andreas Gohr */ function _imageTitle($img) { return $this->_media($img['src'], $img['title'], $img['align'], $img['width'], $img['height'], $img['cache']); } } //Setup VIM: ex: et ts=4 enc=utf-8 :