Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#85 2012-02-24 21:02:51

alanfluff
Member
From: Ottawa, Canada
Registered: 2008-09-15
Posts: 222
Website

Re: smd_wrap: conditionally wrap stuff with tags and labels

You’re most welcome Uli, thanks for the links too and I’m glad this idea worked (courtesy Stef+plugin and jakob) and also that it might be helpful to someone else :) Cheers, -Alan


At LAST I’ve cheerfully donated to the core devs at #TXP. I only wish I were able to give more. Thanks to the devs and ALL fellow TXPers. -A

Offline

#86 2012-02-26 21:14:52

alanfluff
Member
From: Ottawa, Canada
Registered: 2008-09-15
Posts: 222
Website

Re: smd_wrap: conditionally wrap stuff with tags and labels

@Jukka When you said:

If you do heavy regex stuff, like Textile parsing, consider/remember to cache the results…

were you thinking of a specific TXP method for caching results?


At LAST I’ve cheerfully donated to the core devs at #TXP. I only wish I were able to give more. Thanks to the devs and ALL fellow TXPers. -A

Offline

#87 2012-02-27 13:11:00

alanfluff
Member
From: Ottawa, Canada
Registered: 2008-09-15
Posts: 222
Website

Re: smd_wrap: conditionally wrap stuff with tags and labels

[Moderator: If this post is too conversational or otherwise wrong for this thread please move/delete it and let me know my transgression, tks :)]

I’ve improved the section subtitle code but I am sure the regex and steps could be simplified.

Before I was adding Textile in order to differentiate what was the subtitle. This meant an author had to write [Section title text] [delimeter] [startTextile] [subtitle text] [endTextile], for example Home / _is where the heart is_.

Having used textile to identify the subtitle as the part that was <em></em> wrapped, I then needed to strip the <p></p> tags that Textile was dutifully wrapping the entire title in—a waste of time (but a valuable step for my small brain en-route to this improvement).

The revised code checks if the title has a subtitle at all and also only requires the author to write in the format [Section title text] [delimeter] [subtitle text].

Simplification suggestions welcomed. Cheers, -Alan
PS: Thanks to Stef, again, for the mighty smd_if which I am now also using.

This Section title text:

Home @ is where the heart is

produces this output:

<li>
	<a href="http://example.com/">
		Home <span class="visuallyhidden">&mdash;</span><span class="subtitle"> is where the heart is</span>
	</a>
</li>

and this is the code:

<li>
	<txp:section title="0" link="1">
		<txp:smd_if field='<txp:section title="1" link="0" />' operator="contains" value="@">
			<txp:smd_wrap transform="replace|regex|'\/?@'|">
				<txp:smd_wrap transform="replace|regex|'[^@]*$'|">
					<txp:section title="1" link="0" />
				</txp:smd_wrap>
			</txp:smd_wrap>
			<span class="visuallyhidden">&mdash;</span>
			<span class="subtitle">
				<txp:smd_wrap transform="replace|regex|'^((?!([@])).)*@(?)'|">
					<txp:section title="1" link="0" />
				</txp:smd_wrap>
			</span>
		<txp:else />
			<txp:section title="1" link="0" />
		</txp:smd_if>
	</txp:section>
</li>

Last edited by alanfluff (2012-02-27 13:13:24)


At LAST I’ve cheerfully donated to the core devs at #TXP. I only wish I were able to give more. Thanks to the devs and ALL fellow TXPers. -A

Offline

#88 2012-02-27 14:49:04

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,734
Website

Re: smd_wrap: conditionally wrap stuff with tags and labels

Wow, that makes my head hurt. Let me get that straight: you want to split the section title at the @ sign. Could you not do a simple replace operation without all that regex in there.

<li>
  <a href="<txp:site_url /><txp:section />">
    <txp:smd_if field='<txp:section title="1" />' operator="contains" value="@">
      <txp:smd_wrap transform="replace|string| @ | <span class='visuallyhidden'>–</span> <span class='subtitle'>,add|after|</span>"><txp:section title="1" /></txp:smd_wrap>
    <txp:else />
      <txp:section title="1" />
    </txp:smd_if>
  </a>
</li>

Yet another approach would be to use the “split” transform to get the first part and then the split transform again for the second (last) part and then to add the relevant code before and after each bit.

When Jukka said…

If you do heavy regex stuff, like Textile parsing, consider/remember to cache the results…

… he was referring to the fact that textiling is a relatively processor-intensive search replace operation that is costly to do on every page. For this reason txp saves a pre-textiled version of the body, title and excerpt rather than textiling every page on the fly. If you have a lot of on-the-fly textiled stuff on a page, you can use something like aks_cache to save rendered sections of your page that don’t change often (i.e. only when someone changes the section’s heading).


TXP Builders – finely-crafted code, design and txp

Offline

#89 2012-02-27 15:34:30

alanfluff
Member
From: Ottawa, Canada
Registered: 2008-09-15
Posts: 222
Website

Re: smd_wrap: conditionally wrap stuff with tags and labels

THANKS Jakob, that example you provided is much more simple and works perfectly.

I have learnt more about how powerfully smd_wrap can be used from your example.

Thanks for the comment about textile processing, I understood the fact that processing textile would add processor load but didn’t know or had forgotten that TXP:

saves a pre-textiled version of the body, title and excerpt rather than textiling every page on the fly

and I have no experience with cache plugins so thank you for pointing out aks_cache.


At LAST I’ve cheerfully donated to the core devs at #TXP. I only wish I were able to give more. Thanks to the devs and ALL fellow TXPers. -A

Offline

#90 2012-03-07 14:59:34

Mats
Member
From: Sweden
Registered: 2011-01-16
Posts: 30
Website

Re: smd_wrap: conditionally wrap stuff with tags and labels

