Skip to content

Commit

Permalink
Merge branch 'release/1.1.11' into v1
Browse files Browse the repository at this point in the history
  • Loading branch information
khalwat committed Jun 13, 2018
2 parents ac69b6b + ff2b77d commit 2a59863
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Typogrify Changelog

## 1.1.11 - 2018.06.12
### Added
* Added `wordLimit` Twig filter/function that truncates a string by the number of words to a given length

## 1.1.10 - 2018.04.25
### Added
* Added the ability to set the the maximum number of words considered for dewidowing
Expand Down
24 changes: 22 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Typogrify prettifies your web typography by preventing ugly quotes and 'widows'

## Requirements

This plugin requires Craft CMS 3.0.0-RC1 or later.
This plugin requires Craft CMS 3.0.0 or later.

## Installation

Expand Down Expand Up @@ -229,6 +229,26 @@ Or:
{{ truncate(truncateOnWord, 20, '-') }}
```

**wordLimit** - Truncates a string by the number of words to a given length. It first strips any HTML tags from the string before performing truncation.

Usage:

```
{{ someString |wordLimit(20) }}
```

Or:

```
{{ craft.typogrify.wordLimit(someString, 20) }}
```

`wordLimit` also accepts an optional parameter that will be appended to the string if it is truncated (this defaults to '…'):

```
{{ wordLimit(someString, 20, '-') }}
```

**stringy** - Returns a new [Stringy](https://github.com/danielstjules/Stringy) object to your templates, so you can access all of the advanced string manipulation that [Stringy](https://github.com/danielstjules/Stringy) has to offer.

It does *not* strip HTML tags from the string, if you wish to do that, you can use the Twig [striptags](https://twig.symfony.com/doc/2.x/filters/striptags.html) filter.
Expand Down Expand Up @@ -365,7 +385,7 @@ Or:
```

In both examples, the number is `1` so the word would not be pluralized.

**singularize** - Converts a word to its singular form. For example, 'apples' will become 'apple', and 'children' will become 'child'

Usage:
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "nystudio107/craft-typogrify",
"description": "Typogrify prettifies your web typography by preventing ugly quotes and 'widows' and more",
"type": "craft-plugin",
"version": "1.1.10",
"version": "1.1.11",
"keywords": [
"craft",
"cms",
Expand All @@ -24,7 +24,7 @@
"minimum-stability": "dev",
"prefer-stable": true,
"require": {
"craftcms/cms": "^3.0.0-RC1",
"craftcms/cms": "^3.0.0",
"michelf/php-smartypants": "^1.8",
"mundschenk-at/php-typography": "^6.0"
},
Expand Down
17 changes: 17 additions & 0 deletions src/services/TypogrifyService.php
Original file line number Diff line number Diff line change
Expand Up @@ -355,4 +355,21 @@ public function transliterate(string $string, $transliterator = null): string
{
return Inflector::transliterate($string, $transliterator);
}

/**
* Limits a string by word count. If $substring is provided, and truncating occurs, the
* string is further truncated so that the substring may be appended without
* exceeding the desired length.
*
* @param string $string
* @param int $length
* @param string $substring
*
* @return string
*/
public function wordLimit(string $string, int $length, string $substring = ''): string
{
$words = preg_split("/[\s]+/", strip_tags($string) );
return implode(" ", array_slice($words, 0, $length) ) . $substring;
}
}
18 changes: 18 additions & 0 deletions src/twigextensions/TypogrifyTwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public function getFilters()
new \Twig_SimpleFilter('pluralize', [$this, 'pluralize']),
new \Twig_SimpleFilter('singularize', [$this, 'singularize']),
new \Twig_SimpleFilter('transliterate', [$this, 'transliterate']),
new \Twig_SimpleFilter('wordLimit', [$this, 'wordLimit']),
];
}

Expand All @@ -76,6 +77,7 @@ public function getFunctions()
new \Twig_SimpleFunction('pluralize', [$this, 'pluralize']),
new \Twig_SimpleFunction('singularize', [$this, 'singularize']),
new \Twig_SimpleFunction('transliterate', [$this, 'transliterate']),
new \Twig_SimpleFunction('wordLimit', [$this, 'wordLimit']),
];
}

Expand Down Expand Up @@ -300,4 +302,20 @@ public function transliterate(string $string, $transliterator = null)
{
return Template::raw(Typogrify::$plugin->typogrify->transliterate($string, $transliterator));
}

/**
* Limits a string by word count. If $substring is provided, and truncating occurs, the
* string is further truncated so that the substring may be appended without
* exceeding the desired length.
*
* @param string $string
* @param int $length
* @param string $substring
*
* @return string
*/
public function wordLimit(string $string, int $length, string $substring = '')
{
return Template::raw(Typogrify::$plugin->typogrify->wordLimit($string, $length, $substring));
}
}
16 changes: 16 additions & 0 deletions src/variables/TypogrifyVariable.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,4 +245,20 @@ public function transliterate(string $string, $transliterator = null)
{
return Template::raw(Typogrify::$plugin->typogrify->transliterate($string, $transliterator));
}

/**
* Limits a string by word count. If $substring is provided, and truncating occurs, the
* string is further truncated so that the substring may be appended without
* exceeding the desired length.
*
* @param string $string
* @param int $length
* @param string $substring
*
* @return string
*/
public function wordLimit(string $string, int $length, string $substring = '')
{
return Template::raw(Typogrify::$plugin->typogrify->wordLimit($string, $length, $substring));
}
}

0 comments on commit 2a59863

Please sign in to comment.