Skip to content

Commit

Permalink
Merge pull request coduo#61 from thunderer/string-shortcode-removal
Browse files Browse the repository at this point in the history
Shortcode removal utilities
  • Loading branch information
Norbert Orzechowicz committed Nov 21, 2015
2 parents a78a552 + e9087c8 commit 6bed680
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 11 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"symfony/intl": "~2.3",
"symfony/config": "~2.3",
"symfony/translation": "~2.3",
"symfony/yaml": "~2.3"
"symfony/yaml": "~2.3",
"thunderer/shortcode": "~0.5"
},
"require-dev": {
"phpspec/phpspec": "2.0.*",
Expand Down
25 changes: 20 additions & 5 deletions src/Coduo/PHPHumanizer/String.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Coduo\PHPHumanizer;

use Coduo\PHPHumanizer\String\Humanize;
use Coduo\PHPHumanizer\String\ShortcodeProcessor;
use Coduo\PHPHumanizer\String\TextTruncate;
use Coduo\PHPHumanizer\String\HtmlTruncate;
use Coduo\PHPHumanizer\String\WordBreakpoint;
Expand All @@ -14,7 +15,7 @@ class String
* @param bool|true $capitalize
* @param string $separator
* @param array $forbiddenWords
*
*
* @return string
*/
public static function humanize($text, $capitalize = true, $separator = '_', array $forbiddenWords = array())
Expand All @@ -26,13 +27,13 @@ public static function humanize($text, $capitalize = true, $separator = '_', arr
* @param $text
* @param $charactersCount
* @param string $append
*
*
* @return string
*/
public static function truncate($text, $charactersCount, $append = '')
{
$truncate = new TextTruncate(new WordBreakpoint(), $append);

return $truncate->truncate($text, $charactersCount);
}

Expand All @@ -41,13 +42,27 @@ public static function truncate($text, $charactersCount, $append = '')
* @param $charactersCount
* @param string $allowedTags
* @param string $append
*
*
* @return string
*/
public static function truncateHtml($text, $charactersCount, $allowedTags = '', $append = '')
{
$truncate = new HtmlTruncate(new WordBreakpoint(), $allowedTags, $append);

return $truncate->truncate($text, $charactersCount);
}

public static function removeShortcodes($text)
{
$processor = new ShortcodeProcessor();

return $processor->removeShortcodes($text);
}

public static function removeShortcodeTags($text)
{
$processor = new ShortcodeProcessor();

return $processor->removeShortcodeTags($text);
}
}
51 changes: 51 additions & 0 deletions src/Coduo/PHPHumanizer/String/ShortcodeProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Coduo\PHPHumanizer\String;

use Thunder\Shortcode\HandlerContainer\HandlerContainer;
use Thunder\Shortcode\Parser\RegularParser;
use Thunder\Shortcode\Processor\Processor;
use Thunder\Shortcode\Shortcode\ShortcodeInterface;

class ShortcodeProcessor
{
/**
* Removes all shortcodes from given text
*
* @param string $text
*
* @return string
*/
public function removeShortcodes($text)
{
$nullHandler = function() {
return null;
};

return $this->createShortcodeProcessor($nullHandler)->process($text);
}

/**
* Removes only shortcode tags from given text (leaves their content as it is)
*
* @param string $text
*
* @return string
*/
public function removeShortcodeTags($text)
{
$contentHandler = function(ShortcodeInterface $s) {
return $s->getContent();
};

return $this->createShortcodeProcessor($contentHandler)->process($text);
}

private function createShortcodeProcessor($defaultHandler)
{
$handlers = new HandlerContainer();
$handlers->setDefault($defaultHandler);

return new Processor(new RegularParser(), $handlers);
}
}
47 changes: 42 additions & 5 deletions tests/Coduo/PHPHumanizer/Tests/StringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,44 @@ function test_truncate_string_to_word_closest_to_a_certain_number_of_characters_
}

/**
* @dataProvider removeAllShortcodesProvider
*
* @param $text
* @param $expected
*/
function test_remove_all_shortcodes_from_text($text, $expected)
{
$this->assertEquals($expected, String::removeShortcodes($text));
}

/**
* @dataProvider removeShortcodeTagsProvider
*
* @param $text
* @param $expected
*/
function test_remove_only_shortcode_tags_from_text($text, $expected)
{
$this->assertEquals($expected, String::removeShortcodeTags($text));
}

public function removeAllShortcodesProvider()
{
return array(
array('some [text] containing [shortcodes /] and [stuff]with[/stuff] content', 'some containing and content'),
array('some [text] containing [shortcodes /] and [stuff]with[/stuff] content [/text]', 'some '),
);
}

public function removeShortcodeTagsProvider()
{
return array(
array('some [text] containing [shortcodes /] and [stuff]with[/stuff] content', 'some containing and with content'),
array('some [text] containing [shortcodes /] and [stuff]with[/stuff] content [/text]', 'some containing and with content '),
);
}

/**
* @return array
*/
public function humanizeStringProvider()
Expand All @@ -73,7 +110,7 @@ public function truncateStringProvider()
{
$longText = 'Lorem ipsum dolorem si amet, lorem ipsum. Dolorem sic et nunc.';
$shortText = 'Short text';

return array(
array($longText, 'Lorem', 2),
array($longText, 'Lorem ipsum...', 10, '...'),
Expand All @@ -93,19 +130,19 @@ public function truncateStringProvider()
array($shortText, "Short text", 10, '...')
);
}

/**
*
* @return array
*/
public function truncateHtmlStringProvider()
{
$text = '<p><b>HyperText Markup Language</b>, commonly referred to as <b>HTML</b>, is the standard <a href="/wiki/Markup_language" title="Markup language">markup language</a> used to create <a href="/wiki/Web_page" title="Web page">web pages</a>.<sup id="cite_ref-1" class="reference"><a href="#cite_note-1"><span>[</span>1<span>]</span></a></sup> <a href="/wiki/Web_browser" title="Web browser">Web browsers</a> can read HTML files and render them into visible or audible web pages. HTML describes the structure of a <a href="/wiki/Website" title="Website">website</a> <a href="/wiki/Semantic" title="Semantic" class="mw-redirect">semantically</a> along with cues for presentation, making it a markup language, rather than a <a href="/wiki/Programming_language" title="Programming language">programming language</a>.</p>';

return array(
array($text, 3, '<b><i><u><em><strong><a><span>', "<b>HyperText</b>"),
array($text, 3, '<b><i><u><em><strong><a><span>', "<b>HyperText</b>"),
array($text, 12, '<b><i><u><em><strong><a><span>', "<b>HyperText Markup</b>"),
array($text, 30, '<b><i><u><em><strong><a><span>', "<b>HyperText Markup Language</b>, commonly"),
array($text, 30, '<b><i><u><em><strong><a><span>', "<b>HyperText Markup Language</b>, commonly"),
array($text, 50, '<b><i><u><em><strong><a><span>', "<b>HyperText Markup Language</b>, commonly referred to as"),
array($text, 75, '<b><i><u><em><strong><a><span>', '<b>HyperText Markup Language</b>, commonly referred to as <b>HTML</b>, is the standard <a href="/wiki/Markup_language" title="Markup language">markup</a>'),
array($text, 100,'<b><i><u><em><strong><a><span>', '<b>HyperText Markup Language</b>, commonly referred to as <b>HTML</b>, is the standard <a href="/wiki/Markup_language" title="Markup language">markup language</a> used to create'),
Expand Down

0 comments on commit 6bed680

Please sign in to comment.