Hello!

Thanks for another great plugin Stef!

I’m trying to create events in Google calendar from articles using urls. The problem is when trying to convert the letters Å,Ä,Ö. These letters are converted like this by google (the letter Å): %C3%85 . When using convert the plugin prints out %C3%85 as a Å. I haven’t had any success trying both with replace and sanitize. Is this doable with smd_wrap?

/Mats

Offline

#91 2012-03-20 23:12:56

THE BLUE DRAGON
Member
From: Israel
Registered: 2007-11-16
Posts: 637
Website

Re: smd_wrap: conditionally wrap stuff with tags and labels

In the context to my post here

I will like to convert this:

<p>
	property: value
	property: value
	property: value
</p>

Into this:

<p>
	property: <span>value</span>
	<br />property: <span>value</span>
	<br/>property: <span>value</span>
	<br/>a line without a colon
	<br/>a line without a colon
	<br/>property: <span>value</span>
</p>

And this is what I came up with which is working but not the best,
1. I’m pretty sure it can be done in a better way.
2. When a line doesn’t contains a colon, it is still adding a closing </span> tag,
which is not good cause there isn’t any open tag. (Should I use smd_if for this?)

<txp:smd_wrap transform="replace|string|<br />|</span><br />">
	<txp:smd_wrap transform="split|:|: <span>|all, add|after|</span>">
		<txp:php>echo nl2br(trim(custom_field(array('name' => 'extra_properties','escape'=>0))));</txp:php>
	</txp:smd_wrap>
</txp:smd_wrap>

Will appreciate any help please.

Offline

#92 2012-04-08 01:40:16

maniqui
Member
From: Buenos Aires, Argentina
Registered: 2004-10-10
Posts: 3,070
Website

Re: smd_wrap: conditionally wrap stuff with tags and labels

A typical, simple case: I was looking to join some variables with a comma, but only if variables returned something.

To my surprise, I couldn’t find a way to emulate adi_join using smd_wrap.
Unless I’m missing something not so obvious, in the smd_ Universe, this task (join stuff) seems a task more suitable for smd_each (optionally combined with smd_wrap if you want to wrap the whole output with something; for example, some parens ()).


La música ideas portará y siempre continuará

TXP Builders – finely-crafted code, design and txp

Offline

#93 2012-10-24 20:47:41

uli
Moderator
From: Cologne
Registered: 2006-08-15
Posts: 4,310

Re: smd_wrap: conditionally wrap stuff with tags and labels

I’m trying to erase some typical “elbow on space bar” misuses, here: several tabs & spaces combos, and am getting an Unknown modifier '+' on line 203 error for this regex:
<txp:smd_wrap transform="replace|regex|[ \t]+|\t">

The only regex with a modifier in this topic is maniqui’s Ultimate Textilizer wrapper, which uses the plus like I do as far as I can see, but where is my fault? (v0.10 of smd_wrap)

Edit: Same error for {2,} which I should use actually ;)

Last edited by uli (2012-10-24 20:58:32)


In bad weather I never leave home without wet_plugout, smd_where_used and adi_form_links

Offline

#94 2012-10-24 21:58:40

maniqui
Member
From: Buenos Aires, Argentina
Registered: 2004-10-10
Posts: 3,070
Website

Re: smd_wrap: conditionally wrap stuff with tags and labels

hi Uli.
The regex should be between two delimiters, I think. Like:

<txp:smd_wrap transform="replace|regex|#[ \t]{2}#|\t">

BTW, the Ultimate Textilizer wrapper snippet has some minor issues (which I can’t recall now) that I’ve to address… someday. But, overall, it works well in most situations, afaicr.


La música ideas portará y siempre continuará

TXP Builders – finely-crafted code, design and txp

Offline

#95 2012-10-24 22:39:38

uli
Moderator
From: Cologne
Registered: 2006-08-15
Posts: 4,310

Re: smd_wrap: conditionally wrap stuff with tags and labels

Thanks, Julián, the errors are gone now, and parsed results finally show up :)

I’m an absolute beginner at regexes, I thought you had the # for Textile’s ordered lists in there. Are they common in regex land? (They aren’t in the help.)

Must admit that my small “knowledge” comes from the BBEdit manual, ATM, a Mac text editor for coding, which apparently has another syntax here and there. They use {2,} (no typo) for {2+} Edit: (Or is {2+} simply nothing? All the tabs still contained in the source code =p) So I have to experiment quite a lot and try and error. What better resources could I use as reference for working with smd_wrap, just Google for PHP regexes?

Last edited by uli (2012-10-24 22:49:00)


In bad weather I never leave home without wet_plugout, smd_where_used and adi_form_links

Offline

#96 2012-10-24 22:53:10

maniqui
Member
From: Buenos Aires, Argentina
Registered: 2004-10-10
Posts: 3,070
Website

Re: smd_wrap: conditionally wrap stuff with tags and labels

uli wrote:

Thanks, Julián, the errors are gone now, and parsed results finally show up :)

Cool!

I’m an absolute beginner at regexes, I thought you had the # for Textile’s ordered lists in there. Are they common in regex land? (They aren’t in the help.)

Regex delimiters are common on regex land. I arbitrarely used #, but you can use other symbols, as long as you use the same at the very beginning and at the very end. Of course, you will have to escape the delimiter (like this, for example: \#) if, for some reason, you have to use it inside your regex.

What better resources could I use as reference for working with smd_wrap, just Google for PHP regexes?

I’d look into the PHP manual and I also usually look at www.regular-expressions.info/.
Finally, this recent thread at /r/learnprogramming on Reddit may provide you some useful resources too.


La música ideas portará y siempre continuará

TXP Builders – finely-crafted code, design and txp

Offline

Board footer

Powered by FluxBB