1

Topic: Truncate news output in generator?

Is there any way to truncate the news output from News Generator?
Some posts that are copied from online newspapers are so long that no one bothers scrolling to the next news item. It would be better if it was possible to truncate the output, so for instance only the first 4 lines shows.

Re: Truncate news output in generator?

something like this?

<?php

function truncate_string($details,$max)
{
    if(strlen($details)>$max)
    {
        $details = substr($details,0,$max);
        $i = strrpos($details," ");
        $details = substr($details,0,$i);
        $details = $details." ..... ";
    }
    return $details;
}

?>

Usage

<?php

$text = truncate_string("hello there. This is a long string",19);

?>

Produces this

hello there. This .....

you would then have to have it link to "full story".   That is something that I am affraid is outside the scope of what the news generator was intended for.  Rickard offered this Admin plugin as a freebie.  any modifications will undoubtably have to be by the user.

So the answer is yes, it can probably be done. but I am not sure if the author will do requests like this on a onsie-twosie basis.  It would open the floodgates for more requests.  Someone may pick-up the ball and submit it as a mod. who knows...

Re: Truncate news output in generator?

<?php
/***********************************************************************

  Copyright (C) 2002-2005  Rickard Andersson (rickard@punbb.org)

  This file is part of PunBB.

  PunBB is free software; you can redistribute it and/or modify it
  under the terms of the GNU General Public License as published
  by the Free Software Foundation; either version 2 of the License,
  or (at your option) any later version.

  PunBB is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  MA  02111-1307  USA

************************************************************************/

// The forum from which we'll pull the news bits
$forum_id = 21;

// Number of news posts to include in the index
$num_posts_index = 5;

// Path to news item template
$template_path = PUN_ROOT.'plugins/AP_News_Generator/news.tpl';

// Directories in which plugin will save generated markup (must end with slash)
$output_dir_latest = PUN_ROOT.'plugins/AP_News_Generator/';
$output_dir_archive = PUN_ROOT.'plugins/AP_News_Generator/archive/';

/***********************************************************************
                     DO NOT EDIT BELOW THIS LINE
/***********************************************************************/


function prune_para($text)
{
    $paragraph = preg_split("/\s*\n+/", $text);
    if (isset($paragraph[1])) {
        $text = $paragraph[0] . "...";
    }
    return $text;
}

// Make sure no one attempts to run this script "directly"
if (!defined('PUN'))
    exit;

// Tell admin_loader.php that this is indeed a plugin and that it is loaded
define('PUN_PLUGIN_LOADED', 1);


