class QDoc

Public Class Methods

new( options = {} ) click to toggle source

Initializes QDoc

options[:output_directory

set the directory to be created for documentation

options#index_content

set the static index page message

options#bootstratp

:local (default) copies the local file, otherwise CDN stylesheet is used

# File lib/qdoc.rb, line 17
def initialize( options = {} )
  defaults = { output_directory: "doc", index_content: "Choose from the list.", target_extensions: %w(md rc html), bootstrap: :local, bootstrap_version: :v3 }
  options = defaults.merge options
  @out_dir = options[:output_directory]
  
  @target_extensions = options[:target_extensions].join("|")

  @index = { file: "index.html", title: "index", extension: "html", content: options[:index_content], directory: nil, output_file: "index.html", parse: false }
  
  @documents = Array.new
  @locations = Array.new
  @redcarpet = Redcarpet::Markdown.new(Redcarpet::Render::HTML.new( :with_toc_data => true), :autolink => true, :space_after_headers => false, :no_intra_emphasis => true, :tables => true)
  
  @bootstrap_local = (options[:bootstrap] == :local)
  @bootstrap_version = options[:bootstrap_version]
  case @bootstrap_version
  when :v4
    @bootstrap_cdn = BOOTSTRAPv4_CDN
  else
    @bootstrap_cdn = BOOTSTRAP_CDN
  end
end

Public Instance Methods

run() click to toggle source

Causes QDoc to search the current directory and tree, and parse found files

# File lib/qdoc.rb, line 41
def run
  @locations = identify_locations
  @documents = stage_files( @locations )
  @index[:content] = create_index @documents
  make_doc_directory
  @documents.each do |doc|
    doc[:content] = parse_document( doc ) if doc[:parse] #"#{doc[:directory]}/#{doc[:filename]}")
    write_file( "#{@out_dir}/#{doc[:output_file]}", create_page(doc[:content], doc[:title]) )
  end
  copy_bootstrap
end

Private Instance Methods

bootstrap_tables( html ) click to toggle source
# File lib/qdoc.rb, line 83
def bootstrap_tables( html )
  html.gsub('<table>', '<table class="table">')
end
copy_bootstrap() click to toggle source
# File lib/qdoc.rb, line 101
def copy_bootstrap
  bootstrap_file = case @bootstrap_version
  when :v4
    "/bootstrap.v4.min.css"
  else
    "/bootstrap.min.css"
  end
  
  FileUtils.cp( File.dirname(__FILE__) + bootstrap_file, "#{@out_dir}/bootstrap.min.css" ) if @bootstrap_local
end
create_index( documents ) click to toggle source
# File lib/qdoc.rb, line 158
def create_index( documents )
  snippet = "<table class='table table-striped'>
  <thead>
  <tr>
  <th>Location</th><th>Document</th>
  </tr>
  </thead>
  <tbody>"
  documents.each do |doc|
    snippet += "<tr>
    <td>#{doc[:directory]}</td><td><a href='#{doc[:output_file]}'>#{doc[:title]}</a></td>
    </tr>"
  end
  snippet += "</tbody></table>"
  snippet
end
create_nav( content ) click to toggle source
# File lib/qdoc.rb, line 175
def create_nav( content )
  doc = Nokogiri::HTML content
  headers = doc.xpath('/html/body/*[self::h1 or self::h2]') # Nokogiri::XML::NodeSet
  if !headers.empty?
    nav = headers.map { |h|
      if h.attributes["id"] # Markdown
        "  " * ((h.name[1].to_i) - 1) + "* " + "<a href='#" + h.attributes["id"].value + "'>" + h.content + "</a>" if h.attributes["id"]
      else # Redcloth
        "  " * ((h.name[1].to_i) - 1) + "* " + "<a href='#" + h.children.to_s + "'>" + h.children.to_s + "</a>" if !h.attributes["id"]
      end
    }.join("\n")
  else
          nav = ""
  end
  '<div id="navbar" class="bs-sidebar" role="complementary">' + @redcarpet.render( nav ).gsub("<ul>","<ul class='nav'>") + '</div>'
end
create_page( content, title, bootstrap = :local, bootstrap_version = :v3 ) click to toggle source
# File lib/qdoc.rb, line 112
def create_page( content, title, bootstrap = :local, bootstrap_version = :v3 )
  "<!DOCTYPE html>
  <html>
  <head>
    <title>" + title + "</title>
    <meta http-equiv='Content-Type' content='text/html; charset=UTF-8'>
    " + ( @bootstrap_local ? "
    <link href='bootstrap.min.css' rel='stylesheet'>" : @bootstrap_cdn ) + "
    <style>
      .bs-sidebar .nav > li > a {
        padding-top: 0px;
        padding-bottom: 0px;
        padding-left: 10px;
        font-size: 90%;
      }
      .bs-sidebar .nav .nav > li > a {
        padding-top: 0px;
        padding-bottom: 0px;
        padding-left: 20px;
        font-size: 75%;
      }
      .bs-sidebar .nav .nav .nav > li > a {
        padding-top: 0px;
        padding-bottom: 0px;
        padding-left: 60px;
        font-size: 80%;
      }" + ( bootstrap_version = :v4 ? "              pre {
                      padding: 9.5px;
                      background-color: #f5f5f5;
                      border: 1px solid #ccc;
                      border-radius: 4px
              }" : "" ) + "
    </style>
  </head>
  <body>
    <div class='row'>
      <div class='col-xs-12 col-sm-6 col-md-3 col-lg-2 hidden-print'>" + \
  create_nav( content ) + "
      </div>
      <div class='col-xs-12 col-sm-6 col-md-9 col-lg-10'>" + \
  content + "
      </div>
    </div>
  </body>"
end
identify_locations() click to toggle source
# File lib/qdoc.rb, line 59
def identify_locations
  locations = [""] # Current directory
  Dir.foreach('./') do |node|
    next if node =~ /\A\./ || node == @out_dir # || leaf =~ /\.rb\z/
    locations << node if File.directory?(node)
  end
  locations
end
make_doc_directory() click to toggle source
# File lib/qdoc.rb, line 55
def make_doc_directory
    FileUtils.mkdir_p(@out_dir) unless File.directory?(@out_dir)
end
parse_document( doc ) click to toggle source
# File lib/qdoc.rb, line 87
def parse_document( doc )
  filename = doc[:directory] == "" ? doc[:filename] : "#{doc[:directory]}/#{doc[:filename]}"
  contents = File.read( filename )
  case doc[:extension]
  when "md", "rc", "txt"
    # return Hawaiian.encode_html @redcarpet.render( contents )
    @redcarpet.render( contents )
  when "html"
    contents
  else
    nil
  end
end
stage_files( locations ) click to toggle source
# File lib/qdoc.rb, line 68
def stage_files( locations )
  documents = [ @index ]
  locations.each do |dir|
    Dir.foreach("./#{dir}") do |node|
      node.match(/(?<name>.*)\.(?<ext>#{@target_extensions})\z/) do |match|
        filehead = match[:name]
        file_ext = match[:ext]
        filename = filehead + "." + file_ext
        documents << { directory: dir, filename: filename, extension: file_ext, file: filehead + ".html", title: filename, content: nil, parse: true, output_file: "#{dir}-#{filename}.html" }
      end
    end
  end
  documents
end
write_file( filename, content ) click to toggle source
# File lib/qdoc.rb, line 192
def write_file( filename, content )
  File.open(filename, "w") do |o|
    o.write( bootstrap_tables(content) )
    o.close
  end
end