From the plugin wishlist: NP_AutoExtended

Post your new plugins here!
User avatar
admun
Nucleus Guru
Nucleus Guru
Posts: 4088
Joined: Mon Oct 20, 2003 2:57 am
Location: San Francisco, CA, USA
Contact:

From the plugin wishlist: NP_AutoExtended

Postby admun » Wed May 05, 2004 9:40 pm

You can tell I'm a bit bored. 8)

This plugin will split the item body to extended text after x number of words.

It is still have some bug in there when spliting text w/ a URL in it.... I need to find a better way....

As usual, feedback/suggestion welcome. flame to /dev/null. 8)

Instruction:
- Just install this plugin!
- Go ahead and post a new entry. It will split text into the extended text section automatically.

Code: Select all

<?
class NP_AutoExtended extends NucleusPlugin {

   function getName() { return 'AutoExtended'; }
   function getAuthor()  { return 'Edmond Hui (admun)'; }
   function getURL() { return ''; }
   function getVersion() { return 'v0.1'; }
   function getDescription() {
      return 'This plugin splits the item body into extended text if it is longer than a certain size';
   }

   function supportsFeature($what) {
      switch($what) {
        case 'SqlTablePrefix':
          return 1;
        default:
          return 0;
      }
   }

   function getEventList() {
      return array('PreAddItem');
   }

   function install() {
      $this->createOption('split_count','Number of words keep in the body','text','16');
   }

/*
  Known problem:
     - If there is URL in the body, the word split is screwed up.
       What is the better way to do it?
*/
   function event_PreAddItem($data) {
      $wordlist = explode(" ", $data['body']);

      if (sizeof($wordlist) > $this->getOption('split_count')) {
         $i = 1;
         $data['body'] = '';
         foreach ($wordlist as $word) {
            if ($i <= $this->getOption('split_count')) {
               $data['body'] = $data['body'] . $word . " ";
            }
            else {
               $tempext = $tempext . $word . " ";
            }

            $i++;
         }

         $data['body'] = $data['body'] . "... ";
         $data['more'] = "..." . $tempext . "\n\n" . $data['more'];
      }
   }

}
?>
User avatar
Trent
Nucleus Guru
Nucleus Guru
Posts: 1534
Joined: Fri Oct 04, 2002 5:08 pm
Location: Alberta, Canada
Contact:

Postby Trent » Thu May 06, 2004 3:34 am

Wicked Stuff! Thanks again! I will use this one right away!

Trent
Life Through The Eyes Of Trent Adams!
User avatar
admun
Nucleus Guru
Nucleus Guru
Posts: 4088
Joined: Mon Oct 20, 2003 2:57 am
Location: San Francisco, CA, USA
Contact:

Postby admun » Thu May 06, 2004 4:58 am

Did more testing, there are some limiitation for this plugin:

1) If there is a URL in the body, the word count cannot take into account for the link.
2) If the link is on or around the x words, the link split between 2 paragraph.

Solution: do a pre processing on the body that will replace the link, and the link is insert back afterward.

Stay turn for the new version. 8)
User avatar
admun
Nucleus Guru
Nucleus Guru
Posts: 4088
Joined: Mon Oct 20, 2003 2:57 am
Location: San Francisco, CA, USA
Contact:

Postby admun » Fri May 07, 2004 3:49 am

Yes, I think I crack this one. 8)

See the new version that count word properly, taking into account of HTML.

Note: The way I did it is kind of stupid (walking char-by-char)... I'm a brute force guy. If someone think of a better way, sure let me know.

Code: Select all

<?
/*
   History:
     v0.1, May 5, 2004 - Initial version
     v0.2, May 6, 2004 - Fixed URL spliting
     v0.3, May 6, 2004 - Re-implemented event_PreAddItem to deal with HTML properly
*/
class NP_AutoExtended extends NucleusPlugin {

   function getName() { return 'AutoExtended'; }
   function getAuthor()  { return 'Edmond Hui (admun)'; }
   function getURL() { return ''; }
   function getVersion() { return 'v0.3'; }
   function getDescription() {
      return 'This plugin splits the item body into extended text if it is longer than a certain size';
   }