if (isset($_POST['gen_news']))
{
    // Generate front page news
    $result = $db->query('SELECT id, subject FROM '.$db->prefix.'topics WHERE forum_id='.$forum_id.' ORDER BY sticky DESC, posted DESC LIMIT 0, '.$num_posts_index) or error('Unable to fetch topic list', __FILE__, __LINE__, $db->error());
    if (!$db->num_rows($result))
        message('There are no topics to generate news based on in forum with ID = '.$forum_id.'.');

    require PUN_ROOT.'include/parser.php';

    $news_tpl = file_get_contents($template_path) or error('Unable to open new item template '.$template_path.'. Make sure $template_path is correct', __FILE__, __LINE__);

    $fh = @fopen($output_dir_latest.'news.html', 'wb');
    if (!$fh)
        error('Unable to write news to '.$output_dir_latest.'main.html. Please make sure PHP has write access to the directory '.$output_dir_latest, __FILE__, __LINE__);

    while ($cur_topic = $db->fetch_assoc($result))
    {
        $result2 = $db->query('SELECT posted, poster, message, hide_smilies FROM '.$db->prefix.'posts WHERE topic_id='.$cur_topic['id'].' ORDER BY posted ASC LIMIT 1') or error('Unable to fetch topic list', __FILE__, __LINE__, $db->error());
        $cur_post = $db->fetch_assoc($result2);

        $search = array('<news_subject>', '<news_posted>', '<news_poster>', '<news_message>', '<news_comments>');
        $replace = array(pun_htmlspecialchars($cur_topic['subject']), date('Y-m-d H:i', $cur_post['posted']), pun_htmlspecialchars($cur_post['poster']), parse_message(prune_para($cur_post['message']), $cur_post['hide_smilies']), '<a href="'.$pun_config['o_base_url'].'/viewtopic.php?id='.$cur_topic['id'].'">Comments</a>');

        fwrite($fh, str_replace($search, $replace, $news_tpl));
    }

    fclose($fh);


    // Generate monthly archives
    $year_end = intval(date('Y'));
    $month_end = intval(date('n'));

    $year_start = ($month_end != 1) ? $year_end : $year_end-1;
    $month_start = ($month_end != 1) ? $month_end-1 : 12;

    // How far back should we go?
    $result = $db->query('SELECT MIN(posted) FROM '.$db->prefix.'topics WHERE forum_id='.$forum_id.'') or error('Unable to fetch earliest topic', __FILE__, __LINE__, $db->error());
    $history_limit = $db->result($result);

    $year_limit = intval(date('Y', $history_limit));
    $month_limit = intval(date('n', $history_limit));

    while ($year_end > $year_limit || $month_end > $month_limit)
    {
        $result = $db->query('SELECT id, subject FROM '.$db->prefix.'topics WHERE forum_id='.$forum_id.' AND posted>=UNIX_TIMESTAMP(\''.$year_start.'-'.$month_start.'-01\') AND posted<UNIX_TIMESTAMP(\''.$year_end.'-'.$month_end.'-01\') ORDER BY posted DESC') or error('Unable to fetch topic list', __FILE__, __LINE__, $db->error());
        if ($db->num_rows($result))
        {
            $news_tpl = file_get_contents($template_path) or error('Unable to open new item template '.$template_path.'. Make sure $template_path is correct', __FILE__, __LINE__);

            $fh = @fopen($output_dir_archive.$year_end.'-'.($month_end > 9 ? $month_end : '0'.$month_end).'.html', 'wb');
            if (!$fh)
                error('Unable to write news archive to '.$output_dir_archive.$year_end.'-'.($month_end > 9 ? $month_end : '0'.$month_end).'.htmlPlease make sure PHP has write access to the directory '.$output_dir_archive, __FILE__, __LINE__);

            while ($cur_topic = $db->fetch_assoc($result))
            {
                $result2 = $db->query('SELECT posted, poster, message, hide_smilies FROM '.$db->prefix.'posts WHERE topic_id='.$cur_topic['id'].' ORDER BY posted ASC LIMIT 1') or error('Unable to fetch topic list', __FILE__, __LINE__, $db->error());
                $cur_post = $db->fetch_assoc($result2);

                $search = array('<news_subject>', '<news_posted>', '<news_poster>', '<news_message>', '<news_comments>');
                $replace = array(pun_htmlspecialchars($cur_topic['subject']), date('Y-m-d H:i', $cur_post['posted']), pun_htmlspecialchars($cur_post['poster']), parse_message(prune_para($cur_post['message']), $cur_post['hide_smilies']), '<a href="">Comments</a>');

                fwrite($fh, str_replace($search, $replace, $news_tpl));
            }

            fclose($fh);
        }

        $year_end = $year_start;
        $month_end = $month_start;
        $year_start = ($month_end != 1) ? $year_end : $year_end-1;
        $month_start = ($month_end != 1) ? $month_end-1 : 12;
    }

    generate_admin_menu($plugin);

?>
    <div class="block">
        <h2><span>News Generator results</span></h2>
        <div class="box">
            <div class="inbox">
                <p>News generated.</p>
            </div>
        </div>
    </div>
<?php
}
else
{
    generate_admin_menu($plugin);

?>
    <div class="blockform">
        <h2><span>News Generator</span></h2>
        <div class="box">
            <form id="example" method="post" action="<?php echo $_SERVER['REQUEST_URI'] ?>&foo=bar">
                <div class="inform">
                    <fieldset>
                        <legend>Generate static news output</legend>
                        <div class="infldset">
                            <p>This plugin fetches posts from the specified forum and generates static markup for those posts based on the news item template. It outputs news.html containing the most recent news items and a month based archive (e.g. 2005-01.html) for all posts in the forum. <strong>Look at the top of the PHP source code for this plugin to change the settings you see below.</strong></p>
                            <table class="aligntop" cellspacing="0">
                                <tr>
                                    <th scope="row">Fetch news from forum</th>
                                    <td><span><?php echo $forum_id ?></span></td>
                                </tr>
                                <tr>
                                    <th scope="row">No of posts for latest</th>
                                    <td><span><?php echo $num_posts_index ?></span></td>
                                </tr>
                                <tr>
                                    <th scope="row">Using template</th>
                                    <td><span><?php echo $template_path ?></span></td>
                                </tr>
                                <tr>
                                    <th scope="row">Saving latest news to</th>
                                    <td><span><?php echo $output_dir_latest ?></span></td>
                                </tr>
                                <tr>
                                    <th scope="row">Saving archive to</th>
                                    <td><span><?php echo $output_dir_archive ?></span></td>
                                </tr>
                            </table>
                            <div class="fsetsubmit"><input type="submit" name="gen_news" value="Generate news" tabindex="1" /></div>
                        </div>
                    </fieldset>
                </div>
            </form>
        </div>
    </div>
<?php

}

