Skip to content

Commit 71ac22d

Browse files
committed
Overhaul Markdown generation.
1 parent cef9f95 commit 71ac22d

File tree

3 files changed

+50
-9
lines changed

3 files changed

+50
-9
lines changed

CHANGELOG.rdoc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
== 2.5.0
22

3+
* Adding note that Markdown is actually MultiMarkdown, and recommending the use
4+
of the `kramdown` engine for parsing it.
5+
* Improved Markdown formatting considerably.
6+
* Bugfix: Needed to use inline-code tag for column and table names, otherwise
7+
underscores would cause havok with the formatting.
8+
* Bugfix: Markdown syntax was incorrect (can't have trailing spaces before the
9+
closing marker for an emphasis tag).
310
* Bugfix: Remove-annotations wasn't properly finding test/spec files, and
411
wasn't even looking for FactoryGirl factories under the new naming
512
convention.

README.rdoc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,21 @@ By default, columns will be sorted in database order (i.e. the order in which mi
140140

141141
If you prefer to sort alphabetically so that the results of
142142
annotation are consistent regardless of what order migrations are executed in, use --sort.
143+
== Markdown
144+
145+
The format produced is actually MultiMarkdown, making use of the syntax
146+
extension for tables. It's recommended you use `kramdown` as your parser if
147+
you want to use this format. If you're using `yard` to generate documentation,
148+
specify a format of markdown with `kramdown` as the provider by adding this to
149+
your `.yardopts` file:
150+
151+
--markup markdown
152+
--markup-provider kramdown
153+
154+
Be sure to add this to your `Gemfile` as well:
155+
156+
gem 'kramdown', :groups => [:development], :require => false
157+
143158

144159
== WARNING
145160

lib/annotate/annotate_models.rb

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,24 @@ def quote(value)
8787
def get_schema_info(klass, header, options = {})
8888
info = "# #{header}\n"
8989
info<< "#\n"
90-
info<< "# Table name: #{klass.table_name}\n"
90+
if(options[:format_markdown])
91+
info<< "# Table name: `#{klass.table_name}`\n"
92+
info<< "#\n"
93+
info<< "# ### Columns\n"
94+
else
95+
info<< "# Table name: #{klass.table_name}\n"
96+
end
9197
info<< "#\n"
9298

9399
max_size = klass.column_names.map{|name| name.size}.max || 0
94100
max_size += options[:format_rdoc] ? 5 : 1
101+
md_names_overhead = 6
102+
md_type_allowance = 18
103+
bare_type_allowance = 16
95104

96105
if(options[:format_markdown])
97-
info<< sprintf( "# %-#{max_size + 4}.#{max_size + 4}s | %-18.18s | %s\n", 'Field', 'Type', 'Attributes' )
98-
info<< "# #{ '-' * ( max_size + 4 ) } | #{'-' * 18} | #{ '-' * 25 }\n"
106+
info<< sprintf( "# %-#{max_size + md_names_overhead}.#{max_size + md_names_overhead}s | %-#{md_type_allowance}.#{md_type_allowance}s | %s\n", 'Name', 'Type', 'Attributes' )
107+
info<< "# #{ '-' * ( max_size + md_names_overhead ) } | #{'-' * md_type_allowance} | #{ '-' * 27 }\n"
99108
end
100109

101110
cols = klass.columns
@@ -136,14 +145,16 @@ def get_schema_info(klass, header, options = {})
136145
if options[:format_rdoc]
137146
info << sprintf("# %-#{max_size}.#{max_size}s<tt>%s</tt>", "*#{col.name}*::", attrs.unshift(col_type).join(", ")).rstrip + "\n"
138147
elsif options[:format_markdown]
139-
info << sprintf("# **%-#{max_size}.#{max_size}s** | `%-16.16s` | `%s`", col.name, col_type, attrs.join(", ").rstrip) + "\n"
148+
name_remainder = max_size - col.name.length
149+
type_remainder = (md_type_allowance - 2) - col_type.length
150+
info << (sprintf("# **`%s`**%#{name_remainder}s | `%s`%#{type_remainder}s | `%s`", col.name, " ", col_type, " ", attrs.join(", ").rstrip)).gsub('``', ' ').rstrip + "\n"
140151
else
141-
info << sprintf("# %-#{max_size}.#{max_size}s:%-16.16s %s", col.name, col_type, attrs.join(", ")).rstrip + "\n"
152+
info << sprintf("# %-#{max_size}.#{max_size}s:%-#{bare_type_allowance}.#{bare_type_allowance}s %s", col.name, col_type, attrs.join(", ")).rstrip + "\n"
142153
end
143154
end
144155

145156
if options[:show_indexes] && klass.table_exists?
146-
info << get_index_info(klass)
157+
info << get_index_info(klass, options)
147158
end
148159

149160
if options[:format_rdoc]
@@ -155,15 +166,23 @@ def get_schema_info(klass, header, options = {})
155166
end
156167
end
157168

158-
def get_index_info(klass)
159-
index_info = "#\n# Indexes\n#\n"
169+
def get_index_info(klass, options={})
170+
if(options[:format_markdown])
171+
index_info = "#\n# ### Indexes\n#\n"
172+
else
173+
index_info = "#\n# Indexes\n#\n"
174+
end
160175

161176
indexes = klass.connection.indexes(klass.table_name)
162177
return "" if indexes.empty?
163178

164179
max_size = indexes.collect{|index| index.name.size}.max + 1
165180
indexes.sort_by{|index| index.name}.each do |index|
166-
index_info << sprintf("# %-#{max_size}.#{max_size}s %s %s", index.name, "(#{index.columns.join(",")})", index.unique ? "UNIQUE" : "").rstrip + "\n"
181+
if(options[:format_markdown])
182+
index_info << sprintf("# * `%s`%s:\n# * **`%s`**\n", index.name, index.unique ? " (_unique_)" : "", index.columns.join("`**\n# * **`"))
183+
else
184+
index_info << sprintf("# %-#{max_size}.#{max_size}s %s %s", index.name, "(#{index.columns.join(",")})", index.unique ? "UNIQUE" : "").rstrip + "\n"
185+
end
167186
end
168187
return index_info
169188
end

0 commit comments

Comments
 (0)