php调用asciidoctor的核心在于通过exec()或shell_exec()函数执行asciidoctor命令,实现将asciidoc文档转换为html等格式。1. 确保环境正确配置:安装asciidoctor和ruby环境,并确认asciidoctor路径;2. php代码中使用escapeshellcmd()和exec()执行转换命令,并处理返回值以判断执行是否成功;3. 注意权限问题,确保php进程有执行asciidoctor及读写相关文件的权限;4. 处理中文路径或文件名时,使用escapeshellarg()并设置正确的字符编码与locale;5. 使用绝对路径、避免特殊字符,并合理配置asciidoctor选项如doctype、backend、stylesheet等;6. 可通过-gem安装扩展(如asciidoctor-diagram)并在命令中加载以支持额外功能;7. 调试时应检查asciidoctor是否安装正确、exec()是否被禁用以及asciidoc文档是否存在语法错误。整个过程需关注安全性、兼容性与错误日志记录,以保障转换顺利进行。

PHP调用Asciidoctor,本质上就是让PHP脚本能够执行Asciidoctor的命令,从而将Asciidoc格式的文档转换成HTML或其他格式。关键在于正确配置环境,并使用exec()或shell_exec()这类函数来调用Asciidoctor。

解决方案

环境准备:
立即学习“PHP免费学习笔记(深入)”;
sudo apt-get install asciidoctor或sudo yum install asciidoctor)。如果是其他环境,需要按照Asciidoctor官方文档进行安装。/usr/bin/asciidoctor或/usr/local/bin/asciidoctor。可以使用which asciidoctor命令来查找。PHP代码:

