ソースを参照

Typography deployment

euromark 13 年 前
コミット
2204f7686f

+ 49 - 0
Model/Behavior/TypographicBehavior.php

@@ -71,6 +71,23 @@ class TypographicBehavior extends ModelBehavior {
 		}
 
 		$this->settings[$Model->alias] = array_merge($this->settings[$Model->alias], is_array($settings) ? $settings : array());
+		if (empty($this->settings[$Model->alias]['fields'])) {
+			$schema = $Model->schema();
+			$fields = array();
+			foreach ($schema as $field => $v) {
+				if (!in_array($v['type'], array('string', 'text'))) {
+					continue;
+				}
+				if (!empty($v['key'])) {
+					continue;
+				}
+				if (isset($v['length']) && $v['length'] === 1) { //TODO: also skip UUID (lenght 36)?
+					continue;
+				}
+				$fields[] = $field;
+			}
+			$this->settings[$Model->alias]['fields'] = $fields;
+		}
 	}
 
 
@@ -94,6 +111,38 @@ class TypographicBehavior extends ModelBehavior {
 		return true;
 	}
 
+	/**
+	 * Run the behavior over all records of this model
+	 * This is useful if you attach it after some records have already been saved without it.
+	 * @param object $Model Model about to be saved.
+	 * @return int $count Number of affected/changed records
+	 * 2012-08-07 ms
+	 */
+	public function updateTypography(Model $Model, $dryRun = false) {
+		$records = $Model->find('all'); //TODO: in multiple runs with limit
+		$count = 0;
+		foreach ($records as $record) {
+			$changed = false;
+			foreach ($this->settings[$Model->alias]['fields'] as $field) {
+				if (empty($record[$Model->alias][$field])) {
+					continue;
+				}
+				$tmp = $this->_prepareInput($record[$Model->alias][$field]);
+				if ($tmp == $record[$Model->alias][$field]) {
+					continue;
+				}
+				$record[$Model->alias][$field] = $tmp;
+				$changed = true;
+			}
+			if ($changed) {
+				if (!$dryRun) {
+					$Model->save($record, false);
+				}
+				$count++;
+			}
+		}
+		return $count;
+	}
 
 	/**
 	 * Run before a model is saved

+ 39 - 29
Test/Case/Model/Behavior/TypographicBehaviorTest.php

@@ -7,38 +7,34 @@ App::uses('MyCakeTestCase', 'Tools.Lib');
 
 class TypographicBehaviorTest extends MyCakeTestCase {
 
-	public function startTest() {
-		//$this->Comment = ClassRegistry::init('Comment');
-		$this->Comment = new TypographicTestModel();
-		$this->Comment->Behaviors->attach('Tools.Typographic', array('fields'=>array('body'), 'before'=>'validate'));
-	}
-
-	public function setUp() {
+	public $Model;
 
-	}
+	public $fixtures = array('core.article');
 
-	public function tearDown() {
+	public function setUp() {
+		parent::setUp();
 
+		$this->Model = ClassRegistry::init('Article');
+		$this->Model->Behaviors->attach('Tools.Typographic', array('fields'=>array('body'), 'before'=>'validate'));
 	}
 
 	public function testObject() {
-		$this->assertTrue(is_a($this->Comment->Behaviors->Typographic, 'TypographicBehavior'));
+		$this->assertTrue(is_a($this->Model->Behaviors->Typographic, 'TypographicBehavior'));
 	}
 
 
 	public function testBeforeValidate() {
 		$this->out($this->_header(__FUNCTION__), false);
-		// accuracy >= 5
 		$data = array(
-			'name' => 'some Name',
+			'title' => 'some «cool» title',
 			'body' => 'A title with normal "qotes" - should be left untouched',
 		);
-		$this->Comment->set($data);
-		$res = $this->Comment->validates();
+		$this->Model->set($data);
+		$res = $this->Model->validates();
 		$this->assertTrue($res);
 
-		$res = $this->Comment->data;
-		$this->assertSame($data['body'], $res['TypographicTestModel']['body']);
+		$res = $this->Model->data;
+		$this->assertSame($data, $res['Article']);
 
 		$strings = array(
 			'some string with ‹single angle quotes›' => 'some string with "single angle quotes"',
@@ -48,30 +44,44 @@ class TypographicBehaviorTest extends MyCakeTestCase {
 		);
 		foreach ($strings as $was => $expected) {
 			$data = array(
+				'title' => 'some «cool» title',
 				'body' => $was
 			);
-			$this->Comment->set($data);
-			$res = $this->Comment->validates();
+			$this->Model->set($data);
+			$res = $this->Model->validates();
 			$this->assertTrue($res);
 
-			$res = $this->Comment->data;
-			//debug($expected);
-			//debug($res['TestModel']['body']);
-			//die();
-			$this->assertSame($expected, $res['TypographicTestModel']['body']);
+			$res = $this->Model->data;
+			$this->assertSame($data['title'], $res['Article']['title']);
+			$this->assertSame($expected, $res['Article']['body']);
 		}
 
 	}
 
+	/**
+	 * test that not defining fields results in all textarea and text fields being processed
+	 * 2012-08-07 ms
+	 */
+	public function testAutoFields() {
+		$this->Model->Behaviors->detach('Typographic');
+		$this->Model->Behaviors->attach('Tools.Typographic');
+		$data = array(
+			'title' => '„German‟ quotes',
+			'body' => 'mixed double “one” and «two»',
+		);
 
-}
-
-/** other files **/
+		$this->Model->set($data);
+		$res = $this->Model->save();
+		$this->assertTrue((bool)$res);
 
