utf-8.pl 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #! /usr/bin/perl
  2. # Copyright (C) 2004 Alex Schroeder <alex@emacswiki.org>
  3. #
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; either version 3 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. use CGI qw/:standard/;
  17. use CGI::Carp qw(fatalsToBrowser);
  18. use Encode;
  19. sub translate {
  20. my $str = shift;
  21. $str = encode('utf-8', decode('latin-1', $str));
  22. my @letters = split(//, $str);
  23. my @safe = ('a' .. 'z', 'A' .. 'Z', '0' .. '9', '-', '_', '.', '!', '~', '*', "'", '(', ')',
  24. ':', '/', '?', ';', '&');
  25. foreach my $letter (@letters) {
  26. my $pattern = quotemeta($letter);
  27. if (not grep(/$pattern/, @safe)) {
  28. $letter = uc(sprintf("%%%02x", ord($letter)));
  29. }
  30. }
  31. return join('', @letters);
  32. }
  33. if (not param('url')) {
  34. print header(),
  35. start_html('Latin-1 to UTF-8 Escapes'),
  36. h1('Latin-1 to UTF-8 Escapes'),
  37. p('Translates URLs containing URL-encoded Latin-1 to ',
  38. 'URLs containing URL-encoded UTF-8 and redirects to it.'),
  39. start_form(-method=>'GET'),
  40. p('URL: ', textfield('url', '', 70)),
  41. p(submit()),
  42. end_form(),
  43. end_html();
  44. exit;
  45. }
  46. my $str = param('url');
  47. print redirect(translate($str));
  48. # print $str, "\n";
  49. # print translate($str), "\n";
  50. # perl latin-1.pl url=http://www.emacswiki.org/cgi-bin/community/LangueFran%E7aise