Module: Sanscript::Transliterate

Defined in:
lib/sanscript/transliterate.rb,
lib/sanscript/transliterate/schemes.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.all_alternatesObject (readonly)

Returns the value of attribute all_alternates.



17
18
19
# File 'lib/sanscript/transliterate.rb', line 17

def all_alternates
  @all_alternates
end

.brahmic_schemesObject (readonly)

Returns the value of attribute brahmic_schemes.



17
18
19
# File 'lib/sanscript/transliterate.rb', line 17

def brahmic_schemes
  @brahmic_schemes
end

.defaultsObject (readonly)

Returns the value of attribute defaults.



17
18
19
# File 'lib/sanscript/transliterate.rb', line 17

def defaults
  @defaults
end

.roman_schemesObject (readonly)

Returns the value of attribute roman_schemes.



17
18
19
# File 'lib/sanscript/transliterate.rb', line 17

def roman_schemes
  @roman_schemes
end

.scheme_namesObject (readonly)

Returns the value of attribute scheme_names.



17
18
19
# File 'lib/sanscript/transliterate.rb', line 17

def scheme_names
  @scheme_names
end

.schemesObject (readonly)

Returns the value of attribute schemes.



17
18
19
# File 'lib/sanscript/transliterate.rb', line 17

def schemes
  @schemes
end

Class Method Details

.add_brahmic_scheme(name, scheme) ⇒ Object

Add a Brahmic scheme to Sanscript.

Schemes are of two types: “Brahmic” and “roman”. Brahmic consonants have an inherent vowel sound, but roman consonants do not. This is the main difference between these two types of scheme.

A scheme definition is an object (“{}”) that maps a group name to a list of characters. For illustration, see the “devanagari” scheme at the top of this file.

You can use whatever group names you like, but for the best results, you should use the same group names that Sanscript does.



68
69
70
71
72
73
74
75
# File 'lib/sanscript/transliterate.rb', line 68

def add_brahmic_scheme(name, scheme)
  name = name.to_sym
  scheme = scheme.deep_dup
  @schemes[name] = scheme.deep_freeze
  @brahmic_schemes.add(name)
  @scheme_names.add(name)
  scheme
end

.add_roman_scheme(name, scheme) ⇒ Object

Add a roman scheme to Sanscript.

See the comments on Sanscript.add_brahmic_scheme. The “vowel_marks” field can be omitted.



86
87
88
89
90
91
92
93
94
# File 'lib/sanscript/transliterate.rb', line 86

def add_roman_scheme(name, scheme)
  name = name.to_sym
  scheme = scheme.deep_dup
  scheme[:vowel_marks] = scheme[:vowels][1..-1] unless scheme.key?(:vowel_marks)
  @schemes[name] = scheme.deep_freeze
  @roman_schemes.add(name)
  @scheme_names.add(name)
  scheme
end

.brahmic_scheme?(name) ⇒ Boolean

Check whether the given scheme encodes Brahmic Sanskrit.

@param name  the scheme name
@return      boolean


36
37
38
# File 'lib/sanscript/transliterate.rb', line 36

def brahmic_scheme?(name)
  @brahmic_schemes.include?(name.to_sym)
end

.roman_scheme?(name) ⇒ Boolean

Check whether the given scheme encodes romanized Sanskrit.

@param name  the scheme name
@return      boolean


46
47
48
# File 'lib/sanscript/transliterate.rb', line 46

def roman_scheme?(name)
  @roman_schemes.include?(name.to_sym)
end

.transliterate(data, from, to, options = {}) ⇒ Object

/** Transliterate from one script to another.

*


148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/sanscript/transliterate.rb', line 148

def transliterate(data, from, to, options = {})
  from = from.to_sym
  to = to.to_sym
  raise "Scheme not known ':#{from}'" unless @schemes.key?(from)
  raise "Scheme not known ':#{to}'" unless @schemes.key?(to)

  data = data.to_str.dup
  options = @defaults.merge(options)
  map = make_map(from, to)

  data.gsub!(/(<.*?>)/, "##\\1##") if options[:skip_sgml]

  # Easy way out for "{\m+}", "\", and ".h".
  if from == :itrans
    data.gsub!(/\{\\m\+\}/, ".h.N")
    data.gsub!(/\.h/, "")
    data.gsub!(/\\([^'`_]|$)/, "##\\1##")
  end

  if map[:from_roman?]
    transliterate_roman(data, map, options)
  else
    transliterate_brahmic(data, map)
  end
end