首页
/ BBRuby 技术文档

BBRuby 技术文档

2024-12-23 13:10:22作者:咎竹峻Karen

本文档将详细介绍如何安装、使用及API调用BBRuby项目,帮助用户更好地理解和应用该Ruby实现的BBCode转换功能。

1. 安装指南

BBRuby可以通过Ruby的Gem包管理器进行安装。在终端中运行以下命令:

gem install bb-ruby

2. 项目使用说明

BBRuby是一个将BBCode标记转换为HTML的Ruby实现。以下是基本的使用方法:

首先,需要引入BBRuby库:

require 'bb-ruby'

BBRuby已经直接集成到String类中,可以直接在任何字符串对象上使用:

text = "[b]Here is some bold text[/b] followed by some [u]underlined text[/u]"
output = text.bbcode_to_html

BBRuby会自动转义HTML标签。如果不希望进行转义,可以传递false作为第二个参数:

output = text.bbcode_to_html({}, false)

如果只想允许某些标签,可以通过第三个参数进行设置:

output = text.bbcode_to_html({}, true, :enable, :image, :bold, :quote)

也可以禁用某些标签:

output = text.bbcode_to_html({}, true, :disable, :image, :bold, :quote)

还可以直接使用BBRuby类进行转换:

output = BBRuby.to_html(bbcode_markup)

自定义转换规则可以通过以下方式实现:

my_blockquote = {
  'Quote' => [
    /\[quote(:.*)?=(.*?)\](.*?)\[\/quote\1?\]/mi,
    '<div class="quote"><p><cite>\2</cite></p><blockquote>\3</blockquote></div>',
    'Quote with citation',
    '[quote=mike]please quote me[/quote]',
    :quote
  ],
}

text.bbcode_to_html(my_blockquote)

如果需要,也可以使用Proc作为替换:

module BBRuby
  @@tags = @@tags.merge({
      'File' => [
        /\[file(:.*)?=(.*?)\](.*?)\[\/file\1?\]/mi,
        lambda{ |e| "<div class=\"file\"><p><cite>#{e[3]}</cite></p><blockquote>#{file_read_method(e[2])}</blockquote></div>"},
        'File content with citation',
        '[file=script.rb]Script Caption[/file]',
        :file
      ],
    })
end

使用simple_format方法可以格式化输出:

output = text.bbcode_to_html_with_formatting
output = text.bbcode_to_html_with_formatting!

3. 项目API使用文档

以下是BBRuby处理BBCode标签的列表及其对应的符号,用于启用或禁用:

  • [b] :bold
  • [i] :italics
  • [u] :underline
  • [s] :strikeout
  • [del] :delete
  • [ins] :insert
  • [code] :code
  • [size] :size
  • [color] :color
  • [ol] :orderedlist
  • [ul] :unorderedlist
  • [li] :listitem
  • [*] :listitem
  • [list] :listitem
  • [list=1] :listitem
  • [list=a] :listitem
  • [dl] :definelist
  • [dt] :defineterm
  • [dd] :definition
  • [quote] :quote
  • [quote=source] :quote
  • [url=link] :link
  • [url] :link
  • [img size=] :image
  • [img align=] :image
  • [img=] :image
  • [img] :image
  • [youtube] :video
  • [gvideo] :video
  • [vimeo] :video
  • [email] :email
  • [align] :align
  • [left] :left
  • [center] :center
  • [right] :right
  • [br] :br

4. 项目安装方式

项目的安装方式已在安装指南中详细说明,即通过Ruby的Gem包管理器进行安装。

登录后查看全文
热门项目推荐

热门内容推荐

最新内容推荐

项目优选

收起
ohos_react_nativeohos_react_native
React Native鸿蒙化仓库
C++
178
262
RuoYi-Vue3RuoYi-Vue3
🎉 (RuoYi)官方仓库 基于SpringBoot,Spring Security,JWT,Vue3 & Vite、Element Plus 的前后端分离权限管理系统
Vue
866
513
ShopXO开源商城ShopXO开源商城
🔥🔥🔥ShopXO企业级免费开源商城系统,可视化DIY拖拽装修、包含PC、H5、多端小程序(微信+支付宝+百度+头条&抖音+QQ+快手)、APP、多仓库、多商户、多门店、IM客服、进销存,遵循MIT开源协议发布、基于ThinkPHP8框架研发
JavaScript
93
15
openGauss-serveropenGauss-server
openGauss kernel ~ openGauss is an open source relational database management system
C++
129
183
openHiTLSopenHiTLS
旨在打造算法先进、性能卓越、高效敏捷、安全可靠的密码套件,通过轻量级、可剪裁的软件技术架构满足各行业不同场景的多样化要求,让密码技术应用更简单,同时探索后量子等先进算法创新实践,构建密码前沿技术底座!
C
261
302
kernelkernel
deepin linux kernel
C
22
5
cherry-studiocherry-studio
🍒 Cherry Studio 是一款支持多个 LLM 提供商的桌面客户端
TypeScript
598
57
CangjieCommunityCangjieCommunity
为仓颉编程语言开发者打造活跃、开放、高质量的社区环境
Markdown
1.07 K
0
HarmonyOS-ExamplesHarmonyOS-Examples
本仓将收集和展示仓颉鸿蒙应用示例代码,欢迎大家投稿,在仓颉鸿蒙社区展现你的妙趣设计!
Cangjie
398
371
Cangjie-ExamplesCangjie-Examples
本仓将收集和展示高质量的仓颉示例代码,欢迎大家投稿,让全世界看到您的妙趣设计,也让更多人通过您的编码理解和喜爱仓颉语言。
Cangjie
332
1.08 K