Class: Ffnpdf::Chapter

Inherits:
Object
  • Object
show all
Defined in:
lib/ffnpdf/chapter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(story_url, chapter) ⇒ Chapter

Returns a new instance of Chapter.



10
11
12
13
# File 'lib/ffnpdf/chapter.rb', line 10

def initialize(story_url, chapter)
  self.story_url = story_url
  self.chapter = chapter
end

Instance Attribute Details

#chapterObject

Returns the value of attribute chapter.



8
9
10
# File 'lib/ffnpdf/chapter.rb', line 8

def chapter
  @chapter
end

#docObject

Returns the value of attribute doc.



8
9
10
# File 'lib/ffnpdf/chapter.rb', line 8

def doc
  @doc
end

#story_urlObject

Returns the value of attribute story_url.



8
9
10
# File 'lib/ffnpdf/chapter.rb', line 8

def story_url
  @story_url
end

Instance Method Details

#chapter_titleObject



62
63
64
65
66
67
68
# File 'lib/ffnpdf/chapter.rb', line 62

def chapter_title
  if doc.xpath('//form//select[@name="chapter"]/option').count > 1
    doc.xpath('//form//select[@name="chapter"]/option')[chapter - 1].text.gsub(/^[\d]*?\. /, "")
  else
    nil
  end
end

#convert_to_mdObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ffnpdf/chapter.rb', line 33

def convert_to_md
  puts "converting #{padded_ch}.html to #{padded_ch}.md"
  Converter.exec("pandoc #{padded_ch}.html -o #{padded_ch}.md")
  
  if chapter ==1 or chapter_title
    tempfile = File.new("#{padded_ch}.temp", "w")
    if chapter == 1
      tempfile.puts "\\pagenumbering{arabic}"
      tempfile.puts "\\setcounter{page}{1}"
      tempfile.puts
    end

    if (chapter_title)
      tempfile.puts "\#\# #{chapter_title}"
      tempfile.puts
    end
    IO.foreach("#{padded_ch}.md") do |line|
      tempfile.puts line
    end
    tempfile.close
    FileUtils.rm("#{padded_ch}.md")
    File.rename("#{padded_ch}.temp", "#{padded_ch}.md")
  end
end

#padded_chObject



58
59
60
# File 'lib/ffnpdf/chapter.rb', line 58

def padded_ch
  "%04d" % chapter
end

#pull_and_convertObject



15
16
17
18
# File 'lib/ffnpdf/chapter.rb', line 15

def pull_and_convert
  pull_chapter
  convert_to_md
end

#pull_chapterObject



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/ffnpdf/chapter.rb', line 20

def pull_chapter
  puts "pulling chapter #{chapter}"
  pull = HTTParty.get(story_url)
  self.doc = Nokogiri::HTML(pull.body)
  content = doc.css(".storytext")[0].inner_html
  content = content.gsub(/<div class.*?\/div>/m, "")
  content = content.gsub(/<hr.*?>/, "<hr/><p>")
  puts "saving as #{padded_ch}.html"
  tempfile = File.new("#{padded_ch}.html", "w")
  tempfile.puts(content)
  tempfile.close
end