Skip to content
Browse files

tweaks to #58 and #44

  • Loading branch information...
1 parent 1d118b7 commit c97e1ca911b56a6f951be90541c7e7a315ffcaa0 @cldwalker committed Jul 7, 2012
View
6 README.rdoc
@@ -115,6 +115,12 @@ tables/views for any output object:
└───┴───┴───┘
1 row in set
+ # Creates github-markdown
+ >> table [[:a, :b :c]], :markdown => true
+ | 0 | 1 | 2 |
+ |---|---|---|
+ | a | b | c |
+
# Create a table of Dates comparing them with different formats.
>> table [Date.today, Date.today.next_month], :fields=>[:to_s, :ld, :ajd, :amjd, :asctime]
+------------+--------+-----------+-------+--------------------------+
View
2 lib/hirb/helpers.rb
@@ -13,6 +13,6 @@ def self.helper_class(klass)
end
%w{table object_table auto_table tree parent_child_tree vertical_table
- default_table markdown_table unicode_table tab_table}.each do |e|
+ markdown_table unicode_table tab_table}.each do |e|
require "hirb/helpers/#{e}"
end
View
36 lib/hirb/helpers/default_table.rb
@@ -1,36 +0,0 @@
-# Produces a table like this:
-# +----------------+----------+------+--------------+
-# | name | commands | gems | library_type |
-# +----------------+----------+------+--------------+
-# | core/object | 6 | | file |
-# | dir | 7 | | file |
-# | file | 5 | | file |
-# | readme | 4 | | file |
-# | system | 4 | | file |
-# | readmemd | 1 | | file |
-# | github | 9 | | file |
-# | reload_library | 1 | | file |
-# | system_misc | 11 | | file |
-# | tree | 4 | | file |
-# | core/class | 4 | | file |
-# | core/module | 3 | | file |
-# | console | 3 | | file |
-# | syntax | 4 | | file |
-# | core | 6 | | module |
-# | web_core | 5 | | module |
-# +----------------+----------+------+--------------+
-#
-class Hirb::Helpers::DefaultTable < Hirb::Helpers::Table
- CHARS = {
- :top => {:left => '+', :center => '+', :right => '+', :horizontal => '-',
- :vertical => {:outside => '|', :inside => '|'} },
- :middle => {:left => '+', :center => '+', :right => '+', :horizontal => '-'},
- :bottom => {:left => '+', :center => '+', :right => '+', :horizontal => '-',
- :vertical => {:outside => '|', :inside => '|'} }
- }
-
- # Renders a table
- def self.render(rows, options={})
- new(rows, options).render
- end
-end
View
31 lib/hirb/helpers/markdown_table.rb
@@ -1,32 +1,4 @@
-# Produces a table like this:
-#
-# | name | commands | gems | library_type |
-# |---------------- | ---------- | ------------------- | --------------|
-# | core/object | 6 | | file |
-# | dir | 7 | | file |
-# | file | 5 | | file |
-# | readme | 4 | | file |
-# | system | 4 | | file |
-# | readmemd | 1 | | file |
-# | github | 9 | | file |
-# | reload_library | 1 | | file |
-# | system_misc | 11 | | file |
-# | tree | 4 | | file |
-# | core/class | 4 | | file |
-# | core/module | 3 | | file |
-# | console | 3 | | file |
-# | core | 6 | | module |
-# | web_core | 5 | | module |
-# | ansi | 3 | ansi,win32console | file |
-#
-# NOTE: This does not currently include column alignment:
-# | left | center | right |
-# | :------ | :-----: | ----: |
-#
-# NOTE: This does not actually output markdown (or HTML), but rather
-# formats the table in a way which is compatible with markdown tables, and
-# hence compatible with markdown to HTML table conversion.
-#
+# Creates a markdown table for github
class Hirb::Helpers::MarkdownTable < Hirb::Helpers::Table
CHARS = {
:top => {:left => '', :center => '', :right => '', :horizontal => '',
@@ -36,7 +8,6 @@ class Hirb::Helpers::MarkdownTable < Hirb::Helpers::Table
:vertical => {:outside => '|', :inside => ' | '} }
}
- # Renders a markdown-compatible table
def self.render(rows, options={})
new(rows, options).render
end
View
10 lib/hirb/helpers/table.rb
@@ -63,6 +63,14 @@ class Helpers::Table
MIN_FIELD_LENGTH = 3
class TooManyFieldsForWidthError < StandardError; end
+ CHARS = {
+ :top => {:left => '+', :center => '+', :right => '+', :horizontal => '-',
+ :vertical => {:outside => '|', :inside => '|'} },
+ :middle => {:left => '+', :center => '+', :right => '+', :horizontal => '-'},
+ :bottom => {:left => '+', :center => '+', :right => '+', :horizontal => '-',
+ :vertical => {:outside => '|', :inside => '|'} }
+ }
+
class << self
# Main method which returns a formatted table.
@@ -106,7 +114,7 @@ def render(rows, options={})
options[:unicode] ? Helpers::UnicodeTable.render(rows, options) :
options[:tab] ? Helpers::TabTable.render(rows, options) :
options[:markdown] ? Helpers::MarkdownTable.render(rows, options) :
- Helpers::DefaultTable.render(rows, options)
+ new(rows, options).render
rescue TooManyFieldsForWidthError
$stderr.puts "", "** Hirb Warning: Too many fields for the current width. Configure your width " +
"and/or fields to avoid this error. Defaulting to a vertical table. **"
View
30 test/table_test.rb
@@ -507,27 +507,27 @@ def semicolon_join(arr); arr.join('; '); end
end
it "markdown option renders" do
- expected_table = <<-TABLE.unindent
- | a | b |
- |--- | ---|
- | 1 | 2 |
- | 3 | 4 |
-
- 2 rows in set
- TABLE
+ expected_table = <<-TABLE.chomp
+| a | b |
+|--- | ---|
+| 1 | 2 |
+| 3 | 4 |
+
+2 rows in set
+TABLE
table([{:a=>1, :b=>2}, {:a=>3, :b=>4}], :markdown => true).
- should == "\n#{expected_table}" # XXX: line-break hack to restore line break stripped by `unindent`
+ should == "\n#{expected_table}"
end
it "markdown option with no headers renders" do
- expected_table = <<-TABLE.unindent
- | 1 | 2 |
- | 3 | 4 |
+ expected_table = <<-TABLE.chomp
+| 1 | 2 |
+| 3 | 4 |
- 2 rows in set
- TABLE
+2 rows in set
+TABLE
table([{:a=>1, :b=>2}, {:a=>3, :b=>4}], :markdown => true, :headers => false).
- should == "\n#{expected_table}" # XXX: line-break hack to restore line break stripped by `unindent`
+ should == "\n#{expected_table}"
end
it "all_fields option renders all fields" do
View
2 test/test_helper.rb
@@ -45,7 +45,7 @@ class Bacon::Context
class String
def unindent(num=nil)
- regex = num ? /^[ \t\r\f]{#{num}}/ : /^[ \t\r\f]*/
+ regex = num ? /^\s{#{num}}/ : /^\s*/
gsub(regex, '').chomp
end
end

0 comments on commit c97e1ca

Please sign in to comment.
Something went wrong with that request. Please try again.