Skip to content

Commit 1704153

Browse files
committed
Port from Writing With Vim.
0 parents  commit 1704153

14 files changed

+216
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
log.txt
2+
local-preview.pdf
3+
_generated

.ruby-gemset

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
local-leanpub

.ruby-version

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
default

Book.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
_generated/leanpub/chapter1.markdown
2+
_generated/leanpub/appendices.markdown

Gemfile

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
source 'https://rubygems.org'
2+
3+
group :development do
4+
# book stitching
5+
gem 'guard'
6+
gem 'guard-process'
7+
gem 'rb-inotify', require: false
8+
gem 'rb-fsevent', require: false
9+
gem 'rb-fchange', require: false
10+
end

Gemfile.lock

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
GEM
2+
remote: https://rubygems.org/
3+
specs:
4+
coderay (1.0.8)
5+
ffi (1.2.0)
6+
guard (1.5.4)
7+
listen (>= 0.4.2)
8+
lumberjack (>= 1.0.2)
9+
pry (>= 0.9.10)
10+
thor (>= 0.14.6)
11+
guard-process (1.0.1)
12+
guard (>= 0.4.2)
13+
listen (0.6.0)
14+
lumberjack (1.0.2)
15+
method_source (0.8.1)
16+
pry (0.9.10)
17+
coderay (~> 1.0.5)
18+
method_source (~> 0.8)
19+
slop (~> 3.3.1)
20+
rb-fchange (0.0.6)
21+
ffi
22+
rb-fsevent (0.9.2)
23+
rb-inotify (0.8.8)
24+
ffi (>= 0.5.0)
25+
slop (3.3.3)
26+
thor (0.16.0)
27+
28+
PLATFORMS
29+
ruby
30+
31+
DEPENDENCIES
32+
guard
33+
guard-process
34+
rb-fchange
35+
rb-fsevent
36+
rb-inotify

