diff options
| author | Christian Weiske <cweiske@cweiske.de> | 2012-04-03 21:52:06 +0200 |
|---|---|---|
| committer | Christian Weiske <cweiske@cweiske.de> | 2012-04-03 21:52:06 +0200 |
| commit | 7bf061541b0424b427bbbd1300e81d12190c9c54 (patch) | |
| tree | 69e2c7360fc4ec941085b3ce9a91c05dd5c1375a /src/phorkie/Renderer/ReStructuredText.php | |
| parent | 2b4b34a76f42841e964a549fc64c02ba4f60a3f4 (diff) | |
| download | phorkie-7bf061541b0424b427bbbd1300e81d12190c9c54.tar.gz phorkie-7bf061541b0424b427bbbd1300e81d12190c9c54.zip | |
rST rendering is possible now
Diffstat (limited to 'src/phorkie/Renderer/ReStructuredText.php')
| -rw-r--r-- | src/phorkie/Renderer/ReStructuredText.php | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/src/phorkie/Renderer/ReStructuredText.php b/src/phorkie/Renderer/ReStructuredText.php new file mode 100644 index 0000000..136de16 --- /dev/null +++ b/src/phorkie/Renderer/ReStructuredText.php @@ -0,0 +1,56 @@ +<?php +namespace phorkie; + +/** + * Requires cli program "rst2html" (python docutils) to be installed + */ +class Renderer_ReStructuredText +{ + /** + * Converts the rST to HTML + * + * @param File $file File to render + * + * @return string HTML + */ + public function toHtml(File $file) + { + $descriptorspec = array( + 0 => array('pipe', 'r'),//stdin + 1 => array('pipe', 'w'),//stdout + 2 => array('pipe', 'w') //stderr + ); + $process = proc_open('rst2html', $descriptorspec, $pipes); + if (!is_resource($process)) { + //FIXME: fallback to geshi + return $file->getContent(); + } + + fwrite($pipes[0], $file->getContent()); + fclose($pipes[0]); + + $html = stream_get_contents($pipes[1]); + fclose($pipes[1]); + + $errors = stream_get_contents($pipes[2]); + fclose($pipes[2]); + + $retval = proc_close($process); + + //cheap extraction of the rst html body + $html = substr($html, strpos($html, '<body>') + 6); + $html = substr($html, 0, strpos($html, '</body>')); + + if ($retval != 0) { + $html = '<div class="alert">' + . 'rst2html encountered some error; return value ' . $retval . '<br/>' + . 'Error message:' . $errors + . '</div>' + . $html; + } + + return $html; + } +} + +?> |
