mark_maker 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6da9b6765231882d2ded111aae9340b1b776a649
4
- data.tar.gz: 3af219a0ebd74752af6694b61dc0467b081cbaef
3
+ metadata.gz: 76f4840b445542d5b47ffbe2c24cbace45b7bee2
4
+ data.tar.gz: 21963a8d8dbbb87fcdd2fcbf1dc753438dedf243
5
5
  SHA512:
6
- metadata.gz: 7e291fa3de2f051516db8e72157c95236bc2b5892fcffaab4f29c503a103323896aa60bf155596b47c2052d239c12e39131b638d2734fc8b087425c6f16a3748
7
- data.tar.gz: 6a3dfc905d93c90da048c84e19edaf9e1d4614f075c78410558588584bc80412ae607bd423bc38a0b7a5362b8d7cbc564db58428bd1703b710bb8887970376a3
6
+ metadata.gz: 6215987f2defe491353b4a2fdb7d67b65836f0d93054f9c7d21e71e4fc9eddc5a94bf7770d9cce31d4916fabcb88c3d1324f8352c4114c831560e9ae901d1a3a
7
+ data.tar.gz: ed59aa1746b2e05038cccfbc361ba386fa09dcd724ef5d13e4c870c85b79655be9bc19dd0fad1663f24e2cfed3ca52fd10439d2869e903a5389fd94ec39d2441
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.2.1
data/README.md CHANGED
@@ -155,8 +155,8 @@ puts total
155
155
  puts header
156
156
  puts separator
157
157
  puts gen.table_row("First", "A", "$3.99")
158
- puts gen.table_row("Second", "B", "$14.00")
159
- puts gen.table_row("Third", "C", "$1,034.50")
158
+ puts gen.table_row("Second", "BC", "$14.00")
159
+ puts gen.table_row("Third", "DEFGH", "$1,034.50")
160
160
 
161
161
  Produces this terribly ugly markdown ...
162
162
 
@@ -164,17 +164,37 @@ Produces this terribly ugly markdown ...
164
164
  |Col One|Col Two|Col Three|
165
165
  |-------|-------|---------|
166
166
  |First|A|$3.99|
