Commit 2293d1e0 by Paul Alexander

Added support for markdown

parent 6123aa49
== Markdown Fork
Originally forked from https://github.com/openteam/annotated_models.git to add support for generating
annotations compatible with the markdown processor used by YARD.
== Annotate (aka AnnotateModels) == Annotate (aka AnnotateModels)
Add a comment summarizing the current schema to the top or bottom of each of your... Add a comment summarizing the current schema to the top or bottom of each of your...
......
...@@ -61,8 +61,12 @@ OptionParser.new do |opts| ...@@ -61,8 +61,12 @@ OptionParser.new do |opts|
exclusions.each { |exclusion| ENV["exclude_#{exclusion}"] = "yes" } exclusions.each { |exclusion| ENV["exclude_#{exclusion}"] = "yes" }
end end
opts.on('-f', '--format [bare|rdoc]', ['bare', 'rdoc'], 'rdoc: render Schema Infomation as RDoc') do |fmt| opts.on('-f', '--format [bare|rdoc|markdown]', ['bare', 'rdoc', 'markdown'], 'rdoc: render Schema Infomation as RDoc') do |fmt|
ENV['format_#{fmt}'] = 'yes' ENV["format_#{fmt}"] = 'yes'
end
opts.on('--force', 'Force new annotations even if there are no changes.') do |force|
ENV['force'] = 'yes'
end end
end.parse! end.parse!
......
module AnnotateModels module AnnotateModels
class << self
def fmt_header(string)
return string unless ENV['format_markdown']
string.gsub('==', '##')
end
end
# Annotate Models plugin use this header # Annotate Models plugin use this header
COMPAT_PREFIX = "== Schema Info" COMPAT_PREFIX = fmt_header( "== Schema Info" )
PREFIX = "== Schema Information" PREFIX = fmt_header( "== Schema Information" )
END_MARK = "== Schema Information End" END_MARK = fmt_header( "== Schema Information End" )
PATTERN = /^\n?# #{COMPAT_PREFIX}.*?\n(#.*\n)*\n/ PATTERN = /^\n?# #{COMPAT_PREFIX}.*?\n(#.*\n)*\n/
# File.join for windows reverse bar compat? # File.join for windows reverse bar compat?
...@@ -48,6 +56,8 @@ module AnnotateModels ...@@ -48,6 +56,8 @@ module AnnotateModels
end end
end end
# Use the column information in an ActiveRecord class # Use the column information in an ActiveRecord class
# to create a comment block containing a line for # to create a comment block containing a line for
# each column. The line contains the column name, # each column. The line contains the column name,
...@@ -55,11 +65,16 @@ module AnnotateModels ...@@ -55,11 +65,16 @@ module AnnotateModels
def get_schema_info(klass, header, options = {}) def get_schema_info(klass, header, options = {})
info = "# #{header}\n" info = "# #{header}\n"
info<< "#\n" info<< "#\n"
info<< "# Table name: #{klass.table_name}\n" info<< "# Table name: #{klass.table_name}\n" unless klass.table_name == klass.name.tableize
info<< "# Human name: #{klass.model_name.human}\n" unless klass.model_name.human(:default => "").blank? info<< "# Human name: #{klass.model_name.human}\n" unless klass.model_name.human(:default => "").blank?
info<< "#\n" info<< "#\n"
max_size = klass.column_names.map{|name| name.size}.max + (ENV['format_rdoc'] ? 5 : 1) max_size = klass.column_names.map{|name| name.size}.max + (ENV['format_rdoc'] ? 5 : 1)
if ENV['format_markdown']
info<< sprintf( "# %-#{max_size + 4}.#{max_size + 4}s | %-17.17s | %s \n", 'Field', 'Type', 'Attributes' )
info<< "# #{ '-' * ( max_size + 4 ) } | #{'-' * 17} | #{ '-' * 25 } \n"
end
klass.columns.each do |col| klass.columns.each do |col|
attrs = [] attrs = []
attrs << "'#{klass.human_attribute_name(col.name)}'" unless klass.human_attribute_name(col.name, :default => "").blank? attrs << "'#{klass.human_attribute_name(col.name)}'" unless klass.human_attribute_name(col.name, :default => "").blank?
...@@ -96,6 +111,8 @@ module AnnotateModels ...@@ -96,6 +111,8 @@ module AnnotateModels
if ENV['format_rdoc'] if ENV['format_rdoc']
info << sprintf("# %-#{max_size}.#{max_size}s<tt>%s</tt>", "*#{col.name}*::", attrs.unshift(col_type).join(", ")).rstrip + "\n" info << sprintf("# %-#{max_size}.#{max_size}s<tt>%s</tt>", "*#{col.name}*::", attrs.unshift(col_type).join(", ")).rstrip + "\n"
elsif ENV['format_markdown']
info << sprintf("# **%-#{max_size}.#{max_size}s** | `%-15.15s` | `%s `", col.name, col_type, attrs.join(", ")).rstrip + "\n"
else else
info << sprintf("# %-#{max_size}.#{max_size}s:%-15.15s %s", col.name, col_type, attrs.join(", ")).rstrip + "\n" info << sprintf("# %-#{max_size}.#{max_size}s:%-15.15s %s", col.name, col_type, attrs.join(", ")).rstrip + "\n"
end end
...@@ -110,7 +127,7 @@ module AnnotateModels ...@@ -110,7 +127,7 @@ module AnnotateModels
info << "# #{END_MARK}\n" info << "# #{END_MARK}\n"
info << "#++\n\n" info << "#++\n\n"
else else
info << "#\n\n" info << "#\n"
end end
end end
...@@ -150,7 +167,7 @@ module AnnotateModels ...@@ -150,7 +167,7 @@ module AnnotateModels
old_header = old_content.match(header).to_s old_header = old_content.match(header).to_s
new_header = info_block.match(header).to_s new_header = info_block.match(header).to_s
if old_header == new_header if old_header == new_header and not ENV['force']
false false
else else
# Remove old schema info # Remove old schema info
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment