#!/usr/bin/env python
# Copyright (c) 2012 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Formats as a .js file using a map: <english text> -> <localized text>.
"""

import os
import re
import types

from grit import util
from grit.format import interface
from grit.node import io


class TopLevel(interface.ItemFormatter):
  """Writes out the required preamble for JS files."""

  def Format(self, item, lang='en', output_dir='.'):
    """Format the JS file header."""
    assert isinstance(lang, types.StringTypes)
    return '''//  Copyright %d Google Inc. All Rights Reserved.
// This file is automatically generated by GRIT.  Do not edit.
''' % (util.GetCurrentYear())


class StringTable(interface.ItemFormatter):
  """Writes out the string table."""

  def FormatEnd(self, item, lang='en', output_dir='.'):
    return '\n'


class Message(interface.ItemFormatter):
  """Writes out a single message."""

  def Format(self, item, lang='en', output_dir='.'):
    """Format a single message."""
    from grit.node import message

    assert isinstance(lang, types.StringTypes)
    assert isinstance(item, message.MessageNode)

    en_message = item.ws_at_start + item.Translate('en') + item.ws_at_end
    # Remove position numbers from placeholders.
    en_message = re.sub(r'%\d\$([a-z])', r'%\1', en_message)
    # Escape double quotes.
    en_message = re.sub(r'"', r'\"', en_message)

    loc_message = item.ws_at_start + item.Translate(lang) + item.ws_at_end
    # Escape double quotes.
    loc_message = re.sub(r'"', r'\"', loc_message)

    return '\nlocalizedStrings["%s"] = "%s";' % (en_message, loc_message)
