-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ilyas Ronef
committed
Feb 20, 2014
0 parents
commit b87c06e
Showing
15 changed files
with
1,557 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# ddTypograph | ||
|
||
Visit the following [link](http://code.divandesign.biz/modx/ddtypograph) to read the documentation, instructions & changelog. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
/** | ||
* Jare_Exception | ||
* | ||
* @copyright Copyright (c) 2009 E.Muravjev Studio (http://emuravjev.ru) | ||
* @license http://emuravjev.ru/works/tg/eula/ | ||
* @version 2.0.0 | ||
* @author Arthur Rusakov <arthur@emuravjev.ru> | ||
* @category Jare | ||
* @package Jare_Typograph | ||
*/ | ||
class Jare_Exception extends Exception | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
<?php | ||
/** | ||
* Jare_Typograph | ||
* | ||
* @copyright Copyright (c) 2009 E.Muravjev Studio (http://emuravjev.ru) | ||
* @license http://emuravjev.ru/works/tg/eula/ | ||
* @version 2.0.0 | ||
* @author Arthur Rusakov <arthur@emuravjev.ru> | ||
* @category Jare | ||
* @package Jare_Typograph | ||
*/ | ||
class Jare_Typograph | ||
{ | ||
/** | ||
* Перечень названий тофов, идущих с дистрибутивом | ||
* | ||
* @var array | ||
*/ | ||
protected $_baseTof = array('quote', 'dash', 'punctmark', 'number', 'space', 'etc'); | ||
|
||
/** | ||
* Массив из тофов, где каждой паре-ключ соответствует название тофа | ||
* и его объект | ||
* | ||
* @var array | ||
*/ | ||
protected $_tof = array(); | ||
|
||
/** | ||
* Конструктор | ||
* | ||
* @param string $text строка для типографирования | ||
* @return void | ||
*/ | ||
public function __construct($text) | ||
{ | ||
$this->_text = $text; | ||
$this->_text = trim($this->_text); | ||
} | ||
|
||
/** | ||
* Метод для быстрого типографирования текста, при котором не нужно | ||
* делать настройки тофов, их базовых параметров и т.п. | ||
* | ||
* @param string $text строка для типографирования | ||
* @return string | ||
*/ | ||
public static function quickParse($text) | ||
{ | ||
$typograph = new self($text); | ||
return $typograph->parse($typograph->getBaseTofsNames()); | ||
} | ||
|
||
/** | ||
* Возвращает массив из названий тофов, которые идут вместе с дистрибутивом | ||
* | ||
* @return array | ||
*/ | ||
public function getBaseTofsNames() | ||
{ | ||
return $this->_baseTof; | ||
} | ||
|
||
/** | ||
* Добавление тофа в очередь на обработку текста | ||
* | ||
* @param string $name название тофа | ||
* @param Jare_Typograph_Tof $object экземляр класса, унаследованного от 'Jare_Typograph_Tof' | ||
* @throws Jare_Typograph_Exception | ||
* @return void | ||
*/ | ||
public function setTof($name, $object) | ||
{ | ||
$name = strtolower($name); | ||
|
||
if (!$object instanceof Jare_Typograph_Tof) { | ||
require_once 'Pride/Typograph/Exception.php'; | ||
throw new Pride_Typograph_Exception("Tof '$name' class must be extend Jare_Typograph_Tof"); | ||
} | ||
|
||
$this->_tof[$name] = $object; | ||
$this->_tof[$name]->setStringToParse(&$this->_text); | ||
} | ||
|
||
/** | ||
* Получение объекта тофа | ||
* | ||
* Если тоф не был раннее добавлен и при этом он является базовым, экземляр его класса | ||
* будет создан автоматически | ||
* | ||
* @param string $name | ||
* @throws Jare_Typograph_Exception | ||
* @return Jare_Typograph_Tof | ||
*/ | ||
public function getTof($name) | ||
{ | ||
$name = strtolower($name); | ||
|
||
if (!isset($this->_tof[$name])) { | ||
if (!in_array($name, $this->_baseTof)) { | ||
require_once 'Jare/Typograph/Exception.php'; | ||
throw new Jare_Typograph_Exception('Incorrect name of tof'); | ||
} | ||
|
||
$fileName = 'Jare/Typograph/Tof/' . ucfirst($name) . '.php'; | ||
$className = 'Jare_Typograph_Tof_' . ucfirst($name); | ||
|
||
require_once $fileName; | ||
|
||
if (!class_exists($className, false)) { | ||
require_once 'Jare/Typograph/Exception.php'; | ||
throw new Jare_Typograph_Exception('Class not exists'); | ||
} | ||
|
||
$this->setTof($name, new $className); | ||
} | ||
|
||
return $this->_tof[$name]; | ||
} | ||
|
||
/** | ||
* Типографирование текста | ||
* | ||
* @param mixed $tofs строка или массив из названий тофов, которые будут применены при типографирование текста | ||
* @throws Jare_Typograph_Exception | ||
* @return string | ||
*/ | ||
public function parse($tofs) | ||
{ | ||
if (is_string($tofs)) { | ||
$tofs = array($tofs); | ||
} | ||
|
||
if (!is_array($tofs)) { | ||
require_once 'Jare/Typograph/Exception.php'; | ||
throw new Jare_Typograph_Exception('Incorrect type of tof-variable - try set array or string'); | ||
} | ||
|
||
if (!count($tofs)) { | ||
require_once 'Jare/Typograph/Exception.php'; | ||
throw new Jare_Typograph_Exception('You must set 1 or more tofs; your array is empty!'); | ||
} | ||
|
||
require_once 'Jare/Typograph/Tool.php'; | ||
Jare_Typograph_Tool::addCustomBlocks('<pre>', '</pre>'); | ||
Jare_Typograph_Tool::addCustomBlocks('<script>', '</script>'); | ||
Jare_Typograph_Tool::addCustomBlocks('<style>', '</style>'); | ||
|
||
$this->_text = Jare_Typograph_Tool::safeCustomBlocks($this->_text, true); | ||
$this->_text = Jare_Typograph_Tool::safeTagChars($this->_text, true); | ||
$this->_text = Jare_Typograph_Tool::clearSpecialChars($this->_text, Jare_Typograph_Tool::CLEAR_MODE_UTF8_NATIVE | Jare_Typograph_Tool::CLEAR_MODE_HTML_MATTER); | ||
|
||
foreach ($tofs as $tofName) { | ||
$this->getTof($tofName)->parse(); | ||
} | ||
|
||
$this->_text = Jare_Typograph_Tool::safeTagChars($this->_text, false); | ||
$this->_text = Jare_Typograph_Tool::safeCustomBlocks($this->_text, false); | ||
|
||
return $this->_text; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
/** | ||
* @see Jare_Exception | ||
*/ | ||
require_once 'Jare/Exception.php'; | ||
|
||
/** | ||
* Jare_Typograph_Exception | ||
* | ||
* @copyright Copyright (c) 2009 E.Muravjev Studio (http://emuravjev.ru) | ||
* @license http://emuravjev.ru/works/tg/eula/ | ||
* @version 2.0.0 | ||
* @author Arthur Rusakov <arthur@emuravjev.ru> | ||
* @category Jare | ||
* @package Jare_Typograph | ||
*/ | ||
class Jare_Typograph_Exception extends Jare_Exception | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
<?php | ||
/** | ||
* Jare_Typograph_Param | ||
* | ||
* @copyright Copyright (c) 2009 E.Muravjev Studio (http://emuravjev.ru) | ||
* @license http://emuravjev.ru/works/tg/eula/ | ||
* @version 2.0.0 | ||
* @author Arthur Rusakov <arthur@emuravjev.ru> | ||
* @category Jare | ||
* @package Jare_Typograph | ||
*/ | ||
class Jare_Typograph_Param | ||
{ | ||
/** | ||
* Ключи настроек | ||
*/ | ||
const KEY_DISABLE_DEFAULT = '_disable'; | ||
const KEY_DISABLE_USER = 'user_disable'; | ||
const KEY_PARSE_PATTERN = 'pattern'; | ||
const KEY_PARSE_REPLACE = 'replacement'; | ||
const KEY_FUNCTION_LINK = 'function_link'; | ||
const KEY_POSITION = 'position'; | ||
|
||
/** | ||
* Настройки | ||
* | ||
* @var array | ||
*/ | ||
protected $_option = array(); | ||
|
||
/** | ||
* Настройки, которые были заданы по умолчанию при создание экземпляра класса; невозможно изменить | ||
* | ||
* @var array | ||
*/ | ||
protected $_defaultOption = array(); | ||
|
||
/** | ||
* Конструктор | ||
* | ||
* @param array $option массив из настроек, которые будут заданы по умолчанию | ||
* @return void | ||
*/ | ||
public function __construct($option = array()) | ||
{ | ||
$this->_defaultOption = $option; | ||
$this->_option = $option; | ||
} | ||
|
||
/** | ||
* Отключение параметра | ||
* | ||
* @param bool $status | ||
* @return Jare_Typograph_Param | ||
*/ | ||
public function disable($status) | ||
{ | ||
$this->_option[self::KEY_DISABLE_USER] = (bool) $status; | ||
return $this; | ||
} | ||
|
||
/** | ||
* Сброс заданных настроек к тем, которые были заданы при создание экземпляра класса | ||
* | ||
* @throws Jare_Typograph_Param_Exception | ||
* @return Jare_Typograph_Param | ||
*/ | ||
public function reset() | ||
{ | ||
if (!count($this->_defaultOption)) { | ||
require_once 'Jare/Typograph/Param/Exception.php'; | ||
throw new Jare_Typograph_Param_Exception('This parameter does not have default options'); | ||
} | ||
|
||
$this->_option = $this->_defaultOption; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Задание настройки | ||
* | ||
* @param string $name | ||
* @param string $value | ||
* @throws Jare_Typograph_Param_Exception | ||
* @return Jare_Typograph_Param | ||
*/ | ||
public function setOption($name, $value) | ||
{ | ||
$name = strtolower($name); | ||
$value = trim($value); | ||
|
||
if ('_' === substr($name, 0, 1)) { | ||
require_once 'Jare/Typograph/Param/Exception.php'; | ||
throw new Jare_Typograph_Param_Exception('Prefix "_" reserved for system option'); | ||
} | ||
|
||
if (empty($value)) { | ||
require_once 'Jare/Typograph/Param/Exception.php'; | ||
throw new Jare_Typograph_Param_Exception('Empty value. It\'s bad.'); | ||
} | ||
|
||
switch ($name) { | ||
case self::KEY_FUNCTION_LINK: | ||
$this->_option[self::KEY_PARSE_PATTERN] = ''; | ||
$this->_option[self::KEY_PARSE_REPLACE] = ''; | ||
case self::KEY_PARSE_PATTERN: | ||
case self::KEY_PARSE_REPLACE: | ||
$this->_option[self::KEY_FUNCTION_LINK] = ''; | ||
} | ||
|
||
$this->_option[$name] = $value; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Возврат списка заданных настроек параметра | ||
* | ||
* @throws Jare_Typograph_Param_Exception | ||
* @return array | ||
*/ | ||
public function getOptions() | ||
{ | ||
if (!count($this->_option)) { | ||
require_once 'Jare/Typograph/Param/Exception.php'; | ||
throw new Jare_Typograph_Param_Exception('This parameter does not have options!'); | ||
} | ||
|
||
if (!empty($this->_option[self::KEY_FUNCTION_LINK])) { | ||
return $this->_option; | ||
} | ||
|
||
if (empty($this->_option[self::KEY_PARSE_PATTERN]) && !empty($this->_option[self::KEY_PARSE_REPLACE])) { | ||
require_once 'Jare/Typograph/Param/Exception.php'; | ||
throw new Jare_Typograph_Param_Exception('You must set up pattern and replacement or function link'); | ||
} | ||
|
||
return $this->_option; | ||
} | ||
|
||
/** | ||
* Получение значения настройки | ||
* | ||
* @param string $name | ||
* @throws Jare_Typograph_Param_Exception | ||
* @return string | ||
*/ | ||
public function getOption($name) | ||
{ | ||
if (!in_array($name, $this->option)) { | ||
require_once 'Jare/Typograph/Param/Exception.php'; | ||
throw new Jare_Typograph_Param_Exception("This option doesn't have value"); | ||
} | ||
|
||
return $this->_option[$name]; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
assets/snippets/ddtypograph/Jare/Typograph/Param/Exception.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
/** | ||
* @see Jare_Typograph_Exception | ||
*/ | ||
require_once 'Jare/Typograph/Exception.php'; | ||
|
||
/** | ||
* Jare_Typograph_Param_Exception | ||
* | ||
* @copyright Copyright (c) 2009 E.Muravjev Studio (http://emuravjev.ru) | ||
* @license http://emuravjev.ru/works/tg/eula/ | ||
* @version 2.0.0 | ||
* @author Arthur Rusakov <arthur@emuravjev.ru> | ||
* @category Jare | ||
* @package Jare_Typograph | ||
* @subpackage Param | ||
*/ | ||
class Jare_Typograph_Param_Exception extends Jare_Typograph_Exception | ||
{ | ||
} |
Oops, something went wrong.