Skip to content

Commit

Permalink
tweaks to #58 and #44
Browse files Browse the repository at this point in the history
  • Loading branch information
cldwalker committed Jul 7, 2012
1 parent 1d118b7 commit c97e1ca
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 84 deletions.
6 changes: 6 additions & 0 deletions README.rdoc
Expand Up @@ -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]
+------------+--------+-----------+-------+--------------------------+
Expand Down
2 changes: 1 addition & 1 deletion lib/hirb/helpers.rb
Expand Up @@ -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
36 changes: 0 additions & 36 deletions lib/hirb/helpers/default_table.rb

This file was deleted.

31 changes: 1 addition & 30 deletions 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 => '',
Expand All @@ -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
Expand Down
10 changes: 9 additions & 1 deletion lib/hirb/helpers/table.rb
Expand Up @@ -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.
Expand Down Expand Up @@ -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. **"
Expand Down
30 changes: 15 additions & 15 deletions test/table_test.rb
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/test_helper.rb
Expand Up @@ -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
Expand Down

0 comments on commit c97e1ca

Please sign in to comment.