Commit 04d945a1 authored by jepso's avatar jepso

fixes #527

parent c5e60f3e
<?php
class Text_Wiki_Render_Latex_Code extends Text_Wiki_Render {
/**
*
*
* Renders a token into text matching the requested format.
*
*
* @access public
*
*
* @param array $options The "options" portion of the token (second
* element).
*
*
* @return string The text rendered from the token options.
*
*
*/
function token($options)
{
$text = $options['text'];
$attr = $options['attr'];
$type = strtolower($attr['type']);
if ($type == 'php') {
if (substr($options['text'], 0, 5) != '<?php') {
// PHP code example:
......@@ -29,9 +29,9 @@ class Text_Wiki_Render_Latex_Code extends Text_Wiki_Render {
$text = "<?php\n\n" . $options['text'] . "\n\n?>"; // <?php
}
}
$text = "\\begin{lstlisting}\n$text\n\\end{lstlisting}\n\n";
$text = "\\begin{lstlisting}[caption={} {}]\n$text\n\\end{lstlisting}\n\n";
if ($type != '') {
$text = "\\lstset{language=$type}\n" . $text;
} else {
......
......@@ -30,7 +30,8 @@ class Text_Wiki_Render_Xhtml_Code extends Text_Wiki_Render {
'css_code' => null, // class for generic <code>
'css_php' => null, // class for PHP <code>
'css_html' => null, // class for HTML <code>
'css_filename' => null // class for optional filename <div>
'css_filename' => null, // class for optional filename <div>
'code_begin_callback' => null
);
/**
......@@ -47,7 +48,7 @@ class Text_Wiki_Render_Xhtml_Code extends Text_Wiki_Render {
*/
function token($options)
{
{
$text = $options['text'];
$attr = $options['attr'];
$type = strtolower($attr['type']);
......@@ -104,6 +105,11 @@ class Text_Wiki_Render_Xhtml_Code extends Text_Wiki_Render {
$attr['filename'] . '</div>' . $text;
}
$callback = $this->getConf('code_begin_callback');
if ($callback) {
$text = call_user_func($callback) . $text;
}
return "\n$text\n\n";
}
}
......
......@@ -46,6 +46,10 @@ p, ul, ol, dl {
font-size: small;
}
p.caption {
font-style: italic;
}
dd {
margin-bottom: 0.5em;
}
......@@ -110,7 +114,7 @@ pre {
color: black;
background-color: #f7f7f7;
border: 1px solid #d7d7d7;
/* Pup's Box Flow Hack, Rev 2 */
overflow/**/: auto;
\height: 1%;
......@@ -120,9 +124,9 @@ pre {
pre, x:-moz-any-link {
/* overflow: auto causes artefacts */
overflow: hidden;
/* prevents text to be hidden by wrapping it */
white-space: -moz-pre-wrap !important;
white-space: -moz-pre-wrap !important;
}
pre .default {
......
......@@ -34,28 +34,32 @@ class Sensei_Doc_Renderer_Xhtml extends Sensei_Doc_Renderer
{
/**
* Available options
*
*
* (Sensei_Doc_Section|null) section :
* Section to be rendered. If null all sections will be rendered.
*
* (string) url_prefix :
* All URLs pointing to sections will be prefixed with this.
* All URLs pointing to sections will be prefixed with this.
*/
protected $_options = array(
'section' => null,
'url_prefix' => ''
);
private $_chapter;
private $_codeListingsIndex;
public function __construct(Sensei_Doc_Toc $toc, array $options = array())
{
parent::__construct($toc, $options);
$this->_wiki->setRenderConf('xhtml', 'Doclink', 'url_callback', array(&$this, 'makeUrl'));
$this->_wiki->setRenderConf('xhtml', 'Code', 'code_begin_callback', array(&$this, 'codeListingsNumberingCallback'));
}
/**
* Renders table of contents as nested unordered lists.
*
*
* @return string rendered table of contents
*/
public function renderToc()
......@@ -72,7 +76,7 @@ class Sensei_Doc_Renderer_Xhtml extends Sensei_Doc_Renderer
protected function _renderToc($section)
{
$output = '';
if ($section instanceof Sensei_Doc_Toc) {
$class = ' class="tree"';
} elseif ($section !== $this->_options['section']) {
......@@ -80,27 +84,27 @@ class Sensei_Doc_Renderer_Xhtml extends Sensei_Doc_Renderer
} else {
$class = '';
}
$output .= '<ul' . $class . '>' . "\n";
$output .= '<ul' . $class . '>' . "\n";
for ($i = 0; $i < $section->count(); $i++) {
$child = $section->getChild($i);
$text = $child->getIndex() . ' ' . $child->getName();
$href = $this->makeUrl($child);
$output .= '<li><a href="' . $href . '">' . $text . '</a>';
if ($child->count() > 0) {
$output .= "\n";
$output .= $this->_renderToc($child);
}
$output .= '</li>' . "\n";
}
$output .= '</ul>' . "\n";
return $output;
}
......@@ -113,29 +117,29 @@ class Sensei_Doc_Renderer_Xhtml extends Sensei_Doc_Renderer
public function render()
{
$section = $this->_options['section'];
if ($section instanceof Sensei_Doc_Section) {
$content = $this->_renderSection($section);
} else {
// No section was set, so let's render all sections
// No section was set, so let's render all sections
$content = '';
for ($i = 0; $i < count($this->_toc); $i++) {
$content .= $this->_renderSection($this->_toc->getChild($i));
}
}
$output = $this->_options['template'];
$output = str_replace('%TITLE%', $this->_options['title'], $output);
$output = str_replace('%AUTHOR%', $this->_options['author'], $output);
$output = str_replace('%SUBJECT%', $this->_options['subject'], $output);
$output = str_replace('%KEYWORDS%', $this->_options['keywords'], $output);
$output = str_replace('%TOC%', $this->renderToc(), $output);
$output = str_replace('%CONTENT%', $content, $output);
return $output;
}
......@@ -148,21 +152,23 @@ class Sensei_Doc_Renderer_Xhtml extends Sensei_Doc_Renderer
protected function _renderSection(Sensei_Doc_Section $section)
{
$output = '';
$title = $section->getIndex() . ' ' . $section->getName();
$level = $section->getLevel();
if ($level === 1) {
$class = ' class="chapter"';
$title = 'Chapter ' . $title;
$this->_chapter = $section->getIndex();
$this->_codeListingsIndex = 0;
} else {
$class = ' class="section"';
}
$output .= '<div' . $class .'>' . "\n";
$output .= "<h$level>";
if ( ! ($this->_options['section'] instanceof Sensei_Doc_Section)
|| ($level > $this->_options['section']->getLevel())) {
$anchor = $this->makeAnchor($section);
......@@ -171,22 +177,22 @@ class Sensei_Doc_Renderer_Xhtml extends Sensei_Doc_Renderer
} else {
$output .= $title;
}
$output .= "</h$level>";
// Transform section contents from wiki syntax to XHTML
$output .= $this->_wiki->transform($section->getText());
// Render children of this section recursively
for ($i = 0; $i < count($section); $i++) {
$output .= $this->_renderSection($section->getChild($i));
}
$output .= '</div>' . "\n";
return $output;
return $output;
}
public function makeUrl($section)
{
if ($section instanceof Sensei_Doc_Section) {
......@@ -194,22 +200,22 @@ class Sensei_Doc_Renderer_Xhtml extends Sensei_Doc_Renderer
} else {
$path = $section;
}
$url = $this->_options['url_prefix'];
if ($this->_options['section'] instanceof Sensei_Doc_Section) {
$level = $this->_options['section']->getLevel();
$url .= implode(':', array_slice(explode(':', $path), 0, $level));
}
$anchor = $this->makeAnchor($section);
if ($anchor !== '') {
$url .= '#' . $anchor;
}
return $url;
}
public function makeAnchor($section)
{
if ($section instanceof Sensei_Doc_Section) {
......@@ -217,7 +223,7 @@ class Sensei_Doc_Renderer_Xhtml extends Sensei_Doc_Renderer
} else {
$path = $section;
}
if ($this->_options['section'] instanceof Sensei_Doc_Section) {
$level = $this->_options['section']->getLevel();
return implode(':', array_slice(explode(':', $path), $level));
......@@ -225,4 +231,12 @@ class Sensei_Doc_Renderer_Xhtml extends Sensei_Doc_Renderer
return $path;
}
}
public function codeListingsNumberingCallback()
{
$this->_codeListingsIndex++;
$html = '<p class="caption">Listing ' . $this->_chapter . '.'
. $this->_codeListingsIndex . "</p>\n";
return $html;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment