akibatech / wysiwygpreprocessor

一个用于处理wysiwyg/textarea输出的PHP库。

0.0.8 2016-07-11 11:49 UTC

This package is not auto-updated.

Last update: 2024-09-17 09:19:21 UTC


README

Build Status

WYSIWYG Preprocessor 是一个没有任何依赖的PHP库。它是一种用于处理你的HTML文本区的工具箱。

安装

源代码使用Composer管理。

composer require akibatech/wysiwygpreprocessor "0.*"

基本用法

对于给定的textarea,

$textarea = "Check my website http://website.com. Keep in touch at hello@website.com !";

我们希望将链接和电子邮件地址转换为HTML标签

use Akibatech\Wysiwyg\Processor;  
use Akibatech\Wysiwyg\Modifier;  

$processor = new Processor();  

$processor->addModifier(new Modifier\UrlToLink)  
          ->addModifier(new Modifier\MailToLink)  
          ->process($textarea);  

echo $processor->getOutput();  

结果如下

Check my website <a href="http://website.com">http://website.com</a>. Keep in touch at <a href="mailto:hello@website.com">hello@website.com</a> !

自定义修饰符

修饰符很容易自定义。
想象一下,你想将所有链接指向新页面或为其添加自定义类。

$textarea = 'Check out my new site: personnal-website.com';

$modifier = new Akibatech\Wysiwyg\Modifier\UrlToLink();

$modifier->setOptions([
    'class' => 'custom-link',
    'target' => '_blank'
])

$processor = new Akibatech\Wysiwyg\Processor();

$processor->addModifier($modifier)
          ->process($textarea);

echo $processor->getOutput();

结果如下

Check out my new site: <a href="personnal-website.com" class="custom-link" target="_blank">personnal-website.com</a>

修饰符

BBCode

类:Akibatech\Wysiwyg\Modifier\BbCode
描述:应用基本的BBCode来增强你的内容。

示例输入:[b]Hello[/b]
示例输出:<strong>Hello</strong>

