Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

  1. Index
  2. » Archives
  3. » [archived] kgr_safe_excerpt

#49 2005-11-10 01:33:59

KurtRaschke
Plugin Author
Registered: 2004-05-16
Posts: 275

Re: [archived] kgr_safe_excerpt

bq..> jstar wrote:

> Hey Kurt,
Great job on the plugin. I have a quick question for you. Is there any way to get around the kgr_safe_excerpt hiding article images if your article reaches your defined word limit? My setup looks like this: <code><txp:kgr_safe_excerpt words=“100” linktext=“read more &raquo;” /></code>. If I have an image in that article and it’s word count is past 100 it does it’s job, but it also hides the image. Any way around that?
Also how would I add in an anchor class to the href the plugin creates?
Thanks for your help!
-j

ps: I’m running Textpattern · 4.0.2

Well, I don’t have a particularly good solution for either of those issues. The only way I know of to avoid potentially breaking a tag is to strip all tags out if we have to chop off part of the entry, so of course images will get stripped. Feel free to develop a better algorithm and submit a patch.

As to the second issue, again, there’s nothing straightforward, but you can probably hack the code for that purpose.

-Kurt


kurt@kurtraschke.com

Offline

#50 2005-11-10 02:05:29

jstar
Member
Registered: 2005-05-12
Posts: 12
Website

Re: [archived] kgr_safe_excerpt

Thanks for the response Kurt. I’m more of a hack programmer so I really don’t know what I’m doing. My success for the day was getting ifeelcool.com’s version of our plugin working. :) I’ll take a look and probably get really confused and hope someone else can help me out!

ifeelcool.com,
I have striped out all the “junk” from my copy/paste of your modification. Let’s see if it works in the < code > wraptag:

<code>function kgr_safe_excerpt($atts) { if (is_array($atts)) extract($atts);

global $thisarticle; $words = (!empty($words)) ? $words : 20; if (!$styleclass) $styleclass = ‘readmore’; //change $countchars = (isset($countchars)) ? true : false; $countexcerpt = (isset($countexcerpt)) ? true : false; $linktext = (!empty($linktext)) ? $linktext : “lees meer”; $link = “\n\n”. permlink(“”, $linktext).”\n”; $bodycount = ($countchars) ? strlen($thisarticle[“body”]) : count(explode(” “, $thisarticle[“body”])); if (trim($thisarticle[“excerpt”]) != “”) { $excerpt = strip_chars($thisarticle[“excerpt”],Array(“p”)); //strip paragraph-tag- change if ($countexcerpt) { $excerpt = ($countchars) ? substr($excerpt,0,$words) : implode(” “, array_slice(explode(” “, $excerpt), 0, $words)); } $excerpt = $excerpt.”“.$link; } else if ($bodycount < $words) { $excerpt= strip_chars($thisarticle[“body”],Array(“p”)); //change } else { $body = strip_chars($thisarticle[“body”],Array(“p”)); //change $excerpt = ($countchars) ? substr($body,0,$words): implode(” “, array_slice(explode(” “, $body), 0, $words)); $excerpt = $excerpt.”“.$link; } return $excerpt; }</code> <code> //added function to strip tag(s) function strip_chars($string,$chars) { $tags_to_strip = $chars; foreach ($tags_to_strip as $tag) { $string = preg_replace(“/<\/?” . $tag . “(.|\s)*?>/”,”“,$string); } return $string; }</code>

Just so everyone is clear I am in NO WAY trying to take any sort of credit for this code. It was all the work of KurtRaschke & ifeelcool.com.


“On the run from Johnny Law… ain’t no trip to Cleveland.”

Offline

#51 2006-01-08 23:01:55

KurtRaschke
Plugin Author
Registered: 2004-05-16
Posts: 275

Re: [archived] kgr_safe_excerpt

See the first post in this thread (which has been updated) for notes on the latest version, which address the <p>-tag and CSS issues discussed above.

-Kurt


kurt@kurtraschke.com

Offline

#52 2006-02-03 23:04:30

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,250
Website GitHub

Re: [archived] kgr_safe_excerpt

Very cool plugin Kurt. Does exactly what it says on the tin and does it very well.

For my own purposes, I kinda wanted the elegance of yours (the way it chops off at particular words to keep the lists manageable) plus some of the features of beginning, such as the ability to place the ‘read more’ link in the body of the excerpt.

Since I spent a few minutes adding a few options to your plugin, I thought I should publish them so others can use them, or you can make the code cleaner and roll them into your plugin if you like, or just ignore them and swear under your breath at people like me messing up your finely-crafted code.

Anyway, I added 3 options, all of which take the same defaults as your original plugin to remain backwards compatible:

wraptag : specifies the html block tag to surround the excerpt (without the angle brackets). Defaults to ‘p’.
class : assigns a CSS class to the wraptag. Defaults to an empty string (i.e. prints nothing).
linkinside : if specified, will place the ‘read more’ link inside the wraptag. If omitted, the link is placed outside as before.

This means you can use it like:
<code>txp:kgr_safe_excerpt words=“20” linktext=“there is more” linkinside=“1” wraptag=“div” class=“my_class”</code>

to get something like:
<code><div class=“my_class”>Textpattern is the coolest site building tool since, like, well, ever. It is enhanced by the thriving and helpful community……<span class=“readmore”><a href=“somelink” title=“Permanent link to this article”>there is more</a></span></div></code>

I gave it a reasonable testing with pretty much all the options I could think of and it held up well, but the usual disclaimer applies if it falls over/becomes vegetarian/humps your leg/yahde yahde. To get the extra options, edit the v1.0 plugin code in the following 2 places:

1) In the declaration block at the top, add these 3 lines underneath the <code>$styleclass = …</code> line:
<code>
$wraptag = (isset($wraptag)) ? $wraptag : “p”;
$class = (isset($class)) ? $class : “”;
$linkinside = (isset($linkinside)) ? true : false;
</code>

