summaryrefslogtreecommitdiffstats
path: root/lib/puppet/util/reference.rb
diff options
context:
space:
mode:
authorluke <luke@980ebf18-57e1-0310-9a29-db15c13687c0>2007-05-07 20:07:13 +0000
committerluke <luke@980ebf18-57e1-0310-9a29-db15c13687c0>2007-05-07 20:07:13 +0000
commita040bd48b1e36e9e6e363896fe98e093b3b19bd0 (patch)
tree64ea7f1b0111962112824e886f87a1c09e23f5b0 /lib/puppet/util/reference.rb
parentf42a755cc2e4bc77880dec9571ccd11f33f2737f (diff)
downloadpuppet-a040bd48b1e36e9e6e363896fe98e093b3b19bd0.tar.gz
puppet-a040bd48b1e36e9e6e363896fe98e093b3b19bd0.tar.xz
puppet-a040bd48b1e36e9e6e363896fe98e093b3b19bd0.zip
First run at moving references to lib/puppet instead of puppetdoc
git-svn-id: https://reductivelabs.com/svn/puppet/trunk@2477 980ebf18-57e1-0310-9a29-db15c13687c0
Diffstat (limited to 'lib/puppet/util/reference.rb')
-rw-r--r--lib/puppet/util/reference.rb172
1 files changed, 172 insertions, 0 deletions
diff --git a/lib/puppet/util/reference.rb b/lib/puppet/util/reference.rb
new file mode 100644
index 000000000..3b1d7dd94
--- /dev/null
+++ b/lib/puppet/util/reference.rb
@@ -0,0 +1,172 @@
+require 'puppet/util/instance_loader'
+
+# Manage Reference Documentation.
+class Puppet::Util::Reference
+ include Puppet::Util
+
+ extend Puppet::Util::InstanceLoader
+
+ autoload(:reference, 'puppet/reference')
+
+ def self.footer
+ "\n\n----------------\n\n*This page autogenerated on %s*\n" % Time.now
+ end
+
+ def self.modes
+ %w{pdf trac text}
+ end
+
+ def self.newreference(name, options = {}, &block)
+ ref = self.new(name, options, &block)
+ instance_hash(:reference)[symbolize(name)] = ref
+
+ ref
+ end
+
+ def self.page(*sections)
+ depth = 4
+ # Use the minimum depth
+ sections.each do |name|
+ section = reference(name) or raise "Could not find section %s" % name
+ depth = section.depth if section.depth < depth
+ end
+ text = ".. contents:: :depth: 2\n\n"
+ end
+
+ def self.pdf(text)
+ puts "creating pdf"
+ File.open("/tmp/puppetdoc.txt", "w") do |f|
+ f.puts text
+ end
+ rst2latex = %x{which rst2latex}
+ if $? != 0 or rst2latex =~ /no /
+ rst2latex = %x{which rst2latex.py}
+ end
+ if $? != 0 or rst2latex =~ /no /
+ raise "Could not find rst2latex"
+ end
+ rst2latex.chomp!
+ cmd = %{#{rst2latex} /tmp/puppetdoc.txt > /tmp/puppetdoc.tex}
+ output = %x{#{cmd}}
+ unless $? == 0
+ $stderr.puts "rst2latex failed"
+ $stderr.puts output
+ exit(1)
+ end
+ $stderr.puts output
+
+ # Now convert to pdf
+ puts "handling pdf"
+ Dir.chdir("/tmp") do
+ %x{texi2pdf puppetdoc.tex >/dev/null 2>/dev/null}
+ end
+
+ #if FileTest.exists?("/tmp/puppetdoc.pdf")
+ # FileUtils.mv("/tmp/puppetdoc.pdf", "/export/apache/docroots/reductivelabs.com/htdocs/downloads/puppet/reference.pdf")
+ #end
+ end
+
+ def self.references
+ instance_loader(:reference).loadall
+ loaded_instances(:reference).sort { |a,b| a.to_s <=> b.to_s }
+ end
+
+ HEADER_LEVELS = [nil, "=", "-", "+", "'", "~"]
+
+ attr_accessor :page, :depth, :header, :title
+ attr_writer :doc
+
+ def doc
+ if defined?(@doc)
+ return "%s - %s" % [@name, @doc]
+ else
+ return @title
+ end
+ end
+
+ def h(name, level)
+ return "%s\n%s\n" % [name, HEADER_LEVELS[level] * name.to_s.length]
+ end
+
+ def initialize(name, options = {}, &block)
+ @name = name
+ options.each do |option, value|
+ send(option.to_s + "=", value)
+ end
+
+ meta_def(:generate, &block)
+
+ # Now handle the defaults
+ @title ||= "%s Reference" % @name.to_s.capitalize
+ @page ||= @title.gsub(/\s+/, '')
+ @depth ||= 2
+ @header ||= ""
+ end
+
+ # Indent every line in the chunk except those which begin with '..'.
+ def indent(text, tab)
+ return text.gsub(/(^|\A)/, tab).gsub(/^ +\.\./, "..")
+ end
+
+ def paramwrap(name, text, options = {})
+ options[:level] ||= 5
+ #str = "%s : " % name
+ str = h(name, options[:level])
+ if options[:namevar]
+ str += "- **namevar**\n\n"
+ end
+ str += text
+ #str += text.gsub(/\n/, "\n ")
+
+ str += "\n\n"
+ return str
+ end
+
+ def text
+ puts output
+ end
+
+ def to_rest(withcontents = true)
+ # First the header
+ text = h(@title, 1)
+ text += "\n\n**This page is autogenerated; any changes will get overwritten**\n\n"
+ if withcontents
+ text += ".. contents:: :depth: %s\n\n" % @depth
+ end
+
+ text += @header
+
+ text += generate()
+
+ if withcontents
+ text += self.class.footer
+ end
+
+ return text
+ end
+
+ def to_trac
+ "{{{\n#!rst\n#{self.to_rest}\n}}}"
+ end
+
+ def trac
+ File.open("/tmp/puppetdoc.txt", "w") do |f|
+ f.puts self.to_trac
+ end
+
+ puts "Writing %s reference to trac as %s" % [@name, @page]
+ cmd = %{sudo trac-admin /export/svn/trac/puppet wiki import %s /tmp/puppetdoc.txt} % self.page
+ output = %x{#{cmd}}
+ unless $? == 0
+ $stderr.puts "trac-admin failed"
+ $stderr.puts output
+ exit(1)
+ end
+ unless output =~ /^\s+/
+ $stderr.puts output
+ end
+ end
+
+end
+
+# $Id$