diff --git a/lib/puppet/util/reference.rb b/lib/puppet/util/reference.rb index 1ec5880af..ae5f2d44c 100644 --- a/lib/puppet/util/reference.rb +++ b/lib/puppet/util/reference.rb @@ -1,129 +1,139 @@ require 'puppet/util/instance_loader' 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 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 #{name}" depth = section.depth if section.depth < depth end end def self.pdf(text) puts "creating pdf" Puppet::Util.secure_open("/tmp/puppetdoc.txt", "w") do |f| f.puts text end rst2latex = which('rst2latex') || which('rst2latex.py') || raise("Could not find rst2latex") 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 # Now convert to pdf Dir.chdir("/tmp") do %x{texi2pdf puppetdoc.tex >/dev/null 2>/dev/null} 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, :dynamic attr_writer :doc def doc if defined?(@doc) return "#{@name} - #{@doc}" else return @title end end def dynamic? self.dynamic end def markdown_header(name, level) "#{HEADER_LEVELS[level]} #{name}\n\n" end + def markdown_definitionlist(term, definition) + lines = definition.split("\n") + str = "#{term}\n: #{lines.shift}\n" + lines.each do |line| + str << " " if line =~ /\S/ + str << "#{line}\n" + end + str << "\n" + 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 ||= "#{@name.to_s.capitalize} Reference" @page ||= @title.gsub(/\s+/, '') @depth ||= 2 @header ||= "" end # Indent every line in the chunk except those which begin with '..'. def indent(text, tab) text.gsub(/(^|\A)/, tab).gsub(/^ +\.\./, "..") end def option(name, value) ":#{name.to_s.capitalize}: #{value}\n" end def text puts output end def to_markdown(withcontents = true) # First the header text = markdown_header(@title, 1) text << "\n\n**This page is autogenerated; any changes will get overwritten** *(last generated on #{Time.now.to_s})*\n\n" text << @header text << generate text << self.class.footer if withcontents text end end diff --git a/spec/unit/util/reference_spec.rb b/spec/unit/util/reference_spec.rb new file mode 100644 index 000000000..219a673ef --- /dev/null +++ b/spec/unit/util/reference_spec.rb @@ -0,0 +1,29 @@ +#!/usr/bin/env rspec +require 'spec_helper' +require 'puppet/util/reference' + +describe Puppet::Util::Reference do + it "should create valid Markdown extension definition lists" do + my_fragment = nil + Puppet::Util::Reference.newreference :testreference, :doc => "A peer of the type and configuration references, but with no useful information" do + my_term = "A term" + my_definition = <<-EOT +The definition of this term. +We should be able to handle multi-line definitions. + +We should be able to handle multi-paragraph definitions. + EOT + my_fragment = markdown_definitionlist(my_term, my_definition) + end + Puppet::Util::Reference.reference(:testreference).send(:to_markdown, true) + my_fragment.should == <<-EOT +A term +: The definition of this term. + We should be able to handle multi-line definitions. + + We should be able to handle multi-paragraph definitions. + + EOT + end + +end \ No newline at end of file