   function supportsFeature($what) {
      switch($what) {
        case 'SqlTablePrefix':
          return 1;
        default:
          return 0;
      }
   }

   function getEventList() {
      return array('PreAddItem');
   }

   function install() {
      $this->createOption('split_count','Number of words keep in the body','text','20');
   }

   function event_PreAddItem($data) {
     $state = "close";
     $wordcount = 0;
     $tempbody = '';
     $tempext ='';
     for ($i=0; $i < strlen($data['body']); $i++) {
       switch ($data['body'][$i]) {
         case '<':
           $state = "open";
           break;
         case '>':
           if ($state == "close") break;
           $state = "close";
           break;
         case ' ':
           if ($state == "close") $wordcount++;
           break;
         case '\n':
         default:
           break;
       }

       if ($wordcount < $this->getOption('split_word_count'))
         $tempbody = $tempbody . $data['body'][$i];
       else
         $tempext = $tempext . $data['body'][$i];
     }

     if ($wordcount < $this->getOption('split_word_count')) return;

     $data['body'] = $tempbody . "... ";
     $data['more'] = "..." . $tempext . "\n" . $data['more'];
   }

}

?>
User avatar
admun
Nucleus Guru
Nucleus Guru
Posts: 4088
Joined: Mon Oct 20, 2003 2:57 am
Location: San Francisco, CA, USA
Contact:

Postby admun » Thu May 13, 2004 3:58 am

I posted the code on the plugin resource site now.

http://plugins.nucleuscms.org/item/82
amdaily
Posts: 119
Joined: Mon Apr 14, 2003 1:53 am

Postby amdaily » Sat May 29, 2004 6:20 am

Would there by anyway to break it at the first line break?
Subhendu
Posts: 13
Joined: Wed Jul 06, 2005 4:43 pm

Error!

Postby Subhendu » Wed Jul 06, 2005 4:47 pm

Hi...

I'm using v3.2 and while installing the plug-in I'm getting this error :

"Fatal error: Call to a member function on a non-object in /.../.../public_html/nu/nucleus/libs/ADMIN.php on line 5270"

Help please!!

Thanks in advance
jacy
Posts: 46
Joined: Thu Aug 04, 2005 3:32 pm

Postby jacy » Sat Sep 24, 2005 2:01 pm

I installed this and it works quite well but one aspect bugs me a bit, and that's that the extended entry includes a couple line breaks and 2 ellipsis (ellipses?) where the post was split. Fine for that to be on the index page, but once the reader gets to the detail page, it looks a little odd to me. Is there a way to get the item entry to appear in one whole piece?

Or, did anyone figure out a way to break it at a line break? An option for this to break it after X line breaks would be great but I'm no coder so I've no idea how feasible it would be.

TIA
~Jacy
User avatar
admun
Nucleus Guru
Nucleus Guru
Posts: 4088
Joined: Mon Oct 20, 2003 2:57 am
Location: San Francisco, CA, USA
Contact:

Postby admun » Sat Sep 24, 2005 4:46 pm

jacy wrote:I installed this and it works quite well but one aspect bugs me a bit, and that's that the extended entry includes a couple line breaks and 2 ellipsis (ellipses?) where the post was split. Fine for that to be on the index page, but once the reader gets to the detail page, it looks a little odd to me. Is there a way to get the item entry to appear in one whole piece?

May be those new lines are added by your template/skin? I checked the code and I don't remember it's adding them...

Or, did anyone figure out a way to break it at a line break? An option for this to break it after X line breaks would be great but I'm no coder so I've no idea how feasible it would be.

Yeah, it was one of the todo item to split by paragraph... but yet I have time to do it.....
hurry
Posts: 57
Joined: Fri Dec 09, 2005 12:57 pm

Does not load

Postby hurry » Sat Dec 10, 2005 7:23 pm

It says this plugin cannot be loaded for my Nucleus installation v3.22. I am trying 0.3.
User avatar
admun
Nucleus Guru
Nucleus Guru
Posts: 4088
Joined: Mon Oct 20, 2003 2:57 am
Location: San Francisco, CA, USA
Contact:

Re: Does not load

Postby admun » Mon Dec 12, 2005 3:58 pm

