Multiple external filters

M

Martin DeMello

I'm trying to pipe my output through multiple external filters according
to the following logic:

1. If no output file is specified, write to $stdout
2. If the outfile extension is .lout, write lout output
3. If the config file has a path to lout, write ps output (default)
4. If the outfile extension is .pdf, write pdf output

I'm trying to retain the option to dump to stdout to facilitate piping
(in fact, to be totally consistent I should probably have a ps2pdf
config option too and use it if found), leading to the following,
somewhat ugly code:

if CONFIG['lout'].empty? || outfilename =~ /\.lout$/
outfile.puts(out)
else
tf = Tempfile.new("scrabpp")
tf.puts(out)
tf.close
psfile = `#{CONFIG['lout']} #{tf.path}`
if outfilename =~ /\.pdf$/
tf = Tempfile.new("scrabpp")
tf.puts(psfile)
tf.close
outfile.close
system("ps2pdf #{tf.path} #{outfilename}")
else
outfile.puts(psfile)
end
end

Any more elegant way of doing it, especially without the nested ifs?

martin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,156
Messages
2,570,878
Members
47,405
Latest member
DavidCex

Latest Threads

Top