Skip to content

Instantly share code, notes, and snippets.

@juanpabloaj
Created August 30, 2012 01:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juanpabloaj/3521476 to your computer and use it in GitHub Desktop.
Save juanpabloaj/3521476 to your computer and use it in GitHub Desktop.
" http://vim.wikia.com/wiki/HTML_entities
" http://www.bigbaer.com/sidebars/entities/
" FIXME: you don't know how to use the -range argument! (doesn't work)
command! -range HtmlEncode <line1>,<line2>call HtmlEntities('encode')
command! -range HtmlDecode <line1>,<line2>call HtmlEntities('decode')
" TODO: only accept full lines as a range; would be nice for arbitrary selection
function! HtmlEntities(action) range
" & -> &amp; should be the first substitution
let entities = [
\ ['&', '&amp;'],
\ ['"', '&quot;'],
\ ['<', '&lt;'],
\ ['>', '&gt;'],
\ ['¡', '&iexcl;'],
\ ['¢', '&cent;'],
\ ['£', '&pound;'],
\ ['¤', '&curren;'],
\ ['Â¥', '&yen;'],
\ ['€', '&euro;'],
\ ['¦', '&brvbar;'],
\ ['§', '&sect;'],
\ ['¨', '&uml;'],
\ ['©', '&copy;'],
\ ['ª', '&ordf;'],
\ ['«', '&laquo;'],
\ ['¬', '&not;'],
\ ['®', '&reg;'],
\ ['™', '&trade;'],
\ ['¯', '&macr;'],
\ ['°', '&deg;'],
\ ['±', '&plusmn;'],
\ ['²', '&sup2;'],
\ ['³', '&sup3;'],
\ ['´', '&acute;'],
\ ['µ', '&micro;'],
\ ['¶', '&para;'],
\ ['·', '&middot;'],
\ ['¸', '&cedil;'],
\ ['¹', '&sup1;'],
\ ['º', '&ordm;'],
\ ['»', '&raquo;'],
\ ['¼', '&frac14;'],
\ ['½', '&frac12;'],
\ ['¾', '&frac34;'],
\ ['¿', '&iquest;'],
\ ['×', '&times;'],
\ ['÷', '&divide;'],
\ ['À', '&Agrave;'],
\ ['Á', '&Aacute;'],
\ ['Â', '&Acirc;'],
\ ['Ã', '&Atilde;'],
\ ['Ä', '&Auml;'],
\ ['Å', '&Aring;'],
\ ['Æ', '&AElig;'],
\ ['Ç', '&Ccedil;'],
\ ['È', '&Egrave;'],
\ ['É', '&Eacute;'],
\ ['Ê', '&Ecirc;'],
\ ['Ë', '&Euml;'],
\ ['Ì', '&Igrave;'],
\ ['Í', '&Iacute;'],
\ ['Î', '&Icirc;'],
\ ['Ï', '&Iuml;'],
\ ['Ð', '&ETH;'],
\ ['Ñ', '&Ntilde;'],
\ ['Ò', '&Ograve;'],
\ ['Ó', '&Oacute;'],
\ ['Ô', '&Ocirc;'],
\ ['Õ', '&Otilde;'],
\ ['Ö', '&Ouml;'],
\ ['Ø', '&Oslash;'],
\ ['Ù', '&Ugrave;'],
\ ['Ú', '&Uacute;'],
\ ['Û', '&Ucirc;'],
\ ['Ü', '&Uuml;'],
\ ['Ý', '&Yacute;'],
\ ['Þ', '&THORN;'],
\ ['ß', '&szlig;'],
\ ['à', '&agrave;'],
\ ['á', '&aacute;'],
\ ['â', '&acirc;'],
\ ['ã', '&atilde;'],
\ ['ä', '&auml;'],
\ ['Ã¥', '&aring;'],
\ ['æ', '&aelig;'],
\ ['ç', '&ccedil;'],
\ ['è', '&egrave;'],
\ ['é', '&eacute;'],
\ ['ê', '&ecirc;'],
\ ['ë', '&euml;'],
\ ['ì', '&igrave;'],
\ ['í', '&iacute;'],
\ ['î', '&icirc;'],
\ ['ï', '&iuml;'],
\ ['ð', '&eth;'],
\ ['ñ', '&ntilde;'],
\ ['ò', '&ograve;'],
\ ['ó', '&oacute;'],
\ ['ô', '&ocirc;'],
\ ['õ', '&otilde;'],
\ ['ö', '&ouml;'],
\ ['ø', '&oslash;'],
\ ['ù', '&ugrave;'],
\ ['ú', '&uacute;'],
\ ['û', '&ucirc;'],
\ ['ü', '&uuml;'],
\ ['ý', '&yacute;'],
\ ['þ', '&thorn;'],
\ ['ÿ', '&yuml;'],
\ ['Œ', '&OElig;'],
\ ['œ', '&oelig;'],
\ ['Å ', '&Scaron;'],
\ ['Å¡', '&scaron;'],
\ ['Ÿ', '&Yuml;'],
\ ['ˆ', '&circ;'],
\ ['˜', '&tilde'],
\ ['‌', '&zwnj;'],
\ ['‍', '&zwj;'],
\ ['‎', '&lrm;'],
\ ['‏', '&rlm;'],
\ ['–', '&ndash;'],
\ ['—', '&mdash;'],
\ ['‘', '&lsquo;'],
\ ['’', '&rsquo;'],
\ ['‚', '&sbquo;'],
\ ['“', '&ldquo;'],
\ ['”', '&rdquo;'],
\ ['„', '&bdquo;'],
\ ['†', '&dagger;'],
\ ['‡', '&Dagger;'],
\ ['‰', '&permil;'],
\ ['‹', '&lsaquo;'],
\ ['›', '&rsaquo;'],
\ ['ƒ', '&fnof;'],
\ ['•', '&bull;'],
\ ['…', '&hellip;'],
\ ['′', '&prime;'],
\ ['″', '&Prime;'],
\ ['‾', '&oline;'],
\ ['⁄', '&frasl;'],
\ ['℘', '&weierp;'],
\ ['ℑ', '&image;'],
\ ['ℜ', '&real;'],
\ ['ℵ', '&alefsym;'],
\ ['←', '&larr;'],
\ ['↑', '&uarr;'],
\ ['→', '&rarr;'],
\ ['↓', '&darr;'],
\ ['↔', '&harr;'],
\ ['↵', '&crarr;'],
\ ['⇐', '&lArr;'],
\ ['⇑', '&uArr;'],
\ ['⇒', '&rArr;'],
\ ['⇓', '&dArr;'],
\ ['⇔', '&hArr;'],
\ ['∀', '&forall;'],
\ ['∂', '&part;'],
\ ['∃', '&exist;'],
\ ['∅', '&empty;'],
\ ['∇', '&nabla;'],
\ ['∈', '&isin;'],
\ ['∉', '&notin;'],
\ ['∋', '&ni;'],
\ ['∏', '&prod;'],
\ ['∑', '&sum;'],
\ ['−', '&minus;'],
\ ['∗', '&lowast;'],
\ ['√', '&radic;'],
\ ['∝', '&prop;'],
\ ['∞', '&infin;'],
\ ['∠', '&ang;'],
\ ['∧', '&and;'],
\ ['∨', '&or;'],
\ ['∩', '&cap;'],
\ ['∪', '&cup;'],
\ ['∫', '&int;'],
\ ['∴', '&there4;'],
\ ['∼', '&sim;'],
\ ['≅', '&cong;'],
\ ['≈', '&asymp;'],
\ ['≠', '&ne;'],
\ ['≡', '&equiv;'],
\ ['≤', '&le;'],
\ ['≥', '&ge;'],
\ ['⊂', '&sub;'],
\ ['⊃', '&sup;'],
\ ['⊄', '&nsub;'],
\ ['⊆', '&sube;'],
\ ['⊇', '&supe;'],
\ ['⊕', '&oplus;'],
\ ['⊗', '&otimes;'],
\ ['⊥', '&perp;'],
\ ['⋅', '&sdot;'],
\ ['⌈', '&lceil;'],
\ ['⌉', '&rceil;'],
\ ['⌊', '&lfloor;'],
\ ['⌋', '&rfloor;'],
\ ['⟨', '&lang;'],
\ ['⟩', '&rang;'],
\ ['◊', '&loz;'],
\ ['℠', '&#8480'],
\ ['℃', '&#8451;'],
\ ['℅', '&#8453;'],
\ ['℉', '&#8457;'],
\ ['№', '&#8470;'],
\ ['℗', '&#8471;'],
\ ['℞', '&#8478;'],
\ ['Ω', '&#8486;'],
\ ['℧', '&#8487;'],
\ ['☀', '&#9728;'],
\ ['☁', '&#9729;'],
\ ['☂', '&#9730;'],
\ ['☃', '&#9731;'],
\ ['☄', '&#9732;'],
\ ['★', '&#9733;'],
\ ['☆', '&#9734;'],
\ ['☇', '&#9735;'],
\ ['☈', '&#9736;'],
\ ['☉', '&#9737;'],
\ ['☊', '&#9738;'],
\ ['☋', '&#9739;'],
\ ['☌', '&#9740;'],
\ ['☍', '&#9741;'],
\ ['☎', '&#9742;'],
\ ['☏', '&#9743;'],
\ ['☐', '&#9744;'],
\ ['☑', '&#9745;'],
\ ['☒', '&#9746;'],
\ ['☓', '&#9747;'],
\ ['☚', '&#9754;'],
\ ['☛', '&#9755;'],
\ ['☜', '&#9756;'],
\ ['☝', '&#9757;'],
\ ['☞', '&#9758;'],
\ ['☟', '&#9759;'],
\ ['☠', '&#9760;'],
\ ['☡', '&#9761;'],
\ ['☢', '&#9762;'],
\ ['☣', '&#9763;'],
\ ['☤', '&#9764;'],
\ ['☥', '&#9765;'],
\ ['☦', '&#9766;'],
\ ['☧', '&#9767;'],
\ ['☨', '&#9768;'],
\ ['☩', '&#9769;'],
\ ['☪', '&#9770;'],
\ ['☫', '&#9771;'],
\ ['☬', '&#9772;'],
\ ['☭', '&#9773;'],
\ ['☮', '&#9774;'],
\ ['☯', '&#9775;'],
\ ['☰', '&#9776;'],
\ ['☱', '&#9777;'],
\ ['☲', '&#9778;'],
\ ['☳', '&#9779;'],
\ ['☴', '&#9780;'],
\ ['☵', '&#9781;'],
\ ['☶', '&#9782;'],
\ ['☷', '&#9783;'],
\ ['☸', '&#9784;'],
\ ['☹', '&#9785;'],
\ ['☺', '&#9786;'],
\ ['☻', '&#9787;'],
\ ['☽', '&#9789;'],
\ ['☾', '&#9790;'],
\ ['☿', '&#9791;'],
\ ['♀', '&#9792;'],
\ ['♁', '&#9793;'],
\ ['♂', '&#9794;'],
\ ['♃', '&#9795;'],
\ ['♄', '&#9796;'],
\ ['♅', '&#9797;'],
\ ['♆', '&#9798;'],
\ ['♇', '&#9799;'],
\ ['♈', '&#9800;'],
\ ['♉', '&#9801;'],
\ ['♊', '&#9802;'],
\ ['♋', '&#9803;'],
\ ['♌', '&#9804;'],
\ ['♍', '&#9805;'],
\ ['♎', '&#9806;'],
\ ['♏', '&#9807;'],
\ ['♐', '&#9808;'],
\ ['♑', '&#9809;'],
\ ['♒', '&#9810;'],
\ ['♓', '&#9811;'],
\ ['♔', '&#9812;'],
\ ['♕', '&#9813;'],
\ ['♖', '&#9814;'],
\ ['♗', '&#9815;'],
\ ['♘', '&#9816;'],
\ ['♙', '&#9817;'],
\ ['♚', '&#9818;'],
\ ['♛', '&#9819;'],
\ ['♜', '&#9820;'],
\ ['♝', '&#9821;'],
\ ['♞', '&#9822;'],
\ ['♟', '&#9823;'],
\ ['♠', '&spades;'],
\ ['♡', '&#9825;'],
\ ['♢', '&#9826;'],
\ ['♣', '&clubs;'],
\ ['♤', '&#9828;'],
\ ['♥', '&hearts;'],
\ ['♦', '&diams;'],
\ ['♧', '&#9831;'],
\ ['♨', '&#9832;'],
\ ['♩', '&#9833;'],
\ ['♪', '&#9834;'],
\ ['♫', '&#9835;'],
\ ['♬', '&#9836;'],
\ ['♭', '&#9837;'],
\ ['♮', '&#9838;'],
\ ['♯', '&#9839;'],
\ ['✁', '&#9985;'],
\ ['✂', '&#9986;'],
\ ['✃', '&#9987;'],
\ ['✄', '&#9988;'],
\ ['✆', '&#9990;'],
\ ['✇', '&#9991;'],
\ ['✈', '&#9992;'],
\ ['✉', '&#9993;'],
\ ['✌', '&#9996;'],
\ ['✍', '&#9997;'],
\ ['✎', '&#9998;'],
\ ['✏', '&#9999;'],
\ ['✓', '&#10003;'],
\ ['✔', '&#10004;'],
\ ['✕', '&#100005;'],
\ ['✖', '&#10006;'],
\ ['✗', '&#10007;'],
\ ['✘', '&#10008;'],
\ ['✝', '&#10013;'],
\ ['✞', '&#10014;'],
\ ['✟', '&#10015;'],
\ ['✠', '&#10016;'],
\ ['✡', '&#10017;'],
\ ['❛', '&#10075;'],
\ ['❜', '&#10076;'],
\ ['❝', '&#10077;'],
\ ['❞', '&#10078;'],
\ ['Α', '&Alpha;'],
\ ['Β', '&Beta;'],
\ ['Γ', '&Gamma;'],
\ ['Δ', '&Delta;'],
\ ['Ε', '&Epsilon;'],
\ ['Ζ', '&Zeta;'],
\ ['Η', '&Eta;'],
\ ['Θ', '&Theta;'],
\ ['Ι', '&Iota;'],
\ ['Κ', '&Kappa;'],
\ ['Λ', '&Lambda;'],
\ ['Μ', '&Mu;'],
\ ['Ν', '&Nu;'],
\ ['Ξ', '&Xi;'],
\ ['Ο', '&Omicron;'],
\ ['Π', '&Pi;'],
\ ['Ρ', '&Rho;'],
\ ['Σ', '&Sigma;'],
\ ['Τ', '&Tau;'],
\ ['Î¥', '&Upsilon;'],
\ ['Φ', '&Phi;'],
\ ['Χ', '&Chi;'],
\ ['Ψ', '&Psi;'],
\ ['Ω', '&Omega;'],
\ ['α', '&alpha;'],
\ ['β', '&beta;'],
\ ['γ', '&gamma;'],
\ ['δ', '&delta;'],
\ ['ε', '&epsilon;'],
\ ['ζ', '&zeta;'],
\ ['η', '&eta;'],
\ ['θ', '&theta;'],
\ ['ι', '&iota;'],
\ ['κ', '&kappa;'],
\ ['λ', '&lambda;'],
\ ['μ', '&mu;'],
\ ['ν', '&nu;'],
\ ['ξ', '&xi;'],
\ ['ο', '&omicron;'],
\ ['π', '&pi;'],
\ ['ρ', '&rho;'],
\ ['ς', '&sigmaf;'],
\ ['σ', '&sigma;'],
\ ['τ', '&tau;'],
\ ['υ', '&upsilon;'],
\ ['φ', '&phi;'],
\ ['χ', '&chi;'],
\ ['ψ', '&psi;'],
\ ['ω', '&omega;'],
\ ['ϑ', '&thetasym;'],
\ ['ϒ', '&upsih;'],
\ ['ϖ', '&piv;']
\]
if a:action == 'encode'
let pat = 0
let sub = 1
elseif a:action == 'decode'
let pat = 1
let sub = 0
else
return
endif
for pair in entities
let substitution = substitute(pair[sub], '&', '\\\&', 'g')
execute 'silent '.a:firstline.','.a:lastline.'s/'.pair[pat].'/'.substitution.'/eg'
endfor
endfunction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment