Sha256: 778fbe4cff06a6924615a9714a1403b7986fa39fc91a5dfa9c93d5616bb7bb4c

Contents?: true

Size: 1.45 KB

Versions: 30

Compression:

Stored size: 1.45 KB

Contents

require 'rd/search-file'

module RD
  class RBLFile
    include SearchFile

    SUFFIX = "rbl"
    attr_reader :labels
    attr_reader :filename

    def initialize(filename)
      @filename = RBLFile.basename(filename)
      @labels = []
    end

    def RBLFile.create_rbl_file(filename, resolver)
      file = File.open(RBLFile.rbl_file_path(filename), "w")
      file.print(RBLFile.labels_to_string(resolver))
      file.close
    end

    def RBLFile.rbl_file_path(filename)
      basename(filename) + "." + SUFFIX
    end

    def RBLFile.basename(path)
      if /\.(rd|rb)$/ === path
	$`
      else
	path
      end
    end
    
    def RBLFile.labels_to_string(resolver)
      (resolver.collect do |i|
	 i.to_label + " => " + resolver.get_anchor(i)
       end).join("\n")
    end

    def load_rbl_file(search_paths)
      f = search_file(@filename, search_paths, [SUFFIX])
      raise "RBLFile not found." unless f
      src = File.readlines(f).join("")
      @labels = string_to_labels(src)
    end
		   
    def string_to_labels(src)
      labels = []
      src.each_line do |i|
	labels << parse_line(i)
      end
      labels
    end

    def parse_line(src)
      col = src.rindex("=>")
      raise "RBL file parse error." unless col
      label = src[0 .. col - 1].strip
      anchor = src[col + 2 .. -1].strip
      [label, anchor]
    end

    def refer(label)
      label = @labels.find{|i| i[0] == label}
      return nil unless label
      label[1]
    end
  end
end

Version data entries

30 entries across 29 versions & 4 rubygems

Version Path
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/rdtool-0.6.38/lib/rd/rbl-file.rb
tdiary-5.0.8 vendor/bundle/gems/rdtool-0.6.38/lib/rd/rbl-file.rb
tdiary-5.0.5 vendor/bundle/gems/rdtool-0.6.38/lib/rd/rbl-file.rb
tdiary-5.0.5 vendor/bundle/gems/tdiary-5.0.4/vendor/bundle/gems/rdtool-0.6.38/lib/rd/rbl-file.rb
tdiary-5.0.4 vendor/bundle/gems/rdtool-0.6.38/lib/rd/rbl-file.rb
tdiary-4.2.1 vendor/bundle/ruby/2.2.0/gems/rdtool-0.6.38/lib/rd/rbl-file.rb
nishidayuya-rd2odt-0.1.1.20090701.01 lib/rd2odt/rdtool/rd/rbl-file.rb
nishidayuya-rd2odt-0.1.1.20090704.01 lib/rd2odt/rdtool/rd/rbl-file.rb
nishidayuya-rd2odt-0.1.1.20090706.01 lib/rd2odt/rdtool/rd/rbl-file.rb
nishidayuya-rd2odt-0.1.1 lib/rd2odt/rdtool/rd/rbl-file.rb
tdiary-3.2.2.20130518 vendor/rdtool-0.6.38/lib/rd/rbl-file.rb
tdiary-3.2.2.20130508 vendor/rdtool-0.6.38/lib/rd/rbl-file.rb
tdiary-3.2.2.20130507 vendor/rdtool-0.6.38/lib/rd/rbl-file.rb
rdtool-0.6.38 lib/rd/rbl-file.rb
rdtool-0.6.37 lib/rd/rbl-file.rb
rdtool-0.6.36 lib/rd/rbl-file.rb
rdtool-0.6.35 lib/rd/rbl-file.rb
rdtool-0.6.34 lib/rd/rbl-file.rb
rdtool-0.6.33 lib/rd/rbl-file.rb
rdtool-0.6.32 lib/rd/rbl-file.rb