Class: WikiMd
Defined Under Namespace
Classes: Entry
Instance Attribute Summary collapse
-
#active_heading ⇒ Object
readonly
Returns the value of attribute active_heading.
-
#dx ⇒ Object
readonly
Returns the value of attribute dx.
-
#dxsx ⇒ Object
readonly
Returns the value of attribute dxsx.
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
Instance Method Summary collapse
- #create_section(s) ⇒ Object (also: #add_section)
- #delete_section(q) ⇒ Object
- #entries ⇒ Object
- #find(q, exact_match: false) ⇒ Object
- #find_all(q, exact_match: false) ⇒ Object
- #find_tag(tag) ⇒ Object
- #headings ⇒ Object
-
#initialize(wiki = nil, domain: nil, debug: false, base_url: '', tag_base_url: '/tag', order: 'ascending', title: 'MyWiki') ⇒ WikiMd
constructor
A new instance of WikiMd.
- #new_md(x = nil, title: @title, save: true) ⇒ Object (also: #import)
- #read_section(heading, exact_match: false) ⇒ Object
- #save(filename = @filename || 'mywiki.md') ⇒ Object
- #sort! ⇒ Object
- #tags ⇒ Object
- #title ⇒ Object
- #title=(s) ⇒ Object
-
#to_accordion(ascending: true, includetags: false, navbar: false) ⇒ Object
generates an accordion menu in XML format.
- #to_aztoc(base_url: '') ⇒ Object
- #to_rawdx ⇒ Object
- #to_s ⇒ Object
- #to_sections ⇒ Object
- #to_toc ⇒ Object
- #to_xml ⇒ Object
- #update(val) ⇒ Object
- #update_section(*args) ⇒ Object
Constructor Details
#initialize(wiki = nil, domain: nil, debug: false, base_url: '', tag_base_url: '/tag', order: 'ascending', title: 'MyWiki') ⇒ WikiMd
Returns a new instance of WikiMd.
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 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 |
# File 'lib/wiki_md.rb', line 73 def initialize(wiki=nil, domain: nil, debug: false, base_url: '', tag_base_url: '/tag', order: 'ascending', title: 'MyWiki') @domain, @debug, @base_url = domain, debug, base_url @tag_base_url, @order, @title = tag_base_url, order, title if wiki then if wiki.lines.length > 1 and wiki.lstrip[0] != '<' then wiki = "<?wikimd version='1.0'?>\ntitle: Untitled\n\n" + wiki end puts 'before rxfhelper wiki: ' + wiki.inspect if @debug raw_s, type = RXFHelper.read(wiki, auto: false) s = raw_s.strip puts 'after rxfhelper s: ' + s.inspect if @debug puts ('type: ' + type.inspect).debug if debug if debug then puts [ 's: ' + s.inspect, 'type: ' + type.inspect, 's.lines.length: ' + s.lines.length.inspect]\ .join("\n").debug puts ('file exists? ' + FileX.exists?(File.dirname(s)).inspect).debug end if type == :unknown and s.lines.length == 1 then #if FileX.exists?(File.dirname(s)) then puts 'before new_md()'.debug if debug new_md() @filename = wiki @filepath = File.dirname @filename indexfile = File.join(@filepath, 'index.xml') # check for the index.xml file @dx = load_index(indexfile) save() elsif type == :file or type == :dfs @filename = wiki valid, msg = if s =~ /^<\?wikimd\b/ then true else validate(s) end raise WikiMdReadError, msg unless valid puts ('s: ' + s.inspect).debug if @debug @dxsx = read_raw_document s @filepath = File.dirname @filename indexfile = File.join(@filepath, 'index.xml') puts 'indexfile: ' + indexfile.inspect if @debug if s != FileX.read(wiki) then puts '*** file changed! ***'.info if @debug FileX.rm indexfile if FileX.exists? indexfile = File.join(@filepath, 'dxtags.xml') FileX.rm if FileX.exists? save() end # check for the index.xml file @dx = load_index(indexfile) if FileX.exists? indexfile else @dxsx = read_raw_document s end else new_md() end @filepath ||= '.' @dxtags = DynarexTags.new(@filepath, debug: debug) end |
Instance Attribute Details
#active_heading ⇒ Object (readonly)
Returns the value of attribute active_heading.
16 17 18 |
# File 'lib/wiki_md.rb', line 16 def active_heading @active_heading end |
#dx ⇒ Object (readonly)
Returns the value of attribute dx.
16 17 18 |
# File 'lib/wiki_md.rb', line 16 def dx @dx end |
#dxsx ⇒ Object (readonly)
Returns the value of attribute dxsx.
16 17 18 |
# File 'lib/wiki_md.rb', line 16 def dxsx @dxsx end |
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
16 17 18 |
# File 'lib/wiki_md.rb', line 16 def filename @filename end |
Instance Method Details
#create_section(s) ⇒ Object Also known as: add_section
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 203 204 205 206 207 |
# File 'lib/wiki_md.rb', line 177 def create_section(s) @active_heading = title = s[/(?<=^# ).*/] tagline = s.rstrip.lines.last[/(?<=^\+\s).*/] s2 = if tagline then s else a = title.split tagline = a.length > 1 ? a.map(&:capitalize).join : a.first s + "\n\n+ " + tagline end @dxsx.create(x: s2) puts ('@filename: ' + @filename.inspect).debug if @debug return unless @filename record = {title: title + ' #' + tagline.lstrip.split.join(' #'), url: [@base_url, File.basename(@filename)[/.*(?=\.\w+)/], URI.escape(title.gsub(/ /,'_'))].join('/')} @dx.create record FileX.write @filename, @dxsx.to_s @dxtags.add record end |
#delete_section(q) ⇒ Object
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/wiki_md.rb', line 211 def delete_section(q) regex = q.is_a?(String) ? /#{q}/i : q r = @dxsx.dx.all.find {|section| section.x.lines.first =~ regex } return unless r r.delete rx = @dx.all.find {|x| x.title =~ regex} return unless rx title = rx.title rx.delete @dxtags.delete title puts ('deleted title: ' + title.inspect).debug if @debug :section_deleted end |
#entries ⇒ Object
233 234 235 236 237 238 239 240 241 242 |
# File 'lib/wiki_md.rb', line 233 def entries() @dxsx.dx.all.map do |x| puts 'x: ' + x.inspect if @debug Entry.new(x, debug: @debug) end end |
#find(q, exact_match: false) ⇒ Object
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 |
# File 'lib/wiki_md.rb', line 244 def find(q, exact_match: false) puts ('WikiMd::find q: ' + q.inspect).debug if @debug return Entry.new(@dxsx.dx.find(q), debug: @debug) if q =~ /^\d+$/ regex = if q.is_a?(String) then q.gsub!(/_/,' ') exact_match ? /#{q}$/i : /#{q}/i else q end r = @dxsx.dx.all.find do |section| section.x.lines.first =~ regex end puts (' r: ' + r.inspect).debug if @debug return unless r heading2 = r.x.lines.last[/(?<=redirect ).*/] heading2 ? find(heading2) : Entry.new(r, debug: @debug) end |
#find_all(q, exact_match: false) ⇒ Object
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 |
# File 'lib/wiki_md.rb', line 270 def find_all(q, exact_match: false) regex = if q.is_a?(String) then q.gsub!(/_/,' ') exact_match ? /#{q}$/i : /#{q}/i else q end r = @dxsx.dx.all.select do |section| puts 'first: ' + section.x.lines.first.inspect if @debug section.x.lines.first =~ regex end puts (' r: ' + r.inspect).debug if @debug return unless r r.map {|x| Entry.new x } end |
#find_tag(tag) ⇒ Object
293 294 295 |
# File 'lib/wiki_md.rb', line 293 def find_tag(tag) @dxtags.find tag end |
#headings ⇒ Object
297 298 299 |
# File 'lib/wiki_md.rb', line 297 def headings() @dxsx.dx.all.map {|section| section.x.lines.first.chomp[/(?<=# ).*/] } end |
#new_md(x = nil, title: @title, save: true) ⇒ Object Also known as: import
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 |
# File 'lib/wiki_md.rb', line 301 def new_md(x=nil, title: @title, save: true) puts 'inside new_md'.debug if @debug s = nil s, _ = RXFHelper.read(x) if x s ||= <<EOF <?dynarex schema="sections[title]/section(x)" format_mask="[!x]"?> title: #{title} --# EOF @dxsx = DxSectionX.new s, autosave: save, debug: @debug, order: @order end |
#read_section(heading, exact_match: false) ⇒ Object
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 |
# File 'lib/wiki_md.rb', line 322 def read_section(heading, exact_match: false) section = find heading, exact_match: exact_match if section then # find any newly created links which now have a destination page sectionx = section.x.lines.map do |x| link = x[/\?([^\?]+)\?/,1] if link then a = link.split r = a.last =~ /\// if not r then heading = a[0..2].join if find heading then x.sub!(/\?[^\?]+\?/, "[%s](%s)" % [heading, a.last]) end end end x end.join puts ('sectionx: ' + sectionx.inspect).debug if @debug section.x = sectionx unless section.x == sectionx r = section puts ('@domain: ' + @domain.inspect).debug if @debug r.instance_variable_set(:@domain, @domain) r.instance_variable_set(:@tag_url, "%s/%s" % [@tag_base_url, File.basename(@filename)[/.*(?=\.\w+$)/]]) def r.to_html() lines = self.x.lines last_line = lines.last content, = if last_line[0] == '+' then = last_line[/(?<=\+).*/].chomp.split [lines[0..-2].join, \ .map {|x| "+ [%s](%s/%s)" % [x, @tag_url, x]}] else [self.x, []] end s = block_given? ? yield(content) : content html = Martile.new(s, ignore_domainlabel: @domain).to_html = Kramdown::Document.new(.join("\n")).to_html\ .sub('<ul>','<ul id="tags">') html + "\n\n" + end r end end |
#save(filename = @filename || 'mywiki.md') ⇒ Object
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 |
# File 'lib/wiki_md.rb', line 395 def save(filename=@filename || 'mywiki.md') @filename = filename puts '@filename: ' + @filename.inspect if @debug @filepath = File.(File.dirname(@filename).to_s) puts '@filepath: ' + @filepath.inspect if @debug puts 'dx_to_wmd: ' + dx_to_wmd(@dxsx.to_dx) if @debug contents = dx_to_wmd(@dxsx.to_dx) FileX.write @filename=filename, contents # make a backup file FileX.write @filename.sub(/\.\w+$/,'.bak'), contents puts ('before @dxsx save').debug if @debug @dxsx.save filename.sub(/\.md$/, '.xml') @dx = new_index(File.join(@filepath, 'index.xml')) unless @dx end |
#sort! ⇒ Object
417 418 419 420 421 422 423 |
# File 'lib/wiki_md.rb', line 417 def sort!() @dxsx.dx.sort_by! do |rec| rec.element('x').value.lines.first.chomp[/(?<=^# ).*/] end end |
#tags ⇒ Object
425 426 427 |
# File 'lib/wiki_md.rb', line 425 def () @dxtags. end |
#title ⇒ Object
429 430 431 |
# File 'lib/wiki_md.rb', line 429 def title() @dxsx.dx.title() end |
#title=(s) ⇒ Object
433 434 435 |
# File 'lib/wiki_md.rb', line 433 def title=(s) @dxsx.dx.title = s end |
#to_accordion(ascending: true, includetags: false, navbar: false) ⇒ Object
generates an accordion menu in XML format. It can be rendered to HTML using the Martile gem
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 |
# File 'lib/wiki_md.rb', line 444 def to_accordion(ascending: true, includetags: false, navbar: false) doc = Rexle.new("<accordion navbar='#{.to_s}'/>") unsortedrows = entries if then unsortedrows += ().map do |key, value| body = value.map do |x| title = x[:title][/^[^#]+/].rstrip "* [%s](#%s)" % [title, title.downcase.gsub(/\W/,'-')\ .gsub(/-{2,}/,'-').gsub(/^-|-$/,'')] end OpenStruct.new(heading: key + " (#{value.length})", body: body.join("\n"), tag_goto: true) end end puts 'ascending: ' + ascending.inspect if @debug rows = ascending ? unsortedrows.sort_by {|x| x.heading.downcase} \ : unsortedrows puts 'rows: ' + rows.inspect if @debug rows.each do |x| e = Rexle::Element.new('panel') e.add_attribute(title: x.heading) e.add_attribute(class: 'entry') unless x.respond_to? :tag_goto puts 'x.body: ' + x.body.inspect if @debug body = "<body>%s</body>" % \ Martile.new(x.body, ignore_domainlabel: @domain).to_html Rexle.new(body).root.elements.each {|element| e.add element } doc.root.add e end doc.root.xml pretty: true end |
#to_aztoc(base_url: '') ⇒ Object
437 438 439 |
# File 'lib/wiki_md.rb', line 437 def to_aztoc(base_url: '') Yatoc.new(self.entries.map(&:to_s).join).to_aztoc.gsub(/(?<=href=')([^']+)/, base_url + '\1') end |
#to_rawdx ⇒ Object
506 507 508 |
# File 'lib/wiki_md.rb', line 506 def to_rawdx() @dxsx.to_s end |
#to_s ⇒ Object
510 511 512 |
# File 'lib/wiki_md.rb', line 510 def to_s() dx_to_wmd(@dxsx.to_dx) end |
#to_sections ⇒ Object
493 494 495 496 497 498 499 500 |
# File 'lib/wiki_md.rb', line 493 def to_sections() sort!() @dxsx.to_doc.root.xpath('records/section')\ .map {|x| x.xml(pretty: true)}.join("\n") end |
#to_toc ⇒ Object
514 515 516 517 |
# File 'lib/wiki_md.rb', line 514 def to_toc() y = Yatoc.new(Kramdown::Document.new(self.to_sections).to_html) puts y.to_html end |
#to_xml ⇒ Object
502 503 504 |
# File 'lib/wiki_md.rb', line 502 def to_xml() @dxsx.to_doc.xml(declaration: false, pretty: true) end |
#update(val) ⇒ Object
519 520 521 |
# File 'lib/wiki_md.rb', line 519 def update(val) self.method(val[/^<\?dynarex/] ? :import : :update_section).call val end |
#update_section(*args) ⇒ Object
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 |
# File 'lib/wiki_md.rb', line 523 def update_section(*args) puts 'inside update_section'.debug if @debug val, raw_q = args.reverse puts ' val: ' + val.inspect if @debug puts ' raw_q: ' + raw_q.inspect if @debug q = raw_q ? raw_q : val.lines.first[/(?<=^# ).*/] puts ' q: ' + q.inspect if @debug @section = r = find(q) puts 'r: ' + r.inspect if @debug return false unless r content = val =~ /# / ? val : r.x.lines.first + "\n" + val @active_heading = title = content.lines.first[/(?<=^# )[^#]+/].rstrip if content.lines.last[/^\+/] then tagline1 = content.lines.last[/^\+\s+(.*)/,1] else tagline1 = content.lines.first[/(?<=# )[^#]+(.*)/]\ .scan(/(?<=#)\w+/).join(' ') content = '# ' + title + "\n" + content.lines[1..-1].join + "\n\n+ " + tagline1 end rx = @dx.all.find {|x| x.title =~ /#{q}/} puts 'tagline1: ' + tagline1.inspect if @debug s2 = if tagline1 then content else a = title.split tagline1 = a.length > 1 ? a.map(&:capitalize).join : a.first content + "\n\n+ " + tagline1 end puts 's2: ' + s2.inspect if @debug r.x = s2 if rx then # update the index entry if the title or tags have been modified puts 'before update_index' if @debug update_index(rx, tagline1, @active_heading) else # create a new index entry puts 'before new index entry' if @debug newtagline = if tagline1 then tagline1.split.join(' #') else a = @active_heading.split(/ +/) a.length > 1 ? a.map(&:capitalize).join : a.first end record = {title: @active_heading + ' #' + newtagline, url: [@base_url, File.basename(@filename)[/.*(?=\.\w+)/], URI.escape(@active_heading.gsub(/ /, '_'))].join('/')} @dx.create record @dxtags.add record end end |