Skip to content

Instantly share code, notes, and snippets.

@eightbitraptor
Forked from killercup/Readme.md
Last active August 29, 2015 14:18
Show Gist options
  • Save eightbitraptor/6cd5ba08891b23d8e705 to your computer and use it in GitHub Desktop.
Save eightbitraptor/6cd5ba08891b23d8e705 to your computer and use it in GitHub Desktop.

Convert The Rust Programming Language to Epub

This small Ruby script uses pandoc to convert The Rust Programming Language (compilation of the Rust guides) to epub.

Download the EPUB

Script Usage

  1. Install pandoc
  2. Copy trpl-epub.rb file to rust/src/doc/trpl (assuming your clone of the Rust repository is in rust/)
  3. Execute with ruby make-epub.rb.
  4. There should be two new files: _all.md (combined and reformatted markdown, for debugging) and all.epub (the file you want).
  5. Profit!
#! env ruby
TOC_LINK_REGEX = /(?<indent>\s*?)\* \[(?<title>.+?)\]\((?<filename>.+?)\)/
def pandoc(file)
`pandoc #{file} --to markdown_github --base-header-level=3 --indented-code-classes=rust --atx-headers`
end
def normalize_title(title)
# Some chapter titles start with Roman numerals, e.g. "I: The Basics"
title.sub /(([IV]+):\s)/, ''
end
book = <<-eos
---
title: "The Rust Programming Language"
creator: "The Rust Team"
date: #{Time.new().strftime("%Y-%m-%d")}
language: en
description: "This book will teach you about the Rust Programming Language. Rust is a modern systems programming language focusing on safety and speed. It accomplishes these goals by being memory safe without using garbage collection."
...
eos
book << pandoc("README.md")
book << "\n\n"
File.open("SUMMARY.md", "r").each_line do |line|
link = TOC_LINK_REGEX.match(line)
if link
level = link[:indent].length == 0 ? "#" : "##"
book << "#{level} #{normalize_title link[:title]}\n\n"
book << pandoc(link[:filename])
book << "\n\n"
end
end
File.open("_all.md", "w") { |file|
file.write(book)
}
`pandoc _all.md --smart --normalize --standalone --number-sections --output=all.epub`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment