Skip to content

Commit

Permalink
Not atomic commit
Browse files Browse the repository at this point in the history
  • Loading branch information
samvasko committed Jan 26, 2014
1 parent 410cdb6 commit 9b5eff4
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 21 deletions.
4 changes: 3 additions & 1 deletion app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@
require 'fetch'
require 'convert'

File.write('out.html', Fetch.run())
content, toc = Fetch.run
File.write('out.html', content)
Convert.run toc
64 changes: 48 additions & 16 deletions lib/convert.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,52 @@
require 'eeepub'
require 'nokogiri'

epub = EeePub.make do
title 'Vala Tutorial'
creator 'Maciej Piechotka'
publisher 'Samuel Vasko'
date '2014-01-25'
identifier 'http://example.com/book/foo', :scheme => 'URL'
uid 'http://example.com/book/foo'
class Convert
def self.run toc
# Join the forces!
doc = (['header', 'out', 'footer'].map {|f| File.read(f + '.html')}).join
toc = self.parse_toc toc
epub = EeePub.make do
title 'Vala Tutorial'
creator 'Gnome'
publisher 'Samuel Vasko'
date '2014-01-25'
identifier 'https://github.com/bliker/vala-tutorial-book', :scheme => 'URL'
uid 'https://github.com/bliker/vala-tutorial-book'

files ['out.html']
nav toc
end
epub.save('sample.epub')
end

files ['/path/to/foo.html', '/path/to/bar.html'] # or files [{'/path/to/foo.html' => 'dest/dir'}, {'/path/to/bar.html' => 'dest/dir'}]
nav [
{:label => '1. foo', :content => 'foo.html', :nav => [
{:label => '1.1 foo-1', :content => 'foo.html#foo-1'}
]},
{:label => '1. bar', :content => 'bar.html'}
]
end
epub.save('sample.epub')
def self.parse_toc doc
doc = Nokogiri::HTML(doc)
self.mush_ol(doc.css('body > ol'))
end

def self.mush_li li
data = {
:label => li.css('> a').text,
:content => 'out.html' + li.css('a').attr('href')
}
unless li.css('ol').empty?
data[:nav] = self.mush_ol(li.css('ol'))
return data
else
return data
end
end

def self.mush_ol ol
out = []
ol.children.map do |li|
if li.name == 'li'
out << self.mush_li(li)
end
end

return out
end

end
8 changes: 4 additions & 4 deletions lib/fetch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ class Fetch

def self.run
content = self.get_content

toc = self.parse_toc content

self.remove_elements content
self.transform_code content
self.absolute_links content
Expand All @@ -17,7 +17,7 @@ def self.run

['id', 'lang', 'dir'].each { |e| content[0].remove_attribute(e) }

return content
return content, toc
end

private
Expand All @@ -29,7 +29,7 @@ def self.get_content
end

def self.parse_toc content
content.css('.table-of-contents > ol > li > ol')
content.css('.table-of-contents > ol > li > ol').to_html
end

def self.cleanup_headings content
Expand All @@ -40,7 +40,7 @@ def self.cleanup_headings content

def self.absolute_links content
content.css('a').each do |a|
if a.attr('href').starts_with '/'
if a.attr('href')[0] == '/'
a.set_attribute('href', 'https://wiki.gnome.org' + a.attr('href'))
end
end
Expand Down

0 comments on commit 9b5eff4

Please sign in to comment.