Skip to content

Commit

Permalink
Streamlined workflow. Now editing chapters in-place, with a script to…
Browse files Browse the repository at this point in the history
… load them (+deps) into OPF spine/manifest. Misc. other changes.
  • Loading branch information
vi committed Nov 1, 2013
1 parent cf99066 commit e8a8d4f
Show file tree
Hide file tree
Showing 12 changed files with 108 additions and 91 deletions.
23 changes: 8 additions & 15 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,28 +1,21 @@
dl:
dl: clean
wget -c -rnH -k -np http://www.feynmanlectures.caltech.edu/

macros:
./bin/macros
preprocess:
./bin/macros < I_01.html > template/macros.tex
./bin/chapters | xargs ./bin/preprocess

concat:
find * -maxdepth 0 -type f | grep -P 'I_\d+\.html' | xargs bin/concat > flp.html

replace: macros concat
./bin/replace flp.html

preprocess: concat
./bin/mxml flp.html

epub: replace preprocess
epub: preprocess
cp -r ./template/epub/* .
grep -oP '(?<=img src=")[^"]*(?=")' flp.html | xargs zip flp.epub mimetype META-INF/* content.opf flp.html
./bin/opf content.opf
(./bin/chapters; ./bin/images) | zip -@ flp.epub mimetype META-INF/* content.opf

mobi: epub
ebook-convert flp.epub flp.mobi

convert: epub mobi

clean:
git clean -Xf
git clean -Xfd

all: dl convert
3 changes: 3 additions & 0 deletions bin/chapters
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh

find * -maxdepth 0 -type f | grep -P 'I_\d+\.html'
14 changes: 0 additions & 14 deletions bin/concat

This file was deleted.

3 changes: 3 additions & 0 deletions bin/images
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh

./bin/chapters | xargs grep -hoP '(?<=img src=")[^"]*(?=")'
4 changes: 2 additions & 2 deletions bin/macros
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/sh

<I_01.html awk '
awk '
/Macros/ { getline
gsub("], ","] \n")
gsub("^ *","")
Expand All @@ -13,4 +13,4 @@
!($2 in a) { a[$2]
print
}
' >template/macros.tex
'
17 changes: 0 additions & 17 deletions bin/mxml

This file was deleted.

40 changes: 40 additions & 0 deletions bin/opf
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/usr/bin/env ruby

require 'nokogiri'
require 'mime/types'

class OPF
attr_reader :xml
def initialize path
@xml = Nokogiri::XML File.read(path)
end
def add_chapter filename
id, type, href = generics filename
add_manifest id, type, href
add_spine id
end
def add_image filename
id, type, href = generics filename
add_manifest id, type, href
end
private
def generics filename
id = filename.delete ?.
type = MIME::Types.type_for(filename)[0].content_type
href = filename
return [id, type, href]
end
def add_manifest id, type, href
item = @xml.create_element 'item', id: id, href: href, 'media-type' => type
item.parent = @xml.at_css 'manifest'
end
def add_spine id
itemref = @xml.create_element 'itemref', idref: id
itemref.parent = @xml.at_css 'spine'
end
end

opf = OPF.new(ARGV.first)
IO.popen('./bin/chapters').map(&:chomp).each { |filename| opf.add_chapter filename }
IO.popen('./bin/images').map(&:chomp).each { |filename| opf.add_image filename }
File.write ARGV.first, opf.xml.to_xml
51 changes: 51 additions & 0 deletions bin/preprocess
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/usr/bin/env ruby

require 'nokogiri'

class Equation < String
def to_image
str = self.inspect[1..-2]
bin = File.dirname(__FILE__)
cmd = [bin, 'eq2img'].join('/')
IO.popen([cmd, str]).read.chomp
end
def for_html
['<img src="', self.to_image, '">'].join
end
end

class String
def slim
html = self.to_html.css('.document').children
html.css('footer').remove
return html.to_html
end
def prettify
html = self.to_html
html.css('span.tag').each do |tag|
tag.content = tag.content << ?\s
end
return html.to_html
end
def tex2png
delimiters = [
/\\begin{equation\*?}.*?\\end{equation\*?}/m,
/\\begin{align\*?}.*?\\end{align\*?}/m,
/\$+.*?\$+/m
]
delimiters.each do |pattern|
gsub!(pattern) do |equation|
Equation.new(equation).for_html
end
end
return self
end
def to_html
Nokogiri::HTML self
end
end

ARGV.each do |html|
output = File.read(html).slim.prettify.tex2png
File.write html, output
end
31 changes: 0 additions & 31 deletions bin/replace

This file was deleted.

10 changes: 0 additions & 10 deletions template/book.html

This file was deleted.

2 changes: 0 additions & 2 deletions template/epub/content.opf
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
<dc:language>en</dc:language>
</metadata>
<manifest>
<item id="flp" href="flp.html" media-type="application/xhtml+xml"/>
</manifest>
<spine>
<itemref idref="flp">
</spine>
</package>
1 change: 1 addition & 0 deletions template/epub/mimetype
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
application/epub+zip

0 comments on commit e8a8d4f

Please sign in to comment.