Skip to content

Commit

Permalink
Highlight special characters, support comparison and onestring view
Browse files Browse the repository at this point in the history
Fixes #761

Make special char highlighting consistent accross views + highlight source strings
  • Loading branch information
TheoChevalier authored and flodolo committed Aug 2, 2016
1 parent 0732efa commit 719d5e9
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 56 deletions.
30 changes: 3 additions & 27 deletions app/classes/Transvision/ShowResults.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,25 +215,6 @@ public static function formatEntity($entity, $highlight = false)
return $repo . '<span class="superset">&nbsp;&bull;&nbsp;</span>' . $path . '<br>' . $entity;
}

/**
* Highlight specific elements in the string.
*
* @param string $string Source text
* @return string Same string with specific sub-strings in <span>
* elements for styling with CSS
*/
public static function highlight($string)
{
$replacements = [
' ' => '<span class="highlight-space" title="White space"> </span>',
' ' => '<span class="highlight-red" title="Unicode non-breaking space"> </span>',
'' => '<span class="highlight-gray" title="Real ellipsis">…</span>',
'&hellip;' => '<span class="highlight-red" title="HTML ellipsis">…</span>',
];

return Strings::multipleStringReplace($replacements, $string);
}

/**
* Html table of search results used by the main view (needs a lot of refactoring)
*
Expand Down Expand Up @@ -328,20 +309,15 @@ public static function resultsTable($search_object, $search_results, $page)
$target_string = htmlspecialchars($target_string);
$source_string = Utils::highlightString($source_string);
$target_string = Utils::highlightString($target_string);
$source_string = Strings::highlightSpecial($source_string);
$target_string = Strings::highlightSpecial($target_string);

if ($extra_locale) {
$target_string2 = htmlspecialchars($target_string2);
$target_string2 = Utils::highlightString($target_string2);
$target_string2 = Strings::highlightSpecial($target_string2);
}

$replacements = [
' ' => '<span class="highlight-gray" title="Non breakable space"> </span>', // Nbsp highlight
'' => '<span class="highlight-red" title="Thin space"> </span>', // Thin space highlight
'' => '<span class="highlight-gray">…</span>', // Right ellipsis highlight
'&hellip;' => '<span class="highlight-gray">…</span>', // Right ellipsis highlight
];

$target_string = Strings::multipleStringReplace($replacements, $target_string);
$clipboard_target_string = 'clip_' . md5($target_string);
$clipboard_target_string2 = 'clip_' . md5($target_string2);

Expand Down
27 changes: 27 additions & 0 deletions app/classes/Transvision/Strings.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,33 @@ public static function multipleStringReplace($replacements, $string)
return str_replace(array_keys($replacements), $replacements, $string);
}

/**
* Highlight special characters in the string
*
* @param string $string Source text
* @param boolean $exclude_whitespaces Optional param to specify if we need
* to highlight white spaces. White
* spaces are not highlighted by default.
* @return string Same string with specific sub-strings in <span>
* elements for styling with CSS
*/
public static function highlightSpecial($string, $exclude_whitespaces = true)
{
$replacements = [
' ' => '<span class="highlight-space" title="White space"> </span>',
' ' => '<span class="highlight-gray" title="Non breakable space"> </span>',
'' => '<span class="highlight-red" title="Narrow no-break space"> </span>',
'' => '<span class="highlight-gray" title="Real ellipsis">…</span>',
'&hellip;' => '<span class="highlight-red" title="HTML ellipsis">…</span>',
];

if ($exclude_whitespaces) {
unset($replacements[' ']);
}

return self::multipleStringReplace($replacements, $string);
}

/**
* Get multibyte UTF-8 string length, html tags stripped
*
Expand Down
16 changes: 12 additions & 4 deletions app/models/channelcomparison.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,25 @@

/*
Find new locale strings added between repositories, store them with the
reference string.
reference string and highlight special characters.
*/
$added_strings = array_diff_key($strings[$chan1], $strings[$chan2]);
$new_strings = [];
$en_US_strings_chan1 = Utils::getRepoStrings('en-US', $chan1);

foreach ($added_strings as $string_id => $translation) {
$reference_string = isset($en_US_strings_chan1[$string_id])
? $en_US_strings_chan1[$string_id]
: '@N/A@';
: '@@missing@@';

$new_strings[$string_id] = [
'reference' => $reference_string,
'translation' => $translation,
'reference' => Utils::secureText($reference_string),
'translation' => Strings::highlightSpecial(Utils::secureText($translation)),
];
}

