cmsilex/rst

用于解析 reStructuredText 文档的 PHP 库的 Silex 服务提供者

维护者

详细信息

github.com/cmsilex/RST

主页

源码

v1.0.0 2014-09-05 06:26 UTC

This package is not auto-updated.

Last update: 2024-09-14 19:36:38 UTC


README

Build status

用于解析 reStructuredText 文档的 PHP 库

用法

解析器可以这样使用

<?php

$parser = new Gregwar\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
$document = $parser->parse($rst);

// Render it
echo $document;
/* Will output, in HTML mode:
<a id="title.1"></a><h1>Hello world</h1>
<a id="title.1.1"></a><h2>What is it?</h2>
<p>This is a <b>RST</b> document!</p>
<a id="title.1.2"></a><h2>Where can I get it?</h2>
<p>You can get it on the <a href="https://github.com/Gregwar/RST">GitHub page</a></p>
*/

更多信息,您可以查看 test/document/document.rst 及其结果 test/document/document.html

使用构建器

构建器是另一个工具,可以解析整个文档树并生成包含文件的输出目录。

您可以直接使用它

<?php

$builder = new Gregwar\RST\Builder;
$builder->build('input', 'output');

它将解析 input 目录中的所有文件,从 index.rst 开始,查找依赖关系引用,并在 output 目录中生成目标文件。默认格式是 HTML。

您可以使用以下方法来定制构建

  • copy($source, $destination): 将 $source 文件或目录复制到构建的 $destination 文件或目录
  • mkdir($directory): 在构建目录中创建 $directory
  • addHook($function): 添加一个钩子,在解析每个文档之后调用,该钩子将 $document 作为参数调用,然后可以按需修改它
  • addBeforeHook($function): 添加一个钩子,在解析文档之前调用,解析器将作为参数传递

编写指令

步骤 1: 扩展指令类

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

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

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

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

步骤 2: 注册您的指令

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

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

许可证

此库受 MIT 许可证许可