Skip to content

Commit 10226b4

Browse files
committed
update the orphans filter
1 parent 15c9172 commit 10226b4

File tree

6 files changed

+79
-1
lines changed

6 files changed

+79
-1
lines changed

src/DependencyInjection/Configuration.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ public function getConfigTreeBuilder()
6161
$srcset->children()->integerNode('image_width_threshold')->defaultValue(100);
6262
$srcset->children()->arrayNode('breakpoints')->prototype('scalar');
6363

64+
$typography = $rootNode->children()->arrayNode('typography')->addDefaultsIfNotSet();
65+
$typography->children()->scalarNode('orphans')->defaultNull();
66+
6467
return $treeBuilder;
6568

6669
}

src/DependencyInjection/KunstmaanExtraExtension.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ public function load(array $configs, ContainerBuilder $container)
7575
$loader->load('imagine_imgix_services.yml');
7676
}
7777

78+
$container->setParameter('kunstmaan_extra.typography.orphans', $configs['typography']['orphans']);
79+
7880
if ($configs['generate_controller']) {
7981
$container->setDefinition('kunstmaan_extra.typehinting_controller_cache_warmer', (new Definition)
8082
->setClass(TypehintingControllerCacheWarmer::class)

src/Resources/config/twig_extensions.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ services:
7474

7575
ars_thanea_kunstmaan_extra.twig.typography_twig_extension:
7676
class: '%ars_thanea_kunstmaan_extra.twig.typography_twig_extension.class%'
77+
arguments: ['%kunstmaan_extra.typography.orphans%']
7778
public: false
7879
tags:
7980
- { name: twig.extension }

src/Resources/doc/misc.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,22 @@ Replaces phases like `a cat` with `a cat` to keep them in the same line.
128128
}}
129129
```
130130

131+
This filter tries very hard not to break HTML. You can override the used regex with `kunstmaan_extra.typography.orphans` setting; the default is:
132+
133+
```regexp
134+
/
135+
\b # start at a word boundary
136+
(?<!\<) # exclude opening HTML tags
137+
( # first matching group is important for replace
138+
\w{1,2}\.? # orphan optionally followed by a period
139+
(?:
140+
<\/?\w+> # don’t let a closing HTML tag brake the match
141+
)*?
142+
)
143+
\s+ # match one or many whitespace characters
144+
/umx
145+
```
146+
131147
## AdminList Generator
132148

133149
To make routes work, add this entry to your bundle routing config:

src/Twig/TypographyTwigExtension.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,24 @@
44

55
class TypographyTwigExtension extends \Twig_Extension
66
{
7+
private $regex;
8+
9+
public function __construct($regex = null)
10+
{
11+
$this->regex = $regex ?: '/
12+
\b # start at a word boundary
13+
(?<!\<) # exclude opening HTML tags
14+
( # first matching group is important for replace
15+
\w{1,2}\.? # orphan optionally followed by a period
16+
(?:
17+
<\/?\w+> # don’t let a closing HTML tag brake the match
18+
)*?
19+
)
20+
\s+ # match one or many whitespace characters
21+
/umx';
22+
}
23+
24+
725
public function getFilters()
826
{
927
return [
@@ -20,7 +38,7 @@ public function orphans($text)
2038
{
2139
$nonBreakingSpace = ' ';
2240

23-
return preg_replace("/\\b(\\w{1,2}\\.?) /um", '$1' . $nonBreakingSpace, $text);
41+
return preg_replace($this->regex, '$1' . $nonBreakingSpace, $text);
2442
}
2543

2644

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace ArsThanea\KunstmaanExtraBundle\Typography;
4+
5+
use ArsThanea\KunstmaanExtraBundle\Twig\TypographyTwigExtension;
6+
7+
class TypographyTwigExtensionTest extends \PHPUnit_Framework_TestCase
8+
{
9+
/**
10+
* @param string $input
11+
* @param string $expected
12+
* @dataProvider data
13+
*/
14+
public function testOrphans($input, $expected)
15+
{
16+
$service = new TypographyTwigExtension();
17+
18+
$result = $service->orphans($input);
19+
20+
$this->assertEquals($expected, $result);
21+
}
22+
23+
public function data()
24+
{
25+
return [
26+
['this is an orphan', 'this is an orphan'],
27+
['<a href="http://foo.bar">baz a bar</a>', '<a href="http://foo.bar">baz a bar</a>'],
28+
['<a title="this is an orphan">an orphan</a>', '<a title="this is an orphan">an orphan</a>', ],
29+
['<span>this is</span><a> an orphan', '<span>this is</span><a> an orphan'],
30+
['<span>this is</span></a> an orphan', '<span>this is</span></a> an orphan'],
31+
['is <a href="foo">bar</a>', 'is <a href="foo">bar</a>'],
32+
['<span>this is</span></a> an orphan <span>other a</span> orphan', '<span>this is</span></a> an orphan <span>other a</span> orphan'],
33+
['this is an orphan', 'this is an orphan'],
34+
['10 000', '10 000'],
35+
['foo an bar', 'foo an bar'],
36+
];
37+
}
38+
}

0 commit comments

Comments
 (0)