首页
/ BBRuby 技术文档

BBRuby 技术文档

2024-12-10 17:01:07作者:咎竹峻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包管理器进行安装。

热门项目推荐
相关项目推荐

项目优选

收起
Python-100-DaysPython-100-Days
Python - 100天从新手到大师
Python
611
115
HarmonyOS-ExamplesHarmonyOS-Examples
本仓将收集和展示仓颉鸿蒙应用示例代码,欢迎大家投稿,在仓颉鸿蒙社区展现你的妙趣设计!
Cangjie
286
79
mdmd
✍ WeChat Markdown Editor | 一款高度简洁的微信 Markdown 编辑器:支持 Markdown 语法、色盘取色、多图上传、一键下载文档、自定义 CSS 样式、一键重置等特性
Vue
112
25
openHiTLSopenHiTLS
旨在打造算法先进、性能卓越、高效敏捷、安全可靠的密码套件,通过轻量级、可剪裁的软件技术架构满足各行业不同场景的多样化要求,让密码技术应用更简单,同时探索后量子等先进算法创新实践,构建密码前沿技术底座!
C
60
48
RuoYi-Cloud-Vue3RuoYi-Cloud-Vue3
🎉 基于Spring Boot、Spring Cloud & Alibaba、Vue3 & Vite、Element Plus的分布式前后端分离微服务架构权限管理系统
Vue
45
29
go-stockgo-stock
🦄🦄🦄AI赋能股票分析:自选股行情获取,成本盈亏展示,涨跌报警推送,市场整体/个股情绪分析,K线技术指标分析等。数据全部保留在本地。支持DeepSeek,OpenAI, Ollama,LMStudio,AnythingLLM,硅基流动,火山方舟,阿里云百炼等平台或模型。
Go
1
0
Cangjie-ExamplesCangjie-Examples
本仓将收集和展示高质量的仓颉示例代码,欢迎大家投稿,让全世界看到您的妙趣设计,也让更多人通过您的编码理解和喜爱仓颉语言。
Cangjie
205
57
MateChatMateChat
前端智能化场景解决方案UI库,轻松构建你的AI应用,我们将持续完善更新,欢迎你的使用与建议。 官网地址:https://matechat.gitcode.com
383
36
RuoYi-VueRuoYi-Vue
🎉 基于SpringBoot,Spring Security,JWT,Vue & Element 的前后端分离权限管理系统,同时提供了 Vue3 的版本
Java
182
44
frogfrog
这是一个人工生命试验项目,最终目标是创建“有自我意识表现”的模拟生命体。
Java
8
0