html自动播放auto,GitHub - dejan/auto_html: Collection of filters that transform plain text into HTML cod...

AutoHtml

AutoHtml is a collection of filters that transforms plain text into HTML code.

Installation

Add this line to your application's Gemfile:

gem 'auto_html'

And then execute:

$ bundle

Or install it yourself as:

$ gem install auto_html

Abstract

AutoHtml uses concepts found in "Pipes and Filters" processing design pattern:

Filter - transforms an input. In AutoHtml context, this is any object that does the transformation through #call(String) method. Filter options should be passed in initializer. AutoHtml provides some filters already, ie Link, Image, Markdown, etc.

Pipeline - a composition of filters that transforms input by passing the output of one filter as input for the next filter in line. In AutoHtml context, this is the AutoHtml::Pipeline class. Since the same interface (method #call) is used to pass input, we can say that Pipeline is just another Filter, which means it can be used as a building block for other Pipelines, in a mix with other filters.

Examples

link_filter = AutoHtml::Link.new(target: '_blank')

link_filter.call('Checkout out my blog: http://rors.org')

# => 'Checkout out my blog: http://rors.org'

emoji_filter = AutoHtml::Emoji.new

emoji_filter.call(':point_left: yo!')

# => ':point_left: yo!'

# Use Pipeline to combine filters

base_format = AutoHtml::Pipeline.new(link_filter, emoji_filter)

base_format.call('Checkout out my blog: http://rors.org :point_left: yo!')

# => 'Checkout out my blog: http://rors.org :point_left: yo!'

# A pipeline can be reused in another pipeline. Note that the order of filters is important - ie you want

# `Image` before `Link` filter so that URL of the image gets transformed to `img` tag and not `a` tag.

comment_format = AutoHtml::Pipeline.new(AutoHtml::Markdown.new, AutoHtml::Image.new, base_format)

comment_format.call("Hello!\n\n Checkout out my blog: http://rors.org :point_left: yo! \n\n http://gifs.joelglovier.com/boom/booyah.gif")

# => "

Hello!

\n\n

Checkout out my blog: http://rors.org 1f448.png" class="emoji" title=":point_left:" alt=":point_left:" height="20" witdh="20" align="absmiddle" /> yo!

\n\n

" target="_blank">eba9df0f22b256396b94242db8510b96.gif

\n"

Bundled filters

Bellow is the list of bundled filters along with their optional arguments on initialization and their default values.

AutoHtml::Emoji, width: 20, height: 20, asset_root: '/images'

AutoHtml::HtmlEscape

AutoHtml::Image, proxy: nil, alt: nil

AutoHtml::Link, target: nil, rel: nil

AutoHtml::Markdown

AutoHtml::SimpleFormat

Using AutoHtml with ActiveRecord

For performance reasons it's a good idea to store the formated output in the database, in a separate column, to avoid generating the same content on each access.

This can be acomplished simply by overriding the attribute writter:

class Comment < ActiveRecord::Base

FORMAT = AutoHtml::Pipeline.new(

AutoHtml::HtmlEscape.new,

AutoHtml::Markdown.new

)

def text=(t)

super(t)

self[:text_html] = FORMAT.call(t)

end

end

Now, every time text attribute is set, text_html will be set as well:

comment = Comment.new(text: 'Hey!')

comment.text_html # => '

Hey!

'

Licence

AutoHtml is released under the MIT License.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值