Make WordPress Core

Ticket #1268: sem-smart-excerpt.php

File sem-smart-excerpt.php, 3.9 KB (added by Denis de Bernardy, 21 years ago)
Line 
1<?php
2/*
3Plugin Name: Smart Excerpt
4Version: 0.1 alpha
5Plugin URI: http://www.semiologic.com/projects/smart-excerpt/
6Description: Enhances WordPress' default excerpt generator
7Author: Denis de Bernardy
8Author URI: http://www.semiologic.com
9*/
10
11/*
12 * Copyright, license and disclaimer
13 * ---------------------------------
14 * Except where otherwise noted, this software is:
15 * - Copyright 2005, Denis de Bernardy
16 * - Licensed under the terms of the CC/GNU GPL v.2.0
17 *   http://creativecommons.org/licenses/GPL/2.0/
18 * - Provided as is, with NO WARRANTY whatsoever
19**/
20
21
22
23/*
24 * sem_fancy_excerpt()
25 * -------------------
26 * Creates a fancy default excerpt
27**/
28
29function sem_fancy_excerpt( $excerpt )
30{
31global $post;
32
33$excerpt_length = 30;
34
35if ( $excerpt == '' )
36{
37        $excerpt = $post->post_content;
38        $excerpt = apply_filters( 'the_content', $excerpt );
39        $excerpt = strip_tags( $excerpt );
40        $excerpt = preg_replace( "/^\W*(\w+(\W+\w+){".($excerpt_length-1)."}[^\.]*\.)[^$]+/", "$1 (...)", $excerpt );
41}
42return $excerpt;
43} // end sem_fancy_excerpt()
44
45remove_filter('get_the_excerpt', 'wp_trim_excerpt');
46add_filter('get_the_excerpt', 'sem_fancy_excerpt');
47
48
49
50/*
51 * sem_backlink_excerpt()
52 * -------------------
53 * Creates a fancy excerpt for a backlink
54**/
55
56function sem_backlink_excerpt( $text, $link )
57{
58
59// clean up the text
60
61$text = preg_replace( "/<!DOC/", "<DOC", $text ); // strip_tags bug
62$text = preg_replace( "/[\s\r\n\t]+/", " ", $text ); // normalize spaces
63$text = preg_replace( "/ <(h1|h2|h3|h4|h5|h6|p|th|td|li|dt|dd|pre|caption|input|textarea|button|body)[^>]*>/", "\n\n", $text );
64$text = strip_tags( $text, "<title><a>" ); // just keep the tags we need
65
66//echo Markdown( $text );
67
68// split into paragraphs
69
70$p = explode( "\n\n", $text );
71
72// fetch the title
73
74$title = preg_replace( "/^[^<]*<[^>]*title[^>]*>([^<]*)<\/[^>]*title[^>]*>[^$]*/", "$1", $p[0] );
75
76// fetch the first paragraph with the link
77
78$sem_regexp_pb = "/(\\/|\\\|\*|\?|\+|\.|\^|\\$|\(|\)|\[|\]|\||\{|\})/";
79$sem_regexp_fix = "\\\\$1";
80$link = preg_replace( $sem_regexp_pb, $sem_regexp_fix, $link );
81
82for ( $i = 0; $p[$i] && !$excerpt; $i++ )
83{
84        if ( preg_match( "/<a[^>]+".$link."[^>]*>/", $p[$i] ) )
85        {
86                $context = preg_replace( "/.*<a[^>]+".$link."[^>]*>([^>]+)<\/a>.*/", "$1", $p[$i] );
87                $excerpt = trim( strip_tags( $p[$i] ) );
88        }
89}
90
91// return the result
92return array( $title, $context, $excerpt );
93} // end sem_backlink_excerpt ()
94
95
96
97
98function sem_backlink_excerpt_test()
99{
100list ( $title, $context, $excerpt ) = sem_backlink_excerpt( '<title>excerpt title</title>
101<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
102eiusmod tempor incididunt ut labore et dolore <em>magna</em> aliqua. Ut enim
103ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut
104aliquip ex ea <strong>commodo</strong> consequat. Duis aute irure dolor in
105reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
106pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa
107qui officia deserunt mollit anim id est laborum.</p>
108
109<p>sss</p>
110
111<h1>test</h1>
112
113<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
114eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad
115minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip
116ex ea <a href="http://www.semiologic.com">commodo</a> consequat. Duis aute irure dolor in reprehenderit in
117voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur
118sint occaecat cupidatat non proident, sunt in culpa qui officia
119deserunt mollit anim id est laborum.
120<ul>
121<li>unordered list test
122<li>unordered list test
123<li>ordered list test
124<li>ordered list test
125</ul>
126blockquote test</p>', 'http://www.semiologic.com' );
127
128echo "<h1>$title</h1>\n<h2>$context</h2>\n".Markdown($excerpt);
129}
130
131add_action( 'wp_footer', 'sem_backlink_excerpt_test' );
132
133?>