that will only show the first paragraph and if there is another paragraph it will put ... at the end

4

Re: Truncate news output in generator?

Thanks! Damn, I need to learn to code some basic php.. so I can do this myself.

Re: Truncate news output in generator?

Doh.  thats why connorhd is a moderator big_smile  He takes the time to read the code.

6

Re: Truncate news output in generator?

I guess it's easy to make it show the first 2 or 3 paragraphs as well? I tried editing, hehe, but only managed to generate the 3rd paragraph only!
I'm such a novice.. sigh.

Re: Truncate news output in generator?

yeh that gets more complicated its all about

function prune_para($text)
{
    $paragraph = preg_split("/\s*\n+/", $text);
    if (isset($paragraph[1])) {
        $text = $paragraph[0] . "...";
    }
    return $text;
}

for 2 paragraphs

function prune_para($text)
{
    $paragraph = preg_split("/\s*\n+/", $text);
    if (isset($paragraph[2])) {
        $text = $paragraph[0] . $paragraph[1] . "...";
    }
    return $text;
}

for 3

function prune_para($text)
{
    $paragraph = preg_split("/\s*\n+/", $text);
    if (isset($paragraph[3])) {
        $text = $paragraph[0] . $paragraph[1] . $paragraph[2] . "...";
    }
    return $text;
}

you probably get the pattern now

8 (edited by Endre 2005-03-23 18:01)

Re: Truncate news output in generator?

Connorhd wrote:

you probably get the pattern now

Yes.. it's a tricky one, but I'll see what I can do wink

This output doesn't create linebreak between paragraphs tho.. it's all squashed together.

Re: Truncate news output in generator?

ok for 3

function prune_para($text)
{
    $paragraph = preg_split("/\s*\n+/", $text);
    if (isset($paragraph[3])) {
        $text = $paragraph[0] . "\n" . $paragraph[1] . "\n" . $paragraph[2] . "...";
    }
    return $text;
}

sorry about that i'm just coding this in the post box (not a great idea)

10

Re: Truncate news output in generator?

thanks so much, it's perfect now smile
Personally I think this should be included in the News Generator plug-in. Make it an option that users can comment/uncomment depending on whethere this is a feature they wanna use. It makes the generated news much more streamlined, and helps making the frontpage look much nicer.

Re: Truncate news output in generator?

Can anyone show me how to limit output to first x characters of post ( what to modify or make it configurable in AP_News_Generator.php ) ?

Regards