Textpattern tips, tutorials and code snippets

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!

11 Comments Comment feed

Bosh!

I have only one question – how will I know whether I’ve achieved enlightenment or impaired my Sylvian fissure?

Is there a tag trace for that?

A quick correction. Where it says:

If your filename is “An-essay-onTXP(Textpattern)-4.2.0™_(eBook).pdf”,

it should say:

If your filename is “An-essy-on-TXP(Textpattern)-4.2.0(tm)_(eBook).pdf”,

Notice the ( tm ) (without spaces).

Pieman wrote:

I have only one question – how will I know whether I’ve achieved enlightenment or impaired my Sylvian fissure?

If you feel some plasma-like liquid going down your nose and it taste like brain juice, check with the doctor.
If it tastes like green tea, it was just an enlightment.

I thought I fixed that?! Is it ok now?

Hi J,

no, it seems it still has a minor glitch:

It should read:

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).

(that last “TXP”, on the line above, should be a real acronym/abbr, to reflect the real output of this tip)

Thanks!

Not sure I follow you – check it now. If its still wrong, post the exact Textiled code and I’ll correct the article.

Hehehe, the exact Textile code… you are right, sorry… I think we will work it out this time:

If your filename is:

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

you can transform it to:

An essay on TXP 4.2.0™ (eBook).

:) (just remove the space before “notextile”)

OK it should be ok now, I just used a code block instead of notextile…

A smaller top-tip to tip on top of yours:

If you want to clean up your new filename string as described, but without wrapping it in <p></p> tags, etz_striptags is a very handy companion to upm_textile

ps. Jonathan, any chance of enabling comment feeds?

Hey Pieman, care to give a short example of your method with etz_striptags?

Comments feeds – its there – just look to the right under “Feeds and Twitter”. I’ll place it better in the next design revision, coming when I get time..

Well, I could be wrong, but I think in Maniqui’s example,

<txp:upm_textile><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></txp:upm_textile>

Will output

<p>An essay on TXP 4.2.0™ (eBook).</p>

If you wrapped the whole thing in etz_striptags:

<txp:etz_striptags> <txp:upm_textile><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></txp:upm_textile></txp:etz_striptags>

You’d get a cleaner output (without paragraph wrapper) to markup as you see fit.

An essay on TXP 4.2.0™ (eBook).

Of course, you may be happy with the <p>s, in which case, ignore me. I also vaguely remember an attribute for upm_textile which might do the same thing, but I can’t find any help guide for that plugin any more.

Re: comment feeds
Doh – couldn’t see for looking. It doesn’t work for me though https://textpattern.tips/?rss=1&area=comments&id=111 returns ““cannot decode raw data” (NSURLErrorDomain:-1015)” in Safari for me.

Hey Pieman,

Looks like ajw_comments_feed stopped working – its the same in Firefox. Maybe its because of 4.2. I’ll leave it in for now in case its a browser issue.

Anyone know of an alternative?

Add a comment

Use Textile help to style your comments