diff options
author | Joel Uckelman <uckelman@nomic.net> | 2012-02-27 01:46:09 +0100 |
---|---|---|
committer | Joel Uckelman <uckelman@nomic.net> | 2012-02-27 01:46:09 +0100 |
commit | 4988452740e4d8ae347570774f8aaece4372307d (patch) | |
tree | e7a439dd6ac703987c9140f256efd89280a47291 | |
parent | 423d844bc80d5157a23466248f328af99a21b91d (diff) |
Added built_post_subject for stripping tags and Re's added by mailing lists.
-rw-r--r-- | src/build_post.php | 29 | ||||
-rw-r--r-- | test/build_post_test.php | 34 |
2 files changed, 63 insertions, 0 deletions
diff --git a/src/build_post.php b/src/build_post.php new file mode 100644 index 0000000..6d95be1 --- /dev/null +++ b/src/build_post.php @@ -0,0 +1,29 @@ +<?php + +function build_tag_pattern($tag) { + return '/' . preg_quote($tag, '/') . '\\s*/'; +} + +function build_post_subject($listtag, $forumtag, $subject) { + // strip the '[list]' and '[forum]' tags + $tagpat = '/(' . preg_quote($listtag, '/') . + '|' . preg_quote($forumtag, '/') . ')\\s*/'; + $subject = preg_replace($tagpat, '', $subject); + + // strip leading sequences of Re-equivalents + if (preg_match( + '/^((RE|AW|SV|VS)(\\[\\d+\\])?:\\s*)+/i', + $subject, $m, PREG_OFFSET_CAPTURE + )) { + $subject = substr($subject, $m[0][1]); + } + + // ensure nonempty subject + if (trim($subject) == '') { + $subject = '(no subject)'; + } + + return $subject; +} + +?> diff --git a/test/build_post_test.php b/test/build_post_test.php new file mode 100644 index 0000000..72e28a7 --- /dev/null +++ b/test/build_post_test.php @@ -0,0 +1,34 @@ +<?php + +require_once('PHPUnit/Framework.php'); + +require_once(__DIR__ . '/../src/build_post.php'); + +class build_post_test extends PHPUnit_Framework_TestCase { + + /* @dataProvider buildPostSubjectProvider */ + public function testBuildPostSubject($ltag, $ftag, $subject, $expected) { + $this->assertEquals( + build_post_subject($listtag, $forumtag, $subject), + $expected + ); + } + + public function builtPostSubjectProvider() { + return array( + array('[l]', '[f]', '', '(no subject)'), + array('[l]', '[f]', 'Re:', '(no subject)'), + array('[l]', '[f]', 'Subject', 'Subject'), + array('[l]', '[f]', 'Re: Subject', 'Subject'), + array('[l]', '[f]', 'Re: Re: Re: Subject', 'Subject'), + array('[l]', '[f]', '[f] Subject', 'Subject'), + array('[l]', '[f]', '[f] [f] Subject', 'Subject'), + array('[l]', '[f]', '[f] [f] Subject [f]', 'Subject'), + array('[l]', '[f]', '[l] [f] Re: Subject', 'Subject'), + array('[l]', '[f]', 'Re: [l] [f] Subject', 'Subject'), + array('[l]', '[f]', 'Re: Subject [l][f] Subject', 'Subject Subject') + ); + } +} + +?> |