Subversion Repositories LCARS

Rev

Rev 234 | Blame | Compare with Previous | Last modification | View Log | RSS feed

1
var klingon_texts = null;

var klingon_map = {
  "a": "\uF8D0",
  "b": "\uF8D1",
  "ch": "\uF8D2",
  "D": "\uF8D3",
  "e": "\uF8D4",
  "gh": "\uF8D5",
  "H": "\uF8D6",
  "I": "\uF8D7",
  "j": "\uF8D8",
  "l": "\uF8D9",
  "m": "\uF8DA",
  "n": "\uF8DB",

  /* prevents mistranscription of "n-gh" as "ng-h" (trailing garbage) */
  "ngh": "\uF8DB\uF8D5",

  "ng": "\uF8DC",
  "o": "\uF8DD",
  "p": "\uF8DE",
  "q": "\uF8DF",
  "Q": "\uF8E0",
  "r": "\uF8E1",
  "S": "\uF8E2",
  "t": "\uF8E3",
  "tlh": "\uF8E4",
  "u": "\uF8E5",
  "v": "\uF8E6",
  "w": "\uF8E7",
  "y": "\uF8E8",
  "'": "\uF8E9",
  "0": "\uF8F0",
  "1": "\uF8F1",
  "2": "\uF8F2",
  "3": "\uF8F3",
  "4": "\uF8F4",
  "5": "\uF8F5",
  "6": "\uF8F6",
  "7": "\uF8F7",
  "8": "\uF8F8",
  "9": "\uF8F9",
  ",": "\uF8FD",
  ".": "\uF8FE",

  /* mummification glyph, symbol of the empire */
  "Δ": "\uF8FF"
};

var rxKlingon = jsx.object.getKeys(klingon_map);
rxKlingon.sort(function (a, b) { return b.length - a.length; });
rxKlingon = new RegExp(
  rxKlingon.filter(function (e) { return e.length > 1; }).join("|")
    + "|[" + rxKlingon.filter(function (e) { return e.length === 1; }). join("") + "]",
  "ig");

function pIqaDize (s)
{
  return s.replace(
    rxKlingon,
    function (s) {
      return jsx.object.getProperty(klingon_map, s.replace(/\s+/g, " "), s);
    });
}

var LANG_KLINGON_PIQAD = "tlh-pIqaD";

function pIqaD (button)
{
  klingon_texts = jsx.dom.xpath.evaluate(
    './/*[@class = "text" and (@lang = "' + LANG_KLINGON
      + '" or parent::*[@lang = "' + LANG_KLINGON + '"])]',
    document.body);

  if (!klingon_texts)
  {
    return;
  }

  if (document.documentElement.lang == LANG_KLINGON)
  {
    var title = document.getElementsByTagName("title")[0];
    title.setAttribute("data-text", document.title);
    document.title = pIqaDize(document.title);
  }

  for (var i = 0, len = klingon_texts.length; i < len; ++i)
  {
    var text = klingon_texts[i];

    if (!text.hasAttribute("data-html"))
    {
      text.setAttribute("data-html", text.innerHTML);

      if (!text.title)
      {
        text.title = text.textContent;
      }
    }

    var text_nodes = jsx.dom.xpath.evaluate(
      './/text()[parent::*[not(@lang) or (@lang = "' + LANG_KLINGON + '")]]',
      text);

    for (var j = 0, len2 = text_nodes.length; j < len2; ++j)
    {
      var text_node = text_nodes[j];

      text_node.nodeValue = pIqaDize(text_node.nodeValue);
    }

    if (text.lang == LANG_KLINGON)
    {
      text.lang = LANG_KLINGON_PIQAD;
    }
    else if (text.parentNode && text.parentNode.lang == LANG_KLINGON)
    {
      text.parentNode.lang = LANG_KLINGON_PIQAD;
    }
  }

  if (button)
  {
    button.firstChild.lang = LANG_KLINGON;
  }

  var html = document.documentElement;
  if (html.lang == LANG_KLINGON)
  {
    html.setAttribute("data-lang", html.lang);
    html.lang = LANG_KLINGON_PIQAD;
  }
}

function klingonLatinScript (button)
{
  var title = document.getElementsByTagName("title")[0];
  if (document.documentElement.lang == LANG_KLINGON_PIQAD)
  {
    document.title = title.getAttribute("data-text");
    title.removeAttribute("data-text")
  }

  for (var i = 0, len = klingon_texts.length; i < len; ++i)
  {
    var text = klingon_texts[i];

    text.innerHTML = text.getAttribute('data-html');
    text.removeAttribute("data-html");

    if (text.lang == LANG_KLINGON_PIQAD)
    {
      text.lang = LANG_KLINGON;
    }
    else if (text.parentNode && text.parentNode.lang == LANG_KLINGON_PIQAD)
    {
      text.parentNode.lang = LANG_KLINGON;
    }
  }

  if (button)
  {
    button.firstChild.lang = LANG_KLINGON_PIQAD;
  }

  var html = document.documentElement;
  if (html.lang == LANG_KLINGON_PIQAD)
  {
    html.lang = html.getAttribute("data-lang");
    html.removeAttribute("data-lang");
  }
}