bitandblack/typorules

为专业易读文本提供排版改进。

0.8.0 2024-08-21 05:59 UTC

This package is auto-updated.

Last update: 2024-09-21 06:09:21 UTC


README

PHP from Packagist Latest Stable Version Total Downloads License

Bit&Black TypoRules

为PHP编写的专业易读文本排版改进。

目录

安装

此库可与Composer一起使用。通过运行$ composer require bitandblack/typorules将其添加到您的项目中。

使用

Bit&Black TypoRules库附带大量规则,有助于实现更好的排版。此外,还有提供多个规则的规则集。

使用单条规则

单条规则可以这样使用

<?php

use BitAndBlack\TypoRules\Rule\RemoveDuplicatedWhitespaceRule;

$content = 'This is  my    sentence.';

$removeDuplicatedWhitespaceRule = new RemoveDuplicatedWhitespaceRule();

/**
 * This will return a list of all violations, that have been found. 
 * You can use this for more control or an interactive process.
 */
$violations = $removeDuplicatedWhitespaceRule->getViolations($content);

/**
 * This method will return the fixed content. In our example:
 * `This is my sentence.`
 */
$contentFixed = $removeDuplicatedWhitespaceRule->getContentFixed($content);

您可以在/examples/1-single-rule.php下找到类似的示例。

使用规则集

使用规则集可能如下所示

<?php

use BitAndBlack\TypoRules\RuleSet\EnglishRuleSet;

$content = 'This is  my    - terribly - formatted sentence!!!!!';

$englishRuleSet = new EnglishRuleSet();

/**
 * This will return a list of all violations, that have been found.
 * You can use this for more control or an interactive process.
 */
$violations = $englishRuleSet->getViolations($content);

/**
 * This method will return the fixed content. In our example:
 * `This is my — terribly — formatted sentence!!`
 */
$contentFixed = $englishRuleSet->getContentFixed($content);

您可以在/examples/2-rule-set.php下找到类似的示例。

现有规则

有关现有规则的更多信息,请参阅/docs/rules.md

您可以通过实现RuleInterface来添加自定义规则。

现有规则集

有关现有规则集的更多信息,请参阅/docs/rulesets.md

您可以通过实现RuleSetInterface来使用自定义规则集。

自定义

您可以使用withRulewithoutRule方法自定义规则集并添加或删除规则。

如果您想完全自己设置规则集,可以使用CustomRuleSet类。

显示和检查更改

每个规则返回一个违规列表,每个违规都可能说明发现的违规的确切位置。

但是,在某些情况下,您可能希望创建一个精确的diff视图。这可以通过使用CharacterDiff类来完成。这可能看起来像这里

<?php

use BitAndBlack\TypoRules\Diff\CharacterDiff;

$content = 'Content before';
$contentFixed = 'Content, that has been fixed';

$diff = CharacterDiff::create()->getDiff($content, $contentFixed);

CharacterDiff类可以用CliOutputHtmlOutput类初始化,如果您没有设置其中之一,它将自行决定输出格式。

感谢

我们对JoliTypo的贡献者表示感谢,他们的贡献启发了我们开发这个库。

帮助

如果您有任何问题,请随时通过hello@bitandblack.com联系我们。

有关Bit&Black的更多信息,请访问www.bitandblack.com