Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/asciidoctor/latex/core_ext/colored_string.rb,
lib/asciidoctor/latex/core_ext/utility.rb

Overview

Utility methods to display colored strings, e.g., “foo”.cyan, “bar”.red, etc.

Instance Method Summary (collapse)

Instance Method Details

- (Object) apply_macros(macro_list)

The 'apply_macros' method simplifies the

chaining of a sequence of macro applications.
For example, we could say

'yoda'.macro('baz').macro('bar').macro('foo')

      => "\\foo{\\bar{\\baz{yoda}}}"

But it is simpler to say

'yoda'.apply_macros(['foo', 'bar', 'baz'])

    => "\\foo{\\bar{\\baz{yoda}}}"

Because we reverse the argument list, the
application order of the LaTeX macros
matches the order in the argument list.


46
47
48
49
50
51
52
# File 'lib/asciidoctor/latex/core_ext/utility.rb', line 46

def apply_macros(macro_list)
  val = self
  macro_list.reverse.each do |macro_name|
    val = val.macro(macro_name)
  end
  val
end

- (Object) black



36
37
38
# File 'lib/asciidoctor/latex/core_ext/colored_string.rb', line 36

def black
  "\e[1;30m#{self}\e[0m"
end

- (String) blue

Returns:



8
9
10
# File 'lib/asciidoctor/latex/core_ext/colored_string.rb', line 8

def blue
  "\e[1;34m#{self}\e[0m"
end

- (Object) cyan



28
29
30
# File 'lib/asciidoctor/latex/core_ext/colored_string.rb', line 28

def cyan
  "\e[1;36m#{self}\e[0m"
end

- (Object) green



12
13
14
# File 'lib/asciidoctor/latex/core_ext/colored_string.rb', line 12

def green
  "\e[1;32m#{self}\e[0m"
end

- (Object) macro(macro)

This method allows one to compute the strings that represent LaTeX macro applicatins without descent into a hell of backlslshes and braces, especially when more than one macro has to be applied. For example, instead of

content = "\\roleblue\{ #{content}\}"

we say just

content = content.macro('roleblue')

Here is an appication of three macros:

content.macro('roleblue').macro('foo').macro('bar')


24
25
26
# File 'lib/asciidoctor/latex/core_ext/utility.rb', line 24

def macro(macro)
  "\\#{macro}\{#{self}\}"
end

- (Object) magenta



24
25
26
# File 'lib/asciidoctor/latex/core_ext/colored_string.rb', line 24

def magenta
  "\e[1;35m#{self}\e[0m"
end

- (Object) red



16
17
18
# File 'lib/asciidoctor/latex/core_ext/colored_string.rb', line 16

def red
  "\e[1;31m#{self}\e[0m"
end

- (Object) tex_normalize

map '_' to '-' and prefix by 'x' if the leading character is '-'



57
58
59
60
61
62
63
64
# File 'lib/asciidoctor/latex/core_ext/utility.rb', line 57

def tex_normalize
  str = self.gsub('_', '-')
  if str[0] == '-'
    'x'+str
  else
    str
  end
end

- (Object) white



32
33
34
# File 'lib/asciidoctor/latex/core_ext/colored_string.rb', line 32

def white
  "\e[1;37m#{self}\e[0m"
end

- (Object) yellow



20
21
22
# File 'lib/asciidoctor/latex/core_ext/colored_string.rb', line 20

def yellow
  "\e[1;33m#{self}\e[0m"
end