From 3e895ebe6690b15f2ad4e560cba8913ba8afd238 Mon Sep 17 00:00:00 2001 From: Jos Date: Fri, 19 Feb 2016 14:19:32 +0100 Subject: [PATCH 1/8] Add new config option for styling attributes. --- conf/default.php | 1 + conf/metadata.php | 1 + lang/en/settings.php | 1 + lang/ja/settings.php | 1 + 4 files changed, 4 insertions(+) diff --git a/conf/default.php b/conf/default.php index b3f0126..49c767f 100644 --- a/conf/default.php +++ b/conf/default.php @@ -6,3 +6,4 @@ */ $conf['loadBootstrap'] = 0; +$conf['allowStylingAttributes'] = 0; \ No newline at end of file diff --git a/conf/metadata.php b/conf/metadata.php index 8ee671b..2541e6f 100644 --- a/conf/metadata.php +++ b/conf/metadata.php @@ -6,3 +6,4 @@ */ $meta['loadBootstrap'] = array('onoff', '_caution' => 'danger'); +$meta['allowStylingAttributes'] = array('onoff'); diff --git a/lang/en/settings.php b/lang/en/settings.php index f162924..e4dc3f0 100644 --- a/lang/en/settings.php +++ b/lang/en/settings.php @@ -8,3 +8,4 @@ // for the configuration manager $lang['loadBootstrap'] = "Load the Bootstrap vanilla CSS. Disable it if you have installed a Bootstrap based template."; +$lang['allowStylingAttributes'] = "Allow the use of ''class=\"\"'', ''id=\"\"'' and ''style=\"\"'' attributes on all components."; diff --git a/lang/ja/settings.php b/lang/ja/settings.php index c4549f7..7c67c7f 100644 --- a/lang/ja/settings.php +++ b/lang/ja/settings.php @@ -7,3 +7,4 @@ // 設定管理画面用 $lang['loadBootstrap'] = "Bootstrap vanilla CSS を読み込む。Bootstrap を基にしたテンプレートをインストールした場合、無効にします。"; +$lang['allowStylingAttributes'] = "Allow the use of ''class=\"\"'', ''id=\"\"'' and ''style=\"\"'' attributes on all components."; From 89dbb8bffecc11d92ce1f3e6e42c762c9311f16a Mon Sep 17 00:00:00 2001 From: Jos Date: Fri, 19 Feb 2016 14:30:28 +0100 Subject: [PATCH 2/8] Implement styling attributes in the base bootstrap syntax class. --- syntax/bootstrap.php | 67 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 61 insertions(+), 6 deletions(-) diff --git a/syntax/bootstrap.php b/syntax/bootstrap.php index fe208e4..bd9ac17 100644 --- a/syntax/bootstrap.php +++ b/syntax/bootstrap.php @@ -15,12 +15,28 @@ class syntax_plugin_bootswrapper_bootstrap extends DokuWiki_Syntax_Plugin { protected $pattern_start = ''; protected $pattern_end = ''; - protected $template_start = '
'; protected $template_content = '%s'; protected $template_end = '
'; protected $header_pattern = '[ \t]*={2,}[^\n]+={2,}[ \t]*(?=\n)'; protected $tag_attributes = array(); + protected $core_tag_attributes = array( + 'style' => array('type' => 'string', + 'values' => null, + 'required' => false, + 'default' => ''), + + 'class' => array('type' => 'string', + 'values' => null, + 'required' => false, + 'default' => ''), + + 'id' => array('type' => 'integer', + 'values' => null, + 'required' => false, + 'default' => '') + + ); /** * Check default and user attributes @@ -31,6 +47,12 @@ function checkAttributes($attributes = array()) { global $ACT; + if ($this->getConf('allowStylingAttributes')) { + $all_attributes = array_merge($this->tag_attributes, $this->core_tag_attributes); + } else { + $all_attributes = $this->tag_attributes; + } + $default_attributes = array(); $merged_attributes = array(); $checked_attributes = array(); @@ -42,13 +64,13 @@ function checkAttributes($attributes = array()) { } // Save the default values of attributes - foreach ($this->tag_attributes as $attribute => $item) { + foreach ($all_attributes as $attribute => $item) { $default_attributes[$attribute] = $item['default']; } foreach ($attributes as $name => $value) { - if (! isset($this->tag_attributes[$name])) { + if (! isset($all_attributes[$name])) { if ($ACT == 'preview') { msg(sprintf('%s Unknown attribute %s', $msg_title, $name), -1); @@ -58,7 +80,7 @@ function checkAttributes($attributes = array()) { } - $item = $this->tag_attributes[$name]; + $item = $all_attributes[$name]; $required = isset($item['required']) ? $item['required'] : false; $values = isset($item['values']) ? $item['values'] : null; @@ -100,6 +122,39 @@ function checkAttributes($attributes = array()) { } + /** + * This function replaces placeholders in the specified markup with styling attribute values from the user. + * It will only do this if the user has allowed styling attributes in the config. + * The markup string should contain three %s placeholders. + * If the markup already has styling or a class you can put the placeholder within the brackets. + * For example:
or if it doesn't have a class you'd use
+ * @param $markup Html markup element containting placeholders for the styling attributes. + * @param array $attributes Attribute values from user. + * @return The markup passed in with styling parsed. + */ + function parseMarkupSyntax($markup, $attributes = array()) { + if ($this->getConf('allowStylingAttributes')) { + $class = $attributes['class']; + $id = $attributes['id']; + $style = $attributes['style']; + + if (strpos($markup, 'class=') === false) { + $class = 'class="' . $class . '"'; + } + if (strpos($markup, 'id=') === false) { + $id = ''; //There can only be one ID + } + if (strpos($markup, 'style=') === false) { + $style = 'style="' . $style . '"'; + } + + $markup = sprintf($markup, $class, $id, $style); + } else { + $markup = sprintf($markup, '', '', ''); + } + return $markup; + } + function getType(){ return 'formatting'; } function getAllowedTypes() { return array('container', 'formatting', 'substition', 'protected', 'disabled', 'paragraphs'); } @@ -189,12 +244,12 @@ function render($mode, Doku_Renderer $renderer, $data) { if ($mode !== 'xhtml') return false; /** @var Doku_Renderer_xhtml $renderer */ - list($state, $match) = $data; + list($state, $match, $attributes) = $data; switch($state) { case DOKU_LEXER_ENTER: - $markup = $this->template_start; + $markup = $this->parseMarkupSyntax($this->template_start, $attributes); $renderer->doc .= $markup; return true; From 48c9725444f87d815d72664147a08e38d05368a4 Mon Sep 17 00:00:00 2001 From: Jos Date: Fri, 19 Feb 2016 14:43:42 +0100 Subject: [PATCH 3/8] Split parsing styling attributes code up in two functions to make it easier to use for certain components. --- syntax/bootstrap.php | 48 ++++++++++++++++++++++++++------------------ 1 file changed, 29 insertions(+), 19 deletions(-) diff --git a/syntax/bootstrap.php b/syntax/bootstrap.php index bd9ac17..6706842 100644 --- a/syntax/bootstrap.php +++ b/syntax/bootstrap.php @@ -19,7 +19,7 @@ class syntax_plugin_bootswrapper_bootstrap extends DokuWiki_Syntax_Plugin { protected $template_end = ''; protected $header_pattern = '[ \t]*={2,}[^\n]+={2,}[ \t]*(?=\n)'; protected $tag_attributes = array(); - protected $core_tag_attributes = array( + protected $styling_attributes = array( 'style' => array('type' => 'string', 'values' => null, @@ -48,7 +48,7 @@ function checkAttributes($attributes = array()) { global $ACT; if ($this->getConf('allowStylingAttributes')) { - $all_attributes = array_merge($this->tag_attributes, $this->core_tag_attributes); + $all_attributes = array_merge($this->tag_attributes, $this->styling_attributes); } else { $all_attributes = $this->tag_attributes; } @@ -133,28 +133,38 @@ function checkAttributes($attributes = array()) { * @return The markup passed in with styling parsed. */ function parseMarkupSyntax($markup, $attributes = array()) { - if ($this->getConf('allowStylingAttributes')) { - $class = $attributes['class']; - $id = $attributes['id']; - $style = $attributes['style']; - - if (strpos($markup, 'class=') === false) { - $class = 'class="' . $class . '"'; - } - if (strpos($markup, 'id=') === false) { - $id = ''; //There can only be one ID - } - if (strpos($markup, 'style=') === false) { - $style = 'style="' . $style . '"'; - } + $styling = $this->getStylingAttributes($attributes); - $markup = sprintf($markup, $class, $id, $style); - } else { - $markup = sprintf($markup, '', '', ''); + if (strpos($markup, 'class=') === false) { + $styling['class'] = 'class="' . $styling['class'] . '"'; + } + if (strpos($markup, 'id=') === false) { + $styling['id'] = ''; //There can only be one ID + } + if (strpos($markup, 'style=') === false) { + $styling['style'] = 'style="' . $styling['style'] . '"'; } + + $markup = sprintf($markup, $styling['class'], $styling['id'], $styling['style']); return $markup; } + /** + * Get array with styling attributes from the attributes array passed in. + * If the user has styling disabled this array will contain empty strings. + * @param array $attributes The attribute array with user values. + * @return array The array with styling attribute values or empty string. + */ + function getStylingAttributes($attributes = array()) { + $styling = array('class' => '', 'id' => '', 'style' => ''); + if ($this->getConf('allowStylingAttributes')) { + $styling['class'] = $attributes['class']; + $styling['class'] = $attributes['id']; + $styling['class'] = $attributes['style']; + } + return $styling; + } + function getType(){ return 'formatting'; } function getAllowedTypes() { return array('container', 'formatting', 'substition', 'protected', 'disabled', 'paragraphs'); } From 752029e106172ae48889e738bb00eaac00f47eb2 Mon Sep 17 00:00:00 2001 From: Jos Date: Fri, 19 Feb 2016 14:44:56 +0100 Subject: [PATCH 4/8] Fix typo --- syntax/bootstrap.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/syntax/bootstrap.php b/syntax/bootstrap.php index 6706842..748e160 100644 --- a/syntax/bootstrap.php +++ b/syntax/bootstrap.php @@ -159,8 +159,8 @@ function getStylingAttributes($attributes = array()) { $styling = array('class' => '', 'id' => '', 'style' => ''); if ($this->getConf('allowStylingAttributes')) { $styling['class'] = $attributes['class']; - $styling['class'] = $attributes['id']; - $styling['class'] = $attributes['style']; + $styling['id'] = $attributes['id']; + $styling['style'] = $attributes['style']; } return $styling; } From 6bdf5431897afb7f16426ee16091106d1a39f315 Mon Sep 17 00:00:00 2001 From: Jos Date: Fri, 19 Feb 2016 16:36:29 +0100 Subject: [PATCH 5/8] Refactor a lot styling code (All working now) --- syntax/bootstrap.php | 50 +++++++++++++++----------------------------- 1 file changed, 17 insertions(+), 33 deletions(-) diff --git a/syntax/bootstrap.php b/syntax/bootstrap.php index 748e160..56849a2 100644 --- a/syntax/bootstrap.php +++ b/syntax/bootstrap.php @@ -122,46 +122,28 @@ function checkAttributes($attributes = array()) { } - /** - * This function replaces placeholders in the specified markup with styling attribute values from the user. - * It will only do this if the user has allowed styling attributes in the config. - * The markup string should contain three %s placeholders. - * If the markup already has styling or a class you can put the placeholder within the brackets. - * For example:
or if it doesn't have a class you'd use
- * @param $markup Html markup element containting placeholders for the styling attributes. - * @param array $attributes Attribute values from user. - * @return The markup passed in with styling parsed. - */ - function parseMarkupSyntax($markup, $attributes = array()) { - $styling = $this->getStylingAttributes($attributes); - - if (strpos($markup, 'class=') === false) { - $styling['class'] = 'class="' . $styling['class'] . '"'; - } - if (strpos($markup, 'id=') === false) { - $styling['id'] = ''; //There can only be one ID - } - if (strpos($markup, 'style=') === false) { - $styling['style'] = 'style="' . $styling['style'] . '"'; - } - - $markup = sprintf($markup, $styling['class'], $styling['id'], $styling['style']); - return $markup; - } - /** * Get array with styling attributes from the attributes array passed in. * If the user has styling disabled this array will contain empty strings. * @param array $attributes The attribute array with user values. * @return array The array with styling attribute values or empty string. */ - function getStylingAttributes($attributes = array()) { - $styling = array('class' => '', 'id' => '', 'style' => ''); + function getStylingAttributes($attributes) { + $styling = array( + 'class' => '', + 'id' => '', + 'style' => '' + ); + if ($this->getConf('allowStylingAttributes')) { - $styling['class'] = $attributes['class']; - $styling['id'] = $attributes['id']; - $styling['style'] = $attributes['style']; + if (isset($attributes['class']) && $attributes['class']) + $styling['class'] = $attributes['class']; + if (isset($attributes['id']) && $attributes['id']) + $styling['id'] = $attributes['id']; + if (isset($attributes['style']) && $attributes['style']) + $styling['style'] = $attributes['style']; } + return $styling; } @@ -259,7 +241,9 @@ function render($mode, Doku_Renderer $renderer, $data) { switch($state) { case DOKU_LEXER_ENTER: - $markup = $this->parseMarkupSyntax($this->template_start, $attributes); + $style = $this->getStylingAttributes($attributes); + + $markup = sprintf($this->template_start, $style['class'], $style['id'], $style['style']); $renderer->doc .= $markup; return true; From 029ace75d40d0171b443e7c599a4d10bc751c853 Mon Sep 17 00:00:00 2001 From: Jos Date: Fri, 19 Feb 2016 19:33:23 +0100 Subject: [PATCH 6/8] Implement styling attributes for all components. --- script.js | 8 ++++++++ syntax/accordion.php | 7 +++++-- syntax/affix.php | 4 +++- syntax/alert.php | 6 ++++-- syntax/badge.php | 6 +++--- syntax/button.php | 1 + syntax/callout.php | 4 +++- syntax/caption.php | 4 ++-- syntax/carousel.php | 4 +++- syntax/collapse.php | 5 ++++- syntax/column.php | 4 +++- syntax/grid.php | 4 ++-- syntax/hidden.php | 4 ++-- syntax/image.php | 4 +++- syntax/invisible.php | 4 ++-- syntax/jumbotron.php | 4 +++- syntax/label.php | 4 +++- syntax/lead.php | 4 ++-- syntax/list.php | 4 ++-- syntax/nav.php | 4 +++- syntax/pageheader.php | 4 ++-- syntax/pane.php | 5 ++++- syntax/panel.php | 4 +++- syntax/panelbody.php | 4 ++-- syntax/popover.php | 5 +++-- syntax/progress.php | 4 ++-- syntax/progressbar.php | 5 +++-- syntax/row.php | 2 +- syntax/show.php | 4 ++-- syntax/slide.php | 4 ++-- syntax/text.php | 7 +++---- syntax/thumbnail.php | 4 ++-- syntax/tooltip.php | 4 +++- syntax/well.php | 5 ++++- 34 files changed, 97 insertions(+), 53 deletions(-) diff --git a/script.js b/script.js index 13b9c6f..3183d34 100644 --- a/script.js +++ b/script.js @@ -122,6 +122,14 @@ jQuery(document).ready(function() { case 'btnSize': btn_class.push(['btn-', value].join('')); break; + case 'btnClass': + btn_class.push(value); + case 'btnId': + $btn_link.attr('id', value); + break; + case 'btnStyle': + $btn_link.attr('style', value); + break; case 'btnBlock': btn_class.push('btn-block'); break; diff --git a/syntax/accordion.php b/syntax/accordion.php index b82b95d..75a7221 100644 --- a/syntax/accordion.php +++ b/syntax/accordion.php @@ -14,7 +14,7 @@ class syntax_plugin_bootswrapper_accordion extends syntax_plugin_bootswrapper_bootstrap { - protected $pattern_start = ''; + protected $pattern_start = '(?=.*?)'; protected $pattern_end = ''; protected $tag_attributes = array( @@ -42,7 +42,10 @@ function render($mode, Doku_Renderer $renderer, $data) { case DOKU_LEXER_ENTER: $id = $attributes['id']; - $markup = sprintf('
', $id); + $style = $this->getStylingAttributes($attributes); + + $markup = sprintf('
', + $style['class'], $id, $style['style']); $renderer->doc .= $markup; return true; diff --git a/syntax/affix.php b/syntax/affix.php index 576f4b5..e5feed2 100644 --- a/syntax/affix.php +++ b/syntax/affix.php @@ -55,6 +55,7 @@ function render($mode, Doku_Renderer $renderer, $data) { $top = $attributes['offset-top']; $bottom = $attributes['offset-bottom']; $target = $attributes['target']; + $style = $this->getStylingAttributes($attributes); $data = array(); if ($top) { @@ -67,7 +68,8 @@ function render($mode, Doku_Renderer $renderer, $data) { $data[] = sprintf('data-target="%s"', $target); } - $markup = sprintf('
', implode(' ', $data)); + $markup = sprintf('
', + $style['class'], $style['id'], $style['style'], implode(' ', data)); $renderer->doc .= $markup; return true; diff --git a/syntax/alert.php b/syntax/alert.php index eac705c..a7abcd8 100644 --- a/syntax/alert.php +++ b/syntax/alert.php @@ -50,9 +50,11 @@ function render($mode, Doku_Renderer $renderer, $data) { case DOKU_LEXER_ENTER: extract($attributes); + $style = $this->getStylingAttributes($attributes); + print_r($style); - $markup = sprintf('