bhollis/maruku

View on GitHub
docs/exd/exd.rb

Summary

Maintainability
A
35 mins
Test Coverage
require 'maruku'

MaRuKu::Globals[:unsafe_features] = true

def extract_chunks(is, start_reg, end_reg)
    all = []
    current = nil; current_start = nil
    line = 1
    is.each_line do |l|
        if not current
            if l =~ start_reg
                current = []
                current_start = line
            end
        else
            if l =~ end_reg
                current_end = line
                all.push [current.join, current_start, current_end]
                current = nil
            else
                current.push l.sub(/^\s*#/, '')
            end
        end
        line += 1
    end
    return all
end

all_docs = []
dir = File.expand_path("../../../lib/", __FILE__)
sources = Dir[dir + '/**/*.rb'].each do |file|
    File.open(file, 'r') do |f|
        chunks = extract_chunks(f, /^\s*\#\=begin maruku_doc/, /^\s*\#\=end/)
        chunks.each do  |chunk, line_start, line_end|
            doc = Maruku.new(chunk, {:on_error => :raise})
            doc.attributes[:file] = file.gsub(/^.*?\/lib\/maruku\//,'')
            doc.attributes[:line_start] = line_start
            doc.attributes[:line_end] = line_end
            all_docs << doc
        end
    end
end


attributes = {}
misc = []
all_docs.each {|doc|
    if att = doc.attributes[:attribute]
        attributes[att] ||= []
        attributes[att] << doc
    else
        misc << doc
    end
}

include MaRuKu::Helpers

puts attributes.keys

bigdoc = Maruku.new

bigdoc.instance_eval do
    self.attributes[:css] = 'style.css'
    @children << md_header(1, ['Auto-extracted documentation'])
    @children.push *parse_text_as_markdown("
This documentation is auto-generated by reading from the Ruby source
for `maruku` by the program `docs/exd/exd.rb`. 
It is important to have documentation near the code!

Each block is delimited by `=begin` and `=end` blocks and
is a Markdown document:

    ...
    =begin maruku_doc
    Attribute: att1
    Summary: summary for attribute 
    
    Expanded documentation (Markdown format)
    =end 
    ...")
    
    @children << md_header(2, ['Attribute documentation'])
    
    extended = []

    attributes.keys.sort.each do |att| desc = attributes[att]
        h = md_header(3, ["Attribute ", md_code(att)])
        h.attributes[:id] = att
        extended.push  h
    
        if MaRuKu::Globals.has_key? att.to_sym
            default = MaRuKu::Globals[att.to_sym]
            par = md_par(["Default: ", md_code(default.inspect)])
            par.attributes[:class] =  'maruku-att-default'
            extended.push par
        else
            puts "No default for #{att}"
        end
    
        desc.each do |d|
            rev = ""
            file = d.attributes[:file]
            url  = 
            "http://rubyforge.org/viewvc/trunk/lib/maruku/#{file}?annotate=#{rev}&root=maruku"
            
            origin = md_par([
                "Read from file ", 
                md_code(file),
#                md_im_link([md_code(file)], url),
                ", line #{d.attributes[:line_start]}:"
            ])
            
            origin.attributes[:class] = 'maruku-att-origin'
            
            extended.push  origin
            
            extended.push(*d.children)
        end
        
    end

    @children.push(*extended)

    if false
        @children.push md_header(2, ['Other misc. documentation'])

        @children.push *parse_text_as_markdown("
    This other documentation are other bits and pieces found in the code
    that are not related to attributes.")

        misc.each do |d| @children.push(*d.children) end
    end
end

#puts bigdoc.inspect
File.open('exd.html','w') do |f|
    f.puts bigdoc.to_html_document
end
bigdoc.attributes[:latex_use_listings] = true
File.open('exd.tex','w') do |f|
    f.puts bigdoc.to_latex_document
end