用于解析 reStructuredText 文档的 PHP 库

v1.0.0 2018-06-14 10:49 UTC

This package is auto-updated.

Last update: 2024-09-09 22:00:51 UTC


README

Build status Coverage Status

用于解析 reStructuredText 文档的 PHP 库

使用方法

解析器可以这样使用

<?php

$parser = new Dusta\RST\Parser;

// RST document
$rst = '
Hello world
===========

What is it?
----------
This is a **RST** document!

Where can I get it?
-------------------
You can get it on the `GitHub page <https://github.com/Gregwar/RST>`_';

// Parse it
$parser = $parser->parse($rst);
$head = array();
foreach ($parser->headerNodes as $node) {
    $headers[] = $node->render();
}

$headers = $document; // array
$content = $parser->render(); // string

echo '<head>';
foreach ($headers as $value) {
    echo $value;
}
echo '</head>';
echo '<body>';
echo $content;
echo '</body>';

编写指令

步骤 1: 扩展指令类

编写一个扩展 Dusta\RST\Directive 类的自定义类,并定义返回指令名称的 getName() 方法。

然后,您可以重新定义以下方法之一

  • processAction() 如果您的指令只是微调文档而不修改节点
  • processNode() 如果您的指令是添加节点
  • process() 如果您的指令是微调紧跟其后的节点

有关更多信息,请参阅 Directive.php

步骤 2: 注册您的指令

您可以通过直接在您的 Parser 对象上调用 registerDirective() 来注册您的指令。

否则,您还需要通过扩展 Kernel 类并添加自己的逻辑来定义额外的指令来创建自己的内核,有关更多信息,请参阅 Kernel.php。然后,在构造 ParserBuilder 时传递内核。

许可协议

此库受 MIT 许可协议保护