-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbetter-typography.php
197 lines (170 loc) · 6.35 KB
/
better-typography.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<?php
namespace Grav\Plugin;
use Composer\Autoload\ClassLoader;
use Grav\Common\Grav;
use Grav\Common\Plugin;
use PHP_Typography\PHP_Typography;
use PHP_Typography\Settings;
use Twig_SimpleFilter;
/**
* Class BetterTypographyPlugin
* @package Grav\Plugin
*/
class BetterTypographyPlugin extends Plugin
{
/**
* @return array
*
* The getSubscribedEvents() gives the core a list of events
* that the plugin wants to listen to. The key of each
* array section is the event that the plugin listens to
* and the value (in the form of an array) contains the
* callable (or function) as well as the priority. The
* higher the number the higher the priority.
*/
public static function getSubscribedEvents(): array
{
return [
'onPluginsInitialized' => [
// Uncomment following line when plugin requires Grav < 1.7
// ['autoload', 100000],
['onPluginsInitialized', 0]
]
];
}
/**
* Composer autoload
*
* @return ClassLoader
*/
public function autoload(): ClassLoader
{
return require __DIR__ . '/vendor/autoload.php';
}
/**
* Initialize the plugin
*/
public function onPluginsInitialized(): void
{
// Don't proceed if we are in the admin plugin
if ($this->isAdmin()) {
return;
}
// Enable the main events we are interested in
$this->enable([
// Put your main events here
'onPageContentProcessed' => ['onPageContentProcessed', -20],
'onTwigInitialized' => ['onTwigInitialized', 0]
]);
}
public function onTwigInitialized(): void
{
$this->grav['twig']->twig()->addFilter(
new Twig_SimpleFilter('bettertypo', [$this, 'betterTypo'])
);
}
public function onPageContentProcessed(): void
{
$content = $this->grav['page']->content();
$content = $this->betterTypo($content);
$this->grav['page']->setRawContent($content);
}
/**
* languageList
*
* get all supported languages set in System / Languages
*
* @return array
*/
public static function languageList(): array
{
/** @var Grav */
$grav = Grav::instance();
/** @var Data */
$config = $grav['config'];
$languages = [
'default' => 'Default'
];
foreach ($config->get('system.languages.supported', []) as $language) {
$languages[$language] = $language;
}
return $languages;
}
/**
* maxLanguages
*
* @return int
*/
public static function maxLanguages(): int
{
/** @var Grav */
$grav = Grav::instance();
/** @var Data */
$config = $grav['config'];
return count($config->get('system.languages.supported', [])) + 1;
}
/**
* betterTypo
*
* @param string $string
* @param string (optional) $language
* @return string
*/
public function betterTypo(string $string, string $language = null): string
{
$PHPTypoSettings = new Settings(false);
$PHPTypoSettings->set_smart_ordinal_suffix(true);
$PHPTypoSettings->set_smart_ellipses(true);
$PHPTypoSettings->set_smart_marks(true);
$PHPTypoSettings->set_smart_exponents(true);
$PHPTypoSettings->set_smart_fractions(true);
$PHPTypoSettings->set_smart_area_units(true);
$PHPTypoSettings->set_single_character_word_spacing(true);
$PHPTypoSettings->set_fraction_spacing(true);
$PHPTypoSettings->set_unit_spacing(true);
$PHPTypoSettings->set_numbered_abbreviation_spacing(true);
$PHPTypoSettings->set_dewidow(true);
if (!$language) {
$language = $this->grav['page']->language() ?? $this->grav['language']->getLanguage();
if (!$language) {
$language = $this->grav['config']->get('site.default_lang');
}
}
$betterTypoSettings = [];
foreach ($this->config->get('plugins.better-typography.perLanguageSettings', []) as $perLanguageSettings) {
$betterTypoSettings[$perLanguageSettings['language']] = $perLanguageSettings;
}
$betterTypoLanguage = array_key_exists($language, $betterTypoSettings) ? $language : 'default';
$useSmartQuotes = $betterTypoSettings[$betterTypoLanguage]['useSmartQuotes'] ?? true;
$PHPTypoSettings->set_smart_quotes($useSmartQuotes);
if ($useSmartQuotes) {
$smartQuotesPrimary = $betterTypoSettings[$betterTypoLanguage]['smartQuotesStyle'] ?? 'doubleCurled';
$PHPTypoSettings->set_smart_quotes_primary($smartQuotesPrimary);
$smartQuotesSecondary = $betterTypoSettings[$betterTypoLanguage]['smartQuotesStyleSecondary'] ?? 'singleCurled';
$PHPTypoSettings->set_smart_quotes_secondary($smartQuotesSecondary);
}
$useSmartDashes = $betterTypoSettings[$betterTypoLanguage]['useSmartDashes'] ?? true;
$PHPTypoSettings->set_smart_dashes($useSmartDashes);
if ($useSmartDashes) {
$smartDashesStyle = $betterTypoSettings[$betterTypoLanguage]['smartDashesStyle'] ?? 'international';
$PHPTypoSettings->set_smart_dashes_style($smartDashesStyle);
}
$applyHyphenations = $betterTypoSettings[$betterTypoLanguage]['applyHyphenations'] ?? false;
$PHPTypoSettings->set_hyphenation($applyHyphenations);
if ($applyHyphenations) {
$PHPTypoSettings->set_hyphenation_language($language);
}
$applyFrenchSpecific = $betterTypoSettings[$betterTypoLanguage]['applyFrenchSpecific'] ?? false;
if ($applyFrenchSpecific && $language === 'fr') {
$PHPTypoSettings->set_french_punctuation_spacing(true);
$PHPTypoSettings->set_smart_ordinal_suffix_match_roman_numerals(true);
}
$useSmartDiacritics = $betterTypoSettings[$betterTypoLanguage]['useSmartDiacritics'] ?? false;
if ($useSmartDiacritics && $language === $betterTypoSettings[$betterTypoLanguage]['smartDiacriticsLanguage']) {
$PHPTypoSettings->set_smart_diacritics(true);
$PHPTypoSettings->set_diacritic_language($language);
}
$PHPTypo = new PHP_Typography();
return $PHPTypo->process($string, $PHPTypoSettings);
}
}