/**
 * Converts a plain text string to HTML, and turns letters followed by '`'
 * into the appropriate accented Spanish character.
 * @param {string<PLAIN>} s
 * @return {string<HTML>}
 */
function html(s) {
  s = accent(s);
  // Convert HTML special characters into HTML entities.
  s = s.replace(/&/g, '&amp;').replace(/\x3c/g, '&lt;').replace(/\x3e/g, '&gt;')
      .replace(/\x22/g, '&quot;');
  return s;
}

/** Convert shorthands for accented letters into single accented characters. */
var accent;

/** The dual of accent. */
var unaccent;

(function () {
  var accents = {
    'A': '\xc1',
    'E': '\xc9',
    'I': '\xcd',
    'N': '\xd1',
    'O': '\xd3',
    'U': '\xda',
    'a': '\xe1',
    'e': '\xe9',
    'i': '\xed',
    'n': '\xf1',
    'o': '\xf3',
    'u': '\xfa',
  }
  var raccents = accents;
  (function () {
    for (var ch in accents) {
      if (accents.hasOwnProperty(ch)) { raccents[accents[ch]] = ch; }
    }
  })();
  accent = function accent(s) {
    return ('' + s).replace(/([gG])u`([eEiI])/g, '$1\xFC$2')
        .replace(/([gG])U`([eEiI])/g, '$1\xDC$2')
        .replace(/([aeinou])`/gi, function (_, ch) { return accents[ch]; });
  };
  unaccent = function unaccent(s) {
    return ('' + s).replace(/([gG])\xFC([eEiI])/g, '$1u`$2')
        .replace(/([gG])\xDC([eEiI])/g, '$1U`$2')
        .replace(/[\xC1\xC9\xCD\xD1\xD3\xDA\xE1\xE9\xED\xF1\xF3\xFA]/g,
                 function (ch) { return raccents[ch]; });
  };
})();
