Page MenuHomeDevCentral

D107.diff
No OneTemporary

D107.diff

diff --git a/lists/RegexpFactory.php b/lists/RegexpFactory.php
--- a/lists/RegexpFactory.php
+++ b/lists/RegexpFactory.php
@@ -5,10 +5,16 @@
*/
class RegexpFactory {
/**
- * The regular expression
- * @var string
+ * The regular expressions
+ * @var string[]
+ */
+ public $expressions;
+
+ /**
+ * The replacement expressions
+ * @var string[]
*/
- public $expression;
+ public $replaceExpressions;
/**
* The last error
@@ -17,49 +23,91 @@
public $lastError;
/**
+ * Regualar expression to delimit regexps/replaces
+ */
+ const DELIMITER = '/\r\n|\n|\r/';
+
+ /**
* Initializes a new instance of the RegexpFactory object.
*
- * @param string The regular expression
+ * @param string $rexpressions The regular expression
+ * @param string $replaceExpressions The format of the result string
*/
- function __construct ($expression) {
- $this->expression = $expression;
+ function __construct ($expressions, $replaceExpressions) {
+ $this->expressions = preg_split(static::DELIMITER, $expressions);
+ $this->replaceExpressions = preg_split(static::DELIMITER, $replaceExpressions);
+
+ if (count($this->expressions) != count($this->replaceExpressions)) {
+ throw new Exception("The number of expressions and replacements should match.");
+ }
}
/**
* Replaces an expression using regexp, a similar way Apache mod_rewrite and Nginx wreplace do.
*
* @param string $haystack The expression to perform a replacement on
- * @param string $replaceExpression The format of the result string
* @return string The replaced string
*/
- function replace ($haystack, $replaceExpression) {
- return preg_replace($this->expression, $replaceExpression, $haystack);
+ function replace ($haystack) {
+ $text = $haystack;
+ for ($i = 0 ; $i < count($this->expressions) ; $i++) {
+ $expression = $this->expressions[$i];
+ $replaceExpression = $this->replaceExpressions[$i];
+ $text = preg_replace($expression, $replaceExpression, $text);
+ }
+ return $text;
+ }
+
+ /**
+ * Adds delimiters to each regular expression.
+ */
+ public function addDelimiters () {
+ array_walk($this->expressions, function (&$item) {
+ $item = RegexpFactory::addDelimitersToExpression($item);
+ });
}
/**
- * Encloses the regular expression with delimiters.
+ * Encloses the specified regular expression with delimiters.
+ *
+ * @param string $expression The expression to delimit
+ * @return string The expression with delimiters
*/
- function addDelimiters () {
+ public static function addDelimitersToExpression ($expression) {
$delimiters = ['/', '@', '#', '~', '+', '%', '♦', 'µ', '±', '☞'];
//TODO: check if it's okay to use UTF-8 characters as delimiters
foreach ($delimiters as $delimiter) {
- if (strpos($this->expression, $delimiter) === false) {
- $this->expression = $delimiter . $this->expression . $delimiter;
- return;
+ if (strpos($expression, $delimiter) === false) {
+ return $delimiter . $expression . $delimiter;
+ }
+ }
+ throw new Exception("Can't delimite regexp $expression");
+ }
+
+ /**
+ * Determines if the specified expression block is valid.
+ *
+ * @return bool true if each expression is valid; otherwise, false.
+ */
+ public function isValid () {
+ foreach ($this->expressions as $expression) {
+ if (!$this->isValidExpression($expression)) {
+ return false;
}
}
- throw new Exception("Can't delimite regexp $this->expression");
+ return true;
}
/**
* Determines if the specified expression is valid.
*
+ * @param string the regexp to test
* @return bool true if the expression is valid; otherwise, false.
*/
- public function isValid () {
+ public function isValidExpression ($expression) {
$this->lastError = '';
set_error_handler('self::handleErrors');
- $result = preg_match($this->expression, null);
+ $result = preg_match($expression, null);
restore_error_handler();
if ($this->lastError === '' && $result === false) {
$this->lastError = self::getPCREError();
@@ -83,9 +131,9 @@
return true;
}
message_die(
- GENERAL_ERROR,
- $errstr . "<p>Please report this bug. This error should be handled by the regexp factory.</p>",
- 'Regexp factory error'
+ GENERAL_ERROR,
+ $errstr . "<p>Please report this bug. This error should be handled by the regexp factory.</p>",
+ 'Regexp factory error'
);
}
diff --git a/lists/_documents.xml b/lists/_documents.xml
--- a/lists/_documents.xml
+++ b/lists/_documents.xml
@@ -13,5 +13,16 @@
<article>replace</article>
<title>List replace</title>
<description>Performs regexp replacement in a list</description>
+ <head><![CDATA[
+ <link rel="stylesheet" href="/fonts/fi3/foundation-icons.css">]]>
+</head>
+ </document>
+ <document>
+ <article>replace-multi</article>
+ <title>List replace</title>
+ <description>Performs regexp replacement in a list</description>
+ <head><![CDATA[
+ <link rel="stylesheet" href="/fonts/fi3/foundation-icons.css">]]>
+</head>
</document>
</documents>
diff --git a/lists/replace.php b/lists/replace-multi.php
copy from lists/replace.php
copy to lists/replace-multi.php
--- a/lists/replace.php
+++ b/lists/replace-multi.php
@@ -3,11 +3,15 @@
// Handles permanent links
if (array_key_exists('r', $_REQUEST)) {
- $_REQUEST = unserialize(base64_decode($_REQUEST['r']));
+ $request = json_decode(base64_decode($_REQUEST['r'], true));
+ foreach ($request as $key => $value) {
+ $_REQUEST[$key] = $value;
+ }
}
// bit.ly
define('FEATURE_BITLY', array_key_exists('BitLyToken', $Config));
+//define('FEATURE_BITLY', false);
// Options handling
@@ -16,17 +20,17 @@
$enable_split = array_key_exists('split', $_REQUEST) && $_REQUEST["split"] == 'on';
if (array_key_exists('expression', $_REQUEST)) {
- $requestSerialized = base64_encode(serialize($_REQUEST));
+ $requestSerialized = base64_encode(json_encode($_REQUEST));
if (array_key_exists('lists', $_REQUEST) && array_key_exists(0, $_REQUEST['lists']) && array_key_exists('replacement', $_REQUEST)) {
- $regexp = new RegexpFactory($_REQUEST['expression']);
+ $regexp = new RegexpFactory($_REQUEST['expression'], $_REQUEST['replacement']);
$regexp->addDelimiters();
if ($regexp->isValid()) {
$items = explode("\n", $_REQUEST['lists'][0]);
- $replace_callback = function (&$item, $key, $replaceExpression) use ($regexp) {
- $item = $regexp->replace(trim($item), $replaceExpression);
+ $replace_callback = function (&$item, $key) use ($regexp) {
+ $item = $regexp->replace(trim($item));
};
- array_walk($items, $replace_callback, $_REQUEST['replacement']);
+ array_walk($items, $replace_callback);
$result = join("\n", $items);
if ($enable_join) {
$result = join($_REQUEST["joinglue"], $items);
@@ -43,49 +47,33 @@
//If no list is given, or the replacement expression is blank, the result list is blank.
}
?>
- <script>
- /**
- * Updates the form's widgets
- */
- function updateUI () {
- //Checks the join enable box when a glue string is provided
- if (document.getElementById("joinglue").value != "" && !document.getElementById("join").checked) {
- document.getElementById("join").checked = true;
- $("#join-checkbox .checkbox").addClass("checked");
- }
-
- //Checks the split enable box when a split seperator is provided
- if (document.getElementById('splitseparator').value != "" && !document.getElementById("split").checked) {
- document.getElementById("split").checked = true;
- $("#split-checkbox .checkbox").addClass("checked");
- }
- }
- </script>
- <form name="lists" method="post" action="/lists/replace" class="custom">
+ <form name="lists" method="post" action="/lists/replace-multi" class="custom">
+ <div class="row collapse" style="margin-bottom: 0.5em;">
+ <div class="twelve mobile-eight columns">
+ <a href="#" class="button" title="Toggle join/split options" onclick="$('.joinsplit').toggle();"><i class="fi-arrows-expand"></i></a>
+ </div>
+ </div>
<div class="row collapse">
<div class="one mobile-one columns">
- <span class="prefix">Replace</span>
+ <span class="prefix" style="height: 3em;">Replace</span>
</div>
<div class="five mobile-three columns">
- <input
- name="expression" id="expression" type="text"
+ <textarea
+ name="expression" id="expression" rows="3" style="width: 99%;"
placeholder="The Nginx or Apache mod_rewrite-like regular expression"
- value="<?= $_REQUEST['expression'] ?>"
- />
- <?php if (isset($regexp) && $regexp->lastError) echo '<small class="error" style="font-weight: 400;">', $regexp->lastError, '</small>'; ?>
+ ><?= $_REQUEST['expression'] ?></textarea>
+ <?php if (isset($regexp) && $regexp->lastError) { echo '<small class="error" style="font-weight: 400;">', $regexp->lastError, '</small>'; } ?>
</div>
<div class="five mobile-three columns">
- <input
- name="replacement" id="replacement" type="text"
- placeholder="The list format. Use $1, $2, … to use the regexp groups."
- value="<?= $_REQUEST['replacement'] ?>"
- />
+ <textarea id="replacement" name="replacement" rows="3" style="width: 99%;"
+ placeholder="The replaced lines format. Use $1, $2, … to use the regexp groups."
+ ><?= $_REQUEST['replacement'] ?></textarea>
</div>
<div class="one mobile-one columns">
- <input type="submit" class="button expand postfix" value="Format" />
+ <input type="submit" class="button expand postfix" value="OK" />
</div>
</div>
- <div class="row collapse">
+ <div class="row collapse joinsplit hide">
<div class="one mobile-one columns">
<span class="prefix">Join</span>
</div>
@@ -96,7 +84,7 @@
<span id="join-checkbox"><input type="checkbox" id="join" name="join" <?= $enable_join ? 'checked ' : '' ?>/><br /><label for="join">Enable</label></span>
</div>
</div>
- <div class="row collapse">
+ <div class="row collapse joinsplit hide">
<div class="one mobile-one columns">
<span class="prefix">Split</span>
</div>
@@ -109,16 +97,16 @@
</div>
<div class="row collapse">
<div class="six columns">
- <textarea name="lists[0]" rows="16" style="width: 99%;" placeholder="The list to format"><?= $_REQUEST['lists'][0] ?></textarea>
+ <textarea name="lists[0]" rows="16" style="width: 99%;" placeholder="The lines to replace"><?= $_REQUEST['lists'][0] ?></textarea>
</div>
<div class="six columns">
- <textarea name="lists[1]" rows="16" style="width: 99%;" placeholder="The formatted list will appear here."><?= $result ?></textarea>
+ <textarea name="lists[1]" rows="16" style="width: 99%;" placeholder="The replaced lines will appear here."><?= $result ?></textarea>
</div>
</div>
</form>
<?php
if (isset($requestSerialized)) {
- $permUrl = "/lists/replace/?r=$requestSerialized";
+ $permUrl = "/lists/replace-multi/?r=$requestSerialized";
if (FEATURE_BITLY) {
try {
$permUrl = bitly_shorten(get_server_url() . $permUrl);
@@ -131,3 +119,22 @@
?>
<p><strong>Documentation resources:</strong> <a href="http://perldoc.perl.org/perlre.html">PCRE syntax</a> • <a href="http://www.cheatography.com/davechild/cheat-sheets/regular-expressions/">Regular Expressions Cheat Sheet</a>
<br /><strong>Note:</strong> Write your regexp without delimiter.</p>
+
+ <script>
+ /**
+ * Updates the form's widgets
+ */
+ function updateUI () {
+ //Checks the join enable box when a glue string is provided
+ if (document.getElementById("joinglue").value != "" && !document.getElementById("join").checked) {
+ document.getElementById("join").checked = true;
+ $("#join-checkbox .checkbox").addClass("checked");
+ }
+
+ //Checks the split enable box when a split seperator is provided
+ if (document.getElementById('splitseparator').value != "" && !document.getElementById("split").checked) {
+ document.getElementById("split").checked = true;
+ $("#split-checkbox .checkbox").addClass("checked");
+ }
+ }
+ </script>
diff --git a/lists/replace.php b/lists/replace.php
--- a/lists/replace.php
+++ b/lists/replace.php
@@ -14,19 +14,20 @@
$result = '';
$enable_join = array_key_exists('join', $_REQUEST) && $_REQUEST["join"] == 'on';
$enable_split = array_key_exists('split', $_REQUEST) && $_REQUEST["split"] == 'on';
+$enable_join_split = $enable_join || $enable_split;
if (array_key_exists('expression', $_REQUEST)) {
$requestSerialized = base64_encode(serialize($_REQUEST));
if (array_key_exists('lists', $_REQUEST) && array_key_exists(0, $_REQUEST['lists']) && array_key_exists('replacement', $_REQUEST)) {
- $regexp = new RegexpFactory($_REQUEST['expression']);
+ $regexp = new RegexpFactory($_REQUEST['expression'], $_REQUEST['replacement']);
$regexp->addDelimiters();
if ($regexp->isValid()) {
$items = explode("\n", $_REQUEST['lists'][0]);
- $replace_callback = function (&$item, $key, $replaceExpression) use ($regexp) {
- $item = $regexp->replace(trim($item), $replaceExpression);
+ $replace_callback = function (&$item, $key) use ($regexp) {
+ $item = $regexp->replace(trim($item));
};
- array_walk($items, $replace_callback, $_REQUEST['replacement']);
+ array_walk($items, $replace_callback);
$result = join("\n", $items);
if ($enable_join) {
$result = join($_REQUEST["joinglue"], $items);
@@ -43,25 +44,12 @@
//If no list is given, or the replacement expression is blank, the result list is blank.
}
?>
- <script>
- /**
- * Updates the form's widgets
- */
- function updateUI () {
- //Checks the join enable box when a glue string is provided
- if (document.getElementById("joinglue").value != "" && !document.getElementById("join").checked) {
- document.getElementById("join").checked = true;
- $("#join-checkbox .checkbox").addClass("checked");
- }
-
- //Checks the split enable box when a split seperator is provided
- if (document.getElementById('splitseparator').value != "" && !document.getElementById("split").checked) {
- document.getElementById("split").checked = true;
- $("#split-checkbox .checkbox").addClass("checked");
- }
- }
- </script>
<form name="lists" method="post" action="/lists/replace" class="custom">
+ <div class="row collapse" style="margin-bottom: 0.5em;">
+ <div class="twelve mobile-eight columns">
+ <a href="#" class="button" title="Toggle join/split options" onclick="$('.joinsplit').toggle();"><i class="fi-arrows-expand"></i></a>
+ </div>
+ </div>
<div class="row collapse">
<div class="one mobile-one columns">
<span class="prefix">Replace</span>
@@ -77,15 +65,15 @@
<div class="five mobile-three columns">
<input
name="replacement" id="replacement" type="text"
- placeholder="The list format. Use $1, $2, … to use the regexp groups."
+ placeholder="How to replace? Use $1, $2, … to use the regexp groups."
value="<?= $_REQUEST['replacement'] ?>"
/>
</div>
<div class="one mobile-one columns">
- <input type="submit" class="button expand postfix" value="Format" />
+ <input type="submit" class="button expand postfix" value="OK" />
</div>
</div>
- <div class="row collapse">
+ <div class="row collapse joinsplit<?= $enable_join_split ? '' : ' hide' ?>">
<div class="one mobile-one columns">
<span class="prefix">Join</span>
</div>
@@ -96,7 +84,7 @@
<span id="join-checkbox"><input type="checkbox" id="join" name="join" <?= $enable_join ? 'checked ' : '' ?>/><br /><label for="join">Enable</label></span>
</div>
</div>
- <div class="row collapse">
+ <div class="row collapse joinsplit<?= $enable_join_split ? '' : ' hide' ?>">
<div class="one mobile-one columns">
<span class="prefix">Split</span>
</div>
@@ -131,3 +119,22 @@
?>
<p><strong>Documentation resources:</strong> <a href="http://perldoc.perl.org/perlre.html">PCRE syntax</a> • <a href="http://www.cheatography.com/davechild/cheat-sheets/regular-expressions/">Regular Expressions Cheat Sheet</a>
<br /><strong>Note:</strong> Write your regexp without delimiter.</p>
+
+ <script>
+ /**
+ * Updates the form's widgets
+ */
+ function updateUI () {
+ //Checks the join enable box when a glue string is provided
+ if (document.getElementById("joinglue").value != "" && !document.getElementById("join").checked) {
+ document.getElementById("join").checked = true;
+ $("#join-checkbox .checkbox").addClass("checked");
+ }
+
+ //Checks the split enable box when a split seperator is provided
+ if (document.getElementById('splitseparator').value != "" && !document.getElementById("split").checked) {
+ document.getElementById("split").checked = true;
+ $("#split-checkbox .checkbox").addClass("checked");
+ }
+ }
+ </script>

File Metadata

Mime Type
text/plain
Expires
Mon, Sep 30, 23:48 (17 h, 34 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2167374
Default Alt Text
D107.diff (18 KB)

Event Timeline