hurry wrote:It says this plugin cannot be loaded for my Nucleus installation v3.22. I am trying 0.3.

Can you show the exact error?
eraser
Posts: 17
Joined: Sun Sep 11, 2005 1:51 pm

Postby eraser » Tue Dec 13, 2005 1:42 pm

Plugin NP_Autoextend was not loaded (Class not found in file, possible parse error)
User avatar
admun
Nucleus Guru
Nucleus Guru
Posts: 4088
Joined: Mon Oct 20, 2003 2:57 am
Location: San Francisco, CA, USA
Contact:

Postby admun » Tue Dec 13, 2005 3:42 pm

eraser wrote:Plugin NP_Autoextend was not loaded (Class not found in file, possible parse error)

Did you save the file as NP_AutoExtended.php? You _need_ to have it exactly the same as the class name from the code.....
eraser
Posts: 17
Joined: Sun Sep 11, 2005 1:51 pm

Postby eraser » Wed Dec 14, 2005 7:51 am

oh okies well i thought i would just say the plugin thingo under install sais:
Install
Copy the Code and save it as NP_Autoextend.php
install the plugin
change option if needed, which control how many words are keep in the body.
User avatar
admun
Nucleus Guru
Nucleus Guru
Posts: 4088
Joined: Mon Oct 20, 2003 2:57 am
Location: San Francisco, CA, USA
Contact:

Postby admun » Wed Dec 14, 2005 2:25 pm

eraser wrote:oh okies well i thought i would just say the plugin thingo under install sais:
Install
Copy the Code and save it as NP_Autoextend.php
install the plugin
change option if needed, which control how many words are keep in the body.

Hum... it's a typo... It's fixed now.
User avatar
mynd
Posts: 73
Joined: Fri Dec 16, 2005 2:20 pm
Location: New Jersey
Contact:

Postby mynd » Wed Dec 21, 2005 6:11 am

Hello,

Just what I was looking for.. Is this plugin FINAL? Or does there have to be more work done? I would really like to use this plugin if it is finished.

Thanks!
Kind regards,
Mynd

http://www.myndpollution.com
User avatar
admun
Nucleus Guru
Nucleus Guru
Posts: 4088
Joined: Mon Oct 20, 2003 2:57 am
Location: San Francisco, CA, USA
Contact:

Postby admun » Wed Dec 21, 2005 6:40 am

mynd wrote:Hello,

Just what I was looking for.. Is this plugin FINAL? Or does there have to be more work done? I would really like to use this plugin if it is finished.

Thanks!

The plugin is considered in production now.
User avatar
mynd
Posts: 73
Joined: Fri Dec 16, 2005 2:20 pm
Location: New Jersey
Contact:

Postby mynd » Wed Dec 21, 2005 7:14 am

Maybe it is late or something is wrong with my brain.

I just installed this plugin.. I assumed it would chop what was on the main page down to the amount of words that I chose. At the moment I made it 30, just to see what it would do..

Well here is what it did.

http://www.myndpollution.com/

Once again, ( boy am I feeling like a dumbass ). Will this work on only new posts or old as well?

Thanks in advance
User avatar
admun
Nucleus Guru
Nucleus Guru
Posts: 4088
Joined: Mon Oct 20, 2003 2:57 am
Location: San Francisco, CA, USA
Contact:

Postby admun » Wed Dec 21, 2005 7:23 am

It chops only new post after the plugin installed, old posts are not effected.

mynd wrote:Maybe it is late or something is wrong with my brain.

I just installed this plugin.. I assumed it would chop what was on the main page down to the amount of words that I chose. At the moment I made it 30, just to see what it would do..

Well here is what it did.

http://www.myndpollution.com/

Once again, ( boy am I feeling like a dumbass ). Will this work on only new posts or old as well?

Thanks in advance
User avatar
mynd
Posts: 73
Joined: Fri Dec 16, 2005 2:20 pm
Location: New Jersey
Contact:

Postby mynd » Wed Dec 21, 2005 7:27 am

Well, now I feel better.. thanks ;) Have a nice night it is 1:30am here.. time to get some rest.

I appreciate your time and resonses.. They are very valuable to me.

Return to “Plugin Development”