Sha256: fefb38dfdd69277adf1989d562b9ad1b4c09d6d6a1807f1d3d24839ca0b95215

Contents?: true

Size: 1.83 KB

Versions: 3

Compression:

Stored size: 1.83 KB

Contents

module RTFM
  class ManPage < Struct.new(:name, :section, :date, :summary)
    class << self
      def text_section(*args)
        args.each do |sect|
          class_eval %Q{
            def #{sect}
              @#{sect} ||= TextSection.new(:#{sect}, "")
            end
            def #{sect}=(str)
              @#{sect} = TextSection.new(:#{sect}, str)
            end
          }
        end
      end
      alias_method :text_sections, :text_section
      
      def add_section(name, klass)
        klass = klass.to_s.intern
        class_eval %Q{
          def #{name}
            @#{name} ||= #{klass}.new
            yield @#{name} if block_given?
            @#{name}
          end
        }
      end
      
      def all_pages
        @pages ||= []
      end
    end
    
    text_section :bugs, :diagnostics, :compatibility, :standards, :history
    add_section :see_also, SeeAlsoSection
    add_section :description, DescriptionSection
    add_section :authors, AuthorsSection
    add_section :synopsis, SynopsisSection
    
    def initialize(name, section=nil)
      self.class.all_pages << self
      self.name, self.section = name, section
      self.date = DateTime.now
      yield self
    end

    def add_option(name, desc, opts={})
      opt = Option.new(name, desc, opts)
      description.add_option opt
      unless opts.has_key?(:synopsis) && !opts.delete(:synopsis)
        synopsis.add_option(opt)
      end
    end
    alias_method :option, :add_option
    
    def to_groff
      GroffString.groffify do |out|
        out.Dd date.strftime("%B %d, %Y")
        out.Os
        out.Dt name, (section || "")
        out.section "NAME"
        out.Nm name
        out.Nd summary
        [@synopsis, @description, @see_also, @history, @authors, @bugs].each do |sect|
          out << sect.to_groff if sect
        end
      end
    end
  end
  
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rtfm-0.5.2 lib/rtfm/manpage.rb
rtfm-0.5.1 lib/rtfm/manpage.rb
rtfm-0.5.0 lib/rtfm/manpage.rb