<?php
class Asciidoc
{
public static function convert_to_html($source_text)
{
return self::run_asciidoc($source_text);
}
public static function convert_to_pdf($source_text)
{
return self::run_a2x($source_text);
}
private static function run_asciidoc($source_text)
{
$result =
self::run_shell_command(
self::get_home_dir() . '/bin/asciidoc',
array(
'--out-file=-',
'-',
self::SHELL_MULTILINE_TEXT_START
. self::SHELL_NEWLINE
. addslashes($source_text)
. self::SHELL_NEWLINE
. self::SHELL_MULTILINE_TEXT_END
. self::SHELL_NEWLINE,
));
return $result;
}
private static function run_a2x($source_text)
{
$base_temp_file_path = tempnam(sys_get_temp_dir(), 'a2x');
$temp_input_file_path = $base_temp_file_path . '.txt';
$temp_output_file_path = $base_temp_file_path . '.pdf';
file_put_contents($temp_input_file_path, $source_text);
self::run_shell_command(
self::get_home_dir() . '/bin/a2x',
array(
'--no-xmllint',
'--format=pdf',
'--conf-file='
. escapeshellarg(
self::get_home_dir() . '/.asciidoc/a2x.conf'),
'--destination-dir='
. escapeshellarg(dirname($temp_output_file_path)),
escapeshellarg($temp_input_file_path),
));
return file_get_contents($temp_output_file_path);
}
private static function run_shell_command(
$executable_path,
array $arguments=array())
{
$command =
escapeshellcmd($executable_path)
. self::SHELL_ARGUMENT_SEPARATOR
. implode(
self::SHELL_ARGUMENT_SEPARATOR,
$arguments);
return system($command);
}
private static function get_home_dir()
{
$process_user = posix_getpwuid(posix_geteuid());
$result = '/home/' . $process_user['name'];
return $result;
}
const SHELL_ARGUMENT_SEPARATOR = ' ';
const SHELL_MULTILINE_TEXT_START = '<<EOF';
const SHELL_MULTILINE_TEXT_END = 'EOF';
const SHELL_NEWLINE = "\n";
}
?>