Guardfile

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# https://github.com/guard/guard#readme
2+
3+
ignore /_generated/
4+
5+
contents_filename = 'contents.txt'
6+
chapters = File.read(contents_filename).split("\n")
7+
images = Dir['images/**']
8+
puts chapters
9+
files = [*chapters, 'build', *images, contents_filename]
10+
file_string = files.join("|")
11+
puts file_string
12+
13+
guard 'process', command: './build' do
14+
watch(%r{#{file_string}})
15+
end

LICENSE

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (C) 2013 Anthony Panozzo
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of
4+
this software and associated documentation files (the "Software"), to deal in
5+
the Software without restriction, including without limitation the rights to
6+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
7+
of the Software, and to permit persons to whom the Software is furnished to do
8+
so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19+
SOFTWARE.

README.markdown

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Local Leanpub
2+
3+
We don't need to use Leanpub to render our Leanpub book previews. We can do
4+
that locally using [Pandoc](http://johnmacfarlane.net/pandoc/) and save about
5+
ninety seconds each time.
6+
7+
This repo is an extraction of some work I did to stitch together a local
8+
preview of my book on [Writing With Vim](https://leanpub.com/vim-for-writers).
9+
Here is a way to get your own book previews without the need to send your work
10+
up to Leanpub every time.
11+
12+
The setup in this repo takes care of some of the work of moving files and
13+
normalizing differences between Leanpub and Pandoc so that you can easily and
14+
quickly preview the book contents.
15+
16+
## Local previewing
17+
18+
You need to [have Pandoc installed](http://johnmacfarlane.net/pandoc/installing.html).
19+
20+
At the command-line, run `./build` to generate a PDF locally manually. This
21+
will look at the contents of contents.txt to generate a `Book.txt` file, which
22+
is what Leanpub looks at to stitch together your book correctly.
23+
24+
To automate this process, install the Ruby gems locally with `bundle install`.
25+
Then you can run `bundle exec guard` to automatically regenerate the PDF
26+
whenever the text contents of the book change or you add or remove a chapter
27+
from the book. (For my project, I also use `./build` script to correctly
28+
generate the changes for Leanpub, since I do some post processing to insert
29+
images, etc.)
30+
31+
Open up the `local-preview.pdf` file to see an approximation of what your book
32+
will look like when rendered by Leanpub. Many PDF readers have an auto-refresh
33+
setting, so when your book changes you will see the changes update in the book.
34+
35+
## How book contents are laid out
36+
37+
Leanpub looks at certain files to compile the book in question:
38+
39+
* `Book.txt` lists all of the chapters of the final book
40+
* `Sample.txt` lists the chapters in the sample PDF
41+
42+
We have a file called `contents.txt` that holds the values of things in the book.
43+
44+
My convention is that the book contents are located in `chapter[digit].markdown`
45+
files. You can always change the scripts if this does not suit you.
46+
47+
## Installing
48+
49+
Clone this repo and put it into your Dropbox folder that Leanpub looks at.
50+
Then you should be able to run the commands above and see a local preview, and
51+
publish to Leanpub and have a preview up there.
52+
53+
## Pandoc vs. Leanpub
54+
55+
Leanpub used to use the Pandoc rendering system, but they switched to use their
56+
own system to have better control over the rendering process. and they support
57+
many of the same conventions. You should still check your book against
58+
Leanpub's preview feature before publishing, since there are some differences.
59+
For example, For example, Leanpub automatically inserts page breaks between chapters
60+
(depending on your settings) while Pandoc needs the explicit `\\pagebreak`
61+
command (this repo takes care of this difference.)
62+
63+
## Disclaimer
64+
65+
This project is not affiliated with Leanpub or Pandoc. It just uses Pandoc to
66+
approximate the preview function of Leanpub.
67+
68+
## License
69+
70+
This code released under the [MIT License](http://en.wikipedia.org/wiki/MIT_License).

Sample.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
_generated/leanpub/chapter1.markdown

appendices.markdown

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Appendices

build

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/usr/bin/env ruby
2+
3+
require 'fileutils'
4+
5+
chapter_filenames = File.read('contents.txt').split("\n")
6+
7+
base_dir = '_generated'
8+
leanpub_dir = File.join base_dir, 'leanpub'
9+
pandoc_dir = File.join base_dir, 'pandoc'
10+
11+
FileUtils.mkdir_p leanpub_dir
12+
FileUtils.mkdir_p pandoc_dir
13+
14+
# write Book.txt from contents.txt
15+
# AP: I'm not sure if this is bulletproof if we rename chapters,
16+
# but let's try this for now
17+
unless File.read('Book.txt').split("\n").length == chapter_filenames.length
18+
File.open('Book.txt', 'w') do |book|
19+
chapter_filenames.each do |chapter_filename|
20+
book.puts File.join(leanpub_dir, chapter_filename)
21+
end
22+
end
23+
end
24+
25+
# copy over files for image insertion
26+
chapter_filenames.each do |chapter_filename|
27+
FileUtils.cp chapter_filename, leanpub_dir
28+
end
29+
30+
# copy over modified files for futher pandoc processing
31+
chapter_filenames.each do |chapter_filename|
32+
FileUtils.cp File.join(leanpub_dir, chapter_filename), pandoc_dir
33+
end
34+
35+
# pandoc post-processing
36+
chapter_filenames.each do |chapter_filename|
37+
File.open(File.join(pandoc_dir, chapter_filename), 'a') do |f|
38+
f.puts "\nEND OF PANDOC CHAPTER"
39+
f.puts "\n\\pagebreak"
40+
end
41+
end
42+
43+
# stitch the current book into a PDF, sending any errors to the ether
44+
45+
File.open("#{base_dir}/Book.txt", 'w') do |book|
46+
chapter_filenames.each do |chapter_filename|
47+
book.puts File.join(pandoc_dir, chapter_filename)
48+
end
49+
end
50+
51+
`cat #{base_dir}/Book.txt | xargs pandoc \
52+
--smart \
53+
--table-of-contents \
54+
--output=local-preview.pdf`

chapter1.markdown

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Chapter 1

contents.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
chapter1.markdown
2+
appendices.markdown

0 commit comments

Comments
 (0)