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/rust.rb,
lib/sanscript/detect/ruby24.rb,
lib/sanscript/detect/ruby2x.rb,
lib/sanscript/transliterate.rb,
lib/sanscript/transliterate/schemes.rb

Overview

Sanscript.rb detection/transliteration module for Sanskrit.

Defined Under Namespace

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

Constant Summary collapse

RUST_AVAILABLE =

Load rust if available.

rust_load!
VERSION =

The version number

"0.8.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:

  • a string of Sanskrit text

Returns:

  • the Symbol of the scheme, or nil if no match



24
25
26
# File 'lib/sanscript.rb', line 24

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

.rust_disable!bool

Turns off Rust native extension.

Returns:

  • the enabled status of the Rust extension



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

def rust_disable!
  Detect.singleton_class.class_eval do
    alias_method :detect_scheme, :ruby_detect_scheme
  end
  @rust_enabled = false
end

.rust_enable!bool

Turns on Rust extension, if available.

Returns:

  • the enabled status of the Rust extension



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

def rust_enable!
  return false unless RUST_AVAILABLE
  # :nocov:
  Detect.singleton_class.class_eval do
    alias_method :detect_scheme, :rust_detect_scheme
  end
  @rust_enabled = true
  # :nocov:
end

.rust_enabled?bool

Returns the enabled status of the Rust extension.

Returns:

  • the enabled status of the Rust extension



21
22
23
# File 'lib/sanscript/rust.rb', line 21

def rust_enabled?
  @rust_enabled ||= false
end

.rust_load!bool

Attempts to load Rust native extension.

Returns:

  • whether the extension loaded.



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/sanscript/rust.rb', line 7

def rust_load!
  return RUST_AVAILABLE if defined?(RUST_AVAILABLE)
  require "thermite/fiddle"
  Thermite::Fiddle.load_module("init_rusty_sanscript",
                               cargo_project_path: GEM_ROOT,
                               ruby_project_path: GEM_ROOT)
  #:nocov:#
  defined?(Sanscript::Rust) ? true : false
rescue Fiddle::DLError
  false
  #:nocov:#
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:

    • the String to transliterate

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

    • 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:

    • the transliterated String

    Raises:

    • if scheme detection and fallback fail

    • if a provided transliteration scheme is not supported

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

    Returns the transliterated String.

    Parameters:

    • the String to transliterate

    • 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:

    • the transliterated String

    Raises:

    • if scheme detection and fallback fail

    • if a provided transliteration scheme is not supported



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/sanscript.rb', line 53

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