Class: MdEdit
- Inherits:
-
Object
- Object
- MdEdit
- Defined in:
- lib/md_edit.rb
Instance Attribute Summary collapse
-
#phraseslookup ⇒ Object
readonly
Returns the value of attribute phraseslookup.
-
#sections ⇒ Object
readonly
Returns the value of attribute sections.
Instance Method Summary collapse
- #create(s) ⇒ Object
-
#delete(s) ⇒ Object
specify a heading to delete a section.
- #find(s, heading: true) ⇒ Object
-
#initialize(md, debug: false, root: 'Thoughts') ⇒ MdEdit
constructor
pass in a Markdown document or a Markdown filename.
- #query(s, full_trail: false, limit: 10) ⇒ Object (also: #q)
- #to_h ⇒ Object
- #to_outline(bullets: false) ⇒ Object
- #to_s ⇒ Object
-
#update(raw_value, heading: nil) ⇒ Object
(also: #edit)
update a section by heading title e.g.
Constructor Details
#initialize(md, debug: false, root: 'Thoughts') ⇒ MdEdit
pass in a Markdown document or a Markdown filename
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/md_edit.rb', line 19 def initialize(md, debug: false, root: 'Thoughts') @debug = debug s, @filename = if md.lines.length == 1 then File.write(md, "# #{root}\n\n") unless File.exists? md File.exists?(md) ? [File.read(md), md] : md else md end load_sections(s) end |
Instance Attribute Details
#phraseslookup ⇒ Object (readonly)
Returns the value of attribute phraseslookup.
15 16 17 |
# File 'lib/md_edit.rb', line 15 def phraseslookup @phraseslookup end |
#sections ⇒ Object (readonly)
Returns the value of attribute sections.
15 16 17 |
# File 'lib/md_edit.rb', line 15 def sections @sections end |
Instance Method Details
#create(s) ⇒ Object
38 39 40 41 42 43 44 45 46 |
# File 'lib/md_edit.rb', line 38 def create(s) @s = @s.rstrip + "\n\n" + s.sub(/^(?=\w)/,'## ').sub(/#+ [a-z]/)\ {|x| x.upcase}.sub(/(?!=^\n)$/,"\n\n") load_sections(@s) save() :created end |
#delete(s) ⇒ Object
specify a heading to delete a section
50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/md_edit.rb', line 50 def delete(s) key = @sections.keys.grep(/#{s.downcase}/i).first old_value = @sections[key].flatten.join heading = last_heading(key) old_section = heading + old_value @s.sub!(old_section, '') load_sections(@s) save() :deleted end |
#find(s, heading: true) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/md_edit.rb', line 95 def find(s, heading: true) key = @sections.keys\ .grep(/#{s.downcase.gsub('(','\(').gsub(')','\)')}/i).first return unless key headings = key.lines.first.split(/ > /) title = "%s %s" % ['#' * headings.length, headings.last] a = [title, @sections[key].join] heading ? a.join : a end |
#query(s, full_trail: false, limit: 10) ⇒ Object Also known as: q
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/md_edit.rb', line 108 def query(s, full_trail: false, limit: 10) puts 'query() s: ' + s.inspect if @debug results = [] r = @headingslookup.q s puts 'query() r: ' + r.inspect if @debug if r and r.any? then results = if full_trail then r else r.map do |x| headings = x.split(' > ') headings.length > 1 ? headings[1..-1].join(' > ') : headings[0] end.reject(&:empty?) end end r2 = @phraseslookup.q s, search_tags: true if r2 and r2.any? then a = r2.sort_by {|x| -x.length} # attempt to remove duplicate results from the 1 section a2 = a.group_by {|x| x[/\[[^\]]+\]/]} a2.each do |k,v| s4 = v.first[/\]\s*(.*)/mi,1] index = s4 =~ /#{s}/mi s2 = make_snippet(s4, index, words: [2,2]) v[1..-1].each do |x| s5 = x[/\]\s*(.*)/,1] index2 = s5 =~ /#{s}/ if index2 then s3 = make_snippet(s5, index2, words: [2,2]) v.delete x if s2 =~ /#{s3}/ else v.delete x end end end end if a2 then h = a2.values.flatten(1).group_by {|x| x[/\[[^\]]+\]/]} phrases_found = h.to_a.flat_map do |raw_heading,v| # get rid of results which are almost duplicate (contain a # subset of text from the 1st result) phrases = v.map {|x| x[/\] +(.*)/,1]} filtered_phrases = phrases[1..-1].reject {|x| phrases[0].include? x } filtered_phrases.unshift(phrases[0]) # get rid of the top level heading from the heading trailing heading = if raw_heading =~ / > / then raw_heading[1..-2].split(' > ')[1..-1].join(' > ') else raw_heading[1..-2] end filtered_phrases.map {|phrase| "[%s] %s" % [heading, phrase] } end results.concat phrases_found if phrases_found end results.take limit end |
#to_h ⇒ Object
214 215 216 |
# File 'lib/md_edit.rb', line 214 def to_h() @h end |
#to_outline(bullets: false) ⇒ Object
206 207 208 209 210 211 212 |
# File 'lib/md_edit.rb', line 206 def to_outline(bullets: false) a = indentor(@s.scan(/^#+ [^\n]+/).join("\n")) .lines.map {|x| x.sub(/#+ +/,'')} bullets ? a.map{|x| x.sub(/\b/,'- ')} : a.join end |
#to_s ⇒ Object
218 219 220 |
# File 'lib/md_edit.rb', line 218 def to_s() @s end |
#update(raw_value, heading: nil) ⇒ Object Also known as: edit
update a section by heading title e.g. ## To-donn[ ] Vacuum the bedroom
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/md_edit.rb', line 66 def update(raw_value, heading: nil ) value = raw_value.gsub(/\r/,'').chomp + "\n" title = (heading ? heading : value.lines.first.chomp)[/#+ +(.*)/,1] key = @sections.keys.grep(/#{title.downcase}/i).first return unless key old_value = @sections[key].flatten.join puts 'old_value: ' + old_value.inspect if @debug heading = last_heading(key) old_section = value =~ /^#+/ ? heading + old_value : old_value puts 'old_section: ' + old_section.inspect if @debug @s.sub!(old_section, value) puts '@s: ' + @s.inspect if @debug load_sections(@s) save() :updated end |