Class: Sc2epub::Converter
- Inherits:
-
Object
- Object
- Sc2epub::Converter
- Defined in:
- lib/sc2epub/converter.rb
Instance Method Summary collapse
- #dodir(dir) ⇒ Object
- #dofile(path) ⇒ Object
- #dogenerate(doctitle) ⇒ Object
- #doroot(dir) ⇒ Object
-
#initialize(env, root, output) ⇒ Converter
constructor
A new instance of Converter.
- #local(path) ⇒ Object
- #path(path) ⇒ Object
- #title(path) ⇒ Object
Constructor Details
Instance Method Details
#dodir(dir) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/sc2epub/converter.rb', line 105 def dodir dir return [] if File::basename(dir)=~/^\..*/ @dirstack.push({:name =>local(dir), :src =>nil, :path => dir, :type => :dir, :level => @dirstack.size}) cache = {} until(@dirstack.empty?) dir = @dirstack.last[:path] subdir = nil Dir::foreach(dir) do |i| next if i=="."||i==".." path = File::join(dir, i) next if cache[path] if FileTest::directory? path subdir = path break elsif FileTest::file? path dofile(path) cache[path] = true end end if !subdir done = @dirstack.pop cache[done[:path]] = true else path = subdir @dirstack.push({:name =>local(path), :src =>nil, :path => path, :type => :dir, :level => @dirstack.size}) end end end |
#dofile(path) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/sc2epub/converter.rb', line 76 def dofile path #ext = File::extname(path) #if ext == '.html' or ext == '.xhtml' or ext == '.jpg' or ext == '.gif' or ext == '.png' # FileUtils::cp(path, @output) # return #end output = @output s = open(path){|io|io.read} enc = NKF::guess(s) if enc==NKF::BINARY return nil elsif enc!=NKF::ASCII and enc!=NKF::UTF8 s = NKF::nkf('-wxm0', s) end title = title(local(path)) s = CGI::escapeHTML(s) s = @template.xhtml('title'=>local(path), 'body'=>s) npath = title+".html" if @dirstack.last and not @dirstack.last[:src] @dirstack.last[:src] = npath @indexes << @dirstack.last end open(File::join(output, npath), "w") do |io| io.write(s) end level = @dirstack.empty? ? 1 : @dirstack.last[:level]+1 @indexes << {:src => npath, :name =>local(path), :type => :file, :level => level} title end |
#dogenerate(doctitle) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/sc2epub/converter.rb', line 28 def dogenerate doctitle output = @output indexhtml = '' items = ''; c=0; nvitems = '' cover = File::join(File::dirname(__FILE__), 'cover.jpg') FileUtils::cp(cover, output) @indexes.each do |data| title = data[:name] link = data[:src] if data[:type]==:dir && data[:level]+2<=4 tagname = "h" + (data[:level]+2).to_s else tagname = 'p' end indexhtml += "<#{tagname}>" + @template.link('url'=>link, 'title'=>title) + "</#{tagname}>\n" if data[:type]==:file items += @template.item('id'=>"item#{c+=1}", 'link'=>link) nvitems += @template.navi('id'=>"navPoint-#{c}", 'order'=>c.to_s, 'link'=>link, 'title'=>title) end end indexhtml = @template.index('title'=>'index', 'body'=>indexhtml) opf = @template.opf('title'=>doctitle, 'date'=>Time::now.strftime("%Y/%m/%d"), 'lang' => @env[:lang], 'items'=>items, 'author'=>@env[:author]) ncx = @template.ncx('title'=>doctitle, 'navitems'=>nvitems) open(File::join(output, 'index.html'), 'w') do |io| io.write(indexhtml) end opfpath = "#{doctitle}.opf" open(File::join(output, opfpath), 'w') do |io| io.write(opf) end open(File::join(output, 'toc.ncx'), 'w') do |io| io.write(ncx) end Dir::mkdir(File::join(output, 'META-INF')) unless FileTest::exists? File::join(output, 'META-INF') open(File::join(output, 'META-INF', 'container.xml'), 'w') do |io| io.write(@template.container('opfpath'=>opfpath)) end open(File::join(output, 'mimetype'), 'w') do |io| io.puts('application/epub+zip') end makefile = @template.makefile('title'=>doctitle) open(File::join(output, 'Makefile'), 'w') do |io| io.write(makefile) end end |
#doroot(dir) ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/sc2epub/converter.rb', line 135 def doroot dir dir = path(dir) Dir::foreach(dir) do |i| next if i=="."||i==".." path = File::join(dir, i) if FileTest::directory? path dodir(path) elsif FileTest::file? path dofile(path) end end end |
#local(path) ⇒ Object
16 17 18 19 20 21 22 23 24 |
# File 'lib/sc2epub/converter.rb', line 16 def local path if path.index(@root)==0 path = path[@root.size, path.size] end if path[0,1] == "/" path = path[1, path.size] end return path end |
#path(path) ⇒ Object
13 14 15 |
# File 'lib/sc2epub/converter.rb', line 13 def path path r = File::(path) end |
#title(path) ⇒ Object
25 26 27 |
# File 'lib/sc2epub/converter.rb', line 25 def title path path.gsub(/\//, "_").gsub(/\./, '_') end |