return '{{_s' + index + '}}';
};
+ /**
+ * Quote helper used to wrap content in quotes, and escape all quotes present in the content.
+ *
+ * @method quoteHelper
+ * @private
+ * @param {string} sectionText The text to parse the arguments from.
+ * @param {function} helper Used to render subsections of the text.
+ * @return {string}
+ */
+ var quoteHelper = function(sectionText, helper) {
+ var content = helper(sectionText.trim(), this);
+
+ // Escape the {{ and the ".
+ // This involves wrapping {{, and }} in change delimeter tags.
+ content = content
+ .replace('"', '\\"')
+ .replace(/([\{\}]{2,3})/g, '{{=<% %>=}}$1<%={{ }}=%>')
+ ;
+ return '"' + content + '"';
+ };
+
/**
* Add some common helper functions to all context objects passed to templates.
* These helpers match exactly the helpers available in php.
context.str = function() { return stringHelper; };
context.pix = function() { return pixHelper; };
context.js = function() { return jsHelper; };
+ context.quote = function() { return quoteHelper; };
context.globals = { config : config };
context.currentTheme = themeName;
};
--- /dev/null
+<?php
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle 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 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle 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 Moodle. If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * Wrap content in quotes, and escape all quotes used.
+ *
+ * @package core
+ * @category output
+ * @copyright 2016 Andrew Nicols <andrew@nicols.co.uk>
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace core\output;
+
+/**
+ * Wrap content in quotes, and escape all quotes used.
+ *
+ * @copyright 2016 Andrew Nicols <andrew@nicols.co.uk>
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class mustache_quote_helper {
+
+ /**
+ * Wrap content in quotes, and escape all quotes used.
+ *
+ * Note: This helper is only compatible with the standard {{ }} delimeters.
+ *
+ * @param string $text The text to parse for arguments.
+ * @param Mustache_LambdaHelper $helper Used to render nested mustache variables.
+ * @return string
+ */
+ public function quote($text, \Mustache_LambdaHelper $helper) {
+ // Split the text into an array of variables.
+ $content = trim($text);
+ $content = $helper->render($content);
+
+ // Escape the {{ and the ".
+ $content = str_replace('"', '\\"', $content);
+ $content = preg_replace('([{}]{2,3})', '{{=<% %>=}}${0}<%={{ }}=%>', $content);
+ return '"' . $content . '"';
+ }
+}
$loader = new \core\output\mustache_filesystem_loader();
$stringhelper = new \core\output\mustache_string_helper();
+ $quotehelper = new \core\output\mustache_quote_helper();
$jshelper = new \core\output\mustache_javascript_helper($this->page->requires);
$pixhelper = new \core\output\mustache_pix_helper($this);
$helpers = array('config' => $safeconfig,
'str' => array($stringhelper, 'str'),
+ 'quote' => array($quotehelper, 'quote'),
'js' => array($jshelper, 'help'),
'pix' => array($pixhelper, 'pix'));