<?php
function convertAsciidocToHtml(string $asciidocFilePath, string $outputFilePath): bool
{
$asciidoctorPath = '/usr/bin/asciidoctor'; // 替换为你的Asciidoctor可执行文件路径
$command = escapeshellcmd("$asciidoctorPath -o $outputFilePath $asciidocFilePath");
// 记录命令,方便调试
error_log("Executing command: " . $command);
$output = [];
$returnCode = 0;
exec($command, $output, $returnCode);
// 记录输出,方便调试
error_log("Command output: " . implode("\n", $output));
error_log("Return code: " . $returnCode);
if ($returnCode !== 0) {
error_log("Asciidoctor conversion failed with return code: " . $returnCode);
return false;
}
return true;
}
// 示例用法
$asciidocFile = '/path/to/your/document.adoc'; // 替换为你的Asciidoc文件路径
$htmlFile = '/path/to/your/output.html'; // 替换为输出HTML文件的路径
if (convertAsciidocToHtml($asciidocFile, $htmlFile)) {
echo "Asciidoc file converted successfully to HTML!";
} else {
echo "Asciidoc file conversion failed.";
}
?>escapeshellcmd()函数: 这个函数非常重要,它可以确保传递给shell的参数是安全的,防止命令注入攻击。exec()函数: exec()函数执行一个外部程序。它接受三个参数:要执行的命令、输出数组(命令的输出会被写入这个数组)和返回代码(命令的退出状态)。exec()函数的返回代码,如果返回代码不是0,表示命令执行失败。同时,使用error_log()记录命令和输出,方便调试。权限问题:
www-data或apache)需要有执行Asciidoctor的权限。可以通过chmod +x /usr/bin/asciidoctor命令来赋予执行权限。其他方法:
shell_exec()函数: shell_exec()函数也可以用来执行外部程序,它返回命令的完整输出,而不是像exec()那样将输出写入数组。可以使用$output = shell_exec($command);来获取输出。Asciidoctor转换失败的常见原因及排查方法
Asciidoctor转换失败的原因有很多,不仅仅是PHP代码的问题,还可能涉及到环境配置、文件权限、Asciidoc文档本身的问题。
Asciidoctor未正确安装或路径错误:
asciidoctor --version命令,如果能够显示Asciidoctor的版本信息,说明安装成功。然后,确认PHP代码中的$asciidoctorPath变量是否指向正确的Asciidoctor可执行文件路径。可以使用which asciidoctor命令来查找Asciidoctor的路径。PHP进程没有执行Asciidoctor的权限:
www-data或apache)需要有执行Asciidoctor的权限。可以通过以下步骤来检查和修改权限:ps aux | grep httpd或ps aux | grep apache命令来查找。ls -l /usr/bin/asciidoctor命令来查看Asciidoctor可执行文件的权限。chmod +x /usr/bin/asciidoctor命令来赋予执行权限。如果需要更精细的权限控制,可以使用chown命令来修改Asciidoctor的所有者或所属组。PHP进程没有读写Asciidoc文件和输出HTML文件的权限:
ls -l /path/to/your/document.adoc和ls -l /path/to/your/output.html命令来查看文件权限。如果权限不足,可以使用chmod命令来修改权限,或者使用chown命令来修改文件所有者或所属组。Asciidoc文档本身存在语法错误:
asciidoctor -v /path/to/your/document.adoc。如果存在语法错误,Asciidoctor会输出错误信息。根据错误信息修改Asciidoc文档。PHP的exec()函数被禁用:
exec()函数,以防止安全风险。可以通过phpinfo()函数来查看exec()函数是否被禁用。如果被禁用,需要修改PHP配置文件(php.ini),移除disable_functions配置中的exec。注意: 禁用exec()函数是一种安全措施,解除禁用可能会带来安全风险,请谨慎操作。escapeshellcmd()函数转义不正确:
escapeshellcmd()函数可以防止命令注入攻击,但如果使用不当,可能会导致Asciidoctor命令无法正确执行。例如,如果Asciidoc文件路径或输出HTML文件路径包含空格或特殊字符,escapeshellcmd()函数可能会转义这些字符,导致Asciidoctor无法找到文件。可以尝试手动构建Asciidoctor命令,并使用var_dump()函数来查看命令是否正确。Asciidoctor扩展或主题缺失:
PHP调用Asciidoctor时如何处理中文文件名或路径
处理中文文件名或路径的关键在于确保字符编码的一致性,并正确处理转义。
确保文件编码一致:
header('Content-Type: text/html; charset=utf-8');来设置HTTP响应的字符编码。使用escapeshellarg()函数:
escapeshellarg()函数比escapeshellcmd()函数更适合处理包含空格或特殊字符的文件名或路径。escapeshellarg()函数会将参数用单引号括起来,并转义单引号本身,从而确保参数被正确传递给shell。<?php
function convertAsciidocToHtml(string $asciidocFilePath, string $outputFilePath): bool
{
$asciidoctorPath = '/usr/bin/asciidoctor';
$command = escapeshellcmd("$asciidoctorPath -o " . escapeshellarg($outputFilePath) . " " . escapeshellarg($asciidocFilePath));
$output = [];
$returnCode = 0;
exec($command, $output, $returnCode);
if ($returnCode !== 0) {
return false;
}
return true;
}
$asciidocFile = '/path/to/你的文档.adoc'; // 中文文件名
$htmlFile = '/path/to/输出目录/你的文档.html'; // 中文路径
if (convertAsciidocToHtml($asciidocFile, $htmlFile)) {
echo "Asciidoc file converted successfully to HTML!";
} else {
echo "Asciidoc file conversion failed.";
}
?>检查PHP的locale设置:
setlocale()函数来设置locale。<?php
setlocale(LC_ALL, 'zh_CN.UTF-8'); // 设置locale为中文UTF-8
function convertAsciidocToHtml(string $asciidocFilePath, string $outputFilePath): bool
{
$asciidoctorPath = '/usr/bin/asciidoctor';
$command = escapeshellcmd("$asciidoctorPath -o " . escapeshellarg($outputFilePath) . " " . escapeshellarg($asciidocFilePath));
$output = [];
$returnCode = 0;
exec($command, $output, $returnCode);
if ($returnCode !== 0) {
return false;
}
return true;
}
$asciidocFile = '/path/to/你的文档.adoc'; // 中文文件名
$htmlFile = '/path/to/输出目录/你的文档.html'; // 中文路径
if (convertAsciidocToHtml($asciidocFile, $htmlFile)) {
echo "Asciidoc file converted successfully to HTML!";
} else {
echo "Asciidoc file conversion failed.";
}
?>使用绝对路径:
避免在文件名或路径中使用特殊字符:
escapeshellarg()函数来处理包含空格或特殊字符的文件名或路径,但最好还是避免在文件名或路径中使用特殊字符,以减少出错的可能性。Asciidoctor的常用选项和配置
Asciidoctor提供了丰富的选项和配置,可以控制转换过程的各个方面,例如输出格式、样式、标题、属性等。
常用选项:
-o <file> 或 --output <file>:指定输出文件。-d <type> 或 --doctype <type>:指定文档类型。常用的文档类型有article、book、manpage。-b <backend> 或 --backend <backend>:指定后端。常用的后端有html5、docbook5。-a <attribute>=<value> 或 --attribute <attribute>=<value>:设置属性。可以使用属性来控制文档的各个方面,例如标题、作者、版本等。-s 或 --standalone:生成独立的HTML文件,包含所有的CSS和JavaScript。-n 或 --no-header-footer:不生成HTML的头部和尾部。-r <require> 或 --require <require>:加载Ruby库。-v 或 --verbose:显示详细的输出信息。-q 或 --quiet:不显示任何输出信息。配置文件:
asciidoctor.conf或.asciidoctorconfig,位于当前目录或用户主目录下。# asciidoctor.conf # 设置默认后端为html5 :backend: html5 # 设置文档类型为article :doctype: article # 设置属性 :author: Your Name :email: your.email@example.com :revnumber: 1.0
在PHP代码中传递选项:
<?php
function convertAsciidocToHtml(string $asciidocFilePath, string $outputFilePath, array $options = []): bool
{
$asciidoctorPath = '/usr/bin/asciidoctor';
$command = escapeshellcmd($asciidoctorPath);
// 添加选项
foreach ($options as $option => $value) {
$command .= ' ' . escapeshellarg($option . '=' . $value);
}
$command .= ' -o ' . escapeshellarg($outputFilePath) . ' ' . escapeshellarg($asciidocFilePath);
$output = [];
$returnCode = 0;
exec($command, $output, $returnCode);
if ($returnCode !== 0) {
return false;
}
return true;
}
$asciidocFile = '/path/to/your/document.adoc';
$htmlFile = '/path/to/your/output.html';
// 设置选项
$options = [
'doctype' => 'article',
'backend' => 'html5',
'attribute' => 'author=Your Name'
];
if (convertAsciidocToHtml($asciidocFile, $htmlFile, $options)) {
echo "Asciidoc file converted successfully to HTML!";
} else {
echo "Asciidoc file conversion failed.";
}
?>常用属性:
doctype:文档类型。backend:后端。title:文档标题。author:作者。email:作者邮箱。revnumber:版本号。revdate:发布日期。stylesheet:CSS样式表。icons:图标。toc:目录。如何使用Asciidoctor的扩展和主题
Asciidoctor的扩展和主题可以扩展Asciidoctor的功能,改变输出HTML的样式。
扩展:
Asciidoctor的扩展是用Ruby编写的,可以扩展Asciidoctor的语法、转换过程、输出格式等。
常用的扩展有:
asciidoctor-diagram:支持在Asciidoc文档中嵌入UML图、流程图等。asciidoctor-plantuml:支持在Asciidoc文档中嵌入PlantUML图。asciidoctor-rouge:使用Rouge语法高亮器来高亮代码。安装扩展:
gem install asciidoctor-diagram gem install asciidoctor-plantuml gem install asciidoctor-rouge
在Asciidoc文档中使用扩展:
[plantuml,format=png] .... @startuml Alice -> Bob: Authentication Request Bob --> Alice: Authentication Response Alice -> Bob: Another authentication Request Alice <-- Bob: Another authentication Response @enduml ....
在PHP代码中加载扩展:
<?php
function convertAsciidocToHtml(string $asciidocFilePath, string $outputFilePath): bool
{
$asciidoctorPath = '/usr/bin/asciidoctor';
$command = escapeshellcmd("$asciidoctorPath -r asciidoctor-diagram -o $outputFilePath $asciidocFilePath");
$output = [];
$returnCode = 0;
exec($command, $output, $returnCode);
if ($returnCode !== 0) {
return false;
}
return true;
}
$asciidocFile = '/path/to/your/document.adoc';
$htmlFile = '/path/to/your/output.html';
if (convertAsciidocToHtml($asciidocFile, $htmlFile)) {
echo "Asciidoc file converted successfully to HTML!";
} else {
echo "Asciidoc file conversion failed.";
}
?>主题:
Asciidoctor的主题定义了输出HTML的样式。
Asciidoctor默认使用default主题。
可以使用自定义CSS样式表来覆盖默认主题的样式。
可以使用第三方主题,例如asciidoctor-skins。
安装主题:
gem install asciidoctor-skins
在Asciidoc文档中使用主题:
:stylesheet: path/to/your/custom.css
在PHP代码中指定主题:
<?php
function convertAsciidocToHtml(string $asciidocFilePath, string $outputFilePath): bool
{
$asciidoctorPath = '/usr/bin/asciidoctor';
$command = escapeshellcmd("$asciidoctorPath -a stylesheet=path/to/your/custom.css -o $outputFilePath $asciidocFilePath");
$output = [];
$returnCode = 0;
exec($command, $output, $returnCode);
if ($returnCode !== 0) {
return false;
}
return true;
}
$asciidocFile = '/path/to/your/document.adoc';
$htmlFile = '/path/to/your/output.html';
if (convertAsciidocToHtml($asciidocFile, $htmlFile)) {
echo "Asciidoc file converted successfully to HTML!";
} else {
echo "Asciidoc file conversion failed.";
}
?>总的来说,PHP调用Asciidoctor进行文档转换需要关注环境配置、权限问题、参数传递、错误处理等方面。 掌握这些要点,就能顺利地将Asciidoc文档转换为各种格式,并集成到PHP项目中。
以上就是PHP如何调用Asciidoctor转换 Asciidoctor调用教程快速转换文档格式的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号