class Ronn::Index
Maintains a list of links / references to manuals and other resources.
Attributes
Public Class Methods
Source
# File lib/ronn/index.rb 13 def self.[](path) 14 (@indexes ||= {})[index_path_for_file(path)] ||= 15 Index.new(index_path_for_file(path)) 16 end
Retrieve an Index
for <path>, where <path> is a directory or normal file. The index is loaded from the corresponding index.txt file if one exists.
Source
# File lib/ronn/index.rb 18 def self.index_path_for_file(file) 19 File.expand_path( 20 if File.directory?(file) 21 File.join(file, 'index.txt') 22 else 23 File.join(File.dirname(file), 'index.txt') 24 end 25 ) 26 end
Source
# File lib/ronn/index.rb 28 def initialize(path) 29 @path = path 30 @references = [] 31 @manuals = {} 32 33 if block_given? 34 read! yield 35 elsif exist? 36 read! File.read(path) 37 end 38 end
Public Instance Methods
Source
# File lib/ronn/index.rb 87 def <<(path) 88 raise ArgumentError, 'local paths only' if path =~ /(https?|mailto):/ 89 return self if any? { |ref| ref.path == File.expand_path(path) } 90 relative_path = relative_to_index(path) 91 @references << \ 92 if path =~ /\.ronn?$/ 93 reference manual(path).reference_name, relative_path 94 else 95 reference File.basename(path), relative_path 96 end 97 self 98 end
Source
# File lib/ronn/index.rb 79 def [](name) 80 references.find { |ref| ref.name == name } 81 end
Source
# File lib/ronn/index.rb 100 def add_manual(manual) 101 @manuals[File.expand_path(manual.path)] = manual 102 self << manual.path 103 end
Source
# File lib/ronn/index.rb 59 def each(&block) 60 references.each(&block) 61 end
Enumerable and friends
Source
# File lib/ronn/index.rb 41 def exist? 42 File.exist?(path) 43 end
Determine whether the index file exists.
Source
# File lib/ronn/index.rb 105 def manual(path) 106 @manuals[File.expand_path(path)] ||= Document.new(path) 107 end
Source
# File lib/ronn/index.rb 109 def manuals 110 select { |ref| ref.relative? && ref.ronn? } 111 .map { |ref| manual(ref.path) } 112 end
Source
# File lib/ronn/index.rb 46 def read!(data) 47 data.each_line do |line| 48 line = line.strip.gsub(/\s*#.*$/, '') 49 unless line.empty? 50 name, url = line.split(/\s+/, 2) 51 @references << reference(name, url) 52 end 53 end 54 end
Load index data from a string.
Source
# File lib/ronn/index.rb 83 def reference(name, path) 84 Reference.new(self, name, path) 85 end
Source
# File lib/ronn/index.rb 129 def relative_to_index(path) 130 path = File.expand_path(path) 131 index_dir = File.dirname(File.expand_path(self.path)) 132 path.sub(/^#{index_dir}\//, '') 133 end
Source
# File lib/ronn/index.rb 117 def to_text 118 map { |ref| [ref.name, ref.location].join(' ') }.join("\n") 119 end
Converting