Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#25 2011-04-24 06:46:42

MyOtheHedgeFox
Member
From: Russia
Registered: 2010-11-06
Posts: 10
Website

Re: smd_random_text: Pull random items from the TXP environment

Erm, Stef, sorry for the weird question I want to ask, but is it possible to use more-than-one-symbol delimiters (like \n|\n) when parsing textfiles? Could really help with parsing “database” textfiles prepared for fortune-mod or for some other programs that make use of random quotes.
And \n|\n is, after all, less common than [*|*], so it can be used as a failproof measure.

Last edited by MyOtheHedgeFox (2011-04-24 06:47:38)

Offline

#26 2011-04-26 14:09:20

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 10,538
Website GitHub

Re: smd_random_text: Pull random items from the TXP environment

MyOtheHedgeFox wrote:

is it possible to use more-than-one-symbol delimiters (like \n|\n) when parsing textfiles?

Never tried it, but in theory yes. It should split by whatever character(s) you give it. If you’ve tried it and it’s not working, let me know what you tried and how you set your tag / files up: I’ll try it out the same as you have it and, if it doesn’t work, look into a fix.

Last edited by Bloke (2011-04-26 14:09:37)


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#27 2011-04-26 14:16:03

MyOtheHedgeFox
Member
From: Russia
Registered: 2010-11-06
Posts: 10
Website

Re: smd_random_text: Pull random items from the TXP environment

Actually, I didn’t try it yet (woe upon me the newbie from Rashah): that’s why I actually chose to ask you first. Will try.

Offline

#28 2011-04-29 07:04:36

MyOtheHedgeFox
Member
From: Russia
Registered: 2010-11-06
Posts: 10
Website

Re: smd_random_text: Pull random items from the TXP environment

Alrighty, parsing the Discworld fortune database didn’t do what I expected.

The database homepage: http://www.splitbrain.org/projects/fortunes/discworld

My code:

<txp:smd_random_text type="file" source="files/discworld.txt" file_delim="\n%\n"><div class="clearing">{smd_rnd_txt}<br />Rows: {smd_rnd_txt_rows}<br />This is a row number {smd_rnd_txt_thisrow}</div></txp:smd_random_text>

(I’ve renamed discworld into discworld.txt)

results in:

It was all very well going about pure logic and how the universe was ruled by logic and the harmony of numbers, but the plain fact was that the disc was manifestly traversing space on the back of a giant turtle and the gods had a habit of going round to atheists' houses and smashing their windows.
Rows: 1
This is a row number 1

Offline

#29 2011-04-29 07:13:46

MyOtheHedgeFox
Member
From: Russia
Registered: 2010-11-06
Posts: 10
Website

Re: smd_random_text: Pull random items from the TXP environment

That’s even funnier. Attempts to parse othe fortune databases result in only the first lines of the textfiles.
In example, parsing this file results in:

Воспитание - это все. Персик был когда-то горьким миндалем, а цветная

which is the first line of the text.

Tried removing \n’s from the delimiter rules. Didn’t help.

Added later:

Now that’s funny. =)

Made a test textfile for parsing and reformed it like this:

A little pain never hurt anyone.|Christ was born in 4 B.C.|Cum tacent, clamant. When they are silent, they shout. -Cicero|Goes (Went) over like a lead balloon.

This way and with the standard delimiter ([**|**]), it randomizes well.

But when I try arranging the items like this:

A little pain never hurt anyone.

Christ was born in 4 B.C.

Cum tacent, clamant. When they are silent, they shout. -Cicero

Goes (Went) over like a lead balloon.

and set the delimiter to \n\n, it results in outputting only the very top string.

Also, though it’s an expected result, such file:

A little pain never hurt anyone.
Christ was born in 4 B.C.
Cum tacent, clamant. When they are silent, they shout. -Cicero
Goes (Went) over like a lead balloon.

with \n as a delimiter is parsed correctly.
Looks like the plugin doesn’t support multiple-symbols delimiters.

Last edited by MyOtheHedgeFox (2011-04-29 07:26:31)

Offline

#30 2011-05-03 00:05:13

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 10,538
Website GitHub

Re: smd_random_text: Pull random items from the TXP environment

MyOtheHedgeFox wrote:

Looks like the plugin doesn’t support multiple-symbols delimiters.

A correct assertion: a stupid bug prevented their use. But not any more. Enter v0.14 which permits regular expressions in file_delim. You do have to tell it of your intentions by specifying file_delim_type="regex" but that’s the only overhead. After that you will be able to do what you expect, namely:

<txp:smd_random_text
     type="file"
     source="files/discworld.txt"
     file_delim="\n%\n"
     file_delim_type="regex" />

If you don’t tell it that you are using a regex then it will try to split the file contents using the verbatim string that you supply as a file_delim. While this might seem like it would work, the idiosyncracies of PHP’s explode() function means that a character sequence that incorporates control chars (like \n, \t, etc) are treated as a literal sequence of the characters ‘backslash-n’ and so forth, instead of ‘newline’. Permitting regexes also opens up the possibility of creating some pretty interesting file structures!


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#31 2011-05-03 10:21:19

MyOtheHedgeFox
Member
From: Russia
Registered: 2010-11-06
Posts: 10
Website

Re: smd_random_text: Pull random items from the TXP environment

Thanks for the quick answer! =)
Could you also add a small example with this point to your plug-in description? IMO, I wouldn’t guess that I can use something like \n%\n as a regex’ed delimiter without caring much about the regular expressions syntax, without having an example in front of the eyes.

Last edited by MyOtheHedgeFox (2011-05-03 10:26:27)

Offline

#32 2011-05-03 11:16:24

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 10,538
Website GitHub

Re: smd_random_text: Pull random items from the TXP environment

MyOtheHedgeFox wrote:

Could you also add a small example with this point to your plug-in description?

Sure. I’ll make an example up and add it to the help at some point.


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#33 2011-05-03 15:12:28

MyOtheHedgeFox
Member
From: Russia
Registered: 2010-11-06
Posts: 10
Website

Re: smd_random_text: Pull random items from the TXP environment

Yay!

(a plenty of dancing happy kitties, holding geeky banners and screaming something like “STEF RULEZ LIKE OS/2!”)

Offline

#34 2011-05-03 15:15:35

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 10,538
Website GitHub

Re: smd_random_text: Pull random items from the TXP environment

MyOtheHedgeFox wrote:

STEF RULEZ LIKE OS/2!”

I’ll take that as a compliment :-)


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#35 2011-05-03 15:22:37

MyOtheHedgeFox
Member
From: Russia
Registered: 2010-11-06
Posts: 10
Website

Re: smd_random_text: Pull random items from the TXP environment

Well, that is. =) Some old Russian users still think that OS/2 was quite good. Those that met the Fidonet bloom, at least.

Er-r-r, I hope no punishment will follow this off-topic.

Last edited by MyOtheHedgeFox (2011-05-03 15:24:57)

Offline

#36 2012-05-19 05:05:05

maruchan
Member
From: Ukiah, California
Registered: 2010-06-12
Posts: 585
Website

Re: smd_random_text: Pull random items from the TXP environment

Hey Stef, love this plugin. I’m having some troubles getting a new random string on every page reload. Thought it might be asy_jpcache caching the site, but I disabled it and got the same result. I noticed that if I save the article with the tags in it again, I get a new result. Do you think it’s query caching? I emailed my host, waiting to see what they say.

Here’s the page: The 85 Dollar Computer Science Book Title Generator

Offline

Board footer

Powered by FluxBB