Welcome to mirror list, hosted at ThFree Co, Russian Federation.

jquery.rb « formatters « twine « lib - github.com/mapsme/twine.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: bfe8d4c01940a8f5da904b4b17bc957a6b2a4c97 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
module Twine
  module Formatters
    class JQuery < Abstract
      def format_name
        'jquery'
      end

      def extension
        '.json'
      end

      def can_handle_directory?(path)
        Dir.entries(path).any? { |item| /^.+\.json$/.match(item) }
      end

      def default_file_name
        return 'localize.json'
      end

      def determine_language_given_path(path)
        path_arr = path.split(File::SEPARATOR)
        path_arr.each do |segment|
          match = /^((.+)-)?([^-]+)\.json$/.match(segment)
          if match
            return match[3]
          end
        end

        return
      end

      def read(io, lang)
        begin
          require "json"
        rescue LoadError
          raise Twine::Error.new "You must run 'gem install json' in order to read or write jquery-localize files."
        end

        json = JSON.load(io)
        json.each do |key, value|
          set_translation_for_key(key, lang, value)
        end
      end

      def format_file(lang)
        result = super
        return result unless result
        "{\n#{super}\n}\n"
      end

      def format_sections(twine_file, lang)
        sections = twine_file.sections.map { |section| format_section(section, lang) }
        sections.delete_if &:empty?
        sections.join(",\n\n")
      end

      def format_section_header(section)
      end

      def format_section(section, lang)
        definitions = section.definitions.dup

        definitions.map! { |definition| format_definition(definition, lang) }
        definitions.compact! # remove nil definitions
        definitions.join(",\n")
      end

      def key_value_pattern
        "\"%{key}\":\"%{value}\""
      end

      def format_key(key)
        escape_quotes(key)
      end

      def format_value(value)
        escape_quotes(value)
      end
    end
  end
end

Twine::Formatters.formatters << Twine::Formatters::JQuery.new