Module: Sanscript

Defined in:
lib/sanscript.rb,
lib/sanscript/rust.rb,
lib/sanscript/detect.rb,
lib/sanscript/version.rb,
lib/sanscript/benchmark.rb,
lib/sanscript/exceptions.rb,
lib/sanscript/detect/ruby24.rb,
lib/sanscript/detect/ruby2x.rb,
lib/sanscript/transliterate.rb,
lib/sanscript/transliterate/schemes.rb

Overview

:nocov:

Defined Under Namespace

Modules: Benchmark, Detect, Transliterate Classes: DetectionError, SchemeNotSupportedError

Constant Summary collapse

VERSION =

The version number

"0.7.0"

Class Method Summary collapse

Class Method Details

.detect(text) ⇒ Symbol?

Attempts to detect the encoding scheme of the provided string. Simple proxy for Sanscript::Detect.detect_scheme

Parameters:

  • text (String)

    a string of Sanskrit text

Returns:

  • (Symbol, nil)

    the Symbol of the scheme, or nil if no match



28
29
30
# File 'lib/sanscript.rb', line 28

def detect(text)
  Detect.detect_scheme(text)
end

.rust_disable!bool

Turns off Rust native extension.

Returns:

  • (bool)

    the enabled status of the Rust extension



39
40
41
42
43
44
45
46
# File 'lib/sanscript/rust.rb', line 39

def rust_disable!
  Detect.module_eval do
    class << self
      alias_method :detect_scheme, :ruby_detect_scheme
    end
  end
  @rust_enabled = false
end

.rust_enable!bool

Turns on Rust extension, if available.

Returns:

  • (bool)

    the enabled status of the Rust extension



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/sanscript/rust.rb', line 25

def rust_enable!
  if RUST_AVAILABLE
    Detect.module_eval do
      class << self
        alias_method :detect_scheme, :rust_detect_scheme
      end
    end
    @rust_enabled = true
  end
  @rust_enabled
end

.rust_enabled?bool

Returns the enabled status of the Rust extension.

Returns:

  • (bool)

    the enabled status of the Rust extension



19
20
21
# File 'lib/sanscript/rust.rb', line 19

def rust_enabled?
  @rust_enabled
end

.transliterate(text, from, to, **opts) ⇒ String .transliterate(text, to, **opts) ⇒ String

Transliterates a string, optionally detecting its source-scheme first.

Overloads:

  • .transliterate(text, from, to, **opts) ⇒ String

    Returns the transliterated String.

    Parameters:

    • text (String)

      the String to transliterate

    • from (Symbol, nil)

      the name of the scheme to transliterate from, or Nil to detect

    • to (Symbol)

      the name of the scheme to transliterate to

    Options Hash (**opts):

    • :default_scheme (Symbol)

      a default scheme to fall-back to if detection fails

    • :skip_sgml (Boolean) — default: false

      escape SGML-style tags in text string

    • :syncope (Boolean) — default: false

      activate Hindi-style schwa syncope

    Returns:

    • (String)

      the transliterated String

    Raises:

  • .transliterate(text, to, **opts) ⇒ String

    Returns the transliterated String.

    Parameters:

    • text (String)

      the String to transliterate

    • to (Symbol)

      the name of the scheme to transliterate to

    Options Hash (**opts):

    • :default_scheme (Symbol)

      a default scheme to fall-back to if detection fails

    • :skip_sgml (Boolean) — default: false

      escape SGML-style tags in text string

    • :syncope (Boolean) — default: false

      activate Hindi-style schwa syncope

    Returns:

    • (String)

      the transliterated String

    Raises:



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/sanscript.rb', line 57

def transliterate(text, from, to = nil, **opts)
  if to.nil?
    to = from
    from = nil
  end
  if from.nil?
    from = Detect.detect_scheme(text) || opts[:default_scheme] ||
           raise(DetectionError, "String detection and fallback failed.")
  end
  Transliterate.transliterate(text, from, to, opts)
end