mirror of
https://github.com/johnkerl/miller.git
synced 2026-07-21 02:23:20 +00:00
113 lines
3.6 KiB
Ruby
Executable file
113 lines
3.6 KiB
Ruby
Executable file
#!/usr/bin/env ruby
|
|
|
|
$us = File.basename $0
|
|
|
|
require 'getoptlong'
|
|
require 'fileutils'
|
|
require 'json'
|
|
|
|
# ----------------------------------------------------------------
|
|
def main
|
|
input_handle = $stdin
|
|
output_handle = $stdout
|
|
|
|
input_handle.readlines.each do |content_line|
|
|
|
|
if content_line =~ /POKI_INCLUDE_ESCAPED\(([^)]+)\)HERE/
|
|
included_file_name = $1
|
|
include_escaped(included_file_name, output_handle)
|
|
|
|
elsif content_line =~ /POKI_INCLUDE_AND_RUN_ESCAPED\(([^)]+)\)HERE/
|
|
included_file_name = $1
|
|
cmd = File.readlines(included_file_name).join('')
|
|
run_command(cmd, output_handle)
|
|
|
|
# # Page-content directive: run a script which generates HTML and print its output
|
|
# elsif content_line =~ /POKI_RUN_UNESCAPED\(([^)]+)\)HERE/
|
|
# included_file_name = $1
|
|
# cmd = File.readlines(included_file_name).join('')
|
|
# run_command(cmd, output_handle, false)
|
|
#
|
|
# # Page-content directive: include other file (do HTML escapes) and print its output
|
|
# elsif content_line =~ /POKI_RUN_CONTENT_GENERATOR\(([^)]+)\)HERE/
|
|
# cmd = $1
|
|
# run_content_generator(cmd, output_handle)
|
|
#
|
|
# # Page-content directive: format as if included from a file.
|
|
# elsif content_line =~ /POKI_CARDIFY\(([^)]+)\)HERE/
|
|
# content_line = $1
|
|
# cardify(content_line, output_handle, true)
|
|
# elsif content_line =~ /POKI_CARDIFY{{([^)]+)}}HERE/
|
|
# content_line = $1
|
|
# cardify(content_line, output_handle, true)
|
|
#
|
|
# elsif content_line =~ /POKI_CARDIFY{{(.+)}}HERE/
|
|
# content_line = $1
|
|
# cardify(content_line, output_handle, true)
|
|
#
|
|
elsif content_line =~ /POKI_RUN_COMMAND{{(.+)}}HERE/
|
|
cmd = $1
|
|
run_command(cmd, output_handle)
|
|
|
|
# Page-content directive: include other file (do HTML escapes)
|
|
# elsif content_line =~ /POKI_RUN_COMMAND_TOLERATING_ERROR{{(.+)}}HERE/
|
|
# cmd = $1
|
|
# run_command_tolerating_error(cmd, output_handle)
|
|
|
|
else
|
|
output_handle.write(content_line)
|
|
end
|
|
end
|
|
end
|
|
|
|
# ----------------------------------------------------------------
|
|
def include_escaped(included_file_name, output_handle)
|
|
write_card(File.readlines(included_file_name), output_handle, true)
|
|
end
|
|
|
|
# ----------------------------------------------------------------
|
|
def run_command(cmd, output_handle)
|
|
cmd_output = `#{cmd} 2>&1`
|
|
status = $?.to_i
|
|
if status != 0
|
|
raise "\"#{cmd}\" exited with non-zero code #{status}."
|
|
end
|
|
write_card(['$ '+cmd] + cmd_output.split(/\n/), output_handle)
|
|
end
|
|
|
|
## ----------------------------------------------------------------
|
|
#def run_command_tolerating_error(cmd, output_handle)
|
|
# cmd_output = `#{cmd} 2>&1`
|
|
# write_card(['$ '+cmd] + cmd_output.split(/\n/), output_handle, true)
|
|
#end
|
|
#
|
|
## ----------------------------------------------------------------
|
|
#def run_content_generator(cmd, output_handle)
|
|
# cmd_output = `#{cmd} 2>&1`
|
|
# status = $?.to_i
|
|
# if status != 0
|
|
# raise "\"#{cmd}\" exited with non-zero code #{status}."
|
|
# end
|
|
# output_handle.puts(cmd_output)
|
|
#end
|
|
#
|
|
## ----------------------------------------------------------------
|
|
#def cardify(content_line, output_handle)
|
|
# write_card([content_line], output_handle)
|
|
#end
|
|
|
|
# ----------------------------------------------------------------
|
|
def write_card(content_lines, output_handle)
|
|
content_lines.each do |content_line|
|
|
output_handle.write(' ')
|
|
output_handle.puts(content_line)
|
|
end
|
|
end
|
|
|
|
## ----------------------------------------------------------------
|
|
#def html_escape_line(line)
|
|
# line.gsub("&", "&").gsub("<", "<").gsub(">", ">").rstrip
|
|
#end
|
|
|
|
# ================================================================
|
|
main()
|