167
- |Second|B|$14.00|
168
- |Third|C|$1,034.50|
167
+ |Second|BC|$14.00|
168
+ |Third|DEFGH|$1,034.50|
169
169
  ```
170
170
 
171
+ Or, you can pass all the rows in at once like so ...
172
+
173
+ ```ruby
174
+ table_data = [
175
+ ["Col One", "Col Two", "Col Three"],
176
+ [":-", ":-:", "-:"],
177
+ ["First", "A", "$3.99"],
178
+ ["Second", "BC", "$14.00"],
179
+ ["Third", "DEFGH", "$1,034.50"]
180
+ ]
181
+ puts gen.table(*table_data)
182
+ ```
183
+ And get nicely justified markdown like this ...
184
+ ```
185
+ |Col One|Col Two|Col Three|
186
+ |:------|:-----:|--------:|
187
+ |First | A | $3.99|
188
+ |Second | BC | $14.00|
189
+ |Third | DEFGH |$1,034.50|
190
+ ```
171
191
  Which gives you this stunning HTML table ...
172
192
 
173
193
  |Col One|Col Two|Col Three|
174
- |-------|-------|---------|
175
- |First|A|$3.99|
176
- |Second|B|$14.00|
177
- |Third|C|$1,034.50|
194
+ |:------|:-----:|--------:|
195
+ |First | A | $3.99|
196
+ |Second | BC | $14.00|
197
+ |Third | DEFGH |$1,034.50|
178
198
 
179
199
 
180
200
  ### Block Quotes Example
@@ -151,8 +151,8 @@ table_code = <<-EOT
151
151
  puts header
152
152
  puts separator
153
153
  puts gen.table_row("First", "A", "$3.99")
154
- puts gen.table_row("Second", "B", "$14.00")
155
- puts gen.table_row("Third", "C", "$1,034.50")
154
+ puts gen.table_row("Second", "BC", "$14.00")
155
+ puts gen.table_row("Third", "DEFGH", "$1,034.50")
156
156
  EOT
157
157
  puts gen.code_block(*table_code.split("\n"))
158
158
  puts ""
@@ -163,9 +163,27 @@ table_markdown = capture_stdout do
163
163
  end
164
164
  puts gen.fenced_code_block(*table_markdown.string.split("\n"))
165
165
  puts ""
166
+ puts "Or, you can pass all the rows in at once like so ..."
167
+ puts ""
168
+ pretty_table_code = <<-EOT
169
+ table_data = [
170
+ ["Col One", "Col Two", "Col Three"],
171
+ [":-", ":-:", "-:"],
172
+ ["First", "A", "$3.99"],
173
+ ["Second", "BC", "$14.00"],
174
+ ["Third", "DEFGH", "$1,034.50"]
175
+ ]
176
+ puts gen.table(*table_data)
177
+ EOT
178
+ puts gen.fenced_code_language('ruby', *pretty_table_code.split("\n"))
179
+ puts "And get nicely justified markdown like this ..."
180
+ pretty_table_markdown = capture_stdout do
181
+ eval(pretty_table_code)
182
+ end
183
+ puts gen.fenced_code_block(*pretty_table_markdown.string.split("\n"))
166
184
  puts "Which gives you this stunning HTML table ..."
167
185
  puts ""
168
- puts eval(table_code)
186
+ puts eval(pretty_table_code)
169
187
  puts ""
170
188
  puts gen.header3("Block Quotes Example")
171
189
  puts ""
@@ -6,6 +6,42 @@ module MarkMaker
6
6
  underscore * content.size
7
7
  end
8
8
 
9
+ def line_for_left
10
+ end
11
+
12
+ def line_for_right
13
+ end
14
+
15
+ def line_for_center
16
+ end
17
+
18
+ # Justification indicators are a bit of a special case, because the way
19
+ # they actually work is the colon's proximity to the vertical | bars marking
20
+ # the table cells. This means that the center justification indicator, for
21
+ # example :-:, must have the colon directly adjacent to the left | and the
22
+ # right | in order to take effect, like so |:-:|. Any whitespace on either
23
+ # side will cause the center justification to be ignored and default to
24
+ # left justification. So the following *will not* work, |:-: | or | :-: |.
25
+ # In fact the following, | :-:|, will result in right justification. This
26
+ # means we must fill out justification indicators, not just pad them out
27
+ # with spaces, in order for justification markers to look good in our
28
+ # native markdown output (an important consideration in itself) as well
29
+ # as being honored and generating the intended HTML (both being equally
30
+ # important in the MarkDown philosophy).
31
+
32
+ # detect if the given table cell content is a justification indicator
33
+ def justification?(cell)
34
+ cell =~ MarkMaker::RIGHT_JUSTIFY ||
35
+ cell =~ MarkMaker::LEFT_JUSTIFY ||
36
+ cell =~ MarkMaker::CENTER_JUSTIFY
37
+ end
38
+
39
+ # Inspect the cell contents and return a justification indicator
40
+ # as the fill element if the cell is a justification directive.
41
+ def justified_fill(c, fill)
42
+ justification?(c) ? '-' : fill
43
+ end
44
+
9
45
  def header1(title)
10
46
  "#{title}\n#{line_for('=', title)}"
11
47
  end
@@ -85,8 +121,97 @@ module MarkMaker
85
121
  content.inject("|") { |a, e| a << e << "|" }
86
122
  end
87
123
 
124
+ # Table will treat the first line of content as the table header. It
125
+ # will also assess each 'column' and derive the width from the largest
126
+ # cell value, including the header. Justification can be passed in
127
+ # optionally.
128
+
129
+ def table(*content)
130
+ columns = content.transpose
131
+ justified = columns.map { |c| justify(*c) }
132
+ content = justified.transpose
133
+ table = []
134
+ # if content.size >= 1
135
+ # header, separator = table_header(*content[0])
136
+ # table << header << separator
137
+ # end
138
+ content[0, content.size].each { |c| table << table_row(*c) }
139
+ table.map { |t| t + "\n" }
140
+ end
141
+
88
142
  def block_quote(*content)
89
- content.map { |c| "#{BLOCK_QUOTE} #{c}"}
143
+ content.map { |c| "#{BLOCK_QUOTE} #{c}" }
144
+ end
145
+
146
+ def justify(*content)
147
+ # check for a justification marker in the second row
148
+ case content[1]
149
+ when MarkMaker::RIGHT_JUSTIFY
150
+ right_justify(' ', *content)
151
+ when MarkMaker::LEFT_JUSTIFY
152
+ left_justify(' ', *content)
153
+ when MarkMaker::CENTER_JUSTIFY
154
+ center_justify(' ', *content)
155
+ else
156
+ # no justification indicator was found, use a default
157
+ left_justify(' ', *content)
158
+ end
159
+ end
160
+
161
+ def left_justify(fill, *content)
162
+ width = column_width(*content)
163
+
164
+ content.map { |c| c + justified_fill(c, fill) * (width - c.length) }
165
+ end
166
+
167
+ def right_justify(fill, *content)
168
+ width = column_width(*content)
169
+
170
+ content.map { |c| justified_fill(c, fill) * (width - c.length) + c }
171
+ end
172
+
173
+ def center_justify(fill, *content)
174
+ width = column_width(*content)
175
+
176
+ content.map do |c|
177
+ if justification?(c)
178
+ # special case here, as justification must be filled from
179
+ # the middle out in order to meet the markdown spec requirements
180
+ # that will trigger proper justification
181
+ f = []
182
+ f << c
183
+ f << 'a' * width
184
+ fill_justify(justified_fill(c, fill), *f)[0]
185
+ else
186
+ left, right = centered_margins(width, c)
187
+ justified_fill(c, fill) * left + c + justified_fill(c, fill) * right
188
+ end
189
+ end
190
+ end
191
+
192
+ def fill_justify(fill, *content)
193
+ width = column_width(*content)
194
+ content.map do |c|
195
+ # split the cell in two and then add the fill character
196
+ # to the end of the first half of the cell to reach the
197
+ # justified width
198
+ c.insert(c.length / 2, fill * (width - c.length))
199
+ end
200
+ end
201
+
202
+ def column_width(*content)
203
+ longest = content.reduce { |a, e| a.length > e.length ? a : e }
204
+ longest.length
205
+ end
206
+
207
+ def centered_margins(width, content)
208
+ space = width - content.length
209
+ base_margin = space / 2
210
+ remainder_margin = width - content.length - base_margin * 2
211
+ [
212
+ base_margin,
213
+ base_margin + remainder_margin
214
+ ]
90
215
  end
91
216
  end
92
217
  end
@@ -1,4 +1,4 @@
1
1
  # version management
2
2
  module MarkMaker
3
- VERSION = "0.4.0"
3
+ VERSION = "0.5.0"
4
4
  end
data/lib/mark_maker.rb CHANGED
@@ -18,4 +18,7 @@ module MarkMaker
18
18
  FENCE = CODE_TIC * 3
19
19
  EMPHASIS = "*"
20
20
  BLOCK_QUOTE = ">"
21
+ LEFT_JUSTIFY = /^:-+$/
22
+ RIGHT_JUSTIFY = /^-+:$/
23
+ CENTER_JUSTIFY = /^:-*:$/
21
24
  end
data/mark_maker.gemspec CHANGED
@@ -23,4 +23,6 @@ Gem::Specification.new do |spec|
23
23
  spec.add_development_dependency "minitest"
24
24
  spec.add_development_dependency "pry"
25
25
  spec.add_development_dependency "rubocop"
26
+ spec.add_development_dependency "activesupport"
27
+ spec.add_development_dependency "simplecov"
26
28
  end
@@ -1,4 +1,4 @@
1
1
  $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
2
  require 'mark_maker'
3
-
3
+ require 'active_support/core_ext/string'
4
4
  require 'minitest/autorun'
@@ -1,3 +1,6 @@
1
+ require 'simplecov'
2
+ SimpleCov.start
3
+
1
4
  require 'minitest_helper'
2
5
 
3
6
  class TestMarkMaker < Minitest::Test
@@ -152,5 +155,181 @@ class TestMarkMaker < Minitest::Test
152
155
  assert_match(/^> #{Regexp.quote(c)}$/, m)
153
156
  end
154
157
  end
158
+
159
+ # This test should always pass. It is here simple to validate the proper
160
+ # usage of a heredoc as a test method
161
+
162
+ def test_heredoc_method
163
+ desired_output = <<-EOS.strip_heredoc
164
+ This should contain no intents.
165
+
166
+ It should be 3 lines long.
167
+ EOS
168
+ test_output = "This should contain no intents.\n\nIt should be 3 lines long.\n"
169
+ assert_equal(desired_output, test_output, "Output must exactly match.")
170
+ end
171
+
172
+ def test_column_width
173
+ gen = MarkMaker::Generator.new
174
+ assert_equal(5, gen.column_width("One", "Two", "12345", "Four"))
175
+ end
176
+
177
+ def test_pretty_table_generation
178
+ pretty_table = <<-EOS.strip_heredoc
179
+ |Col One|Col Two|Col Three|
180
+ |:------|:-----:|--------:|
181
+ |First | A | $3.99|
182
+ |Second | BC | $14.00|
183
+ |Third | DEFGH |$1,034.50|
184
+ EOS
185
+ table_data = [
186
+ ["Col One", "Col Two", "Col Three"],
187
+ [":-", ":-:", "-:"],
188
+ ["First", "A", "$3.99"],
189
+ ["Second", "BC", "$14.00"],
190
+ ["Third", "DEFGH", "$1,034.50"]
191
+ ]
192
+ gen = MarkMaker::Generator.new
193
+ markup = gen.table(*table_data)
194
+ assert_equal(pretty_table, markup.join)
195
+ end
196
+
197
+ def test_left_justify
198
+ test_justified = ["a ", "bbb ", "cc ", "d d"]
199
+ gen = MarkMaker::Generator.new
200
+ justified = gen.left_justify(' ', "a", "bbb", "cc", "d d")
201
+ assert_equal(test_justified, justified)
202
+ end
203
+
204
+ def test_right_justify
205
+ test_justified = [" a", "bbb", " cc"]
206
+ gen = MarkMaker::Generator.new
207
+ gen_justified = gen.right_justify(' ', "a", "bbb", "cc")
208
+ assert_equal(test_justified, gen_justified)
209
+ end
210
+
211
+ def test_center_justify
212
+ test_justified = [" a ", "bbbbb", " ccc ", " dd ", "e ee "]
213
+ gen = MarkMaker::Generator.new
214
+ gen_justified = gen.center_justify(' ', "a", "bbbbb", "ccc", "dd", "e ee")
215
+ assert_equal(test_justified, gen_justified)
216
+ end
217
+
218
+ def test_column_width
219
+ gen = MarkMaker::Generator.new
220
+ width = gen.column_width("a", "bbb", "ccccc", "dd")
221
+ assert_equal(5, width)
222
+ end
223
+
224
+ def test_centered_margins
225
+ gen = MarkMaker::Generator.new
226
+ left, right = gen.centered_margins(5, "cc")
227
+ assert_equal(1, left)
228
+ assert_equal(2, right)
229
+ end
230
+
231
+ def test_justify_detection
232
+ gen = MarkMaker::Generator.new
233
+
234
+ assert(gen.justification?(":-"))
235
+ assert(gen.justification?(":-:"))
236
+ assert(gen.justification?("-:"))
237
+ assert(gen.justification?(":------"))
238
+ assert(gen.justification?(":------:"))
239
+ assert(gen.justification?(":-:"))
240
+ assert(gen.justification?("------:"))
241
+ refute(gen.justification?("stuff"))
242
+ refute(gen.justification?("stuff:it"))
243
+ refute(gen.justification?(":bad news"))
244
+ refute(gen.justification?("bad news:"))
245
+ refute(gen.justification?("---"))
246
+ refute(gen.justification?("-"))
247
+ end
248
+
249
+ def test_left_justification_indicators
250
+ assert_match(MarkMaker::LEFT_JUSTIFY, ":-")
251
+ assert_match(MarkMaker::LEFT_JUSTIFY, ":---------")
252
+ refute_match(MarkMaker::LEFT_JUSTIFY, "-")
253
+ refute_match(MarkMaker::LEFT_JUSTIFY, "-:")
254
+ refute_match(MarkMaker::LEFT_JUSTIFY, ":--:")
255
+ refute_match(MarkMaker::LEFT_JUSTIFY, "-----")
256
+ refute_match(MarkMaker::LEFT_JUSTIFY, "")
257
+ refute_match(MarkMaker::LEFT_JUSTIFY, ":")
258
+ refute_match(MarkMaker::LEFT_JUSTIFY, "::")
259
+ end
260
+
261
+ def test_right_justification_indicators
262
+ assert_match(MarkMaker::RIGHT_JUSTIFY, "-:")
263
+ assert_match(MarkMaker::RIGHT_JUSTIFY, "---------:")
264
+ refute_match(MarkMaker::RIGHT_JUSTIFY, "-")
265
+ refute_match(MarkMaker::RIGHT_JUSTIFY, ":---")
266
+ refute_match(MarkMaker::RIGHT_JUSTIFY, ":--:")
267
+ refute_match(MarkMaker::RIGHT_JUSTIFY, "-----")
268
+ refute_match(MarkMaker::RIGHT_JUSTIFY, "")
269
+ refute_match(MarkMaker::RIGHT_JUSTIFY, ":")
270
+ refute_match(MarkMaker::RIGHT_JUSTIFY, "::")
271
+ end
272
+
273
+ def test_center_justification_indicators
274
+ assert_match(MarkMaker::CENTER_JUSTIFY, ":-:")
275
+ assert_match(MarkMaker::CENTER_JUSTIFY, ":---------:")
276
+ assert_match(MarkMaker::CENTER_JUSTIFY, "::")
277
+ refute_match(MarkMaker::CENTER_JUSTIFY, "-")
278
+ refute_match(MarkMaker::CENTER_JUSTIFY, "-:")
279
+ refute_match(MarkMaker::CENTER_JUSTIFY, ":--")
280
+ refute_match(MarkMaker::CENTER_JUSTIFY, "-----")
281
+ refute_match(MarkMaker::CENTER_JUSTIFY, "")
282
+ refute_match(MarkMaker::CENTER_JUSTIFY, ":")
283
+ end
284
+
285
+ def test_fill_justify
286
+ gen = MarkMaker::Generator.new
287
+ filled = gen.fill_justify('-', 'header', ":-:", "more", "stuff")
288
+ assert_equal(":----:", filled[1])
289
+ end
290
+
291
+ # def test_determine_justification
292
+ # justifiers = ["-:", ":-", ":--:", "---:", ":---", "::", "---", "zzz"]
293
+ # justifiers.each do |j|
294
+ # case j
295
+ # when MarkMaker::RIGHT_JUSTIFY
296
+ # puts "#{j} will be right justified"
297
+ # when MarkMaker::LEFT_JUSTIFY
298
+ # puts "#{j} will be left justified"
299
+ # when MarkMaker::CENTER_JUSTIFY
300
+ # puts "#{j} will be center justified"
301
+ # else
302
+ # puts "#{j} is an invalid justification indicator"
303
+ # end
304
+ # end
305
+ # end
306
+
307
+ # def test_right_justify_table_column
308
+ # right_justified = <<-EOS.strip_heredoc
309
+ # | Justified|
310
+ # |----------:|
311
+ # | a|
312
+ # |bbbbbbbbbbb|
313
+ # | ccc|
314
+ # EOS
315
+ # gen = MarkMaker::Generator.new
316
+ # markdown = gen.table("Justified", ?????)
317
+ # assert_equal(right_justified, markup, "Column content should be right justified.")
318
+ # end
319
+
320
+
321
+ # def test_pretty_table_justified_generation
322
+ # pretty_table = <<-EOS.strip_heredoc
323
+ # |Col One|Col Two|Col Three |Data Sized |
324
+ # |-------|-------|----------|-------------|
325
+ # |First | A | $3.99|xxxxxxxxxxxxx|
326
+ # |Second | BC | $14.00|y |
327
+ # |Third | DEF | $1,034.50|z |
328
+ # |Fourth |GHIJKLM|$10,123.45|a |
329
+ # EOS
330
+
331
+ # markup = 'nada'
332
+ # assert_equal(pretty_table, markup)
333
+ # end
155
334
  end
156
335
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mark_maker
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brad Schneider
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-19 00:00:00.000000000 Z
11
+ date: 2015-10-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -80,6 +80,34 @@ dependencies:
80
80
  - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: activesupport
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: simplecov
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
83
111
  description: Programatically generate markdown documents.
84
112
  email:
85
113
  - brad.schneider@me.com
@@ -90,6 +118,7 @@ extra_rdoc_files: []
90
118
  files:
91
119
  - ".gitignore"
92
120
  - ".rubocop.yml"
121
+ - ".ruby-version"
93
122
  - ".travis.yml"
94
123
  - CHANGELOG.md
95
124
  - Gemfile
@@ -123,7 +152,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
123
152
  version: '0'
124
153
  requirements: []
125
154
  rubyforge_project:
126
- rubygems_version: 2.4.5
155
+ rubygems_version: 2.4.6
127
156
  signing_key:
128
157
  specification_version: 4
129
158
  summary: Markdown generator.