Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
  • 2 commits
  • 3 files changed
  • 0 comments
  • 2 contributors
Jan 25, 2012
Rob Sanchez added all config options to settings screen
set to use built in EE charset
added ability to provide tidy:options in the template
0a956f7
Fred Calsen Merge pull request #1 from rsanchez/master
Lots of improvements
fe7ce2d
10  README.md
Source Rendered
@@ -4,9 +4,13 @@ Finally we got a template_post_parse hook in EE, and you know what that means? T
4 4
 
5 5
 # 1, 2, 3 PARTY.
6 6
 
7  
-0. Tweak the Tidy config.
8  
-1. Put it in your third_party folder.
9  
-2. Enable
  7
+1. Tweak the Tidy config.
  8
+2. Put it in your third_party folder.
  9
+3. Enable
  10
+4. (optional) Configure your [options](http://tidy.sourceforge.net/docs/quickref.html) in the extension settings
  11
+5. (optional) Use the {tidy:options} tag:
  12
+
  13
+	`{tidy:options indent="yes" wrap="200"}`
10 14
 
11 15
 # Time spent on this
12 16
 
218  tidy_template/ext.tidy_template.php
@@ -28,13 +28,102 @@ class Tidy_template_ext {
28 28
 	
29 29
 	public $settings 		= array();
30 30
 	public $description		= 'Clean template with HTML Tidy';
31  
-	public $docs_url		= '';
  31
+	public $docs_url		= 'https://github.com/sjelfull/sf.tidy_template.ee_addon';
32 32
 	public $name			= 'Tidy Template';
33  
-	public $settings_exist	= 'n';
34  
-	public $version			= '0.1';
  33
+	public $settings_exist	= 'y';
  34
+	public $version			= '0.2';
35 35
 	
36 36
 	private $EE;
37 37
 	
  38
+	public static $default_settings = array(
  39
+		'add-xml-decl' => 'no',
  40
+		'add-xml-space' => 'no',
  41
+		'alt-text' => '',
  42
+		'anchor-as-name' => 'yes',
  43
+		'assume-xml-procins' => 'no',
  44
+		'bare' => 'no',
  45
+		'clean' => 'no',
  46
+		'css-prefix' => '',
  47
+		'decorate-inferred-ul' => 'no',
  48
+		'doctype' => 'auto',
  49
+		'drop-empty-paras' => 'yes',
  50
+		'drop-font-tags' => 'no',
  51
+		'drop-proprietary-attributes' => 'no',
  52
+		'enclose-block-text' => 'no',
  53
+		'enclose-text' => 'no',
  54
+		'escape-cdata' => 'no',
  55
+		'fix-backslash' => 'yes',
  56
+		'fix-bad-comments' => 'yes',
  57
+		'fix-uri' => 'yes',
  58
+		'hide-comments' => 'no',
  59
+		'hide-endtags' => 'no',
  60
+		'indent-cdata' => 'no',
  61
+		'input-xml' => 'no',
  62
+		'join-classes' => 'no',
  63
+		'join-styles' => 'yes',
  64
+		'literal-attributes' => 'no',
  65
+		'logical-emphasis' => 'no',
  66
+		'lower-literals' => 'yes',
  67
+		'merge-divs' => 'auto',
  68
+		'merge-spans' => 'auto',
  69
+		'ncr' => 'yes',
  70
+		'new-blocklevel-tags' => '',
  71
+		'new-empty-tags' => '',
  72
+		'new-inline-tags' => '',
  73
+		'new-pre-tags' => '',
  74
+		'numeric-entities' => 'no',
  75
+		'output-html' => 'no',
  76
+		'output-xhtml' => 'no',
  77
+		'output-xml' => 'no',
  78
+		'preserve-entities' => 'no',
  79
+		'quote-ampersand' => 'yes',
  80
+		'quote-marks' => 'no',
  81
+		'quote-nbsp' => 'yes',
  82
+		'repeated-attributes' => 'keep-last',
  83
+		'replace-color' => 'no',
  84
+		'show-body-only' => 'no',
  85
+		'uppercase-attributes' => 'no',
  86
+		'uppercase-tags' => 'no',
  87
+		'word-2000' => 'no',
  88
+		'accessibility-check' => '0',
  89
+		'show-errors' => '6',
  90
+		'show-warnings' => 'yes',
  91
+		'break-before-br' => 'no',
  92
+		'indent' => 'no',
  93
+		'indent-attributes' => 'no',
  94
+		'indent-spaces' => '2',
  95
+		'markup' => 'yes',
  96
+		'punctuation-wrap' => 'no',
  97
+		'sort-attributes' => 'none',
  98
+		'split' => 'no',
  99
+		'tab-size' => '8',
  100
+		'vertical-space' => 'no',
  101
+		'wrap' => '68',
  102
+		'wrap-asp' => 'yes',
  103
+		'wrap-attributes' => 'no',
  104
+		'wrap-jste' => 'yes',
  105
+		'wrap-php' => 'yes',
  106
+		'wrap-script-literals' => 'no',
  107
+		'wrap-sections' => 'yes',
  108
+		'ascii-chars' => 'no',
  109
+		//'char-encoding' => 'ascii',
  110
+		//'input-encoding' => 'latin1', //use built-in EE encoding
  111
+		//'language' => '',
  112
+		//'newline' => '',
  113
+		'output-bom' => 'auto',
  114
+		//'output-encoding' => 'ascii', //use built-in EE encoding
  115
+		//'error-file' => '',
  116
+		'force-output' => 'no',
  117
+		//'gnu-emacs' => 'no',
  118
+		//'gnu-emacs-file' => '',
  119
+		'keep-time' => 'no',
  120
+		//'output-file' => '',
  121
+		'quiet' => 'no',
  122
+		//'slide-style' => '',
  123
+		'tidy-mark' => 'yes',
  124
+		'write-back' => 'no',
  125
+	);
  126
+	
38 127
 	/**
39 128
 	 * Constructor
40 129
 	 *
@@ -77,6 +166,69 @@ public function activate_extension()
77 166
 	// ----------------------------------------------------------------------
78 167
 	
79 168
 	/**
  169
+	 * settings
  170
+	 * 
  171
+	 * @access	public
  172
+	 * @return	void
  173
+	 */
  174
+	public function settings()
  175
+	{
  176
+		$special = array(
  177
+			'doctype' => array('omit', 'auto', 'strict', 'transitional', 'user'),
  178
+			'merge-divs' => array('auto', 'yes', 'no'),
  179
+			'merge-spans' => array('auto', 'yes', 'no'),
  180
+			'repeated-attributes' => array('keep-first', 'keep-last'),
  181
+			'accessibility-check' => array('0' => 'tidy_classic', '1' => 'priority_1_checks', '2' => 'priority_2_checks', '3' => 'priority_3_checks'),
  182
+			'output-bom' => array('auto', 'yes', 'no'),
  183
+		);
  184
+		
  185
+		$settings = array();
  186
+		
  187
+		foreach (self::$default_settings as $key => $value)
  188
+		{
  189
+			//it's a bool string
  190
+			if (in_array($value, array('yes', 'no')))
  191
+			{
  192
+				//give 'em a yes checkbox
  193
+				$settings[$key] = array('c', array('yes' => 'yes'), array($value));
  194
+			}
  195
+			//it's not just a text input
  196
+			else if (isset($special[$key]))
  197
+			{
  198
+				//select menu
  199
+				if (is_array($special[$key]))
  200
+				{
  201
+					$settings[$key] = array('s', array());
  202
+					
  203
+					foreach ($special[$key] as $i => $option)
  204
+					{
  205
+						if (is_int($i))
  206
+						{
  207
+							$settings[$key][1][$option] = $option;
  208
+						}
  209
+						else
  210
+						{
  211
+							$settings[$key][1][$i] = $option;
  212
+						}
  213
+					}
  214
+					
  215
+					$settings[$key][2] = $special[$key][0];
  216
+				}
  217
+				else
  218
+				{
  219
+					$settings[$key] = $special[$key];
  220
+				}
  221
+			}
  222
+			else
  223
+			{
  224
+				$settings[$key] = $value;
  225
+			}
  226
+		}
  227
+		
  228
+		return $settings;
  229
+	}
  230
+	
  231
+	/**
80 232
 	 * clean_template
81 233
 	 *
82 234
 	 * @param 
@@ -84,22 +236,50 @@ public function activate_extension()
84 236
 	 */
85 237
 	public function clean_template($template, $sub,	$site_id)
86 238
 	{
87  
-		if (function_exists('tidy_parse_string')) {
88  
-
89  
-			// Add Code for the update_template_end hook here.  
90  
-			// Specify configuration
91  
-			$config = array(
92  
-			 'indent'         => true,
93  
-			 'output-html'   => true,
94  
-			 'wrap'           => 200,
95  
-			 'markup'           => true);
96  
-
97  
-			// Specify encoding
98  
-			$encoding = 'utf8';
99  
-
100  
-			// repair HTML
101  
-			$template = tidy_repair_string($template, $config, $encoding);
102  
-
  239
+		if ($this->EE->extensions->last_call !== FALSE)
  240
+		{
  241
+			$template = $this->EE->extensions->last_call;
  242
+		}
  243
+		
  244
+		//only run on the final template
  245
+		if ( ! $sub && function_exists('tidy_repair_string'))
  246
+		{
  247
+			//grab the cached settings if they're not already there for some reason
  248
+			if (empty($this->settings))
  249
+			{
  250
+				$this->settings = $this->EE->extensions->s_cache[__CLASS__];
  251
+			}
  252
+			
  253
+			$options = $this->settings;
  254
+			
  255
+			//b/c of the way EE stores checkboxes as arrays
  256
+			foreach ($options as $key => $value)
  257
+			{
  258
+				if (is_array($value))
  259
+				{
  260
+					$options[$key] = (bool) $value;
  261
+					$value = ( ! empty($value)) ? 'yes' : 'no';
  262
+				}
  263
+				
  264
+				// strip settings which match the default settings
  265
+				if ($value === self::$default_settings[$key])
  266
+				{
  267
+					unset($options[$key]);
  268
+				}
  269
+			}
  270
+			
  271
+			//grab in-template parameters: {tidy:options merge-divs="yes"}
  272
+			if (strstr($template, '{tidy:options') && preg_match('/{tidy:options (.*?)}/', $template, $match))
  273
+			{
  274
+				$template = str_replace($match[0], '', $template);
  275
+				
  276
+				$options = array_merge($options, $this->EE->functions->assign_parameters($match[1]));
  277
+			}
  278
+			
  279
+			//use EE's config value for encoding
  280
+			$options['char-encoding'] = strtolower(str_replace(array(' ', '-'), '', $this->EE->config->item('charset')));
  281
+			
  282
+			$template = tidy_repair_string($template, $options, $options['char-encoding']);
103 283
 		}
104 284
 
105 285
 	 	return $template;
11  tidy_template/language/english/tidy_template_lang.php
... ...
@@ -0,0 +1,11 @@
  1
+<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2
+
  3
+$lang = array();
  4
+
  5
+foreach (array_keys(Tidy_template_ext::$default_settings) as $key)
  6
+{
  7
+	$lang[$key] = '<a href="http://tidy.sourceforge.net/docs/quickref.html#'.$key.'" target="_blank">'.$key.'</a>';
  8
+}
  9
+
  10
+/* End of file tidy_lang.php */
  11
+/* Location: ./system/expressionengine/third_party/tidy/language/english/tidy_lang.php */

No commit comments for this range

Something went wrong with that request. Please try again.