summaryrefslogtreecommitdiff
path: root/inc/parser/xhtml.php
diff options
context:
space:
mode:
authorAndreas Gohr <andi@splitbrain.org>2006-02-19 00:07:44 +0100
committerAndreas Gohr <andi@splitbrain.org>2006-02-19 00:07:44 +0100
commitc5a8fd9606f6ad8de87e3f7563aa190872e302c0 (patch)
tree7b48fb8c45afc527870a2bc94289fb1378b95ab2 /inc/parser/xhtml.php
parent9fb6b269b0b72fc8009c114029588e1c08b426ba (diff)
downloadrpg-c5a8fd9606f6ad8de87e3f7563aa190872e302c0.tar.gz
rpg-c5a8fd9606f6ad8de87e3f7563aa190872e302c0.tar.bz2
create unique IDs for sections
This patch finally completes the support for unique section IDs. To achive this the mechanism how the TOC is build was changed. The TOC now is build in the renderer only. Currently the TOC will be rendered in the end_document function and is then prepended to the doc. This should ensure compatibility with the rest of the code. Adding support for separating the TOC from the page should now be a simpler task in the future. TODO: - Update base class - remove commented old TOC code - make sure no other parts of the code use any of the old TOC code darcs-hash:20060218230744-7ad00-40c5463d93f8ae1c543fb4fed747b2047b4c1302.gz
Diffstat (limited to 'inc/parser/xhtml.php')
-rw-r--r--inc/parser/xhtml.php76
1 files changed, 72 insertions, 4 deletions
diff --git a/inc/parser/xhtml.php b/inc/parser/xhtml.php
index 1c7343cda..a54e5bcab 100644
--- a/inc/parser/xhtml.php
+++ b/inc/parser/xhtml.php
@@ -25,7 +25,10 @@ require_once DOKU_INC . 'inc/parser/renderer.php';
*/
class Doku_Renderer_xhtml extends Doku_Renderer {
- var $doc = '';
+ // @access public
+ var $doc = ''; // will contain the whole document
+ var $toc = array(); // will contain the Table of Contents
+
var $headers = array();
@@ -42,6 +45,9 @@ class Doku_Renderer_xhtml extends Doku_Renderer {
var $store = '';
function document_start() {
+ //reset some internals
+ $this->toc = array();
+ $this->headers = array();
}
function document_end() {
@@ -81,8 +87,39 @@ class Doku_Renderer_xhtml extends Doku_Renderer {
}
$this->doc .= '</div>'.DOKU_LF;
}
+
+ // prepend the TOC
+ $this->doc = $this->render_TOC().$this->doc;
+ }
+
+ /**
+ * Return the TOC rendered to XHTML
+ *
+ * @author Andreas Gohr <andi@splitbrain.org>
+ */
+ function render_TOC(){
+ if(!count($this->toc)) return '';
+ global $lang;
+ $out = '<div class="toc">'.DOKU_LF;
+ $out .= '<div class="tocheader toctoggle" id="toc__header">';
+ $out .= $lang['toc'];
+ $out .= '</div>'.DOKU_LF;
+ $out .= '<div id="toc__inside">'.DOKU_LF;
+ $out .= html_buildlist($this->toc,'toc',array($this,'_tocitem'));
+ $out .= '</div>'.DOKU_LF.'</div>'.DOKU_LF;
+ return $out;
}
+ /**
+ * Callback for html_buildlist
+ */
+ function _tocitem($item){
+ return '<span class="li"><a href="#'.$item['hid'].'" class="toc">'.
+ $this->_xmlEntities($item['title']).'</a></span>';
+ }
+
+/** FIXME deprecated
+
function toc_open() {
global $lang;
$this->doc .= '<div class="toc">'.DOKU_LF;
@@ -122,6 +159,8 @@ class Doku_Renderer_xhtml extends Doku_Renderer {
$this->doc .= '</div>'.DOKU_LF.'</div>'.DOKU_LF;
}
+*/
+
function header($text, $level, $pos) {
global $conf;
//handle section editing
@@ -132,7 +171,21 @@ class Doku_Renderer_xhtml extends Doku_Renderer {
$this->lastsec = $pos;
}
- $this->doc .= DOKU_LF.'<h'.$level.'><a name="'.$this->_headerToLink($text).'" id="'.$this->_headerToLink($text).'">';
+ // create a unique header id
+ $hid = $this->_headerToLink($text,'true');
+
+ //handle TOC
+//FIXME if($this->meta['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);
+ }
+
+ // write the header
+ $this->doc .= DOKU_LF.'<h'.$level.'><a name="'.$hid.'" id="'.$hid.'">';
$this->doc .= $this->_xmlEntities($text);
$this->doc .= "</a></h$level>".DOKU_LF;
}
@@ -969,11 +1022,26 @@ class Doku_Renderer_xhtml extends Doku_Renderer {
/**
* Creates a linkid from a headline
+ *
+ * @param string $title The headline title
+ * @param boolean $create Create a new unique ID?
+ * @author Andreas Gohr <andi@splitbrain.org>
*/
- function _headerToLink($title) {
- $title = str_replace(':','',cleanID($title,true));
+ function _headerToLink($title,$create=false) {
+ $title = str_replace(':','',cleanID($title,true)); //force ASCII
$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 = 1 : $num++;
+ }
+ $title = $title.$num;
+ $this->headers[] = $title;
+ }
+
return $title;
}