Class: Flee_to_md::Blog
- Inherits:
-
Object
- Object
- Flee_to_md::Blog
- Defined in:
- lib/flee_to_md.rb
Instance Attribute Summary collapse
-
#pages ⇒ Object
Returns the value of attribute pages.
Instance Method Summary collapse
-
#initialize(xml, filename) ⇒ Blog
constructor
A new instance of Blog.
- #normalize(xml) ⇒ Object
- #write ⇒ Object
Constructor Details
#initialize(xml, filename) ⇒ Blog
Returns a new instance of Blog.
12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/flee_to_md.rb', line 12 def initialize (xml, filename) @filename = filename document = Nokogiri::XML normalize(xml) @pages = Array.new items = document.xpath("//item") prog = ProgressBar.create(:title => "Reading", :total => items.length) items.each_with_index do |item, index| @pages.push Page.new(item) # break if index == 4 prog.increment end end |
Instance Attribute Details
#pages ⇒ Object
Returns the value of attribute pages.
11 12 13 |
# File 'lib/flee_to_md.rb', line 11 def pages @pages end |
Instance Method Details
#normalize(xml) ⇒ Object
24 25 26 27 |
# File 'lib/flee_to_md.rb', line 24 def normalize(xml) # iron out some kinks that are causing errors xml.gsub(/data-image/, 'src') end |
#write ⇒ 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 |
# File 'lib/flee_to_md.rb', line 28 def write foldername = @filename.gsub(/\.xml$/, '') @foldername = foldername i = 1 until File.exists?(@foldername) == false @foldername = "#{i}-#{foldername}" i += 1 end prog = ProgressBar.create(:title => "Writing", :total => @pages.length) Dir.mkdir @foldername Dir.mkdir "#{@foldername}/attachments" @pages.each do |page| if page. != "" # is an attachment extension = /(\.[\w\d]+)$/ if page. =~ extension filename = "#{page.title}#{page..match(extension)[1]}" else filename = page.title end open(page.) do |f| File.open("#{@foldername}/attachments/#{filename}","wb") do |file| file.puts f.read end end else # is not an attachment str = "---\n" str << "title: '#{page.title}'\n" str << "date: #{page.date.strftime "%Y-%m-%d %H:%M:%S %z"}\n" if page.categories.length > 0 str << "categories: [#{page.categories.join(", ")}]\n" end if page..length > 0 str << "tags: [#{page..join(", ")}]\n" end if page.type == "linkpost" str << "link: #{page.link}\n" end str << "---\n\n" str << page.markdown.gsub(/\\("|'|\[|\]|\:)/,'\1') page_filename = "#{page.date.strftime('%Y-%m-%d')}-#{page.title.downcase.gsub(/\s+/, '-').gsub(/[^-\w\d]/,'')}.md".gsub(/[^\w\d](.md)$/, '.md') File.open("#{@foldername}/#{page_filename}", 'w') { |file| file.write(str) } end prog.increment end puts "Written to #{@foldername}/" return @foldername end |