hyphenator-8.x-1.x-dev/hyphenator.module
hyphenator.module
<?php
/**
* @file
* Module hook implementations.
*/
/**
* Define hyphenator library url
*/
define('HYPHENATOR_WEBSITE_URL', 'https://github.com/mnater/hyphenator');
/**
* Implements hook_page_attachments().
*/
function hyphenator_page_attachments(array &$attachments) {
if (!hyphenator_get_library_path()) {
return;
}
$conf = \Drupal::config('hyphenator.settings');
// All the settings that are actually passed through into the hyphenator()
// function are contained in this array.
$options = [];
foreach (_hyphenator_get_js_default_config() as $key => $value) {
$option = $conf->get($key);
switch ($value['type']) {
case 'select':
case 'number':
$options[$key] = (int) $option;
break;
case 'boolean':
$options[$key] = ($option == 'true') ? TRUE : FALSE;
break;
default:
if (!empty($option)) {
$options[$key] = $option;
}
break;
}
}
$attachments['#attached']['library'][] = 'hyphenator/hyphenator.load';
$attachments['#attached']['library'][] = 'hyphenator/hyphenator';
$attachments['#attached']['drupalSettings']['hyphenator'] = [
'selector' => $conf->get('selector'),
'options' => $options,
'exceptions' => \Drupal::state()->get('hyphenator_exceptions', []),
];
}
/**
* Get the location of the hyphenator library.
*/
function hyphenator_get_library_path() {
// The following logic is taken from libraries_get_libraries()
$searchdir = [];
// Similar to 'modules' and 'themes' directories inside an installation
// profile, installation profiles may want to place libraries into a
// 'libraries' directory.
$searchdir[] = 'profiles/' . \Drupal::installProfile() . '/libraries';
// Always search sites/all/libraries.
$searchdir[] = 'sites/all/libraries';
// Always search libraries.
$searchdir[] = 'libraries';
// Also search sites/<domain>/*.
$searchdir[] = \Drupal::service('site.path') . '/libraries';
foreach ($searchdir as $dir) {
if (file_exists($dir . '/Hyphenator/Hyphenator.js')) {
return $dir . '/Hyphenator';
}
}
return FALSE;
}
/**
* Hold a structure reflecting the settings of Hyphenator.js.
*/
function _hyphenator_get_js_default_config() {
return [
'donthyphenateclassname' => [
'title' => 'Exclude class',
'type' => 'string',
'default' => 'donthyphenate',
'description' => t("A string containing the css-class-name for elements that should not be hyphenated. Default: 'donthyphenate'"),
],
'urlclassname' => [
'title' => 'URL class name',
'type' => 'string',
'default' => 'urlhyphenate',
'description' => t("URLs are automatically detected and Hyphenator.js inserts zero width spaces at good breaking points. Default: 'urlhyphenate'"),
],
'minwordlength' => [
'title' => 'Min word length',
'type' => 'number',
'default' => 6,
'description' => t('A number wich indicates the minimal length of words to hyphenate. Default: 6'),
],
'hyphenchar' => [
'title' => 'Hyphenchar',
'type' => 'string',
'default' => '',
'description' => t('To check hyphenation you can set hyphenchar to a visible character.'),
],
'urlhyphenchar' => [
'title' => 'URL hyphenchar',
'type' => 'string',
'default' => '',
'description' => t('When processing URLs and email-adresses a hyphen would invalidate the text.'),
],
'displaytogglebox' => [
'title' => 'Display togglebox',
'type' => 'boolean',
'default' => 'false',
'description' => t("If you want the user to be able to turn off an back on hyphenation, you can display the 'toggleBox'."),
],
'remoteloading' => [
'title' => 'Remote loading',
'type' => 'boolean',
'default' => 'true',
'description' => t('Hyphenator.js automatically loads the pattern files for each language, if they are available. Alternatively you can load the pattern files manually and disable the remote loading mechanism.'),
],
'enablecache' => [
'title' => 'Enable cache',
'type' => 'boolean',
'default' => 'true',
'description' => t('Hyphenator caches words that have been hyphenated to fasten execution.'),
],
'enablereducedpatternset' => [
'title' => 'Enable reduced pattern set',
'type' => 'boolean',
'default' => 'false',
'description' => t('This property is used by the reducePatternSet-tool. It stores all patterns used in a run in the language-object. They can be retrieved with the Hyphenator.getRedPatternSet(lang)-method.'),
],
'intermediatestate' => [
'title' => 'Intermediate state',
'type' => 'string',
'default' => 'hidden',
'description' => t("When a paragraph is hyphenated, the browser does a reflow of the layout, which you might like or not. Default: 'hidden'"),
],
'safecopy' => [
'title' => 'Safe copy',
'type' => 'boolean',
'default' => 'true',
'description' => t('This property this property tells Hyphenator.js if it should remove hyphenation from copied text.'),
],
'doframes' => [
'title' => 'Do frames',
'type' => 'boolean',
'default' => 'false',
'description' => t('Hyphenator does not automatically hyphenate text in frames nor iframes.'),
],
'storagetype' => [
'title' => 'Storage type',
'type' => 'string',
'default' => 'local',
'description' => t("To speed up Hyphenator.js the pattern files are stored in DOM storage, if the browser supports it. You can change this to sessionStorage (option 'session') or turn this feature off (by setting storagetype to 'none'. Default: 'local'"),
],
'orphancontrol' => [
'title' => 'Orphan control',
'type' => 'select',
'list' => [
1 => '1 (default): last word is hyphenated',
2 => '2: last word is not hyphenated',
3 => '3: last word is not hyphenated and last space is non breaking',
],
'default' => 1,
'description' => t('In some cases it may happen that one single syllable comes to the last line'),
],
'dohyphenation' => [
'title' => 'Do hyphenation',
'type' => 'boolean',
'default' => 'true',
'description' => t('Sometimes you like to run the script, prepare everything, but not to hyphenate yet. In this case you can set the option dohyphenation to false.'),
],
'persistentconfig' => [
'title' => 'Persistent config',
'type' => 'boolean',
'default' => 'false',
'description' => t('With the option persistentconfig set to true, all configuration options are stored in a object called Hyphenator_config in the storage type defined by the property storagetype.'),
],
'defaultlanguage' => [
'title' => 'Default language',
'type' => 'string',
'default' => '',
'description' => t("If you can't set a lang-attribute to the appropriate html-tag it may be interesting to set the default language."),
],
'useCSS3hyphenation' => [
'title' => 'Use CSS3 hyphenation',
'type' => 'boolean',
'default' => 'false',
'description' => t('In cases where CSS3-Hyphenation is supported by the browser, Hyphenator.js can activate it for the supported languages instead of computing the hyphenation points by itself.'),
],
'unhide' => [
'title' => 'Unhide',
'type' => 'string',
'default' => 'wait',
'description' => t("By default – if intermediatestate is set to the default hidden – Hyphenator.js waits until all elements are processed and finally unhides them. In some cases a progressive unhiding is more usefull. Default: 'wait'"),
],
];
}
/**
* Build an array of the language strings found in the Hyphenator.js library.
*/
function _hyphenator_get_available_languages() {
static $languages;
if (isset($languages)) {
return $languages;
}
$library_path = hyphenator_get_library_path();
$patterns = $library_path . '/patterns';
$languages = [];
if (is_dir($patterns)) {
if ($handle = opendir($patterns)) {
while (FALSE !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
$language = basename($entry, '.js');
$languages[$language] = $language;
}
}
closedir($handle);
}
}
asort($languages);
return $languages;
}
