首页
/ PandocRuby 技术文档

PandocRuby 技术文档

2024-12-23 10:14:42作者:卓炯娓

1. 安装指南

1.1 安装 Pandoc

首先,您需要安装 Pandoc。Pandoc 是一个用于将一种标记格式转换为另一种格式的 Haskell 库和命令行工具。您可以通过以下链接获取安装指南:

Pandoc 安装指南

1.2 安装 PandocRuby

PandocRuby 是一个 Ruby 封装库,用于调用 Pandoc 进行文档格式转换。您可以通过 RubyGems 安装 PandocRuby:

gem install pandoc-ruby

如果您使用 Bundler 进行依赖管理,可以在 Gemfile 中添加以下内容:

gem 'pandoc-ruby'

然后运行 bundle install 进行安装。

2. 项目的使用说明

2.1 基本用法

PandocRuby 提供了简单易用的接口来调用 Pandoc 进行文档格式转换。以下是一个基本的示例,将 Markdown 格式的字符串转换为 reStructuredText 格式:

require 'pandoc-ruby'
@converter = PandocRuby.new('# Markdown Title', from: :markdown, to: :rst)
puts @converter.convert

2.2 使用 #convert 类方法

您也可以直接使用 #convert 类方法进行转换:

puts PandocRuby.convert('# Markdown Title', from: :markdown, to: :html)

2.3 其他参数

PandocRuby 支持将其他参数转换为命令行选项,符号和字符串用于选项,哈希用于带参数的选项。例如:

PandocRuby.convert('# Markdown Title', :s, {f: :markdown, to: :rst}, '--wrap=none', :table_of_contents)

等同于:

echo "# Markdown Title" | pandoc -s -f markdown --to=rst --wrap=none --table-of-contents

2.4 使用 #to_[writer] 方法

PandocRuby 还提供了 #to_[writer] 实例方法,用于直接转换为特定格式。例如:

PandocRuby.new('# Example').to_html(:ascii)
# => "<h1 id="example">Example</h1>"

2.5 转换文件

PandocRuby 支持将一个或多个文件路径作为输入,文件内容将被连接并转换:

# 单个文件路径
PandocRuby.new(['/path/to/file1.docx'], from: 'docx').to_html
# 多个文件路径
PandocRuby.new(['/path/to/file1.docx', '/path/to/file1.docx'], from: 'docx').to_html

3. 项目API使用文档

3.1 类方法

  • PandocRuby.convert(input, *options): 将输入内容转换为目标格式。
  • PandocRuby.new(input, *options): 创建一个 PandocRuby 实例。

3.2 实例方法

  • #convert: 将输入内容转换为目标格式。
  • #to_[writer]: 将输入内容转换为特定格式。

3.3 可用的读取器和写入器

  • PandocRuby::READERS: 可用的读取器列表。
  • PandocRuby::STRING_WRITERS: 可用的字符串写入器列表。
  • PandocRuby::BINARY_WRITERS: 可用的二进制写入器列表。
  • PandocRuby::WRITERS: 所有可用的写入器列表。

4. 项目安装方式

4.1 通过 RubyGems 安装

gem install pandoc-ruby

4.2 通过 Bundler 安装

在 Gemfile 中添加:

gem 'pandoc-ruby'

然后运行 bundle install

4.3 设置 Pandoc 路径

如果您需要指定 Pandoc 可执行文件的路径,可以使用以下方法:

PandocRuby.pandoc_path = '/path/to/pandoc'

通过以上步骤,您可以顺利安装并使用 PandocRuby 进行文档格式转换。

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