Heading 1 | Heading 2 |
This is cell 1 text |
This is cell 2 text |
(:sourcecodeend:)
*/
$RecipeInfo['Pm Pygment']['Version'] = '2017-12-24';
Markup(
'pmpygment',
'fulltext',
'/\\(:sourcecode(?:\\s+([^\\n]+)):\\)(.*?)\\(:sourcecodeend:\\)/si',
"PmPygment");
function PmPygment($m) {
$options = $m[1];
$sourceblock = preg_replace("/<:vspace>/", "", $m[2]);
global $PubCacheDir, $UploadFileFmt, $UploadUrlFmt, $EnableDirectDownload,
$HTMLStylesFmt, $PmPygmentPgm, $PmPygmentCacheDir, $PmPygmentIFrameDefaultStyle,
$PmPygmentSecureAttachments;
SDV($PubCacheDir, "pub/cache");
SDV($PmPygmentPgm, "/usr/bin/pygmentize");
SDV($PmPygmentCacheDir, "$PubCacheDir");
SDV($PmPygmentIFrameDefaultStyle, '#wikitext .pmpygmentframe { width: 100%; border: none; }');
SDV($PmPygmentSecureAttachments, 0);
$version = '1.3';
$defaults = array(
'lang' => '',
'title' => '',
'style' => '',
'linenos' => 'false',
'linenostart' => 1,
'hl_lines' => '',
'height' => '100%',
'iframe' => 'false',
'noclasses' => 'true',
);
$opt = array_merge($defaults, ParseArgs($options));
if ($opt['lang'] == '') {
$out='Source: Option lang=<language> required
';
return Keep($out);
}
$checkmd5 = md5("$version"."$options"."$sourceblock");
// this recipe could be extended to handle files that already exist.
$inlinefile = rawurlencode("pmpygment-".$checkmd5.'.html');
$upname = MakeUploadName($pagename, $inlinefile);
$dirpath = FmtPageName("$UploadFileFmt", $pagename);
$filepath = FmtPageName("$UploadFileFmt/$upname", $pagename);
$HTMLStylesFmt['pmpygmentframe'] = "$PmPygmentIFrameDefaultStyle";
$HTMLStylesFmt['highlighttable'] = '.highlighttable { width: 100%; }';
if (!file_exists($filepath)) {
if (!file_exists($dirpath)) {
mkdir($dirpath,$mode=0755,$recursive=true);
}
$cmd="$PmPygmentPgm -f html";
// Add lang
$cmd=$cmd . ' ' . '-l ' . $opt['lang'];
// Get the ball rolling on opts
$pygmentopts = "-O linenostart=" . $opt['linenostart'];
if ($opt['iframe'] == 'true') {
$pygmentopts = $pygmentopts . ",full";
$opt['noclasses'] = 'false';
}
if ($opt['noclasses'] == 'true') {
$pygmentopts = $pygmentopts . ",noclasses";
}
if ($opt['style'] != '') {
$pygmentopts = $pygmentopts . ",style=".$opt['style'];
}
if ($opt['linenos'] != 'false') {
$pygmentopts = $pygmentopts . ",linenos";
}
if ($opt['hl_lines'] != '') {
$pygmentopts = $pygmentopts . ",hl_lines='" . $opt['hl_lines'] . "'";
}
if ($opt['title'] != '') {
$pygmentopts = $pygmentopts . ",title='" . $opt['title'] . "'";
}
// Add pygment options
$cmd=$cmd . ' ' . $pygmentopts;
// finally add outfile
$cmd=$cmd . ' -o ' . $filepath;
// Call pygmentize
$pygment_descriptors = array(
0 => array('pipe', 'r'),
1 => array('pipe', 'w'),
2 => array('pipe', 'a')
);
$pygment_proc = proc_open($cmd, $pygment_descriptors, $pipes);
if (is_resource($pygment_proc)) {
stream_set_blocking($pipes[0], 0);
stream_set_blocking($pipes[1], 0);
stream_set_blocking($pipes[2], 0);
fwrite($pipes[0], html_entity_decode($sourceblock));
fclose($pipes[0]);
$std_out=stream_get_contents($pipes[1]);
fclose($pipes[1]);
$err_out=stream_get_contents($pipes[2]);
fclose($pipes[2]);
$pygment_proc_rc = proc_close($pygment_proc);
if ($pygment_proc_rc) {
unlink($filepath);
$out="pygmentize has errored. $err_out\n$std_out
";
}
}
}
if (file_exists($filepath)) {
$uploadpath = PUE(FmtPageName((IsEnabled($EnableDirectDownload, 1) && !$PmPygmentSecureAttachments)
? "$UploadUrlFmt$UploadPrefixFmt/$upname"
: "{\$PageUrl}?action=download&upname=$upname", $pagename));
if ($opts['iframe'] == 'true') {
$out='';
} else {
$out=file_get_contents($uploadpath);
}
return Keep($out);
} else {
return Keep($out);
}
}