<?php
/*
Plugin Name: Smart Excerpt
Version: 0.1 alpha
Plugin URI: http://www.semiologic.com/projects/smart-excerpt/
Description: Enhances WordPress' default excerpt generator
Author: Denis de Bernardy
Author URI: http://www.semiologic.com
*/

/*
 * Copyright, license and disclaimer
 * ---------------------------------
 * Except where otherwise noted, this software is:
 * - Copyright 2005, Denis de Bernardy
 * - Licensed under the terms of the CC/GNU GPL v.2.0
 *   http://creativecommons.org/licenses/GPL/2.0/
 * - Provided as is, with NO WARRANTY whatsoever
**/



/*
 * sem_fancy_excerpt()
 * -------------------
 * Creates a fancy default excerpt
**/

function sem_fancy_excerpt( $excerpt )
{
global $post;

$excerpt_length = 30;

if ( $excerpt == '' )
{
	$excerpt = $post->post_content;
	$excerpt = apply_filters( 'the_content', $excerpt );
	$excerpt = strip_tags( $excerpt );
	$excerpt = preg_replace( "/^\W*(\w+(\W+\w+){".($excerpt_length-1)."}[^\.]*\.)[^$]+/", "$1 (...)", $excerpt );
}
return $excerpt;
} // end sem_fancy_excerpt()

remove_filter('get_the_excerpt', 'wp_trim_excerpt');
add_filter('get_the_excerpt', 'sem_fancy_excerpt');



/*
 * sem_backlink_excerpt()
 * -------------------
 * Creates a fancy excerpt for a backlink
**/

function sem_backlink_excerpt( $text, $link )
{

// clean up the text

$text = preg_replace( "/<!DOC/", "<DOC", $text ); // strip_tags bug
$text = preg_replace( "/[\s\r\n\t]+/", " ", $text ); // normalize spaces
$text = preg_replace( "/ <(h1|h2|h3|h4|h5|h6|p|th|td|li|dt|dd|pre|caption|input|textarea|button|body)[^>]*>/", "\n\n", $text );
$text = strip_tags( $text, "<title><a>" ); // just keep the tags we need

//echo Markdown( $text );

// split into paragraphs

$p = explode( "\n\n", $text );

// fetch the title

$title = preg_replace( "/^[^<]*<[^>]*title[^>]*>([^<]*)<\/[^>]*title[^>]*>[^$]*/", "$1", $p[0] );

// fetch the first paragraph with the link

$sem_regexp_pb = "/(\\/|\\\|\*|\?|\+|\.|\^|\\$|\(|\)|\[|\]|\||\{|\})/";
$sem_regexp_fix = "\\\\$1";
$link = preg_replace( $sem_regexp_pb, $sem_regexp_fix, $link );

for ( $i = 0; $p[$i] && !$excerpt; $i++ )
{
	if ( preg_match( "/<a[^>]+".$link."[^>]*>/", $p[$i] ) )
	{
		$context = preg_replace( "/.*<a[^>]+".$link."[^>]*>([^>]+)<\/a>.*/", "$1", $p[$i] );
		$excerpt = trim( strip_tags( $p[$i] ) );
	}
}

// return the result
return array( $title, $context, $excerpt );
} // end sem_backlink_excerpt ()




function sem_backlink_excerpt_test()
{
list ( $title, $context, $excerpt ) = sem_backlink_excerpt( '<title>excerpt title</title>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
eiusmod tempor incididunt ut labore et dolore <em>magna</em> aliqua. Ut enim
ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut
aliquip ex ea <strong>commodo</strong> consequat. Duis aute irure dolor in
reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa
qui officia deserunt mollit anim id est laborum.</p>

<p>sss</p>

<h1>test</h1>

<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad
minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip
ex ea <a href="http://www.semiologic.com">commodo</a> consequat. Duis aute irure dolor in reprehenderit in
voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur
sint occaecat cupidatat non proident, sunt in culpa qui officia
deserunt mollit anim id est laborum.
<ul>
<li>unordered list test
<li>unordered list test
<li>ordered list test
<li>ordered list test
</ul>
blockquote test</p>', 'http://www.semiologic.com' );

echo "<h1>$title</h1>\n<h2>$context</h2>\n".Markdown($excerpt);
}

add_action( 'wp_footer', 'sem_backlink_excerpt_test' );

?>