首页
/ 项目技术文档——Acts as Markup

项目技术文档——Acts as Markup

2024-12-23 02:56:59作者:姚月梅Lane

1. 安装指南

首先,你需要将 acts_as_markup 添加到你的 Gemfile 中:

gem "acts_as_markup"

然后,运行以下命令进行安装:

bundle install

确保你已经安装了以下库,因为它们是项目所必需的:

  • RedCloth:用于处理 Textile 文本。
  • BlueCloth、RDiscount、Ruby PEG、Maruku 或 Redcarpet:作为 Markdown 处理器。

2. 项目的使用说明

本项目允许你在 ActiveRecord 模型中指定包含 Markdown、Textile 和 RDoc 的列。你可以使用 to_s 方法获取原始标记文本,或使用 to_html 方法获取格式化后的 HTML。

此外,你可以有一个模型,其中包含一个列,它包含一个标记文本的列,另一个列定义了要处理它的语言。如果字段被列为 "markdown"、"textile" 或 "rdoc"(不区分大小写),它将以此处理,对于标记语言的任何其他值,将作为普通字符串传递。

此 AR 扩展可以使用五种不同的 Markdown 处理后端:BlueCloth、RDiscount、Ruby PEG、Redcarpet 或 Maruku。你可以在环境配置文件(environment.rb)中设置一个配置值来指定你想使用的库:

ActsAsMarkup.markdown_library = :bluecloth

默认情况下将使用 RDiscount。

3. 项目API使用文档

以下是一些示例用法:

使用 acts_as_markdown

class Post < ActiveRecord
  acts_as_markdown :body
end

@post = Post.find(:first)
@post.body.to_s     # => "## Markdown Headline"
@post.body.to_html  # => "<h2> Markdown Headline</h2>"

使用 acts_as_textile

class Post < ActiveRecord
  acts_as_textile :body
end

@post = Post.find(:first)
@post.body.to_s     # => "h2. Textile Headline"
@post.body.to_html  # => "<h2>Textile Headline</h2>"

使用 acts_as_rdoc

class Post < ActiveRecord
  acts_as_rdoc :body
end

@post = Post.find(:first)
@post.body.to_s     # => "== RDoc Headline"
@post.body.to_html  # => "<h2>RDoc Headline</h2>"

使用 acts_as_markup

class Post < ActiveRecord
  acts_as_markup :language => :markdown, :columns => [:body]
end

@post = Post.find(:first)
@post.body.to_s     # => "## Markdown Headline"
@post.body.to_html  # => "<h2> Markdown Headline</h2>"

使用带 :variable 语言的 acts_as_markup

class Post < ActiveRecord
  acts_as_markup :language => :variable, :columns => [:body]
end

@post = Post.find(:first)
@post.markup_language      # => "markdown"
@post.body.to_s            # => "## Markdown Headline"
@post.body.to_html         # => "<h2> Markdown Headline</h2>"

使用选项

class Post < ActiveRecord
  acts_as_markdown :body, :markdown_options => [ :filter_html ]
end

class Post < ActiveRecord
  acts_as_textile :body, :textile_options => [ [ :filter_html ] ]
end

4. 项目安装方式

请遵循上述安装指南。简要概括如下:

  1. acts_as_markup 添加到 Gemfile 中。
  2. 运行 bundle install
  3. 根据需要配置 Markdown 库。

确保所有必需的依赖项都已正确安装。

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