Class: HateDa::MdBuilder
- Inherits:
-
Object
- Object
- HateDa::MdBuilder
- Defined in:
- lib/hateda2md/mdbuilder.rb
Instance Attribute Summary collapse
-
#entries ⇒ Object
readonly
Returns the value of attribute entries.
Instance Method Summary collapse
- #build_entries(filepath) ⇒ Object
- #filter(pattern, opt = {}, &replace) ⇒ Object
-
#initialize(path) ⇒ MdBuilder
constructor
A new instance of MdBuilder.
- #pre_defined_filters(alias_flag = false) ⇒ Object
- #run(*range) ⇒ Object
- #save_to_files(opt = {}) ⇒ Object
- #set(item, *args) ⇒ Object
Constructor Details
#initialize(path) ⇒ MdBuilder
Returns a new instance of MdBuilder.
16 17 18 |
# File 'lib/hateda2md/mdbuilder.rb', line 16 def initialize(path) @entries = build_entries(path) end |
Instance Attribute Details
#entries ⇒ Object (readonly)
Returns the value of attribute entries.
15 16 17 |
# File 'lib/hateda2md/mdbuilder.rb', line 15 def entries @entries end |
Instance Method Details
#build_entries(filepath) ⇒ Object
20 21 22 23 24 25 26 27 28 29 |
# File 'lib/hateda2md/mdbuilder.rb', line 20 def build_entries(filepath) xml = Nokogiri::XML(open filepath) xml.search('day').map do |ent| date = ent.attributes['date'].value body = ent.css('body').text.strip mdbody = nil title = ent.attributes['title'].value HateDa::Entry[date, body, mdbody, title] end end |
#filter(pattern, opt = {}, &replace) ⇒ Object
35 36 37 |
# File 'lib/hateda2md/mdbuilder.rb', line 35 def filter(pattern, opt={}, &replace) entries.each { |ent| ent.filter(pattern, opt, &replace) } end |
#pre_defined_filters(alias_flag = false) ⇒ Object
39 40 41 |
# File 'lib/hateda2md/mdbuilder.rb', line 39 def pre_defined_filters(alias_flag=false) HateDa::Converter.pre_defined_filters(alias_flag) end |
#run(*range) ⇒ Object
43 44 45 46 47 48 49 50 51 |
# File 'lib/hateda2md/mdbuilder.rb', line 43 def run(*range) range = [0..-1] if range.empty? entries[*range].map do |entry| md = entry.to_md(entry.ent_body) entry.ent_title = get_title(entry) if entry.ent_title.empty? entry.ent_mdbody = md entry end end |
#save_to_files(opt = {}) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/hateda2md/mdbuilder.rb', line 53 def save_to_files(opt={}) opt = {dir:'md', ext:'md'}.update(opt) md_entries = entries.select { |ent| ent.ent_mdbody } unless md_entries.empty? Dir.mkdir(opt[:dir]) unless Dir.exist?(opt[:dir]) md_entries.each do |ent| path = "#{opt[:dir]}/#{ent.ent_date}-#{title_for_file(ent.ent_title)}.#{opt[:ext]}" File.open(path, 'w') do |f| f.puts header(ent.ent_title, ent.ent_date) f.puts ent.ent_mdbody f.puts footnotes(ent.stocks[:footnotes]) unless ent.stocks[:footnotes].empty? end end end rescue => e print "class => #{e.class}\nmessage => #{e.}\nbacktrace => #{e.backtrace}\n" end |
#set(item, *args) ⇒ Object
31 32 33 |
# File 'lib/hateda2md/mdbuilder.rb', line 31 def set(item, *args) entries.each { |ent| ent.set item, *args } end |