Class: Ffnpdf::Chapter
- Inherits:
-
Object
- Object
- Ffnpdf::Chapter
- Defined in:
- lib/ffnpdf/chapter.rb
Instance Attribute Summary collapse
-
#chapter ⇒ Object
Returns the value of attribute chapter.
-
#doc ⇒ Object
Returns the value of attribute doc.
-
#story_url ⇒ Object
Returns the value of attribute story_url.
Instance Method Summary collapse
- #chapter_title ⇒ Object
- #convert_to_md ⇒ Object
-
#initialize(story_url, chapter) ⇒ Chapter
constructor
A new instance of Chapter.
- #padded_ch ⇒ Object
- #pull_and_convert ⇒ Object
- #pull_chapter ⇒ Object
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
#chapter ⇒ Object
Returns the value of attribute chapter.
8 9 10 |
# File 'lib/ffnpdf/chapter.rb', line 8 def chapter @chapter end |
#doc ⇒ Object
Returns the value of attribute doc.
8 9 10 |
# File 'lib/ffnpdf/chapter.rb', line 8 def doc @doc end |
#story_url ⇒ Object
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_title ⇒ Object
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_md ⇒ Object
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_ch ⇒ Object
58 59 60 |
# File 'lib/ffnpdf/chapter.rb', line 58 def padded_ch "%04d" % chapter end |
#pull_and_convert ⇒ Object
15 16 17 18 |
# File 'lib/ffnpdf/chapter.rb', line 15 def pull_and_convert pull_chapter convert_to_md end |
#pull_chapter ⇒ Object
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 |