jekyll-database-tables 0.1.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/jekyll-database-tables/formatters.rb +30 -22
- data/lib/jekyll-database-tables/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: c1cef8889f7925d214a208eb901e489637ce8f63be1346707a09999db1ab0e0b
|
|
4
|
+
data.tar.gz: 998be79150f29b3f21ec55941a09a85e0aa02e400ca356bb17520c0c3aba4395
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 0f423a612a85b39803b79f4377b70a24be7eabb4bb2efd483fda04c1b64580e772bd987e98633d49c5b605ff1ca834a6d115c2d55233f28b7e30485c9f214734
|
|
7
|
+
data.tar.gz: 6c544d7525320f94a06ee988e0810409a05335f1e63f329f4d8315e3ac92d9eb835d28b85de28bf19320b800f698a6fb5505f7305731cd6c6ba45d3e8b49b6cd
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require 'cgi'
|
|
4
|
+
|
|
3
5
|
module Jekyll
|
|
4
6
|
module DatabaseTables
|
|
5
7
|
# Abstract base class for table formatters.
|
|
@@ -80,43 +82,49 @@ module Jekyll
|
|
|
80
82
|
def render(table, title: nil)
|
|
81
83
|
widths = column_widths(table)
|
|
82
84
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
lines << separator(widths)
|
|
89
|
-
lines << "\n"
|
|
90
|
-
|
|
91
|
-
table.rows.each do |row|
|
|
92
|
-
lines << format_row(row.cells, widths)
|
|
93
|
-
lines << "\n"
|
|
94
|
-
end
|
|
95
|
-
|
|
96
|
-
"<pre class=\"psql-table\">\n#{lines}</pre>\n"
|
|
85
|
+
<<~HTML
|
|
86
|
+
<pre class="psql-table">
|
|
87
|
+
#{table_rows(table, widths, title).join("\n")}
|
|
88
|
+
</pre>
|
|
89
|
+
HTML
|
|
97
90
|
end
|
|
98
91
|
|
|
99
|
-
#
|
|
92
|
+
# formats a row as space-padded cells joined by +|+.
|
|
100
93
|
#
|
|
101
|
-
# @param cells [
|
|
102
|
-
# @param widths [
|
|
103
|
-
# @return [
|
|
94
|
+
# @param cells [array<#to_s>] the cell values to render
|
|
95
|
+
# @param widths [array<integer>] the column widths to left-justify against
|
|
96
|
+
# @return [string] the formatted row, with trailing whitespace stripped
|
|
104
97
|
def format_row(cells, widths)
|
|
105
98
|
cells.each_with_index.map do |cell, i|
|
|
106
|
-
cell.to_s.ljust(widths[i])
|
|
99
|
+
CGI.escapeHTML(cell.to_s.ljust(widths[i]))
|
|
107
100
|
end.join(' | ').rstrip
|
|
108
101
|
end
|
|
109
102
|
|
|
110
|
-
#
|
|
103
|
+
# renders a +-+-+ separator sized to the given column widths.
|
|
111
104
|
#
|
|
112
|
-
# @param widths [
|
|
113
|
-
# @return [
|
|
105
|
+
# @param widths [array<integer>] the column widths
|
|
106
|
+
# @return [string] the separator line
|
|
114
107
|
def separator(widths)
|
|
115
108
|
widths.map { |w| '-' * w }.join('-+-')
|
|
116
109
|
end
|
|
117
110
|
|
|
118
111
|
private
|
|
119
112
|
|
|
113
|
+
def table_rows(table, widths, title)
|
|
114
|
+
[
|
|
115
|
+
title_row(title, widths),
|
|
116
|
+
format_row(table.headers, widths),
|
|
117
|
+
separator(widths),
|
|
118
|
+
*table.rows.map { |row| format_row(row.cells, widths) }
|
|
119
|
+
].compact
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
def title_row(title, widths)
|
|
123
|
+
return unless title
|
|
124
|
+
|
|
125
|
+
CGI.escapeHTML(title).center(total_width(widths)).rstrip
|
|
126
|
+
end
|
|
127
|
+
|
|
120
128
|
def total_width(widths)
|
|
121
129
|
widths.sum + ((widths.length - 1) * 3)
|
|
122
130
|
end
|