在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:dejan/auto_html开源软件地址:https://github.com/dejan/auto_html开源编程语言:Ruby 100.0%开源软件介绍:AutoHtmlAutoHtml is a collection of filters that transforms plain text into HTML code. See live demo. InstallationAdd this line to your application's Gemfile: gem 'auto_html' And then execute: $ bundle Or install it yourself as: $ gem install auto_html AbstractAutoHtml uses concepts found in "Pipes and Filters" processing design pattern:
Exampleslink_filter = AutoHtml::Link.new(target: '_blank')
link_filter.call('Checkout out my blog: http://rors.org')
# => 'Checkout out my blog: <a target="blank" href="http://rors.org">http://rors.org</a>'
emoji_filter = AutoHtml::Emoji.new
emoji_filter.call(':point_left: yo!')
# => '<img src="/images/emoji/unicode/1f448.png" class="emoji" title=":point_left:" alt=":point_left:" height="20" witdh="20" align="absmiddle" /> 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: <a href="http://rors.org">http://rors.org</a> <img src="/images/emoji/unicode/1f448.png" class="emoji" title=":point_left:" alt=":point_left:" height="20" witdh="20" align="absmiddle" /> 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")
# => "<p>Hello!</p>\n\n<p>Checkout out my blog: <a href="<img src="http://rors.org" target="_blank">http://rors.org</a> <img src="/images/emoji/unicode/1f448.png" />" class="emoji" title=":point_left:" alt=":point_left:" height="20" witdh="20" align="absmiddle" /> yo! </p>\n\n<p><a href="<img src="http://gifs.joelglovier.com/boom/booyah.gif" />" target="_blank"><img src="http://gifs.joelglovier.com/boom/booyah.gif" /></a></p>\n" Bundled filtersBellow is the list of bundled filters along with their optional arguments on initialization and their default values.
Using AutoHtml with ActiveRecordFor 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 comment = Comment.new(text: 'Hey!')
comment.text_html # => '<p>Hey!</p>' LicenceAutoHtml is released under the MIT License. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论