Skip to content

Commit

Permalink
documents manage their own indexes
Browse files Browse the repository at this point in the history
  • Loading branch information
rtomayko committed Jun 20, 2010
1 parent 7756316 commit a22286a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 19 deletions.
21 changes: 10 additions & 11 deletions bin/ronn
Expand Up @@ -158,21 +158,13 @@ if server
exit 0
end

##
# Load up the index

index_path = "#{File.dirname(ARGV.first || '.')}/index.txt"
index = Ronn::Index.new(index_path, options)
ARGV.each { |file| index << file }

##
# Build Pipeline

pid = nil
wr = STDOUT
ARGV.each do |file|
doc = index.manual(file)

documents = ARGV.map { |file| Ronn::Document.new(file, options) }
documents.each do |doc|
# setup the man pipeline if the --man option was specified
if view && !build
rd, wr = IO.pipe
Expand Down Expand Up @@ -219,6 +211,13 @@ ARGV.each do |file|
end
end

# Write index.txt files

if write_index
File.open(index.path, 'wb') { |fd| fd.puts(index.to_text) }
indexes = documents.map { |doc| doc.index }.uniq
indexes.each do |index|
File.open(index.path, 'wb') do |fd|
fd.puts(index.to_text)
end
end
end
2 changes: 2 additions & 0 deletions lib/ronn/document.rb
Expand Up @@ -78,6 +78,8 @@ def initialize(path=nil, attributes={}, &block)
@styles = %w[man]
@manual, @organization, @date = nil
@markdown, @input_html, @html = nil
@index = Ronn::Index[path || '.']
@index.add_manual(self) if path && name

attributes.each { |attr_name,value| send("#{attr_name}=", value) }
end
Expand Down
34 changes: 26 additions & 8 deletions lib/ronn/index.rb
Expand Up @@ -7,12 +7,28 @@ class Index
include Enumerable

attr_reader :path
attr_reader :attributes
attr_reader :references

def initialize(path, attributes={}, &bk)
# 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.
def self.[](path)
(@indexes ||= {})[index_path_for_file(path)] ||=
Index.new(index_path_for_file(path))
end

def self.index_path_for_file(file)
File.expand_path(
if File.directory?(file)
File.join(file, 'index.txt')
else
File.join(File.dirname(file), 'index.txt')
end
)
end

def initialize(path, &bk)
@path = path
@attributes = attributes.merge(:index => self)
@references = []
@manuals = {}

Expand Down Expand Up @@ -62,9 +78,6 @@ def empty?
references.empty?
end

def find_by_name(name)
end

def [](name)
references.find { |ref| ref.name == name }
end
Expand All @@ -75,8 +88,8 @@ def reference(name, path)

def <<(path)
raise ArgumentError, "local paths only" if path =~ /(https?|mailto):/
relative_path = relative_to_index(path)
return self if any? { |ref| ref.path == File.expand_path(path) }
relative_path = relative_to_index(path)
@references << \
if path =~ /\.ronn?$/
reference manual(path).reference_name, relative_path
Expand All @@ -86,8 +99,13 @@ def <<(path)
self
end

def add_manual(manual)
@manuals[File.expand_path(manual.path)] = manual
self << manual.path
end

def manual(path)
@manuals[File.expand_path(path)] ||= Document.new(path, attributes)
@manuals[File.expand_path(path)] ||= Document.new(path)
end

def manuals
Expand Down

0 comments on commit a22286a

Please sign in to comment.