akh/typograf

文本排版

v0.6.0 2024-09-25 06:42 UTC

This package is auto-updated.

Last update: 2024-09-27 11:43:03 UTC


README

tests codecov

使用php进行文本排版,帮助设置不可断行空格、正确引号和纠正小错误。

内容

安装

composer require akh/typograf

使用

$t = new \Akh\Typograf\Typograf();
$typoText = $t->apply('"Привет, мир!"');
echo $typoText; //«Привет, мир!»

开启和关闭规则

$t = new \Akh\Typograf\Typograf();
// Включить правило
$t->enableRule('Nbsp\ReplaceNbsp');
// Включить все правила в группе 
$t->enableRule('Nbsp\*');
// Включить все правила
$t->enableRule('*');

// Отключить правило
$t->disableRule('Nbsp\ReplaceNbsp'); 
// Отключить все правила в группе
$t->disableRule('Nbsp\*'); 
// Отключить все правила
$t->disableRule('*'); 

部分关闭

要关闭文本段落的排版,需要将其包裹在

<span class="no-typo">"Привет"</span>

添加自定义规则

$typo = new \Akh\Typograf\Typograf();
$simpleRule = new class extends \Akh\Typograf\Rule\AbstractRule {
    public $name = 'Замена названия сайта';
    protected $sort = 1000;
    public function handler(string $text): string
    {
        return str_replace('old.ru', 'new.ru', $text);
    }
};

$typo->addRule($simpleRule);
$typo->apply('old.ru'); //new.ru

灵感来源于