forked from asciidoctor/asciidoctor-extensions-lab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
textql-block.rb
48 lines (45 loc) · 1.56 KB
/
textql-block.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
require 'asciidoctor/extensions' unless RUBY_ENGINE == 'opal'
include ::Asciidoctor
# A block that queries the CSV content in the block using textql
# (https://github.com/dinedal/textql) and generates an AsciiDoc table with the
# results.
#
# The textql command must be available on your PATH.
#
# Usage
#
# [textql%header, "select name, age from tbl where age >= 40"]
# ....
# name,age
# John,15
# Paul,50
# Susan,38
# Meghan,49
# ....
#
Extensions.register(:textql) {
block {
named :textql
on_context :literal
name_positional_attributes 'query', 'header'
parse_content_as :raw
process do |parent, reader, attrs|
# TODO assume header if second line is blank
input_header = (attrs.has_key? 'header-option') ? reader.lines.first : nil
output_header = (attrs.has_key? 'header') ? (attrs.delete 'header') : input_header
output_header = nil if output_header == 'None'
cmd = %(echo "#{reader.source}" | /home/dallen/opt/gocode/bin/textql#{input_header ? ' -header' : nil} -sql "#{attrs.delete 'query'}")
result_lines = %x(#{cmd}).chomp.split "\n"
table_delimiter = ',==='
result_lines.unshift *[output_header, ''] if output_header
result_lines.unshift table_delimiter
result_lines << table_delimiter
through_attrs = ['id', 'role', 'title'].inject({}) {|collector, key|
collector[key] = attrs[key] if attrs.has_key? key
collector
}
parse_content parent, result_lines, through_attrs
#create_open_block parent, result_lines, through_attrs
end
}
}