-class TypographicTestModel extends AppModel {
+		$expected = array(
+			'title' => '"German" quotes',
+			'body' => 'mixed double "one" and "two"',
+		);
 
-	public $useTable = false;
-	public $displayField = 'name';
+		$this->assertSame($expected['title'], $res['Article']['title']);
+		$this->assertSame($expected['body'], $res['Article']['body']);
+	}
 
 
 }

+ 85 - 10
Test/Case/View/Helper/TypographyHelperTest.php

@@ -18,6 +18,7 @@ class TypographyHelperTest extends MyCakeTestCase {
  */
 	public function setUp() {
 		parent::setUp();
+		Configure::write('Typography.locale', '');
 
 		$this->Typography = new TypographyHelper(new View(null));
 	}
@@ -34,28 +35,61 @@ class TypographyHelperTest extends MyCakeTestCase {
 	}
 
 /**
- * testAutoTypography method
+ * testFormatCharacter method
  *
  * @return void
  */
-	public function testAutoTypography() {
-		$str = 'Some \'funny\' and "funky" test with a new
+	public function testFormatCharacter() {
+		$strs = array(
+			'"double quotes"' 				=> '“double quotes”',
+			'"testing" in "theory" that is' => '“testing” in “theory” that is',
+			"Here's what I'm" 				=> 'Here’s what I’m',
+			'&' 							=> '&',
+			'&' 						=> '&',
+			' '						=> ' ',
+			'--'							=> '—',
+			'foo...'						=> 'foo…',
+			'foo..'							=> 'foo..',
+			'foo...bar.'					=> 'foo…bar.',
+			'test.  new'					=> 'test.  new',
+		);
 
-paragraph and a
-	new line tabbed in.';
+		foreach ($strs as $str => $expected) {
+			$result = $this->Typography->formatCharacters($str);
+			echo pre($result);
+			$this->assertEquals($expected, $result);
+		}
 
-		$res = $this->Typography->autoTypography($str);
-		$this->out($res);
-		$this->out(h($res));
+		//$this->tearDown();
+		//$this->setUp();
+
+		Configure::write('Typography.locale', 'low');
+		$strs = array(
+			'"double quotes"' 				=> '„double quotes‟',
+			'"testing" in "theory" that is' => '„testing‟ in „theory‟ that is',
+			"Here's what I'm" 				=> 'Here‛s what I‛m',
+		);
+		foreach ($strs as $str => $expected) {
+			$result = $this->Typography->formatCharacters($str);
+			echo pre($result);
+			$this->assertEquals($expected, $result);
+		}
 	}
 
 /**
- * testFormatCharacter method
+ * testAutoTypography method
  *
  * @return void
  */
-	public function testFormatCharacter() {
+	public function testAutoTypography() {
+		$str = 'Some \'funny\' and "funky" test with a new
 
+paragraph and a
+	new line tabbed in.';
+
+		$res = $this->Typography->autoTypography($str);
+		echo pre($res);
+		debug($res);
 	}
 
 /**
@@ -64,7 +98,48 @@ paragraph and a
  * @return void
  */
 	public function testNl2brExceptPre() {
+		$str = <<<EOH
+Hello, I'm a happy string with some new lines.
+
+I like to skip.
+
+Jump
+
+and sing.
+
+<pre>
+I am inside a pre tag.  Please don't mess with me.
+
+k?
+</pre>
+
+That's my story and I'm sticking to it.
+
+The End.
+EOH;
+
+		$expected = <<<EOH
+Hello, I'm a happy string with some new lines.<br />
+<br />
+I like to skip.<br />
+<br />
+Jump<br />
+<br />
+and sing.<br />
+<br />
+<pre>
+I am inside a pre tag.  Please don't mess with me.
+
+k?
+</pre><br />
+<br />
+That's my story and I'm sticking to it.<br />
+<br />
+The End.
+EOH;
 
+		$result = $this->Typography->nl2brExceptPre($str);
+		$this->assertEquals($expected, $result);
 	}
 
 }

+ 15 - 9
View/Helper/TypographyHelper.php

@@ -16,13 +16,16 @@ App::uses('AppHelper', 'View/Helper');
  */
 
 /**
- * Typography Class
- *
+ * Typography Class converted to Cake Helper
  *
  * @access		private
  * @category	Helpers
  * @author		ExpressionEngine Dev Team
  * @link		http://codeigniter.com/user_guide/helpers/
+ *
+ * @modified Mark Scherer
+ * @cakephp 2.x
+ * @php 5
  */
 class TypographyHelper extends AppHelper {
 
@@ -232,10 +235,13 @@ class TypographyHelper extends AppHelper {
 	 * @param	string
 	 * @return	string
 	 */
-	public function formatCharacters($str) {
+	public function formatCharacters($str, $locale = null) {
 		//static $table;
+		if ($locale === null) {
+			$locale = Configure::read('Typography.locale');
+		}
 
-		$locale = array(
+		$locales = array(
 			'default' => array(
 				'leftSingle' => '&#8216;', # &lsquo;
 				'rightSingle' => '&#8217;', # &rsquo;
@@ -250,7 +256,6 @@ class TypographyHelper extends AppHelper {
 			)
 		);
 
-
 		if (!isset($table)) {
 			$table = array(
 				// nested smart quotes, opening and closing
@@ -294,10 +299,11 @@ class TypographyHelper extends AppHelper {
 				// ampersands, if not a character entity
 				'/&(?!#?[a-zA-Z0-9]{2,};)/'		=> '&amp;'
 			);
-		}
-
-		foreach ($table as $key => $val) {
-			$table[$key] = str_replace($locale['default'], $locale['low'], $val);
+			if ($locale && !empty($locales[$locale])) {
+				foreach ($table as $key => $val) {
+					$table[$key] = str_replace($locales['default'], $locales[$locale], $val);
+				}
+			}
 		}
 
 		return preg_replace(array_keys($table), $table, $str);