dustinwilson/texturize

将ASCII标点符号转换为印刷正确的标点符号

0.5.1 2022-01-31 14:37 UTC

This package is auto-updated.

Last update: 2024-08-29 06:07:23 UTC


README

将ASCII标点符号(“texturizes”)转换为印刷正确的标点符号。基于古老的WordPress方法,用于同一目的。与这个库的主要区别在于,这个库可以转换复杂标记和属性内的字符串。

使用前警告

我已经以各种形式使用了这段代码超过10年,但因为我没有进行测试,所以我最初将其作为“beta”软件发布。为此编写测试将是肤浅的,但我必须有时间去做。

要求

  • PHP 8.0.2或更高版本,以下扩展
  • Composer 2.0或更高版本

文档

Texturize是一个单例静态类。

dW\Texturize

class dW\Texturize {
    // Flags for toggling features
    public static bool $curlyQuotes = true;
    public static bool $dashes = true;
    public static bool $ellipses = true;

    // Text nodes which are descendants of these elements won't be selected for
    // texturization
    public static array $ignoredAncestors = [ 'code', 'command', 'kbd', 'keygen', 'pre', 'samp', 'script', 'style', 'tt', 'var' ];

    // Attributes which should be texturized
    public static array $includedAttributes = [ '@alt', '@aria-label', 'meta[not(@name="viewport")]/@content', '@longdesc', '@rel', '@rev', '@title' ];


    public static function withString(string $data): string;
    public static function withDOM(\DOMDocument|\DOMElement &$node): \DOMDocument|\DOMElement;
}

属性

  • curlyQuotes (bool): 如果为true,则将曲线引号(例如:""转换为“”)。
  • dashes (bool): 如果为true,则将破折号(例如:--转换为–)。
  • ellipses (bool): 如果为true,则将省略号(例如:...转换为…)。
  • ignoredAncestors (array): 一个包含元素名称的数组,其文本节点后代将被忽略。
  • includedAttributes (array): 一个包含XPath片段的数组,对应于应进行texturize的属性。

dW\Texturize::withString

"Texturizes"指定的字符串。

public static function withString(string $data): string;
  • data: 要"Texturize"的字符串

dW\Texturize::withDOM

"Texturizes"指定的\DOMDocument\DOMElement。它了解相邻元素,并将成功转换复杂的DOM结构。

public static function withDOM(\DOMDocument|\DOMElement &$node): \DOMDocument|\DOMElement;
  • node: 要texturize的\DOMDocument\DOMElement