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.
6 7 8 |
# File 'lib/hateda2md/mdbuilder.rb', line 6 def initialize(path) @entries = build_entries(path) end |
Instance Attribute Details
#entries ⇒ Object (readonly)
Returns the value of attribute entries.
5 6 7 |
# File 'lib/hateda2md/mdbuilder.rb', line 5 def entries @entries end |
Instance Method Details
#build_entries(filepath) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/hateda2md/mdbuilder.rb', line 10 def build_entries(filepath) xml = REXML::Document.new(open filepath) res = [] xml.elements.each('diary/day') do |ent| date = ent.attributes['date'] body = ent.elements['body'].text.strip mdbody = nil title = ent.attributes['title'] res << HateDa::Entry[date, body, mdbody, title] end res end |
#filter(pattern, opt = {}, &replace) ⇒ Object
27 28 29 |
# File 'lib/hateda2md/mdbuilder.rb', line 27 def filter(pattern, opt={}, &replace) entries.each { |ent| ent.filter(pattern, opt, &replace) } end |
#pre_defined_filters(alias_flag = false) ⇒ Object
31 32 33 |
# File 'lib/hateda2md/mdbuilder.rb', line 31 def pre_defined_filters(alias_flag=false) HateDa::Converter.pre_defined_filters(alias_flag) end |
#run(*range) ⇒ Object
35 36 37 38 39 40 41 42 43 |
# File 'lib/hateda2md/mdbuilder.rb', line 35 def run(*range) range = [0..-1] if range.empty? entries[*range].map do |entry| md = entry.to_md(entry.ent_body) # place this before use of get_title() entry.ent_title = get_title(entry) if entry.ent_title.empty? entry.ent_mdbody = md entry end end |
#save_to_files(opt = {}) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/hateda2md/mdbuilder.rb', line 45 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
23 24 25 |
# File 'lib/hateda2md/mdbuilder.rb', line 23 def set(item, *args) entries.each { |ent| ent.set item, *args } end |