选项
默认标签为:b, i, u, left, right, center, quote, link, img, size和color。
选项是通配符BBCode标签。键是想要的BBCode标签,选项是HTML替换。
如果模式作为数组给出,它可以访问标签选项,如[link=http://github.com]my profile[/link]作为<a href="$1">$2</a>

[  
    // New tag called [yellow]text in yellow[/yellow]  
    'yellow' => '<span style="color: yellow;">$1</span>',  
    // Disable default "b" tag  
    'b' => null  
]  

解析变量

类:Akibatech\Wysiwyg\Modifier\ParseVariables
描述:替换一组预定义变量。

示例输入:Hello %name%!
示例输出:Hello John!

选项
你可以指定分隔符和可接受的变量。

[  
    // My custom delimiter. Vars are parsed in this delimiter. Default is "%".  
    'in' => '%',  
    // Accepted vars
    'accept' => [
        'name' => 'Joe', // %name% => Joe
        'email' => 'email@example.com' // %email% => email@example.com
    ]
]  

绝对路径

类:Akibatech\Wysiwyg\Modifier\AbsolutePath
描述:将"href"和"src"属性替换为绝对值。

示例输入:<img src="../../files/sea.jpg" />
示例输出:<img src="/files/sea.jpg" />

选项
你可以指定自定义的前缀为你路径。

[  
    // Custom prefix. Default is '/'.  
    'prefix' => 'http://site.com/', // <img src="http://site.com/files/sea.jpg" />
]  

词语过滤器

类:Akibatech\Wysiwyg\Modifier\WordsFilter
描述:从文本中移除词语列表。作为审查系统。

示例输入:Cunt!
示例输出:[censored]!

选项
列表和替换。

[  
    // Words list as an array.  
    'words' => ['word1', 'word2'], // No defaults words.
    // Replacement
    'replace' => '[censored]' // Wanted replacement, default to [censored]
]  

空段落

类:Akibatech\Wysiwyg\Modifier\EmptyParagraphs
描述:从内容中删除空段落。

示例输入:<p></p><p>Hello</p><p>&nbsp;</p>
示例输出:<p>Hello</p>

选项
无。

邮件到链接

类:Akibatech\Wysiwyg\Modifier\MailToLink
描述:将电子邮件地址转换为可点击的链接标签。

示例输入:email@company.com
示例输出:<a href="mailto:email@company.com">email@company.com</a>

选项

[  
    // Will replace "@" by "<at>", set to false to disable...  
    'at' => '<at>',  
]  

NlToBr

类:Akibatech\Wysiwyg\Modifier\NlToBr
描述:将换行符替换为HTML换行符。类似于php原生函数nl2br()。

示例输入:hello world
示例输出:hello<br>world

选项

[  
    // Linebreak symbol to search. Defaults to "\n"  
    'search' => "\n",  
    // HTML to replace. Defaults to "<br>"  
    'replace' => '<br />'  
]  

StripTags

类:Akibatech\Wysiwyg\Modifier\StripTags
描述:从输入中移除HTML标签。类似于php原生函数strip_tags()。

示例输入:<p>hello world</p>
示例输出:hello world

选项

[  
    // Allowed HTML tags (see strip_tags documentation). Defaults, none.  
    'allow' => "<a>",  
]  

URL到链接

类:Akibatech\Wysiwyg\Modifier\UrlToLink
描述:将网址转换为可点击的链接标签。

示例输入:https://www.github.com
示例输出:<a href="https://www.github.com">https://www.github.com</a>

选项

[  
    // Add a custom class to all generated tags. No defaults.    
    'class' => 'link',  
    // Customize the link target. No defaults.  
    'target' => '_blank'  
]  

YouTube链接到Iframe

类:Akibatech\Wysiwyg\Modifier\YoutubeLinkToIframe
描述:将YouTube链接(长链接和短链接)转换为嵌入视频播放器(iframe)。

示例输入:我的新视频:https://youtu.be/wBqM2ytqHY4
示例输出:我的新视频:<iframe src="https://www.youtube.com/embed/wBqM2ytqHY4?controls=1&rel=0&showinfo=1" class="youtube-iframe" width="560" height="315" frameborder="0" allowfullscreen></iframe>

选项

[
    // Custom class added to the player
    'class'  => 'youtube-iframe',
    // Custom width (in px) or null
    'width'  => 560,
    // Custom height (in px) or null
    'height' => 315,
    // Allow fullscreen
    'allow_fullscreen' => true,
    // Enable youtube suggestions when video ends
    'with_suggestions' => false,
    // Display video info
    'with_infos' => true,
    // Display video controls
    'with_controls' => true
]

您自己的修饰符

您可以通过添加自己的修饰符轻松扩展预处理器。
您只需创建一个实现 ModifierInterface 的类。同时,您还被鼓励扩展 AbstractModifier 以访问常用方法(setOptions, getOptions, ...)。

基本上,修饰符通过一个公共方法 handle($input) 接收要转换的输入。
选项通过一个公共方法 defaultOptions() 处理,该方法返回一个包含可用选项的数组。在您的修饰符体中,您可以使用实例属性 options 访问这些选项。

可调用修饰符

您还有可能添加一个动态修饰符。
方法 "addModifier" 也接受一个回调函数。

示例

$processor->addModifier(function($input) {
    return str_rot13('hello'); // Will return "uryyb"
});

单元测试

WYSIWYG 预处理器使用 PHPUnit 进行了测试。
请确保您已安装 composer 开发依赖项,并输入:

vendor/bin/phpunit

作者

作者:[Marceau Casals](https://marceau.casals.fr) 和 [所有贡献者](https://github.com/MarceauKa/WYSIWYG-Preprocessor/graphs/contributors)
许可证:[MIT 许可证](https://en.wikipedia.org/wiki/MIT_License)