Class: Kramdown::Latexish::Lexical

Inherits:
Object
  • Object
show all
Defined in:
lib/kramdown/latexish/lexical.rb

Overview

Lexical tools, including localisation

The class this is included into shall provide the method

lang:

The language in use, one of :english or :french

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(language) ⇒ Lexical

Returns a new instance of Lexical.



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
# File 'lib/kramdown/latexish/lexical.rb', line 16

def initialize(language)
  @language = language

  # The specifications from which everything else is derived
  @localisation = {
    :abstract => {
      :english => 'abstract(s)',
      :french  => 'abstract(s)',
    },
    :definition => {
      :english => 'definition(s)',
      :french  => 'définition(s)',
    },
    :postulate => {
      :english => 'postulate(s)',
      :french  => 'postulat(s)',
    },
    :property => {
      :english => 'property(<ies)',
      :french  => 'propriété(s)',
    },
    :lemma => {
      :english => 'lemma(s)',
      :french  => 'lemme(s)',
    },
    :theorem => {
      :english => 'theorem(s)',
      :french  => 'théorème(s)',
    },
    :corollary => {
      :english => 'corollary(<ies)',
      :french  => 'corollaire(s)',
    },
    :section => {
      :english => 'section(s)',
      :french  => 'section(s)',
    },
    :reference => {
      :english => 'reference(s)',
      :french  => 'référence(s)',
    },
    :eqn => {
      :english => 'eqn(s)',
      :french  => 'éqn(s)'
    },
    :and => {
      :english => 'and',
      :french  => 'et',
    },
  }

  # The list of languages, computed from @localisation
  @languages = @localisation.values.map(&:keys).reduce(:&)

  # Associate e.g. "property" and "properties" to :property
  @reverse_localisation = Hash[@languages.map {|lang|
    h = {}
    @localisation.keys.map do |category|
      h[localise(category, :singular)] = category
      h[localise(category, :plural)]   = category
    end
    [lang, h]
  }]
end

Instance Attribute Details

#languageObject (readonly)

The active language



11
12
13
# File 'lib/kramdown/latexish/lexical.rb', line 11

def language
  @language
end

#languagesObject (readonly)

All supported languages



14
15
16
# File 'lib/kramdown/latexish/lexical.rb', line 14

def languages
  @languages
end

Instance Method Details

#and(array, joined: true, nbsp: false) ⇒ Object

The lexical conjonction with commas and the word “and” of the given words

This method is versatile as it can take an array of strings, or of more complex objects, and return an array or a joined string.

SYNOPSIS

and(%w(apples pears)) is “apple and pears”

and(%w(apples pears), joined:false) is [“apple”, “ and ”, pears“]

and([E(“apples”), E(“pears”)], joined:false) is

E(“apples”), E(“ and ”), E(“pears”)

Passing ‘joined:true` in this case would most likely not make sense and lead to an error, unless the objects returned by E(…) support enough of the String API.

Commas appear with three elements or more

and(%w(apples bananas pears)) is “apples, bananas, and pears”

The method knows that in some languages the equivalent of that final “and” is not preceded by a comma. For example, in French

and(%(pommes bananes poires)) is “pommes, bananes et poires”



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/kramdown/latexish/lexical.rb', line 139

def and(array, joined: true, nbsp: false)
  and_ = localise(:and, language)
  comma_sep = ', '
  and_sep_2 = ' ' + and_
  # Some languages put a comma before "and", others don't
  and_sep_n = ([:english].include?(language) ? ', ' : ' ') + and_
  space = nbsp ? '&nbsp;' : ' '
  and_sep_2 += space
  and_sep_n += space

  if block_given?
    comma_sep = yield(comma_sep)
    and_sep_2 = yield(and_sep_2)
    and_sep_n = yield(and_sep_n)
  end
  output = case array.size
  when 1
    array
  when 2
    [array[0], and_sep_2, array[1]]
  else
    seps = Array.new(array.size - 2).fill(comma_sep) + [and_sep_n]
    array.zip(seps).flatten.compact
  end
  if joined
    output.join
  else
    output
  end
end

#localise(symbol, form = :singular) ⇒ Object

The word in the current language and specified singular/plural form corresponding to the given symbol. E.g. :property => “propriété” for form=:singular in French



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/kramdown/latexish/lexical.rb', line 84

def localise(symbol, form=:singular)
  %r{^ (?<singular> [[:alpha:]]+)
     (
       \(
         (?<back><+)?
         (?<plural_ending> [[:alpha:]]+)
       \)
     )?
  }x =~ @localisation[symbol][language]
  return singular if plural_ending.nil?
  stop = back.nil? ? -1 : -back.length - 1
  case form
  when :singular
    singular
  when :plural
    singular[..stop] + plural_ending
  else
    raise "Unknown form: #{form}"
  end
end

#symbolise(word) ⇒ Object

The symbol corresponding to the given word, i.e. the reverse of ‘localise`. E.g. “properties” => :property in English



108
109
110
# File 'lib/kramdown/latexish/lexical.rb', line 108

def symbolise(word)
  @reverse_localisation[language][word.downcase]
end