diff options
Diffstat (limited to 'lib/puppet/util/reference.rb')
-rw-r--r-- | lib/puppet/util/reference.rb | 324 |
1 files changed, 162 insertions, 162 deletions
diff --git a/lib/puppet/util/reference.rb b/lib/puppet/util/reference.rb index 78304217c..62bab643e 100644 --- a/lib/puppet/util/reference.rb +++ b/lib/puppet/util/reference.rb @@ -3,205 +3,205 @@ require 'fileutils' # Manage Reference Documentation. class Puppet::Util::Reference - include Puppet::Util - include Puppet::Util::Docs - - extend Puppet::Util::InstanceLoader - - instance_load(:reference, 'puppet/reference') - - def self.footer - "\n\n----------------\n\n*This page autogenerated on #{Time.now}*\n" - end - - def self.modes - %w{pdf trac text markdown} - end + include Puppet::Util + include Puppet::Util::Docs - def self.newreference(name, options = {}, &block) - ref = self.new(name, options, &block) - instance_hash(:reference)[symbolize(name)] = ref + extend Puppet::Util::InstanceLoader - ref - end + instance_load(:reference, 'puppet/reference') - def self.page(*sections) - depth = 4 - # Use the minimum depth - sections.each do |name| - section = reference(name) or raise "Could not find section #{name}" - depth = section.depth if section.depth < depth - end - text = ".. contents:: :depth: 2\n\n" - end + def self.footer + "\n\n----------------\n\n*This page autogenerated on #{Time.now}*\n" + end + + def self.modes + %w{pdf trac text markdown} + end - def self.pdf(text) - puts "creating pdf" - Puppet::Util.secure_open("/tmp/puppetdoc.txt", "w") do |f| - f.puts text - end - rst2latex = %x{which rst2latex} - if $CHILD_STATUS != 0 or rst2latex =~ /no / - rst2latex = %x{which rst2latex.py} - end - if $CHILD_STATUS != 0 or rst2latex =~ /no / - raise "Could not find rst2latex" - end - rst2latex.chomp! - cmd = %{#{rst2latex} /tmp/puppetdoc.txt > /tmp/puppetdoc.tex} - Puppet::Util.secure_open("/tmp/puppetdoc.tex","w") do |f| - # If we get here without an error, /tmp/puppetdoc.tex isn't a tricky cracker's symlink - end - output = %x{#{cmd}} - unless $CHILD_STATUS == 0 - $stderr.puts "rst2latex failed" - $stderr.puts output - exit(1) - end - $stderr.puts output + def self.newreference(name, options = {}, &block) + ref = self.new(name, options, &block) + instance_hash(:reference)[symbolize(name)] = ref - # Now convert to pdf - Dir.chdir("/tmp") do - %x{texi2pdf puppetdoc.tex >/dev/null 2>/dev/null} - end + 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 #{name}" + depth = section.depth if section.depth < depth end + text = ".. contents:: :depth: 2\n\n" + end - def self.markdown(name, text) - puts "Creating markdown for #{name} reference." - dir = "/tmp/#{Puppet::PUPPETVERSION}" - FileUtils.mkdir(dir) unless File.directory?(dir) - Puppet::Util.secure_open(dir + "/#{name}.rst", "w") do |f| - f.puts text - end - pandoc = %x{which pandoc} - if $CHILD_STATUS != 0 or pandoc =~ /no / - pandoc = %x{which pandoc} - end - if $CHILD_STATUS != 0 or pandoc =~ /no / - raise "Could not find pandoc" - end - pandoc.chomp! - cmd = %{#{pandoc} -s -r rst -w markdown #{dir}/#{name}.rst -o #{dir}/#{name}.mdwn} - output = %x{#{cmd}} - unless $CHILD_STATUS == 0 - $stderr.puts "Pandoc failed to create #{name} reference." - $stderr.puts output - exit(1) - end + def self.pdf(text) + puts "creating pdf" + Puppet::Util.secure_open("/tmp/puppetdoc.txt", "w") do |f| + f.puts text + end + rst2latex = %x{which rst2latex} + if $CHILD_STATUS != 0 or rst2latex =~ /no / + rst2latex = %x{which rst2latex.py} + end + if $CHILD_STATUS != 0 or rst2latex =~ /no / + raise "Could not find rst2latex" + end + rst2latex.chomp! + cmd = %{#{rst2latex} /tmp/puppetdoc.txt > /tmp/puppetdoc.tex} + Puppet::Util.secure_open("/tmp/puppetdoc.tex","w") do |f| + # If we get here without an error, /tmp/puppetdoc.tex isn't a tricky cracker's symlink + end + output = %x{#{cmd}} + unless $CHILD_STATUS == 0 + $stderr.puts "rst2latex failed" + $stderr.puts output + exit(1) + end + $stderr.puts output - File.unlink(dir + "/#{name}.rst") + # Now convert to pdf + Dir.chdir("/tmp") do + %x{texi2pdf puppetdoc.tex >/dev/null 2>/dev/null} end - def self.references - instance_loader(:reference).loadall - loaded_instances(:reference).sort { |a,b| a.to_s <=> b.to_s } + end + + def self.markdown(name, text) + puts "Creating markdown for #{name} reference." + dir = "/tmp/#{Puppet::PUPPETVERSION}" + FileUtils.mkdir(dir) unless File.directory?(dir) + Puppet::Util.secure_open(dir + "/#{name}.rst", "w") do |f| + f.puts text + end + pandoc = %x{which pandoc} + if $CHILD_STATUS != 0 or pandoc =~ /no / + pandoc = %x{which pandoc} + end + if $CHILD_STATUS != 0 or pandoc =~ /no / + raise "Could not find pandoc" + end + pandoc.chomp! + cmd = %{#{pandoc} -s -r rst -w markdown #{dir}/#{name}.rst -o #{dir}/#{name}.mdwn} + output = %x{#{cmd}} + unless $CHILD_STATUS == 0 + $stderr.puts "Pandoc failed to create #{name} reference." + $stderr.puts output + exit(1) end - HEADER_LEVELS = [nil, "=", "-", "+", "'", "~"] + File.unlink(dir + "/#{name}.rst") + end - attr_accessor :page, :depth, :header, :title, :dynamic - attr_writer :doc + def self.references + instance_loader(:reference).loadall + loaded_instances(:reference).sort { |a,b| a.to_s <=> b.to_s } + end - def doc - if defined?(@doc) - return "#{@name} - #{@doc}" - else - return @title - end - end + HEADER_LEVELS = [nil, "=", "-", "+", "'", "~"] - def dynamic? - self.dynamic - end + attr_accessor :page, :depth, :header, :title, :dynamic + attr_writer :doc - def h(name, level) - "#{name}\n#{HEADER_LEVELS[level] * name.to_s.length}\n\n" + def doc + if defined?(@doc) + return "#{@name} - #{@doc}" + else + return @title end + end - def initialize(name, options = {}, &block) - @name = name - options.each do |option, value| - send(option.to_s + "=", value) - end + def dynamic? + self.dynamic + end - meta_def(:generate, &block) + def h(name, level) + "#{name}\n#{HEADER_LEVELS[level] * name.to_s.length}\n\n" + end - # Now handle the defaults - @title ||= "#{@name.to_s.capitalize} Reference" - @page ||= @title.gsub(/\s+/, '') - @depth ||= 2 - @header ||= "" + def initialize(name, options = {}, &block) + @name = name + options.each do |option, value| + send(option.to_s + "=", value) end - # Indent every line in the chunk except those which begin with '..'. - def indent(text, tab) - text.gsub(/(^|\A)/, tab).gsub(/^ +\.\./, "..") - end + meta_def(:generate, &block) - def option(name, value) - ":#{name.to_s.capitalize}: #{value}\n" - end + # Now handle the defaults + @title ||= "#{@name.to_s.capitalize} Reference" + @page ||= @title.gsub(/\s+/, '') + @depth ||= 2 + @header ||= "" + end - def paramwrap(name, text, options = {}) - options[:level] ||= 5 - #str = "#{name} : " - str = h(name, options[:level]) - str += "- **namevar**\n\n" if options[:namevar] - str += text - #str += text.gsub(/\n/, "\n ") + # Indent every line in the chunk except those which begin with '..'. + def indent(text, tab) + text.gsub(/(^|\A)/, tab).gsub(/^ +\.\./, "..") + end - str += "\n\n" - end + def option(name, value) + ":#{name.to_s.capitalize}: #{value}\n" + end - # Remove all trac links. - def strip_trac(text) - text.gsub(/`\w+\s+([^`]+)`:trac:/) { |m| $1 } - end + def paramwrap(name, text, options = {}) + options[:level] ||= 5 + #str = "#{name} : " + str = h(name, options[:level]) + str += "- **namevar**\n\n" if options[:namevar] + str += text + #str += text.gsub(/\n/, "\n ") - def text - puts output - end + str += "\n\n" + 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** *(last generated on #{Time.now.to_s})*\n\n" - text += ".. contents:: :depth: #{@depth}\n\n" if withcontents + # Remove all trac links. + def strip_trac(text) + text.gsub(/`\w+\s+([^`]+)`:trac:/) { |m| $1 } + end - text += @header + def text + puts output + end - text += generate + def to_rest(withcontents = true) + # First the header + text = h(@title, 1) + text += "\n\n**This page is autogenerated; any changes will get overwritten** *(last generated on #{Time.now.to_s})*\n\n" + text += ".. contents:: :depth: #{@depth}\n\n" if withcontents - text += self.class.footer if withcontents + text += @header - text - end + text += generate - def to_text(withcontents = true) - strip_trac(to_rest(withcontents)) - end + text += self.class.footer if withcontents - def to_trac(with_contents = true) - "{{{\n#!rst\n#{self.to_rest(with_contents)}\n}}}" - end + text + end + + def to_text(withcontents = true) + strip_trac(to_rest(withcontents)) + end - def trac - Puppet::Util.secure_open("/tmp/puppetdoc.txt", "w") do |f| - f.puts self.to_trac - end + def to_trac(with_contents = true) + "{{{\n#!rst\n#{self.to_rest(with_contents)}\n}}}" + end - puts "Writing #{@name} reference to trac as #{@page}" - cmd = %{sudo trac-admin /opt/rl/trac/puppet wiki import %s /tmp/puppetdoc.txt} % self.page - output = %x{#{cmd}} - unless $CHILD_STATUS == 0 - $stderr.puts "trac-admin failed" - $stderr.puts output - exit(1) - end - unless output =~ /^\s+/ - $stderr.puts output - end + def trac + Puppet::Util.secure_open("/tmp/puppetdoc.txt", "w") do |f| + f.puts self.to_trac + end + + puts "Writing #{@name} reference to trac as #{@page}" + cmd = %{sudo trac-admin /opt/rl/trac/puppet wiki import %s /tmp/puppetdoc.txt} % self.page + output = %x{#{cmd}} + unless $CHILD_STATUS == 0 + $stderr.puts "trac-admin failed" + $stderr.puts output + exit(1) + end + unless output =~ /^\s+/ + $stderr.puts output end + end end |