blob: 7ecc2f70aa13d3658b2fda1452c114fbe5431b08 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
<?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)) {
return '<div class="alert alert-error">'
. 'Cannot open process to execute rst2html'
. '</div>';
}
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;
}
}
?>
|