Change filenames into nice file titles

As per TXP 4.2.0, files uploaded to TXP (on Contents -> Files tab) do not have a title field. Files just have a name, and that name is, literally, the filename of the uploaded file.

Geniuses can hack into Textpattern to add a field to the database, add the corresponding input field to the admin screen and voilà! They have titles for files. But some of us aren’t geniuses and having a hacked Textpattern could lead to nightmares.

The idea is to generate a nice file “title” from the filename, by removing dashes (or underscores) and file extension. Of course, this works better if you rely on having some naming convention for your files, but nothing that cannot be extended.

So, first, grab rah_replace (if you are adventurous enough, you may grab pax_grep and take this tip to the next level).

  1. rah_replace plugin

We will use rah_replace to remove characters and file extensions from the filename.

How?

In the file_download or file_download_list form (or contained tags, if used as containers) you are using for “rendering” the different chunks of file information, put something like this:

<txp:rah_replace from="-,_,.pdf,.doc,.zip,.mp3,.rtf" to=" "><txp:file_download_name /></txp:rah_replace> 

See what this does? It replaces dashes, underscores and a bunch of file extensions for whitespace.

So, if your file name is “An-essay-on-TXP-4.2.0_(eBook).pdf”, you will get this nice title: An essay on TXP 4.2.0 (eBook).

Cool, isn’t it? And now, did I mention there is going to be an extra bonus tip? I lied! I’m going to give you two extra tips at the price of one.

Get file extension

For regex ninjas, this may be an easy one using raw PHP or pax_grep. But then, what could we do, poor souls, that will cry at the mere mention of “use a regex” as a solution to a simple problem?

So, TXP lacks of titles for filenames, and it doesn’t has a nice <txp:file_download_ext /> tag.

rah_replace to the rescue!

The code you are about to see may generate brain damage or a satori…

<txp:rah_replace from='<txp:rah_replace from=".pdf,.doc,.rtf,.zip" to=""><txp:file_download_name /></txp:rah_replace>.' to=""><txp:file_download_name /></txp:rah_replace>

We are using rah_replace inside the “from” attribute of another rah_replace. Insane tags in tags power!

The inner rah_replace “cleans” the file_download_name, by just removing its extension (for a predefined list of extensions). Then, we use that “cleaned” name as the value for “from” attribute, on another rah_replace tag, also wrapping a file_download_name tag. Notice the little extra dot (.) at the end of the “from” attribute. It’s there to also remove the dot, so to end up with just the file extension (pdf, doc, zip, etc).

So, we are telling it to replace the filename with just nothing. And, we end up with the file extension. Then, you can use it to add the proper icon next to the file (by using CSS or by just adding an <img /> next to it).

You tell us what crazy things you could do now that you have the file extension!

Using upm_textile to improve our new file “titles”

upm_textile is one of those plugins that you just love. It brings Textile power to every piece of text on your site… and our new file titles aren’t an exception.

If your filename is:

An-essay-on-TXP(Textpattern)-4.2.0(tm)_(eBook).pdf

You can transform it to:

An essay on TXP 4.2.0™ (eBook).

Try it:

<txp:upm_textile><txp:rah_replace from="-,_,.pdf" to=" ">"An-essay-on-TXP(Textpattern)-4.2.0_(tm)_(eBook).pdf"</txp:rah_replace></txp:upm_textile>

That’s all! Enjoy!

comments powered by Disqus