tablerize 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +1 -0
- data/.rspec +3 -0
- data/.travis.yml +7 -0
- data/LICENSE +20 -0
- data/Makefile +15 -0
- data/README.md +264 -0
- data/bin/tablerize +17 -0
- data/examples/example-1.html +9 -0
- data/examples/example-1.yml +27 -0
- data/examples/example-2.html +13 -0
- data/examples/example-2.yml +33 -0
- data/lib/tablerize/html_element.rb +67 -0
- data/lib/tablerize/version.rb +3 -0
- data/lib/tablerize.rb +71 -0
- data/spec/spec_helper.rb +1 -0
- data/spec/tablerize_spec.rb +19 -0
- data/tablerize.gemspec +22 -0
- metadata +83 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: e70970cea20a8c6ee05fe6ed66d56f69389d40cd
|
4
|
+
data.tar.gz: 577fb09939cf9995bef8886dd81f1ef9ffc46f9d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 08fbcc9174c61c2b4c09bf6dab052f8106b34b01f991f4f27d73cb75654e4c6652e96b59e5d6919becc136e1d26076758413a0dbebdb5968bb9cac68e4f8acfd
|
7
|
+
data.tar.gz: b6bc1783e850f6a49317cc5a84625fe87b1f0b1213a9eaf60d97920590e8819742e8a764ead5e6ce3186eb18b475fe26001790d6b4dc31fc7ab418b82c21dc9b
|
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
/tablerize-*.gem
|
data/.rspec
ADDED
data/.travis.yml
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2015 IFTTT Inc
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Makefile
ADDED
data/README.md
ADDED
@@ -0,0 +1,264 @@
|
|
1
|
+
Tablerize
|
2
|
+
=========
|
3
|
+
|
4
|
+
Tablerize is a format for writing tables using YAML/JSON-compatible data
|
5
|
+
structures, and Ruby code to convert it to HTML.
|
6
|
+
|
7
|
+
|
8
|
+
## Usage
|
9
|
+
|
10
|
+
To install
|
11
|
+
|
12
|
+
```shell
|
13
|
+
cd tablerize
|
14
|
+
make install
|
15
|
+
# or (soon)
|
16
|
+
gem install tablerize
|
17
|
+
```
|
18
|
+
|
19
|
+
You can use it in Ruby...
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
require 'tablerize'
|
23
|
+
puts Tablerize.load_file(path).to_html
|
24
|
+
# or
|
25
|
+
puts Tablerize.load(yaml_string).to_html
|
26
|
+
# or
|
27
|
+
puts Tablerize.make_table(object_from_yaml_or_json).to_html
|
28
|
+
```
|
29
|
+
|
30
|
+
...or from the command line
|
31
|
+
|
32
|
+
```shell
|
33
|
+
tablerize path/to/yaml-table.yml [...]
|
34
|
+
```
|
35
|
+
|
36
|
+
|
37
|
+
## Why?
|
38
|
+
|
39
|
+
Markdown is easy on the eyes. It helps you write formatted documents in plain
|
40
|
+
text in a way that is meaningful even without rendering. One thing that [the
|
41
|
+
original Markdown specification] doesn't support is tables. But many authors
|
42
|
+
writing in Markdown want to write tables, so Markdown libraries have come up
|
43
|
+
with various but similar ways of representing tables in Markdown.
|
44
|
+
|
45
|
+
[the original Markdown specification]: http://daringfireball.net/projects/markdown/syntax
|
46
|
+
|
47
|
+
Tables exist to help you line things up. But these Markdown tables force you to
|
48
|
+
either line things up yourself, or deal with the unreadable results. Here's some
|
49
|
+
a table from the [Markdown Cheatsheet]:
|
50
|
+
|
51
|
+
[Markdown Cheatsheet]: https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet
|
52
|
+
|
53
|
+
```
|
54
|
+
| Tables | Are | Cool |
|
55
|
+
| ------------- |:-------------:| -----:|
|
56
|
+
| col 3 is | right-aligned | $1600 |
|
57
|
+
| col 2 is | centered | $12 |
|
58
|
+
| zebra stripes | are neat | $1 |
|
59
|
+
```
|
60
|
+
|
61
|
+
This is pretty readable, but unless you know exactly what the table looks like
|
62
|
+
beforehand, lining everything up takes a lot of time! But that's okay, you don't
|
63
|
+
_actually_ need to do that, you can just have this mess instead:
|
64
|
+
|
65
|
+
```
|
66
|
+
Markdown | Less | Pretty
|
67
|
+
--- | --- | ---
|
68
|
+
*Still* | `renders` | **nicely**
|
69
|
+
1 | 2 | 3
|
70
|
+
```
|
71
|
+
|
72
|
+
Why is this less pretty? Because, while tables exist to organize data, the data
|
73
|
+
looks pretty disorganized here. This goes against the concept of having raw
|
74
|
+
Markdown be readable!
|
75
|
+
|
76
|
+
Depending what Markdown library you're using, the examples above might not even
|
77
|
+
render. Many flavors of Markdown tables exist, and accept table syntax in
|
78
|
+
varying degrees of leniency. Not only does this make writing tables even harder
|
79
|
+
("which Markdown library am I using and does it support table feature X?"), but
|
80
|
+
it suggests that these these libraries are just making concessions with the
|
81
|
+
constraints of representing a table in this way. Tables take data, something
|
82
|
+
that should be computer-readable, and make them human-readable. Unrendered
|
83
|
+
Markdown tables, sadly, usually are neither.
|
84
|
+
|
85
|
+
Oh, and with this traditional syntax, you can pretty much forget about nesting
|
86
|
+
tables.
|
87
|
+
|
88
|
+
|
89
|
+
## So what's Tablerize?
|
90
|
+
|
91
|
+
**Tablerize** attempts to solve these problems. In its purest form, it is a
|
92
|
+
specification of a _human-readable representation of tables in YAML/JSON-
|
93
|
+
compatible data_. Since YAML is human-readable, so can Tablerize. This project
|
94
|
+
also includes a Ruby library and command-line tool to convert this YAML- based
|
95
|
+
format into HTML tables. It can be run with:
|
96
|
+
|
97
|
+
```shell
|
98
|
+
tablerize path/to/yaml-table.yml [...]
|
99
|
+
```
|
100
|
+
|
101
|
+
A complementary project, [kramdown-tablerize], allows embedding of YAML
|
102
|
+
tables into [kramdown] Markdown documents.
|
103
|
+
|
104
|
+
[kramdown]: http://kramdown.gettalong.org/
|
105
|
+
[kramdown-tablerize]: https://github.com/IFTTT/kramdown-tablerize
|
106
|
+
|
107
|
+
|
108
|
+
## Format
|
109
|
+
|
110
|
+
Here's example: Searching for "statistics" on Google news, I come across [a
|
111
|
+
Forbes article] with a neat little table I want to type up. Let's try it now
|
112
|
+
([examples/example-1.yml]):
|
113
|
+
|
114
|
+
[a Forbes article]: http://www.forbes.com/sites/gregorymcneal/2014/06/27/nsa-releases-new-statistical-details-about-surveillance/
|
115
|
+
[examples/example-1.yml]: examples/example-1.yml
|
116
|
+
|
117
|
+
```yaml
|
118
|
+
class: [statistics-table, nsa-surveillance-details]
|
119
|
+
|
120
|
+
cols:
|
121
|
+
- name: authority
|
122
|
+
- name: num_orders
|
123
|
+
- name: num_targets
|
124
|
+
|
125
|
+
data:
|
126
|
+
- class: table-header
|
127
|
+
authority: Legal Authority
|
128
|
+
num_orders: Annual Number of Orders
|
129
|
+
num_targets: Estimated Number of Targets Affected
|
130
|
+
- authority: |
|
131
|
+
__FISA Orders__
|
132
|
+
Based on probable cause
|
133
|
+
(Title I and III of FISA, Sections 703 and 704 of FISA)
|
134
|
+
num_orders: "1,167 orders"
|
135
|
+
num_targets: "1,144"
|
136
|
+
- authority: |
|
137
|
+
__Section 702__
|
138
|
+
of FISA
|
139
|
+
num_orders: "1 order"
|
140
|
+
num_targets: "89,138"
|
141
|
+
- authority: |
|
142
|
+
__FISA Pen Register/Trap and Trace__
|
143
|
+
(Title IV of FISA)
|
144
|
+
num_orders: "131 orders"
|
145
|
+
num_targets: "319"
|
146
|
+
```
|
147
|
+
|
148
|
+
Here's what it looks like as HTML, using a common Markdown stylesheet ("GitHub"
|
149
|
+
on Mou/Macdown):
|
150
|
+
|
151
|
+
![screenshot](https://cloud.githubusercontent.com/assets/1570168/5449994/b09875c8-84b3-11e4-9a6a-b489a391d221.png)
|
152
|
+
|
153
|
+
Here's an example that illustrates some of the more advanced features of
|
154
|
+
Tablerize ([examples/example-2.yml]):
|
155
|
+
|
156
|
+
[examples/example-2.yml]: examples/example-2.yml
|
157
|
+
|
158
|
+
```yaml
|
159
|
+
class: [http-spec-exchange, another-class] # this line is optional
|
160
|
+
|
161
|
+
cols: # column specifications and ordering
|
162
|
+
- name: k # is used to identify the column below
|
163
|
+
class: http-key # is applied to each cell (td) in the column
|
164
|
+
- name: v
|
165
|
+
class: http-value
|
166
|
+
|
167
|
+
data: # data, by row
|
168
|
+
- v: GET # v corresponds to cols.1.name above
|
169
|
+
k: Method # k corresponds to cols.0.name above
|
170
|
+
- k: Parameters # the order of the columns in data doesn't matter
|
171
|
+
v:
|
172
|
+
# nest tables by nesting another YAML dictionary, in the same format
|
173
|
+
class: http-spec-params
|
174
|
+
|
175
|
+
cols:
|
176
|
+
- name: k
|
177
|
+
- name: v
|
178
|
+
|
179
|
+
data:
|
180
|
+
- k: '`client_id`' # backticks must be quoted!
|
181
|
+
v: |
|
182
|
+
A client ID for your service as set in your configuration.
|
183
|
+
|
184
|
+
a new line, wow! Let's see regular Markdown tables do that...
|
185
|
+
# <p>...</p> gets inserted only if there are multiple paragraphs
|
186
|
+
- k: '`type`'
|
187
|
+
v: '`code`'
|
188
|
+
- k: '`state`'
|
189
|
+
v: An anti-forgery token provided by the API.
|
190
|
+
- k: redirect_uri
|
191
|
+
v: '`https://example.com/api/{{your_service}}/authorize`'
|
192
|
+
```
|
193
|
+
|
194
|
+
With the right CSS, it becomes this:
|
195
|
+
|
196
|
+
![screenshot](https://cloud.githubusercontent.com/assets/1570168/3435774/15108594-0099-11e4-8175-d820206c471e.png)
|
197
|
+
|
198
|
+
|
199
|
+
## Tips & Caveats
|
200
|
+
|
201
|
+
- YAML tip: Backticks `` ` `` and some other characters need to be quoted
|
202
|
+
because they have special meaing in YAML or are otherwise not allowed to be
|
203
|
+
unquoted by YAML. Other suspicious characters include commas `,`, ampersands
|
204
|
+
`&`, and asterisks `*` and have been implicated in similar crimes. If
|
205
|
+
readability isn't an issue and there's been a syntax error spree in your
|
206
|
+
area, you can go ahead and quote every string just be safe.
|
207
|
+
|
208
|
+
- Don't use `class` as a column name, since it is used for classes. Unless you
|
209
|
+
really want to. In which case you can. But it still will be used for
|
210
|
+
classes.
|
211
|
+
|
212
|
+
- Auto column classes: As a convenience, if a table has a class `my-table` and
|
213
|
+
a column is named `column-1`, then all the cells in the column will have the
|
214
|
+
class `my-table-column-1`. This only happens for the first table class
|
215
|
+
listed. If you don't want this to happen, make the first table class `null`;
|
216
|
+
it will be ignored and columns will not have automatically-generated
|
217
|
+
classes.
|
218
|
+
|
219
|
+
|
220
|
+
## The Road Ahead
|
221
|
+
|
222
|
+
- Support using representing two-column tables as key and value. YAML doesn't
|
223
|
+
support ordered dictionaries, so this will be done by looking at the only
|
224
|
+
key- value pair inside each dictionary in a list:
|
225
|
+
|
226
|
+
```yaml
|
227
|
+
data:
|
228
|
+
- wake up: done
|
229
|
+
- brush teeth: done
|
230
|
+
- eat breakfast: not done
|
231
|
+
```
|
232
|
+
|
233
|
+
- Allow HTML attributes to be placed anywhere classes currently can.
|
234
|
+
|
235
|
+
- Allow arbitrary HTML _and_ tables as siblings together inside cells. This
|
236
|
+
will probably be implemented by recursively calling [kramdown-yaml-
|
237
|
+
tablerize], if installed.
|
238
|
+
|
239
|
+
- Allow empty cells to be created by simply not including keys or making keys
|
240
|
+
with no value (represented as `null`/`nil`/`None`). Possibly add a "strict
|
241
|
+
mode" setting that causes these to error instead.
|
242
|
+
|
243
|
+
- Support `thead` and `tfoot`. Perhaps add support `tbody`, which will be a
|
244
|
+
synonym for `data`.
|
245
|
+
|
246
|
+
- For simple tables, allow outputting to Markdown, for GitHub and other sites
|
247
|
+
that don't allow HTML in Markdown.
|
248
|
+
|
249
|
+
- [textmate/yaml.tmbundle], the YAML syntax highlighter used by TextMate,
|
250
|
+
Sublime Text, and GitHub isn't perfect. Fix the plugin and use the
|
251
|
+
examples in this README as test cases. _Update: GitHub now highlights the
|
252
|
+
YAML in this file almost correctly, but [textmate/yaml.tmbundle] doesn't
|
253
|
+
seem to be updated. GitHub's probably using something else to do syntax
|
254
|
+
highlighting._
|
255
|
+
|
256
|
+
[textmate/yaml.tmbundle]: https://github.com/textmate/yaml.tmbundle
|
257
|
+
|
258
|
+
## Credit
|
259
|
+
|
260
|
+
**Tablerize** was originally designed and written by [@szhu] at [@IFTTT].
|
261
|
+
|
262
|
+
[rfc1459/kramdown-gist]: https://github.com/rfc1459/kramdown-gist
|
263
|
+
[@szhu]: https://github.com/szhu
|
264
|
+
[@IFTTT]: https://github.com/IFTTT
|
data/bin/tablerize
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# Command-line interface to the tablerize gem.
|
3
|
+
|
4
|
+
if ARGV.empty?
|
5
|
+
$stderr.puts "usage: #{$PROGRAM_NAME} path/to/yaml-table.yml [...]"
|
6
|
+
exit 1
|
7
|
+
end
|
8
|
+
|
9
|
+
begin
|
10
|
+
require_relative '../lib/tablerize'
|
11
|
+
rescue LoadError
|
12
|
+
require 'tablerize'
|
13
|
+
end
|
14
|
+
|
15
|
+
ARGV.each do |path|
|
16
|
+
puts Tablerize.load_file(path).to_html
|
17
|
+
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
<table class="statistics-table nsa-surveillance-details"><tbody>
|
2
|
+
<tr class="table-header"><td class="statistics-table-authority">Legal Authority</td><td class="statistics-table-num_orders">Annual Number of Orders</td><td class="statistics-table-num_targets">Estimated Number of Targets Affected</td></tr>
|
3
|
+
<tr><td class="statistics-table-authority"><strong>FISA Orders</strong><br />
|
4
|
+
Based on probable cause (Title I and III of FISA, Sections 703 and 704 of FISA)</td><td class="statistics-table-num_orders">1,167 orders</td><td class="statistics-table-num_targets">1,144</td></tr>
|
5
|
+
<tr><td class="statistics-table-authority"><strong>Section 702</strong><br />
|
6
|
+
of FISA</td><td class="statistics-table-num_orders">1 order</td><td class="statistics-table-num_targets">89,138</td></tr>
|
7
|
+
<tr><td class="statistics-table-authority"><strong>FISA Pen Register/Trap and Trace</strong><br />
|
8
|
+
(Title IV of FISA)</td><td class="statistics-table-num_orders">131 orders</td><td class="statistics-table-num_targets">319</td></tr>
|
9
|
+
</tbody></table>
|
@@ -0,0 +1,27 @@
|
|
1
|
+
class: [statistics-table, nsa-surveillance-details]
|
2
|
+
|
3
|
+
cols:
|
4
|
+
- name: authority
|
5
|
+
- name: num_orders
|
6
|
+
- name: num_targets
|
7
|
+
|
8
|
+
data:
|
9
|
+
- class: table-header
|
10
|
+
authority: Legal Authority
|
11
|
+
num_orders: Annual Number of Orders
|
12
|
+
num_targets: Estimated Number of Targets Affected
|
13
|
+
- authority: |
|
14
|
+
__FISA Orders__
|
15
|
+
Based on probable cause (Title I and III of FISA, Sections 703 and 704 of FISA)
|
16
|
+
num_orders: "1,167 orders"
|
17
|
+
num_targets: "1,144"
|
18
|
+
- authority: |
|
19
|
+
__Section 702__
|
20
|
+
of FISA
|
21
|
+
num_orders: "1 order"
|
22
|
+
num_targets: "89,138"
|
23
|
+
- authority: |
|
24
|
+
__FISA Pen Register/Trap and Trace__
|
25
|
+
(Title IV of FISA)
|
26
|
+
num_orders: "131 orders"
|
27
|
+
num_targets: "319"
|
@@ -0,0 +1,13 @@
|
|
1
|
+
<table class="http-spec-exchange another-class"><tbody>
|
2
|
+
<tr><td class="http-key http-spec-exchange-k">Method</td><td class="http-value http-spec-exchange-v">GET</td></tr>
|
3
|
+
<tr><td class="http-key http-spec-exchange-k">Parameters</td><td class="http-value http-spec-exchange-v">
|
4
|
+
<table class="http-spec-params"><tbody>
|
5
|
+
<tr><td class="http-spec-params-k"><code>client_id</code></td><td class="http-spec-params-v"><p>A client ID for your service as set in your configuration.</p>
|
6
|
+
|
7
|
+
<p>a new line, wow! Let’s see regular Markdown tables do that…</p></td></tr>
|
8
|
+
<tr><td class="http-spec-params-k"><code>type</code></td><td class="http-spec-params-v"><code>code</code></td></tr>
|
9
|
+
<tr><td class="http-spec-params-k"><code>state</code></td><td class="http-spec-params-v">An anti-forgery token provided by the API.</td></tr>
|
10
|
+
<tr><td class="http-spec-params-k">redirect_uri</td><td class="http-spec-params-v"><code>https://example.com/api/{{your_service}}/authorize</code></td></tr>
|
11
|
+
</tbody></table>
|
12
|
+
</td></tr>
|
13
|
+
</tbody></table>
|
@@ -0,0 +1,33 @@
|
|
1
|
+
class: [http-spec-exchange, another-class] # this line is optional
|
2
|
+
|
3
|
+
cols: # column specifications and ordering
|
4
|
+
- name: k # is used to identify the column below
|
5
|
+
class: http-key # is applied to each cell (td) in the column
|
6
|
+
- name: v
|
7
|
+
class: http-value
|
8
|
+
|
9
|
+
data: # data, by row
|
10
|
+
- v: GET # v corresponds to cols.1.name above
|
11
|
+
k: Method # k corresponds to cols.0.name above
|
12
|
+
- k: Parameters # the order of the columns in data doesn't matter
|
13
|
+
v:
|
14
|
+
# nest tables by nesting another YAML dictionary, in the same format
|
15
|
+
class: http-spec-params
|
16
|
+
|
17
|
+
cols:
|
18
|
+
- name: k
|
19
|
+
- name: v
|
20
|
+
|
21
|
+
data:
|
22
|
+
- k: '`client_id`' # backticks must be quoted!
|
23
|
+
v: |
|
24
|
+
A client ID for your service as set in your configuration.
|
25
|
+
|
26
|
+
a new line, wow! Let's see regular Markdown tables do that...
|
27
|
+
# <p>...</p> gets inserted only if there are multiple paragraphs
|
28
|
+
- k: '`type`'
|
29
|
+
v: '`code`'
|
30
|
+
- k: '`state`'
|
31
|
+
v: An anti-forgery token provided by the API.
|
32
|
+
- k: redirect_uri
|
33
|
+
v: '`https://example.com/api/{{your_service}}/authorize`'
|
@@ -0,0 +1,67 @@
|
|
1
|
+
require 'erb'
|
2
|
+
|
3
|
+
module Tablerize
|
4
|
+
class HtmlElement
|
5
|
+
end
|
6
|
+
|
7
|
+
# A RawHtmlElement represents arbitrary HTML.
|
8
|
+
class RawHtmlElement < HtmlElement
|
9
|
+
attr_reader :html
|
10
|
+
alias_method :to_html, :html
|
11
|
+
|
12
|
+
def initialize(html)
|
13
|
+
@html = html
|
14
|
+
end
|
15
|
+
end
|
16
|
+
NEWLINE = RawHtmlElement.new("\n")
|
17
|
+
|
18
|
+
# A StructuredHtmlElement represents HTML tags enclosing an array of
|
19
|
+
# HtmlElements.
|
20
|
+
class StructuredHtmlElement < HtmlElement
|
21
|
+
attr_accessor :tag
|
22
|
+
attr_reader :attrs, :classes, :children
|
23
|
+
|
24
|
+
def initialize(tag, opts = {})
|
25
|
+
@tag = tag
|
26
|
+
@attrs = {}
|
27
|
+
@classes = []
|
28
|
+
@children = []
|
29
|
+
add_class opts[:class] unless opts[:class].nil?
|
30
|
+
@attrs.update opts[:attrs] unless opts[:attrs].nil?
|
31
|
+
end
|
32
|
+
|
33
|
+
def add_class(classes)
|
34
|
+
if classes.respond_to? :each
|
35
|
+
classes.each do |klass|
|
36
|
+
add_single_class klass
|
37
|
+
end
|
38
|
+
else
|
39
|
+
add_single_class classes
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def to_html
|
44
|
+
"<#{tag}#{attrs_html(@attrs)}>#{inner_html}</#{tag}>"
|
45
|
+
end
|
46
|
+
|
47
|
+
private
|
48
|
+
|
49
|
+
def add_single_class(klass)
|
50
|
+
return if klass.nil? || klass.empty?
|
51
|
+
return if @classes.include? klass
|
52
|
+
@classes << klass
|
53
|
+
end
|
54
|
+
|
55
|
+
def attrs_html(attrs)
|
56
|
+
out = %( class="#{ERB::Util.h @classes.join(' ')}") if @classes.length > 0
|
57
|
+
attrs.each do |attr, value|
|
58
|
+
out << %( #{attr}="#{h value}")
|
59
|
+
end
|
60
|
+
out
|
61
|
+
end
|
62
|
+
|
63
|
+
def inner_html
|
64
|
+
@children.map(&:to_html).join('')
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
data/lib/tablerize.rb
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
require_relative 'tablerize/version'
|
2
|
+
require_relative 'tablerize/html_element'
|
3
|
+
|
4
|
+
require 'yaml'
|
5
|
+
|
6
|
+
module Tablerize
|
7
|
+
# Tablerizes a YAML file, returning a Tablerize::HtmlElement.
|
8
|
+
def self.load_file(path)
|
9
|
+
data = YAML.load_file path
|
10
|
+
make_table(data)
|
11
|
+
end
|
12
|
+
|
13
|
+
# Tablerizes a YAML string, returning a Tablerize::HtmlElement.
|
14
|
+
def self.load(content)
|
15
|
+
data = YAML.load content
|
16
|
+
make_table(data)
|
17
|
+
end
|
18
|
+
|
19
|
+
# Tablerizes an object, returning a Tablerize::HtmlElement.
|
20
|
+
def self.make_table(data)
|
21
|
+
table = StructuredHtmlElement.new('table', class: data['class'])
|
22
|
+
tbody = StructuredHtmlElement.new('tbody')
|
23
|
+
tbody.children << NEWLINE
|
24
|
+
cols = data['cols']
|
25
|
+
data['data'].each do |row|
|
26
|
+
tr = StructuredHtmlElement.new('tr', class: row['class'])
|
27
|
+
cols.each do |col|
|
28
|
+
td = StructuredHtmlElement.new('td', class: col['class'])
|
29
|
+
col_key = col['name']
|
30
|
+
if (col_class_prefix = table.classes[0])
|
31
|
+
td.add_class "#{col_class_prefix}-#{col_key}"
|
32
|
+
end
|
33
|
+
cell = row[col_key]
|
34
|
+
if cell.is_a?(Hash)
|
35
|
+
td.children << NEWLINE
|
36
|
+
td.children << make_table(cell)
|
37
|
+
td.children << NEWLINE
|
38
|
+
else
|
39
|
+
td.children << RawHtmlElement.new(markdown_strip cell)
|
40
|
+
end
|
41
|
+
tr.children << td
|
42
|
+
end
|
43
|
+
tbody.children << tr
|
44
|
+
tbody.children << NEWLINE
|
45
|
+
end
|
46
|
+
table.children << tbody
|
47
|
+
table
|
48
|
+
end
|
49
|
+
|
50
|
+
private
|
51
|
+
|
52
|
+
# This method can easily be replaced to use any other Markdown library.
|
53
|
+
# As of now, you'll have to manually modify this method.
|
54
|
+
def self.markdown(text)
|
55
|
+
require 'kramdown'
|
56
|
+
Kramdown::Document.new(text).to_html.strip
|
57
|
+
end
|
58
|
+
|
59
|
+
# Process markdown removing <p>...</p> from one-liners.
|
60
|
+
def self.markdown_strip(text)
|
61
|
+
html = markdown text
|
62
|
+
p_start = '<p>'
|
63
|
+
p_end = '</p>'
|
64
|
+
if html.start_with?(p_start) && html.end_with?(p_end) &&
|
65
|
+
html.scan(p_start).count == 1 && html.scan(p_end).count == 1
|
66
|
+
html[3...-4]
|
67
|
+
else
|
68
|
+
html
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require_relative '../lib/tablerize'
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Tablerize do
|
4
|
+
['example-1', 'example-2'].each do |test_name|
|
5
|
+
context test_name do
|
6
|
+
reference_result = File.read "examples/#{test_name}.html"
|
7
|
+
|
8
|
+
it "should be correctly converted via load_file" do
|
9
|
+
test_result = Tablerize.load_file("examples/#{test_name}.yml").to_html
|
10
|
+
expect(test_result.chomp).to eq(reference_result.chomp)
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should be correctly converted via load" do
|
14
|
+
test_result = Tablerize.load(File.read "examples/#{test_name}.yml").to_html
|
15
|
+
expect(test_result.chomp).to eq(reference_result.chomp)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
data/tablerize.gemspec
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require './lib/tablerize/version'
|
2
|
+
|
3
|
+
Gem::Specification.new do |gem|
|
4
|
+
gem.name = 'tablerize'
|
5
|
+
gem.summary = 'Convert YAML to HTML tables.'
|
6
|
+
gem.description = <<-END
|
7
|
+
Tablerize converts YAML to HTML tables. Say goodbye to aligning tables
|
8
|
+
in Markdown.
|
9
|
+
END
|
10
|
+
|
11
|
+
gem.version = Tablerize::VERSION
|
12
|
+
|
13
|
+
gem.homepage = 'https://github.com/IFTTT/tablerize'
|
14
|
+
gem.authors = ['Sean Zhu']
|
15
|
+
gem.email = 'opensource+tablerize@szhu.me'
|
16
|
+
gem.license = 'MIT'
|
17
|
+
|
18
|
+
gem.add_dependency 'kramdown', '~> 1.2', '>= 1.2.0'
|
19
|
+
gem.executables = ['tablerize']
|
20
|
+
gem.files = `git ls-files`.split
|
21
|
+
gem.require_paths = ['lib']
|
22
|
+
end
|
metadata
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: tablerize
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Sean Zhu
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-01-20 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: kramdown
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.2'
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 1.2.0
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - "~>"
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.2'
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 1.2.0
|
33
|
+
description: |2
|
34
|
+
Tablerize converts YAML to HTML tables. Say goodbye to aligning tables
|
35
|
+
in Markdown.
|
36
|
+
email: opensource+tablerize@szhu.me
|
37
|
+
executables:
|
38
|
+
- tablerize
|
39
|
+
extensions: []
|
40
|
+
extra_rdoc_files: []
|
41
|
+
files:
|
42
|
+
- ".gitignore"
|
43
|
+
- ".rspec"
|
44
|
+
- ".travis.yml"
|
45
|
+
- LICENSE
|
46
|
+
- Makefile
|
47
|
+
- README.md
|
48
|
+
- bin/tablerize
|
49
|
+
- examples/example-1.html
|
50
|
+
- examples/example-1.yml
|
51
|
+
- examples/example-2.html
|
52
|
+
- examples/example-2.yml
|
53
|
+
- lib/tablerize.rb
|
54
|
+
- lib/tablerize/html_element.rb
|
55
|
+
- lib/tablerize/version.rb
|
56
|
+
- spec/spec_helper.rb
|
57
|
+
- spec/tablerize_spec.rb
|
58
|
+
- tablerize.gemspec
|
59
|
+
homepage: https://github.com/IFTTT/tablerize
|
60
|
+
licenses:
|
61
|
+
- MIT
|
62
|
+
metadata: {}
|
63
|
+
post_install_message:
|
64
|
+
rdoc_options: []
|
65
|
+
require_paths:
|
66
|
+
- lib
|
67
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '0'
|
72
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
requirements: []
|
78
|
+
rubyforge_project:
|
79
|
+
rubygems_version: 2.2.2
|
80
|
+
signing_key:
|
81
|
+
specification_version: 4
|
82
|
+
summary: Convert YAML to HTML tables.
|
83
|
+
test_files: []
|