1<?php
2/**
3 * Render Plugin for XHTML output with preserved linebreaks and markup in headers
4 *
5 * @author Max Westen <max@dlmax.org>
6 * @author Chris Smith <chris@jalakai.co.uk>
7 * @author Danny Lin <danny0838[at]pchome[dot]com[dot]tw>
8 */
9
10
11if(!defined('DOKU_INC')) die();
12if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
13
14require_once DOKU_INC . 'inc/parser/xhtml.php';
15
16/**
17 * The Renderer
18 */
19class renderer_plugin_header3 extends Doku_Renderer_xhtml {
20
21  function canRender($format) {
22    return ($format=='xhtml');
23  }
24
25  function reset() {
26    $this->doc = '';
27    $this->footnotes = array();
28    $this->lastsec = 0;
29    $this->store = '';
30    $this->_counter = array();
31  }
32
33
34  /**
35   * Replace the normal linebreak and render it as an XHTML Linebreak
36   * @param $text the text to output
37   */
38  function cdata($text) {
39    $this->doc .= str_replace("\n","<br />\n",$this->_xmlEntities($text));
40  }
41
42
43  /**
44   * $sectionedits is private and cannot be accessed by plugins,
45   * so this dirty hack is required
46   *
47   * Get rid of this renderer if it's fixed...
48   */
49  public $sectionedits = array(); // A stack of section edit data
50
51  /**
52   * Copied from xhtml.php, no change
53   */
54  public function startSectionEdit($start, $type, $title = null) {
55    static $lastsecid = 0;
56    $this->sectionedits[] = array(++$lastsecid, $start, $type, $title);
57    return 'sectionedit' . $lastsecid;
58  }
59
60  public function finishSectionEdit($end = null) {
61    list($id, $start, $type, $title) = array_pop($this->sectionedits);
62    if (!is_null($end) && $end <= $start) {
63      return;
64    }
65    $this->doc .= "<!-- EDIT$id " . strtoupper($type) . ' ';
66    if (!is_null($title)) {
67      $this->doc .= '"' . str_replace('"', '', $title) . '" ';
68    }
69    $this->doc .= "[$start-" . (is_null($end) ? '' : $end) . '] -->';
70  }
71
72  function document_end() {
73    // Finish open section edits.
74    while (count($this->sectionedits) > 0) {
75      if ($this->sectionedits[count($this->sectionedits) - 1][1] <= 1) {
76        // If there is only one section, do not write a section edit
77        // marker.
78        array_pop($this->sectionedits);
79      } else {
80        $this->finishSectionEdit();
81      }
82    }
83
84    if ( count ($this->footnotes) > 0 ) {
85      $this->doc .= '<div class="footnotes">'.DOKU_LF;
86
87      $id = 0;
88      foreach ( $this->footnotes as $footnote ) {
89        $id++;   // the number of the current footnote
90
91        // check its not a placeholder that indicates actual footnote text is elsewhere
92        if (substr($footnote, 0, 5) != "@@FNT") {
93
94          // open the footnote and set the anchor and backlink
95          $this->doc .= '<div class="fn">';
96          $this->doc .= '<sup><a href="#fnt__'.$id.'" id="fn__'.$id.'" name="fn__'.$id.'" class="fn_bot">';
97          $this->doc .= $id.')</a></sup> '.DOKU_LF;
98
99          // get any other footnotes that use the same markup
100          $alt = array_keys($this->footnotes, "@@FNT$id");
101
102          if (count($alt)) {
103            foreach ($alt as $ref) {
104              // set anchor and backlink for the other footnotes
105              $this->doc .= ', <sup><a href="#fnt__'.($ref+1).'" id="fn__'.($ref+1).'" name="fn__'.($ref+1).'" class="fn_bot">';
106              $this->doc .= ($ref+1).')</a></sup> '.DOKU_LF;
107            }
108          }
109
110          // add footnote markup and close this footnote
111          $this->doc .= $footnote;
112          $this->doc .= '</div>' . DOKU_LF;
113        }
114      }
115      $this->doc .= '</div>'.DOKU_LF;
116    }
117
118    // Prepare the TOC
119    global $conf;
120    if($this->info['toc'] && is_array($this->toc) && $conf['tocminheads'] && count($this->toc) >= $conf['tocminheads']){
121      global $TOC;
122      $TOC = $this->toc;
123    }
124
125    // make sure there are no empty paragraphs
126    $this->doc = preg_replace('#<p>\s*</p>#','',$this->doc);
127  }
128}
129
130//Setup VIM: ex: et ts=4 enc=utf-8 :
131