Skip to content

Instantly share code, notes, and snippets.

@mojavelinux
Last active August 29, 2015 13:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mojavelinux/8856117 to your computer and use it in GitHub Desktop.
Save mojavelinux/8856117 to your computer and use it in GitHub Desktop.
Asciidoctor textql extension example
[textql%header, "select name, title, length(name) from tbl where title like 'C%'", "Name,Title,Length of Name"]
....
id,name,title
1,Paul,CEO
2,Jeff,CTO
3,Dmitri,Vice President
....
Note
In a real scenario, the CSV data would be read from an include directive.
require 'asciidoctor'
require 'asciidoctor/extensions'
include ::Asciidoctor
Extensions.register do
block :textql do
on_contexts :literal
parse_content_as :raw
map_attributes ['query', 'header']
process do |parent, reader, attrs|
input_header = (attrs.has_key? 'header-option') ? reader.lines.first : nil
output_header = (attrs.has_key? 'header') ? attrs['header'] : input_header
cmd = %(echo "#{reader.source}" | /home/dallen/opt/gocode/bin/textql#{input_header ? ' -header' : nil} -sql "#{attrs['query']}")
result_lines = %x(#{cmd}).chomp.split "\n"
if output_header
result_lines.unshift ''
result_lines.unshift output_header
end
result_lines.unshift ",==="
result_lines << ",==="
container = create_open_block parent, nil, nil, :content_model => :compound
Lexer.parse_blocks((Reader.new result_lines), container)
container
end
end
end
Asciidoctor.render_file 'sample.adoc', :in_place => true
Name Title Name Length

Paul

CEO

4

Jeff

CTO

4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment