@@ -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