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

.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

.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.

Parameters:

  • name

    the scheme name

  • scheme

    the scheme data itself. This should be constructed as described above.



66
67
68
# File 'lib/sanscript/transliterate.rb', line 66

def add_brahmic_scheme(name, scheme)
  @schemes[name.to_sym] = scheme.deep_dup.deep_freeze
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.

Parameters:

  • name

    the scheme name

  • scheme

    the scheme data itself



79
80
81
82
83
84
85
# File 'lib/sanscript/transliterate.rb', line 79

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)
end

.roman_scheme?(name) ⇒ Boolean

Check whether the given scheme encodes romanized Sanskrit.

@param name  the scheme name
@return      boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/sanscript/transliterate.rb', line 44

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

.scheme_namesObject

Return a list of available schemes.

@return      array of scheme identifiers


34
35
36
# File 'lib/sanscript/transliterate.rb', line 34

def scheme_names
  @schemes.keys.sort!
end

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

/** Transliterate from one script to another.

*

Parameters:

  • data

    the string to transliterate

  • from

    the source script

  • to

    the destination script

  • options (defaults to: {})

    transliteration options

Returns:

  • the finished string



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/sanscript/transliterate.rb', line 129

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