2) Replace the <code>$excerpt = …</code> line inside the <code>if ($didtruncate) { … }</code> block with these 2 horrible-looking lines of code:
<code>
$tagstart = “<”.$wraptag.(($class==“)?”” class=\”$class\”“).”>”;
$excerpt = $tagstart.$excerpt.”…”.(($linkinside)?$link:“).”</”.$wraptag.”>”.(($linkinside)?””<br />$link”);
</code>

Job done.

Right then, I’ll climb back into my box. Hope it’s of use to someone.


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

#53 2006-02-04 21:40:54

KurtRaschke
Plugin Author
Registered: 2004-05-16
Posts: 275

Re: [archived] kgr_safe_excerpt

Thanks…that looks like a good set of improvements. When I get a chance I’ll roll them into the code and get a new version out there.

I’m wondering, though, do we really need separate CSS classes for the wraptag and the readmore span?

I’m thinking that one could probably get away with a class on the wraptag and a sufficient amount of trickery with CSS selectors.

-Kurt


kurt@kurtraschke.com

Offline

#54 2006-02-05 00:50:19

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,250
Website GitHub

Re: [archived] kgr_safe_excerpt

>Kurt wrote
>I’m wondering, though, do we really need separate CSS classes for the wraptag
>and the readmore span? I’m thinking that one could probably get away with a
>class on the wraptag and a sufficient amount of trickery with CSS selectors.

True, true. I tend to be a bit tag-tastic when it comes to divs and spans and often go through afterwards removing extraneous classes/tags that make better/leaner code declared with CSS selectors.

You could certainly rationalise the classes here, whichever way makes sense. And you’re right, the ‘read more’ could be styled easily using CSS selectors when it’s inside the wraptag. Just thinking out loud though: if someone chose to employ the ‘read more’ link outside the wraptag, as in your original v1.0, would they be able to style it without its own dedicated class? It’d just be <code><span>read more</span></code> and one might not want to style all span tags the same way on the page?

I’m the first to admit I’m not that hot on CSS and selectors so maybe I’m missing a trick here… please correct me if I’m wrong so I can learn a better way of doing things!


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

#55 2006-02-05 01:40:39

KurtRaschke
Plugin Author
Registered: 2004-05-16
Posts: 275

Re: [archived] kgr_safe_excerpt

> Bloke wrote:
> It’d just be <code><span>read more</span></code> and one might not want to style all span tags the same way on the page?

Well, (supposing that we apply a class of excerpt to the excerpt <p>) you could do p.excerpt + span to target a <span> immediately following an excerpt. Note, though, that this will not work in IE—but that’s what IE7 (not to be confused with the real IE 7) is for.

And if the span is inside the <p>, then we can do: p.excerpt > span. Again, it really is valid CSS, but it won’t work in IE. As in the case above, IE7 can fix that.

-Kurt

Last edited by KurtRaschke (2006-02-05 01:53:54)


kurt@kurtraschke.com

Offline

#56 2006-02-05 16:22:28

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,250
Website GitHub

Re: [archived] kgr_safe_excerpt

