Class: BitClust::RefsDatabase
Overview
Corresponds to db-x.y.z/refs file.
Class Method Summary collapse
Instance Method Summary collapse
- #[](type, mid, linkid) ⇒ Object
- #[]=(type, mid, linkid, desc) ⇒ Object
- #extract(entry) ⇒ Object
-
#initialize ⇒ RefsDatabase
constructor
A new instance of RefsDatabase.
- #save(s) ⇒ Object
Constructor Details
#initialize ⇒ RefsDatabase
Returns a new instance of RefsDatabase.
36 37 38 |
# File 'lib/bitclust/refsdatabase.rb', line 36 def initialize @h = {} end |
Class Method Details
.load(src) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/bitclust/refsdatabase.rb', line 13 def self.load(src) if src.respond_to?(:to_str) # @type var src: _ToStr buf = fopen(src.to_str, 'r:UTF-8'){|f| f.read} elsif src.respond_to?(:to_io) # @type var src: _ToIO buf = src.to_io.read else # @type var src: _Reader buf = src.read end refs = self.new buf&.each_line{|l| if /((?:\\,|[^,])+),((?:\\,|[^,])+),((?:\\,|[^,])+),((?:\\,|[^,])+)\n/ =~ l type, id, linkid, desc = [$1, $2, $3, $4].map{|e| e&.gsub(/\\(.)/){|s| $1 == ',' ? ',' : s } } type || raise; id || raise; linkid || raise; desc || raise refs[type, id, linkid] = desc end } refs end |
Instance Method Details
#[](type, mid, linkid) ⇒ Object
44 45 46 |
# File 'lib/bitclust/refsdatabase.rb', line 44 def [](type, mid, linkid) @h[[type.to_s, mid, linkid]] end |
#[]=(type, mid, linkid, desc) ⇒ Object
40 41 42 |
# File 'lib/bitclust/refsdatabase.rb', line 40 def []=(type, mid, linkid, desc) @h[[type.to_s, mid, linkid]] = desc end |
#extract(entry) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/bitclust/refsdatabase.rb', line 67 def extract(entry) entry.source.each_line{|l| anchor, label = if /\A={1,6}\[a:([-\w]+)\] *(.*)/ =~ l [$1, $2] elsif /\A\#{1,6} +(.*?) *\{#([-\w]+)\}\s*\z/ =~ l # md ソース: 「### 見出し {#anchor}」形式(M3 ネイティブパース)。 # ラベルは rd 表示形へ戻す(\` エスケープ解除等。pattern_matching) Kernel.require 'bitclust/markdown_to_rrd' [$2, ::BitClust::MarkdownToRRD.restore_description($1 || raise)] end next unless anchor entry.labels.each{|name| self[entry.class.type_id, name, anchor || raise] = label || raise } } end |
#save(s) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/bitclust/refsdatabase.rb', line 48 def save(s) if s.respond_to?(:to_str) # @type var s: _ToStr path = s.to_str io = fopen(path, 'w:UTF-8') elsif s.respond_to?(:to_io) # @type var s: _ToIO io = s.to_io else io = s end # @type var io: IO @h.sort.each{|k, v| io.write( [k, v].flatten.map{|e| e.gsub(/,/, '\\,') }.join(',') + "\n" ) } io.close end |