// Highlight special characters in common strings
foreach ($common_strings as $key => &$value) {
$common_strings[$key] = Strings::highlightSpecial(Utils::secureText($value));
$strings[$chan2][$key] = Strings::highlightSpecial(Utils::secureText($strings[$chan2][$key]));
}
12 changes: 6 additions & 6 deletions app/views/channelcomparison.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@
<?php foreach ($common_strings as $key => $value) : ?>
<tr>
<?=$td('Key', ShowResults::formatEntity($key))?>
<?=$td($chan1, Utils::secureText($value))?>
<?=$td($chan2, Utils::secureText($strings[$chan2][$key]))?>
<?=$td($chan1, $value)?>
<?=$td($chan2, $strings[$chan2][$key])?>
</tr>
<?php endforeach; ?>
</tbody>
Expand All @@ -91,12 +91,12 @@
<?php foreach ($new_strings as $string_id => $string_values): ?>
<tr>
<?=$td('Entity', ShowResults::formatEntity($string_id))?>
<?php if ($new_strings[$string_id]['reference'] != '@N/A@'): ?>
<?=$td('en-US', Utils::secureText($string_values['reference']))?>
<?php if ($new_strings[$string_id]['reference'] != '@@missing@@'): ?>
<?=$td('en-US', $string_values['reference'])?>
<?php else: ?>
<?=$td('en-US', '<em class="error">(not available)</em>')?>
<?=$td('en-US', '<em class="error">Warning: Missing string</em>')?>
<?php endif; ?>
<?=$td($locale, Utils::secureText($string_values['translation']))?>
<?=$td($locale, $string_values['translation'])?>
</tr>
<?php endforeach; ?>
<tbody>
Expand Down
2 changes: 1 addition & 1 deletion app/views/consistency.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
echo '<a href="' . $search_link . '" title="Search for this string">' . Utils::secureText($data['source']) . "</a></td>\n";
echo '<td>';
foreach ($data['target'] as $target) {
echo '<div class="inconsistent_translation highlight-specialchars">' . ShowResults::highlight(Utils::secureText($target)) . "</div>\n";
echo '<div class="inconsistent_translation highlight-specialchars">' . Strings::highlightSpecial(Utils::secureText($target), false) . "</div>\n";
}
echo "</td>\n</tr>\n";
}
Expand Down
2 changes: 1 addition & 1 deletion app/views/onestring.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
if (! $translation) {
echo " <td><em class='error'>Warning: Empty string</em></td><td></td>\n";
} else {
echo " <td lang='#{$locale}' {$rtl_support} >" . Utils::secureText($translation) . "</td>\n" .
echo " <td lang='#{$locale}' {$rtl_support} >" . Strings::highlightSpecial(Utils::secureText($translation)) . "</td>\n" .
" <td><a class='onestring_search' href='{$search_link}' title='Search for the entity in this locale'>🔍</a></td>\n";
}

Expand Down
11 changes: 5 additions & 6 deletions app/views/results_entities.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,9 @@
$bz_target_string = $target_string = isset($tmx_target[$entity])
? Utils::secureText($tmx_target[$entity])
: '@@missing@@';
// Highlight non-breaking spaces only after strings have been escaped
$target_string = str_replace(' ', '<span class="highlight-gray"> </span>', $target_string);

$source_string = Utils::secureText($tmx_source[$entity]);
// Highlight special characters only after strings have been escaped
$target_string = Strings::highlightSpecial($target_string);
$source_string = Strings::highlightSpecial(Utils::secureText($tmx_source[$entity]));

$clipboard_target_string = 'clip_' . md5($target_string);

Expand All @@ -46,8 +45,8 @@
$bz_target_string2 = $target_string2 = isset($tmx_target2[$entity])
? Utils::secureText($tmx_target2[$entity])
: '';
// Highlight non-breaking spaces only after strings have been escaped
$target_string2 = str_replace(' ', '<span class="highlight-gray"> </span>', $target_string2);
// Highlight special characters only after strings have been escaped
$target_string2 = Strings::highlightSpecial($target_string2);

$clipboard_target_string2 = 'clip_' . md5($target_string2);

Expand Down
8 changes: 0 additions & 8 deletions tests/units/Transvision/ShowResults.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,6 @@ public function testGetTMXResults($a, $b, $c)
->isEqualTo($c);
}

public function testHighlight()
{
$obj = new _ShowResults();
$this
->string($obj->highlight('Foo is bar ; Bar is Foo…'))
->isEqualTo('Foo<span class="highlight-space" title="White space"> </span>is<span class="highlight-space" title="White space"> </span>bar<span class="highlight-red" title="Unicode non-breaking space"> </span>;<span class="highlight-red" title="Unicode non-breaking space"> </span>Bar<span class="highlight-space" title="White space"> </span>is<span class="highlight-space" title="White space"> </span>Foo<span class="highlight-gray" title="Real ellipsis">…</span>');
}

public function getTranslationMemoryResultsDP()
{
include TMX . 'en-US/cache_en-US_central.php';
Expand Down
11 changes: 11 additions & 0 deletions tests/units/Transvision/Strings.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,17 @@ public function testmultipleStringReplace($a, $b, $c)
->isEqualTo($c);
}

public function testHighlightSpecial()
{
$obj = new _Strings();
$this
->string($obj->highlightSpecial('Foo is bar ; Bar is Foo…'))
->isEqualTo('Foo is bar<span class="highlight-gray" title="Non breakable space"> </span>;<span class="highlight-gray" title="Non breakable space"> </span>Bar is Foo<span class="highlight-gray" title="Real ellipsis">…</span>');
$this
->string($obj->highlightSpecial('Foo is bar ; Bar is Foo…', false))
->isEqualTo('Foo<span class="highlight-space" title="White space"> </span>is<span class="highlight-space" title="White space"> </span>bar<span class="highlight-gray" title="Non breakable space"> </span>;<span class="highlight-gray" title="Non breakable space"> </span>Bar<span class="highlight-space" title="White space"> </span>is<span class="highlight-space" title="White space"> </span>Foo<span class="highlight-gray" title="Real ellipsis">…</span>');
}

public function getLengthDP()
{
return [
Expand Down
6 changes: 3 additions & 3 deletions web/style/transvision.css
Original file line number Diff line number Diff line change
Expand Up @@ -474,18 +474,18 @@ table tr td.no_highligth {
white-space: pre-wrap; /* That allows exposing contiguous white spaces */
}

.highlight-specialchars .highlight-gray {
.highlight-gray {
background-color: rgba(0, 0, 0, 0.2);
}

.highlight-specialchars .highlight-space {
.highlight-space {
background-color: rgba(0, 0, 0, 0.1);
display: inline-block;
height: 0.5em;
vertical-align: middle;
}

.highlight-specialchars .highlight-red {
.highlight-red {
background-color: rgba(255, 0, 0, 0.4);
}

Expand Down

0 comments on commit 719d5e9

Please sign in to comment.