Question2answer: show excerpt in the RSS feed

Posted on In QA

There is a “Include full text in feeds:” option in the “RSS feeds” configuration panel but no options/method to only show excerpt instead of the “full text” or nothing for RSS feeds. This need to hack the question2answer source code. Details are in the answer.

The changes based on Question2asnwer 1.5.4:

diff --git a/qa-include/qa-index.php b/qa-include/qa-index.php
index 1a5845a..f49d83c 100644
--- a/qa-include/qa-index.php
+++ b/qa-include/qa-index.php
@@ -158,7 +158,7 @@
                                                ob_start('ob_gzhandler');
                                                
                        if (substr($requestlower, 0, 5)=='feed/')
-                               require QA_INCLUDE_DIR.'qa-feed.php';
+                               require QA_INCLUDE_DIR.'qa-feed-excerpt.php';
                        else
                                require QA_INCLUDE_DIR.'qa-page.php';
                }
@@ -169,4 +169,4 @@
 
 /*
        Omit PHP closing tag to help avoid accidental output
-*/
 No newline at end of file
+*/


diff qa-feed.php qa-feed-excerpt.php 
40a41,74
> 	function qa_feed_excerpt($string, $length)
> /*
> 	Return no more than $length characters from $string after converting it to a single line, by
> 	removing words from the middle (and especially towards the end)
> */
> 	{
> 		$string=strtr($string, "rnt", '  ');
> 		
> 		if (qa_strlen($string)>$length) {
> 			$remaining=$length-5;
> 			
> 			$words=qa_string_to_words($string, false, true);
> 			$countwords=count($words);
> 			
> 			$prefix='';
> 			
> 			for ($addword=0; $addword<$countwords; $addword++) {
> 				$word=array_shift($words);
> 				
> 				if (qa_strlen($word)>$remaining)
> 					break;
> 				
> 				$prefix.=$word;
> 				
> 				$remaining-=qa_strlen($word);
> 			}
> 			
> 			$string=$prefix.' ... ';
> 		}
> 		
> 		return $string;
> 	}
> 
> 
402c436,438
< 			$lines[]='<description>'.qa_html($htmlcontent).'</description>'; // qa_html() a second time to put HTML code inside XML wrapper
---
> 			$lines[]='<description>'.
> 			qa_html(qa_feed_excerpt($htmlcontent, 400)).
> 			'</description>'; // qa_html() a second time to put HTML code inside XML wrapper
432c468
< */
 No newline at end of file
---
> */

The patch for question2answer 1.6.3 can be downloaded here:

0001-qa-feed-excerpt-for-question2answer.patch.

Note that I copied the qa-feed.php to qa-feed-excerpt.php before this patch.

Leave a Reply

Your email address will not be published. Required fields are marked *