Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 11-13 of 13 total

URI Encoding in Ruby

To encode the web parameters for a URL query string.

   1  
   2  require 'uri'
   3  val = URI.escape("my parameter value")

converting some french chars with python

because translate and maketrans don't love utf-8 ;-(
   1  
   2      import string
   3      french=u"15 résultats trouvés".encode("utf_16")
   4      
   5      sfrom = u"àâäéèêëïîôöûùüç".encode("utf_16")
   6      sto   = u"aaaeeeeiioouuuc".encode("utf_16")
   7      print french.translate( string.maketrans(sfrom,sto) )

Change Gmail encoding to display thai.

Some emails send from yahoo and hotmail will display thai incorrectly. This bookmarklet re-encode it by shifting the unicode numbers. Can be applied to some language whose encoding is in the same "shifting" order.
   1  
   2  javascript:(function(){  map=[];for(i=161;i<251;i++)map[i]=String.fromCharCode(i+3424);  function thai(s){s2='';for(var i=0;i<s.length;i++){n=s.charCodeAt(i);if(n>160&&n<251) s2+=map[n];else s2+=s.charAt(i)}return s2}   function rc_thai(el){if(el.nodeType== 3){el.data=thai(el.data);return} if(el.tagName == 'SCRIPT') return; for (var i=0; i<el.childNodes.length; i++) rc_thai(el.childNodes[i])}   for(i=0;i<4;i++){if(fi=window.frames[0].frames[i].document.getElementById('fi')) break}  rc_thai(fi); })();

The one-liner version above maybe difficult to read.
Here's a reorganized one
   1  
   2  javascript:(function(){
   3  map=[];
   4  /* create conversion table */
   5  for(i=161;i<251;i++) {
   6    map[i]=String.fromCharCode(i+3424);
   7  }
   8  
   9  function thai(s){
  10    s2='';
  11    for(var i=0;i<s.length;i++){
  12      n=s.charCodeAt(i);
  13      if(n>160&&n<251) 
  14        s2+=map[n];
  15      else 
  16        s2+=s.charAt(i)
  17    }
  18    return s2
  19  }   
  20  
  21  /* recursively convert encoding of sub-element */
  22  function rc_thai(el){
  23    if(el.nodeType== 3){
  24      el.data=thai(el.data);
  25      return
  26    } 
  27    if(el.tagName == 'SCRIPT') 
  28      return; 
  29    for (var i=0; i<el.childNodes.length; i++)
  30      rc_thai(el.childNodes[i])
  31  }   
  32  
  33  /* finding the content element in sub-sub-frame */
  34  for(i=0;i<4;i++){
  35    if(fi=window.frames[0].frames[i].document.getElementById('fi'))
  36      break
  37  }  
  38  
  39  /* change that element (and all its descendants */
  40  rc_thai(fi); 
  41  })();
« Newer Snippets
Older Snippets »
Showing 11-13 of 13 total