Skip to content

Commit e06eea6

Browse files
authored
Merge pull request #2465 from infosiftr/unicode-vs-bytes
Trim descriptions based on bytes, not characters
2 parents 2532cd2 + 6fbb028 commit e06eea6

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

push.pl

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
use Term::UI;
1616
use Term::ReadLine;
1717

18-
my $hubLengthLimit = 25_000;
18+
require bytes; # this is not recommended, but we *only* use "bytes::length" from it to determine whether we need to do a more correct conversion to/from bytes for trimming (see $hubLengthLimit and usages)
19+
20+
my $hubLengthLimit = 25_000; # NOTE: this is *bytes*, not characters 🙃
1921
my $githubBase = 'https://github.com/docker-library/docs/tree/master'; # TODO point this at the correct "dist-xxx" branch based on "namespace"
2022

2123
my $username;
@@ -75,7 +77,7 @@ sub prompt_for_edit {
7577
$proposedText =~ s%$supportedTagsRegex%$sponsoredLinks$1$2%;
7678
}
7779

78-
if ($lengthLimit > 0 && length($proposedText) > $lengthLimit) {
80+
if ($lengthLimit > 0 && bytes::length($proposedText) > $lengthLimit) {
7981
# TODO https://github.com/docker/hub-beta-feedback/issues/238
8082
my $fullUrl = "$githubBase/$proposedFile";
8183
my $shortTags = "-\tSee [\"Supported tags and respective \`Dockerfile\` links\" at $fullUrl]($fullUrl#supported-tags-and-respective-dockerfile-links)\n\n";
@@ -91,9 +93,18 @@ sub prompt_for_edit {
9193
$trimmedText =~ s%$supportedTagsRegex%$sponsoredLinks$1$tagsNote%ms;
9294
# (we scrape until the next "h1" or a line starting with a link which is likely a build status badge for an architecture-namespace)
9395

94-
if (length($trimmedText) > $lengthLimit) {
95-
# ... if that doesn't do the trick, then do our older naïve description trimming
96-
$trimmedText = $startingNote . substr $proposedText, 0, ($lengthLimit - length($startingNote . $endingNote));
96+
if (bytes::length($trimmedText) > $lengthLimit) {
97+
# ... if that doesn't do the trick, then do our older naïve description trimming (respecting utf8; see https://www.perlmonks.org/?node_id=1230659 and https://perldoc.perl.org/utf8)
98+
$trimmedText = $proposedText;
99+
utf8::encode($trimmedText);
100+
$trimmedText = $startingNote . substr $trimmedText, 0, ($lengthLimit - bytes::length($startingNote . $endingNote));
101+
# (deal with the potential of "bytes::substr" here cutting us in the middle of a unicode glyph, which is arguably a much worse problem than the markdown cutting described below 😬 again, see https://www.perlmonks.org/?node_id=1230659)
102+
$trimmedText =~ s/(?:
103+
[\xC0-\xDF]
104+
| [\xE0-\xEF] [\x80-\xBF]?
105+
| [\xF0-\xF7] [\x80-\xBF]{0,2}
106+
)\z//x;
107+
utf8::decode($trimmedText);
97108

98109
# adding the "ending note" (https://github.com/docker/hub-feedback/issues/2220) is a bit more complicated as we have to deal with cutting off markdown ~cleanly so it renders correctly
99110
# TODO deal with "```foo" appropriately (so we don't drop our note in the middle of a code block) - the Hub's current markdown rendering (2022-04-07) does not auto-close a dangling block like this, so this isn't urgent

0 commit comments

Comments
 (0)