> KurtRaschke wrote:
>…you could do p.excerpt + span to target a <span> immediately following an excerpt…
> And if the span is inside the <p>, then we can do: p.excerpt > span.

Way cool. Guess I should really look at the whole CSS manual at w3c.org one day instead of just the easy bits :-)

>Note, though, that this will not work in IE…
Typical. But then, imo, IE users get what’s coming to ‘em for putting up with that heap! I do what I can to beat IE6 into some crude form of web usability (within reason), but the bits that blatantly disregard the specs, I blatantly disregard the browser. The pseudo-elements :before and :after spring to mind immediately.

Anyway, yeah, rationalise away in that case. I’ll keep an eye out for the next official release. Meanwhile I’m off to condense my own CSS with this newfound skill of + and > selectors — thanks for putting me straight on that.


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

#57 2006-04-09 10:16:38

marios
Archived Plugin Author
Registered: 2005-03-12
Posts: 1,253

Re: [archived] kgr_safe_excerpt

Really usefull plugin, it’s like a (You can not live without it one)
However in Debugging mode , I am getting the following error notices.
<code>tag_error <txp:kgr_safe_excerpt count=“60” linktext=”…more…” mode=“chars”/> -> Notice: Uninitialized string offset: 0 on line 522
/lib/txplib_misc.php:522 gAtt()
/lib/txplib_misc.php:528 gAtt()
/publish/taghandlers.php:1460 lAtts()
/lib/txplib_misc.php(463) : eval()’d code:17 permlink()
/publish.php:914 kgr_safe_excerpt()
/publish.php:884 processTags()
/publish.php:884 preg_replace_callback()
/publish/taghandlers.php:1685 parse()
/publish.php:910 if_article_category()
/publish.php:884 processTags()</code>

I am using it on 4.03 and call it with the following article_form code on the section front pagepage
<code><txp:if_section name=“archives”>

<txp:if_article_category number=“1”>

<txp:if_different>

<h2 id=”<txp:category1 />”>Articles filed under <txp:category1 title=1 /></h2>
</txp:if_different>
<h3><txp:permlink><txp:title /></txp:permlink></h3>

<txp:kgr_safe_excerpt count=“60” linktext=”…more…” mode=“chars” />

</txp:if_article_category>

</txp:if_section></code>

Any Ideas, why it throws those Errors ?

EDIT.:VOID ABOVE, found it,the plugin is irresponsible for that, I mistakenly was calling an if_section conditional twice, once in the page template, and once in the article form, all is well, thanks.(I just leave this post here as a reminder)

regards, marios

Last edited by marios (2006-04-09 10:36:18)


⌃ ⇧ < ⌃ ⇧ >

Offline

#58 2006-04-09 20:05:11

tripdragon
Member
Registered: 2005-07-22
Posts: 19

Re: [archived] kgr_safe_excerpt

sooo llost
I was showing my posts with a simple txp:article limit=“3” /
Now I am trying
txp:article limit=“3” /
txp:kgr_safe_excerpt count=“100” linktext=“Read More” /
But I get no excerpts …. just sam e ol same ol, And yes I have it turned on..

Last edited by tripdragon (2006-04-09 20:09:58)

Offline

#59 2006-04-09 20:19:06

KurtRaschke
Plugin Author
Registered: 2004-05-16
Posts: 275

Re: [archived] kgr_safe_excerpt

tripdragon,

The <txp:kgr_safe_excerpt /> tag goes within an existing article form—you will probably need to edit the ‘default’ article form and replace the <txp:body /> tag with the <txp:kgr_safe_excerpt /> tag.

-Kurt


kurt@kurtraschke.com

Offline

#60 2006-04-09 20:27:29

tripdragon
Member
Registered: 2005-07-22
Posts: 19

Re: [archived] kgr_safe_excerpt

KurtRaschke wrote:

tripdragon,
The <txp:kgr_safe_excerpt /> tag goes within an existing article form—you will probably need to edit the ‘default’ article form and replace the <txp:body /> tag with the <txp:kgr_safe_excerpt /> tag.
-Kurt

Like this ??
<txp:article limit="2" kgr_safe_excerpt count="1" linktext="Read More" />
or
<txp:body limit="2" kgr_safe_excerpt count="1" linktext="Read More" />

I have never had <txp:body />
Literly all I have in my default code box is <txp:article limit="3" /> like so http://filmsandwich.com/

Last edited by tripdragon (2006-04-09 20:28:56)

Offline

  1. Index
  2. » Archives
  3. » [archived] kgr_safe_excerpt

Board footer

Powered by FluxBB