-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTypographyExtension.php
83 lines (74 loc) · 2.34 KB
/
TypographyExtension.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
namespace Parisek\Twig;
use PHP_Typography\Settings;
use PHP_Typography\PHP_Typography;
use Symfony\Component\Yaml\Yaml;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
final class TypographyExtension extends AbstractExtension {
protected $settings_file_path = __DIR__ . '/typography.yml';
public function __construct($settings_file_path = '') {
if(!empty($settings_file_path)) {
$this->settings_file_path = $settings_file_path;
}
}
public function getFilters() {
return [
new TwigFilter('typography', [
$this,
'applyTypography',
], [
'is_safe' => [
'html'
]
]),
];
}
/**
* Runs the PHP-Typography.
*
* @param string $string
* The string of text to apply the filter to.
* @param array $arguments
* An optional array containing the settings for the typography library.
* This should be set as a hash (key value pair) in twig template.
* @param bool $use_defaults
* - TRUE: a sane set of defaults are loaded.
* - FALSE: settings will need to be passed in and no defaults will
* be applied.
*
* @return string
* A processed and filtered string to return to the template.
*
* @throws \Exception
* An exception is thrown if a string is not passed.
*/
public function applyTypography($string, array $arguments = [], $use_defaults = TRUE) {
$settings = new Settings($use_defaults);
// Load the defaults from the theme and merge them with any
// supplied arguments from the calling function in the template.
$arguments = array_merge(self::getDefaults(), $arguments);
// Process the arguments and add them to the settings object.
foreach ($arguments as $setting => $value) {
$settings->{$setting}($value);
}
$typo = new PHP_Typography();
// Process the string with any provided arguments (and/or defaults) and
// return it.
$string = $typo->process($string, $settings);
return $string;
}
/**
* Gets defaults from a YAML file if it exists.
*
* @return array
* A set of defaults loaded from a YAML file if found.
*/
private function getDefaults() {
$defaults = [];
if (file_exists($this->settings_file_path)) {
$defaults = (array) Yaml::parse(file_get_contents($this->settings_file_path));
}
return $defaults;
}
}