Initial commit

This commit is contained in:
isUnknown 2026-02-12 15:22:46 +01:00
commit 65e0da7e11
1397 changed files with 596542 additions and 0 deletions

View file

@ -0,0 +1,79 @@
<?php
/**
* This file is part of Kirby-Typography. A port of wp-Typography
* for Kirby CMS (https://getkirby.com).
*
* Copyright of Kirby-Typography:
* 2016 Fabian Michael.
*
* Copyright of wp-Typography (included in this package):
* 2014-2016 Peter Putzer.
* 2012-2013 Marie Hogebrandt.
* 2009-2011 KINGdesk, LLC.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* ***
*
* @package Kirby\Plugings\Typography
* @author Fabian Michael <hallo@fabianmichael.de>
* @license http://www.gnu.org/licenses/gpl-2.0.html
*/
namespace Kirby\Plugins\Typography;
use Cache\Driver\File as FileCache;
use Dir;
use F;
class Cache {
protected $cacheRoot;
protected $driver;
protected function __construct() {
$this->cacheRoot = kirby()->roots()->cache() . DS . 'plugins' . DS . 'typography';
if (!f::exists($this->cacheRoot)) {
dir::make($this->cacheRoot);
}
$this->driver = new FileCache($this->cacheRoot);
}
public static function instance() {
static $instance;
return $instance ?: $instance = new self();
}
public function driver() {
return $this->driver;
}
public function root() {
return $this->cacheRoot;
}
public function status() {
return [
'size' => dir::size($this->cacheRoot),
'files' => sizeof(dir::read($this->cacheRoot)),
];
}
public function __call($name, $arguments) {
return call_user_func_array([$this->driver, $name], $arguments);
}
}

View file

@ -0,0 +1,215 @@
<?php
/**
* This file is part of Kirby-Typography. A port of wp-Typography
* for Kirby CMS (https://getkirby.com).
*
* Copyright of Kirby-Typography:
* 2016 Fabian Michael.
*
* Copyright of wp-Typography (included in this package):
* 2014-2016 Peter Putzer.
* 2012-2013 Marie Hogebrandt.
* 2009-2011 KINGdesk, LLC.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* ***
*
* @package Kirby\Plugings\Typography
* @author Fabian Michael <hallo@fabianmichael.de>
* @license http://www.gnu.org/licenses/gpl-2.0.html
*/
namespace Kirby\Plugins\Typography\Component;
use Kirby\Plugins\Typography\KirbyTypography;
use Kirby\Plugins\Typography\Cache;
use Field;
use L;
class Typography extends \Kirby\Component\Smartypants {
public $version = '1.0.0-beta1';
public function defaults() {
return [
'typography' => true,
'typography.debug' => false,
// general attributes
'typography.ignore.tags' => ['code', 'head', 'kbd', 'object',
'option', 'pre', 'samp', 'script',
'noscript', 'noembed', 'select',
'style', 'textarea', 'title',
'var', 'math'],
'typography.ignore.classes' => [ 'vcard', 'typography--off' ],
'typography.ignore.ids' => [],
// smart characters
'typography.quotes' => true,
'typography.quotes.primary' => 'doubleCurled',
'typography.quotes.secondary' => 'singleCurled',
'typography.dashes' => true,
'typography.dashes.style' => 'em',
'typography.dashes.spacing' => true,
'typography.diacritics' => false,
'typography.diacritics.language' => 'en-US',
'typography.diacritics.custom' => [],
'typography.ellipses' => true,
'typography.marks' => true,
'typography.ordinal.suffix' => true,
'typography.math' => true,
'typography.fractions' => true,
'typography.exponents' => true,
// smart spacing
'typography.wordspacing.singlecharacter' => true,
'typography.fraction.spacing' => true,
'typography.punctuation.spacing.french' => false,
'typography.units.spacing' => true,
'typography.units.custom' => [],
'typography.dewidow' => true,
'typography.dewidow.maxlength' => 5,
'typography.dewidow.maxpull' => 5,
'typography.wrap.hardhyphens' => true,
'typography.wrap.url' => true,
'typography.wrap.email' => true,
'typography.wrap.url.minafter' => 5,
'typography.domains.custom' => ['local', // local network links
'onion', 'exit', 'tor', // TOR Project
'test', 'example',
],
'typography.space.collapse' => true,
'typography.space.nobreak.narrow' => false,
// character styling
'typography.style.ampersands' => false,
'typography.style.caps' => true,
'typography.style.numbers' => false,
// hanging punctuation
'typography.style.punctuation.hanging' => true,
'typography.style.quotes.initial' => true,
'typography.style.quotes.initial.tags' => ['p', 'h1', 'h2', 'h3', 'h4',
'h5', 'h6', 'blockquote',
'li', 'dd', 'dt'],
// hyphenation
'typography.hyphenation' => true,
'typography.hyphenation.language' => 'en-US',
'typography.hyphenation.minlength' => 7,
'typography.hyphenation.minbefore' => 3,
'typography.hyphenation.minafter' => 2,
'typography.hyphenation.headings' => true,
'typography.hyphenation.allcaps' => true,
'typography.hyphenation.titlecase' => true,
'typography.hyphenation.compounds' => true,
'typography.hyphenation.exceptions' => [],
// css classes
'typography.class.ampersand' => 'char--ampersand',
'typography.class.caps' => 'char--caps',
'typography.class.numbers' => 'char--numbers',
'typography.class.fraction.numerator' => 'frac--numerator',
'typography.class.fraction.denominator' => 'frac--denominator',
'typography.class.quote.initial.single' => 'char--singleQuoteInitial',
'typography.class.quote.initial.double' => 'char--doubleQuoteInitial',
'typography.class.push.single' => 'push--singleQuote',
'typography.class.push.double' => 'push--doubleQuote',
'typography.class.pull.single' => 'pull--singleQuote',
'typography.class.pull.double' => 'pull--doubleQuote',
'typography.class.ordinal' => 'char--ordinalSuffix',
];
}
protected $settingsSerialized;
protected $typo;
protected $_localized = false;
protected function hash($text) {
if (is_null($this->settingsSerialized)) {
$settings = ['typography.version' => $this->version];
foreach ($this->defaults() as $key => $val) {
$settings[$key] = $this->kirby->options[$key];
}
$this->settingsSerialized = serialize($settings);
}
return md5($this->settingsSerialized . $text);
}
public function typography() {
if (!$this->typo) {
$this->typo = new KirbyTypography(false);
}
return $this->typo;
}
protected function localize() {
if ($this->_localized) return; // this method can only be called once
if ($this->kirby->site->multilang()) {
$language = l::get();
foreach (array_keys($this->defaults()) as $option) {
if (isset($language[$option])) {
// override defaults and settings from config file, if localized setting is available
$this->kirby->options[$option] = $language[$option];
}
}
}
$this->_localized = true;
}
protected function processText($text) {
$typo = $this->typography();
return $typo->process($text);
}
public function parse($text, $force = false, Field $field = NULL) {
$this->localize(); // cannot use the `configure` method to do this, because it gets executed before languages are loaded
if (!$this->kirby->option('typography')) {
return $text;
} else {
if ($this->kirby->option('typography.debug')) {
// Skip caching when in debug mode
return $this->processText($text);
}
$cache = Cache::instance();
$cacheKey = $this->hash($text);
$parsedText = $cache->get($cacheKey, false);
if ($parsedText === false) {
$parsedText = $this->processText($text);
$cache->set($cacheKey, $parsedText);
}
return $parsedText;
}
}
}

