`'s are automatically allowed when hacking by paragraph._
54 |
55 | ```
56 | {{ entry.richTextField|hacksaw(limit='10', allow='') }}
57 | ```
58 |
59 | **Note:** If you are including HTML in the append parameter, the elements must be present in the `allow` parameter. If you are including a Craft variable in any parameter, it must be added using the Twig concatenation operator, `~`. Example of both:
60 |
61 | ```
62 | {{ entry.richTextField|hacksaw(hack='w', limit='100', allow='', append='Continue...') }}
63 | ```
64 |
65 | Brought to you by [Ryan Shrum](http://ryanshrum.com)
66 |
--------------------------------------------------------------------------------
/composer.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "ryanshrum/hacksaw",
3 | "description": "A simple text truncation plugin for Craft CMS.",
4 | "type": "craft-plugin",
5 | "authors": [
6 | {
7 | "name": "Ryan Shrum",
8 | "homepage": "http://ryanshrum.com"
9 | }
10 | ],
11 | "require": {
12 | "composer/installers": "~1.0"
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/releases.json:
--------------------------------------------------------------------------------
1 | [
2 | {
3 | "version": "2.0.0",
4 | "downloadUrl": "https://github.com/ryanshrum/hacksaw/archive/master.zip",
5 | "date": "2016-09-13T01:04:38.366Z",
6 | "notes": [
7 | "[Added] Initial release"
8 | ]
9 | }
10 | ]
11 |
--------------------------------------------------------------------------------
/resources/icon-mask.svg:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/resources/icon.svg:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/twigextensions/HacksawTwigExtension.php:
--------------------------------------------------------------------------------
1 | new \Twig_Filter_Method($this, 'hacksaw', [
33 | 'is_safe' => array('html')
34 | ]),
35 | );
36 | }
37 |
38 | public function hacksaw($content, $hack = 'p', $limit = 1, $allow = null, $append = null)
39 | {
40 | if ($hack == 'c' || $hack == 'chars' || $hack == 'characters')
41 | {
42 | $clean_content = $this->cleanHtml($content, $allow);
43 |
44 | if (mb_strlen($clean_content) <= $limit)
45 | {
46 | $return = $clean_content;
47 | }
48 | else
49 | {
50 | $return = preg_replace('/\s+?(\S+)?$/u', '', mb_substr($clean_content, 0, $limit)) . $append;
51 | }
52 |
53 | return $this->closeTags($return);
54 |
55 | }
56 | else if ($hack == 'w' || $hack == 'words')
57 | {
58 | $clean_content = $this->cleanHtml($content, $allow);
59 |
60 | if (str_word_count($clean_content) <= $limit)
61 | {
62 | $return = $clean_content;
63 | }
64 | else
65 | {
66 | $word_count = str_word_count($clean_content, 0);
67 |
68 | if ($word_count > $limit)
69 | {
70 | $words = preg_split('/\s+/u', $clean_content);
71 | $clean_content = implode(' ', array_slice($words, 0, $limit));
72 | $return = $clean_content;
73 |
74 | if (preg_match("/[0-9.!?,;:]$/u", $clean_content))
75 | {
76 | $return = mb_substr($clean_content, 0, -1);
77 | }
78 |
79 | $return .= $append;
80 | }
81 | }
82 |
83 | return $this->closeTags($return);
84 | }
85 | else if ($hack = 'p' || $hack == 'paragraphs')
86 | {
87 | $clean_content = $this->cleanHtml($content, $allow . " ");
88 | $paragraphs = array_filter(explode(" ", str_replace(" ";
93 |
94 | foreach ($paragraphs as $key => $paragraph)
95 | {
96 | $return .= " " . $paragraph;
97 |
98 | if ($key < $paragraphs_count)
99 | {
100 | $return .= "