RiverMan Media

RiverMan Media 1

TextSmash PDF Print E-mail
Written by Jacob A Stevens   
Tuesday, 29 April 2008 19:51

TextSmash is a Java-based text insertion tool designed to help you combine the contents of text files. For example, say you have an html file called "template.html." Within template.html is a <div> tag where you want the contents of your page, the file "article.html" to be inserted. Within article you want to place boilerplate copyright information, "footer.html." TextSmash is a simple command line utility that uses insertion variables to make this very easy.

I’m not going to pretend that TextSmash is a fancy, robust, program that I could sell for hundreds of dollars. It does one thing, and it does it adequately. The Java code is less than 300 lines long, and there is no user interface and not much error checking. Normally I wouldn’t even release such a primitive tool, but I’ve found it so useful in my projects that I thought it would be a shame not to share it.

Download
You can download TextSmash using the following link. The .zip archive contains both the .java source code and the compiled class file.

textsmash.zip

Java: Required!
TextSmash requires the Java runtime environment. Anything above version 1.3 should work. You will also need to set your system environment variables so that java.exe is in the environment path. In Windows, you can set this using the Control Panel->System->Advanced-Environment variables dialog. Add the path of your Java runtime to the PATH variable.

Usage
TextSmash is simple to use after you adapt yourself (and your files) to its way of thinking.

The basic operation in TextSmash is to read an input file, and replace special parts of the file, called insertion strings, with other files. For example, say you had the following two files:

file_a.txt:
I am full of text. !* file_b.txt *! After that insertion, I am REALLY full of text.

file_b.txt
TextSmash ROCKS the HIZZOUSE!

If you run the following command…

java TextSmash output.txt file_a.txt

…the contents of output.txt would be:

I am full of text. TextSmash ROCKS the HIZZOUSE! After that insertion, I am REALLY full of text.

But wait! There’s more! What if you don’t want to hardcode the filenames within your source files. Try this:

file_a.txt:
I am full of text. !* $0 *! After that insertion, I am REALLY full of text.

file_b.txt
TextSmash ROCKS the HIZZOUSE!

Run this command (noticed we’ve specified file_b.txt as a variable now)…

java TextSmash output.txt file_a.txt file_b.txt

…the contents of output.txt will be:

output.txt
I am full of text. TextSmash ROCKS the HIZZOUSE! After that insertion, I am REALLY full of text.

However, if we had run this:

java TextSmash output.txt file_a.txt file_c.txt

…Now the output becomes

output.txt
I am full of text. File_c.txt was inserted here! After that insertion, I am REALLY full of text.

That’s it! That’s all there is to it. Replace part of one file with another file. However, you can do some other sneaky stuff, which I’ll explain next.

Advanced Stuff
Insertion strings can themselves contain filenames and variables. For example, let’s say we have the following files:

File_a.txt
Pre-insert. !* file_b.txt file_c.txt *! Post insert.

File_b.txt
Now I’m in file b, inserting a file here: !* $0 *!

File_c.txt
INSERT FILE C!

If we run…
Java TextSmash output.txt file_a.txt

…we get:

output.txt
Pre-insert. Now I’m in file b, inserting a file here: INSERT FILE C! Post insert.

That’s right! Insertions and variables are recursive! Just to illustrate, let’s watch a variable filename trickle down through a whole series of recursive calls.

File_a.txt
FILE_A !* file_b.txt $0 $1*! FILE_A

File_b.txt
FILE_B !* file_c.txt $0 $1*! FILE_B

File_c.txt
FILE_C !* $0 $1 *! FILE_C

File_d.txt
FILE_D !* $0 *! FILE_D

File_e.txt
FILE_E

If we call…

Java TextSmash output.txt file_a.txt file_d.txt file_e.txt

…we wil get

FILE_A FILE_B FILE_C FILE_D FILE_E FILE_D FILE_C FILE_B FILE_A

It’s magical! But be careful, make sure you don’t include files in a circular manner… the Java stack will recurse infinitely, the program will crash, and you’ll lose… well, absolutely nothing. But you will have to figure out what’s wrong.

Also notice how in the above example, the indices changed as we passed fewer arguments into the insertion string in file_c.txt

I hope you find this tool useful! Thanks!

Bugs
I’ve never had a bug in anything I’ve programmed before. However, in the event that you notice an undocumented feature, send an email to This e-mail address is being protected from spambots, you need JavaScript enabled to view it and I’ll remove that feature right away.

License
You’re free to use this tool in any way you want. It would be awesome if you gave RiverMan Media credit if you re-post it though. And if you try to sell it… well, good luck with that.

Version History

2008.04.29 Version 1.0 Initial Release.