View file

@ -0,0 +1,187 @@
<?php
namespace Kirby\Plugins\Typography;
use PHP_Typography\PHP_Typography;
class KirbyTypography extends PHP_Typography {
protected $domainListUrl = 'http://data.iana.org/TLD/tlds-alpha-by-domain.txt';
protected $domainListCacheLifetime = 10080; // one week in minutes
protected $dashStylesMap = [
'en' => 'international',
'em' => 'traditionalUS',
];
private $cssClassKeys = [
'typography.class.caps' => 'caps',
'typography.class.numbers' => 'numbers',
'typography.class.ampersand' => 'amp',
'typography.class.quote.initial.single' => 'quo',
'typography.class.quote.initial.double' => 'dquo',
'typography.class.push.single' => 'push-single',
'typography.class.push.double' => 'push-double',
'typography.class.pull.single' => 'pull-single',
'typography.class.pull.double' => 'pull-double',
'typography.class.fraction.numerator' => 'numerator',
'typography.class.fraction.denominator' => 'denominator',
'typography.class.ordinal' => 'ordinal',
];
protected $kirby;
function __construct($set_defaults = true, $init = 'now') {
$this->kirby = kirby();
// ignore arguments of the constructor
parent::__construct(true);
$o = $this->kirby->options;
// general attributes
$this->set_tags_to_ignore($o['typography.ignore.tags']);
$this->set_classes_to_ignore($o['typography.ignore.classes']);
$this->set_ids_to_ignore($o['typography.ignore.ids']);
// smart characters
$this->set_smart_quotes($o['typography.quotes']);
$this->set_smart_quotes_primary($o['typography.quotes.primary']);
$this->set_smart_quotes_secondary($o['typography.quotes.secondary']);
$this->set_smart_dashes($o['typography.dashes']);
$this->set_smart_dashes_style($o['typography.dashes.style']);
$this->set_dash_spacing($o['typography.dashes.spacing']);
$this->set_smart_diacritics($o['typography.diacritics']);
$this->set_diacritic_language($o['typography.diacritics.language']);
$this->set_diacritic_custom_replacements($o['typography.diacritics.custom']);
$this->set_smart_ellipses($o['typography.ellipses']);
$this->set_smart_marks($o['typography.marks']);
$this->set_smart_ordinal_suffix($o['typography.ordinal.suffix']);
$this->set_smart_math($o['typography.math']);
$this->set_smart_fractions($o['typography.fractions']);
$this->set_smart_exponents($o['typography.exponents']);
// smart spacing
$this->set_single_character_word_spacing($o['typography.wordspacing.singlecharacter']);
$this->set_fraction_spacing($o['typography.fraction.spacing']);
$this->set_french_punctuation_spacing($o['typography.punctuation.spacing.french']);
$this->set_unit_spacing($o['typography.units.spacing']);
$this->set_units($o['typography.units.custom']);
$this->set_dewidow($o['typography.dewidow']);
$this->set_max_dewidow_length($o['typography.dewidow.maxlength']);
$this->set_max_dewidow_pull($o['typography.dewidow.maxpull']);
$this->set_wrap_hard_hyphens($o['typography.wrap.hardhyphens']);
$this->set_url_wrap($o['typography.wrap.url']);
$this->set_email_wrap($o['typography.wrap.email']);
$this->set_min_after_url_wrap($o['typography.wrap.url.minafter']);
$this->set_space_collapse($o['typography.space.collapse']);
$this->set_true_no_break_narrow_space($o['typography.space.nobreak.narrow']);
// character styling
$this->set_style_ampersands($o['typography.style.ampersands']);
$this->set_style_caps($o['typography.style.caps']);
$this->set_style_numbers($o['typography.style.numbers']);
$this->set_style_hanging_punctuation($o['typography.style.punctuation.hanging']);
$this->set_style_initial_quotes($o['typography.style.quotes.initial']);
$this->set_initial_quote_tags($o['typography.style.quotes.initial.tags']);
// hyphenation
$this->set_hyphenation($o['typography.hyphenation']);
$this->set_hyphenation_language($o['typography.hyphenation.language']);
$this->set_min_length_hyphenation($o['typography.hyphenation.minlength']);
$this->set_min_before_hyphenation($o['typography.hyphenation.minbefore']);
$this->set_min_after_hyphenation($o['typography.hyphenation.minafter']);
$this->set_hyphenate_headings($o['typography.hyphenation.headings']);
$this->set_hyphenate_all_caps($o['typography.hyphenation.allcaps']);
$this->set_hyphenate_title_case($o['typography.hyphenation.titlecase']);
$this->set_hyphenate_compounds($o['typography.hyphenation.compounds']);
$this->set_hyphenation_exceptions($o['typography.hyphenation.exceptions']);
// set css classes
foreach($this->cssClassKeys as $optionKey => $classKey) {
$this->css_classes[$classKey] = $o[$optionKey];
}
}
/**
* Sets the typographical conventions used by smart_dashes.
*
* Allowed values for $style:
* - "em" (alias: "traditionalUS")
* - "en" (alias: "international")
*
* @param string $style Optional. Default "en".
*/
public function set_smart_dashes_style( $style = 'en' ) {
if (isset($this->dashStylesMap[$style])) {
// Translate dash styles for PHP-Typography
$style = $this->dashStylesMap[$style];
}
return parent::set_smart_dashes_style($style);
}
/**
* Try to fetch a list of top-level domains from the IANA. If fetching of that
* file fails, load a list of top-level domains from a file.
*
* @param string $path The full path and filename.
* @return string A list of top-level domains concatenated with '|'.
*/
function get_top_level_domains_from_file($path) {
$cache = Cache::instance();
$cacheKey = 'domain-list-' . md5($this->domainListUrl);
$cacheKeyLastAttempt = 'domain-list-last-attempt';
$cacheKeyMaxFetchInterval = 30;
$domains = $cache->get($cacheKey);
if (!$domains) {
$lastAttempt = $cache->get($cacheKeyLastAttempt, false);
if (!$lastAttempt || $lastAttempt !== $this->domainListUrl) { // only retry, if last attempt to fetch the domain list failed.
if ($domainListTextFileContents = @file_get_contents($this->domainListUrl)) {
$domains = [];
foreach (explode("\n", $domainListTextFileContents) as $line) {
if ( preg_match('#^[a-zA-Z0-9][a-zA-Z0-9-]*$#', $line, $matches ) ) {
$domains[] = strtolower( $matches[0] );
}
}
if (sizeof($domains) > 0) {
$domains = implode('|', $domains);
$cache->set($cacheKey, $domains, $this->domainListCacheLifetime);
} else {
$domains = null;
}
}
}
}
if (!$domains) {
$cache->set($cacheKeyLastAttempt, $this->domainListUrl, $cacheKeyMaxFetchInterval);
// Fallback to local copy of IANA domains
$domains = parent::get_top_level_domains_from_file($path);
}
if (is_array($this->kirby->options['typography.domains.custom']) &&
sizeof($this->kirby->options['typography.domains.custom']) > 0) {
$domains .= '|' . implode('|', $this->kirby->options['typography.domains.custom']);
}
return $domains;
}
protected function getClassAttr($str = '') {
return (!empty($str) ? ' class="' . $str . '"' : '');
}
}