On 22 Jul 09:36, Richard Parsons wrote:
Hi,
Can anyone recommend to me a commandline templating system?
I want to be able to store information about clients and cases in simple text files and then merge that information into precedent documents. I'll then save and print those documents.
My perfect script would be something like this:
echo "name='Richard Parsons'\naddress='10 Green Hill etc'" > me.txt echo "name='Miles Davies'\naddress='14 Jazz Road etc'" > miles.txt echo "Hey $to.name of $to.address,\nGreat album!\nTake care,\n$from.name of $from.address" > precedent.txt magic-template-script -from me.txt --to miles.txt < precedent.txt > letter.txt
I'm thinking of using Cheetah (http://www.cheetahtemplate.org/), but am not particularly attached to it. I want something that I can run straight from bash if possible, but if scripting is necessary I most prefer python.
Any comments or thoughts?
Exactly what are you expecting as the output? As far as I can gather, you just want a plain text output... Am I right in thinking that you're making a letter? So the output for that would be something like:
----- Begin Output ----- Miles Davies 14 Jazz Road Blah blah blah
Date
Richard Parsons 10 Green Hill Blah blah blah
Hey Miles Davies of 14 Jazz Road Great Album! Take Care Richard Parsons of 10 Green Hill ----- End Output -----
Or similar?
So, *assuming* that your files are of the form that you stipulate (i.e. very much sourceable bash files, with variable='value' style) then something like:
----- Begin Script ----- #!/bin/bash
set -e set -u
trap usage EXIT
function usage() { cat <<EOU >&2 Usage: $0 <from_file> <to_file> > output_file.txt EOU }
function fill_template() { cat <<EOT $to_name $to_address
$(date +"%A %e %B %Y")
Hey $to_name of $to_address, Great Album. Take Care, $from_name of $from_address EOT }
function get_from() { unset name unset address source $from_file from_address="$address" from_name="$name" }
function get_to() { unset name unset address source $to_file to_address="$address" to_name="$name" }
from_file=$1 to_file=$2
to_name='' to_address='' from_name='' from_address=''
get_from get_to fill_template
trap EXIT ----- End Script -----
Not the neatest of scripts, but would do the job... with a bit more work could be made quite flexible too.
Cheers,