utf-8.pl 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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 2 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, write to the
  16. # Free Software Foundation, Inc.
  17. # 59 Temple Place, Suite 330
  18. # Boston, MA 02111-1307 USA
  19. use CGI qw/:standard/;
  20. use CGI::Carp qw(fatalsToBrowser);
  21. use Encode;
  22. sub translate {
  23. my $str = shift;
  24. $str = encode('utf-8', decode('latin-1', $str));
  25. my @letters = split(//, $str);
  26. my @safe = ('a' .. 'z', 'A' .. 'Z', '0' .. '9', '-', '_', '.', '!', '~', '*', "'", '(', ')',
  27. ':', '/', '?', ';', '&');
  28. foreach my $letter (@letters) {
  29. my $pattern = quotemeta($letter);
  30. if (not grep(/$pattern/, @safe)) {
  31. $letter = uc(sprintf("%%%02x", ord($letter)));
  32. }
  33. }
  34. return join('', @letters);
  35. }
  36. if (not param('url')) {
  37. print header(),
  38. start_html('Latin-1 to UTF-8 Escapes'),
  39. h1('Latin-1 to UTF-8 Escapes'),
  40. p('Translates URLs containing URL-encoded Latin-1 to ',
  41. 'URLs containing URL-encoded UTF-8 and redirects to it.'),
  42. start_form(-method=>'GET'),
  43. p('URL: ', textfield('url', '', 70)),
  44. p(submit()),
  45. end_form(),
  46. end_html();
  47. exit;
  48. }
  49. my $str = param('url');
  50. print redirect(translate($str));
  51. # print $str, "\n";
  52. # print translate($str), "\n";
  53. # perl latin-1.pl url=http://www.emacswiki.org/cgi-bin/community/LangueFran%E7aise