Module: Opulent::Logger

Defined in:
lib/opulent/logger.rb

Class Method Summary collapse

Class Method Details

.black(text) ⇒ Object

Colors available in the terminal



19
20
21
# File 'lib/opulent/logger.rb', line 19

def black(text)
  colorize(text, "\e[30m")
end

.blue(text) ⇒ Object



35
36
37
# File 'lib/opulent/logger.rb', line 35

def blue(text)
  colorize(text, "\e[34m")
end

.colorize(text, color_code) ⇒ Object

Color the input text with the chosen color

Parameters:

  • text (String)

    the string that will be colored

  • color_code (String)

    preset code for certain colors



12
13
14
15
# File 'lib/opulent/logger.rb', line 12

def colorize(text, color_code)
  require_windows_libs
  "#{color_code}#{text}\e[0m"
end

.compile_error(template, error, *data) ⇒ Object

Output an error message based on class context and input data

Parameters:

  • klass (Symbol)

    Class in which the error happens

  • error (Symbol)

    Error identification symbol

  • data (Array)

    Data to be displayed with the error



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/opulent/logger.rb', line 185

def compile_error(template, error, *data)
  case error
  when :explicit_end
    message = <<-ERROR
Explicit "end" evaluation nodes are not allowed. End expressions are
inserted automatically.
    ERROR
  end

  fail <<-OPULENT_ERROR
\n
[Opulent Compiler] Runtime Error

#{message}

  OPULENT_ERROR
end

.cyan(text) ⇒ Object



43
44
45
# File 'lib/opulent/logger.rb', line 43

def cyan(text)
  colorize(text, "\e[36m")
end

.default(text) ⇒ Object



51
52
53
# File 'lib/opulent/logger.rb', line 51

def default(text)
  colorize(text, "\e[38m")
end

.error(type, *data) ⇒ Object

Display an error message based on context



121
122
123
124
125
126
127
128
129
130
# File 'lib/opulent/logger.rb', line 121

def error(type, *data)
  case type
  when :parse
    parse_error data[0], data[1], data[2], data[3], data[4..-1]
  when :compile
    compile_error data[0], data[1], data[2..-1]
  when :exec
    exec_error data[0], data[1..-1]
  end
end

.exec_error(error, *data) ⇒ Object

Output an error message based on class context and input data

Parameters:

  • klass (Symbol)

    Class in which the error happens

  • error (Symbol)

    Error identification symbol

  • data (Array)

    Data to be displayed with the error



138
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
169
170
171
172
173
174
175
176
177
# File 'lib/opulent/logger.rb', line 138

def exec_error(error, *data)
  case error
  when :input
    message = <<-ERROR
Given input file #{data[0].inspect} does not exist or an incorrect path
has been specified.
    ERROR
  when :layout_error
    message = <<-ERROR
Missing input or incorrect file extension for layout [-l] argument.
Found #{data[0]} instead.
    ERROR
  when :locals_file
    message = <<-ERROR
Given context file #{data[0].inspect} does not exist or
an incorrect path has been specified.
    ERROR
  when :locals_file_format
    message = <<-ERROR
Unknown file extension #{data[0].inspect} given as locals file. Please use
JSON or YAML as input.
    ERROR
  when :input_arguments
    message = <<-ERROR
Unknown input argument [#{data[0]}] has been encountered.
    ERROR
  when :no_input
    message = <<-ERROR
You haven't specified an input file.
    ERROR
  end

  fail <<-OPULENT_ERROR
\n
[Opulent Engine] Runtime Error

#{message}

  OPULENT_ERROR
end

.green(text) ⇒ Object



27
28
29
# File 'lib/opulent/logger.rb', line 27

def green(text)
  colorize(text, "\e[32m")
end

.log(message, *data) ⇒ Object

Output an error message based on class context and input data

Parameters:

  • message (Symbol)

    Message to be displayed

  • data (Array)

    Data to be displayed with the message



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/opulent/logger.rb', line 74

def log(message, *data)
  case message
  when :version
    title = 'Version'
    message = <<-LOG
Version #{Opulent::VERSION} is currently installed.
    LOG
  when :successful_render
    title = 'Render Complete'
    message = <<-LOG
Successfully rendered #{data[0].inspect} to #{data[1].inspect}.
    LOG
  when :successful_render_print
    title = 'Render Complete'
    message = <<-LOG
Successfully rendered #{data[0].inspect}.

No output file specified. Writing result to terminal.

#{data[1]}"

    LOG
  when :help
    title = 'Help'
    message = <<-LOG
You can use the following commands with the Opulent Command  Line Interface:

opulent input.op output.html    Render an input file and write the result to
                          the output file.
opulent layout [-l] layout.op   Render an input file using given input
                          layout file.
opulent help [-h]               Show available command line options.
opulent version [-v]            Show installed version.
    LOG
  end

  puts <<-OPULENT_LOG
\n
[Opulent Engine] #{title}

#{message}

  OPULENT_LOG
end

.magenta(text) ⇒ Object



39
40
41
# File 'lib/opulent/logger.rb', line 39

def magenta(text)
  colorize(text, "\e[35m")
end

.parse_error(code, line, character, error, *data) ⇒ Object

Output an error message based on class context and input data

Parameters:

  • klass (Symbol)

    Class in which the error happens

  • error (Symbol)

    Error identification symbol

  • data (Array)

    Data to be displayed with the error



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/opulent/logger.rb', line 209

def parse_error(code, line, character, error, *data)
  line += 1

  case error
  when :unknown_node_type
    message = <<-ERROR
An unknown node type has been encountered.
    ERROR
  when :expected
    if [:'(', :'{', :'[', :'<'].include? data[0]
      data[0] = "#{Tokens.bracket data[0]}"
    end
    message = <<-ERROR
Expected to find a :#{data[0]} token on line #{line} of input.
    ERROR
  when :root
    message = <<-ERROR
Unknown node type encountered on line #{line} of input.
    ERROR
  when :assignments_colon
    message = <<-ERROR
Unexpected end of element attributes reached on line #{line}
of input.
    ERROR
  when :assignments_comma
    message = <<-ERROR
Unexpected end of element attributes reached on line #{line}
of input. Expected to find an attribute value.
    ERROR
  when :expression
    message = <<-ERROR
Unexpected end of expression reached on line #{line} of input.
Expected to find another expression term.
    ERROR
  when :control_child
    message = <<-ERROR
Unexpected control structure child found on line #{line} of input.
Expected to find a parent #{data[0]} structure.
    ERROR
  when :whitespace_expression
    message = <<-ERROR
Unexpected end of expression reached on line #{line} of input.
Please use paranthesis for method parameters
    ERROR
  when :definition
    message = <<-ERROR
Unexpected start of definition on line #{line} of input.
Found a definition inside another definition or element.
    ERROR
  when :self_enclosing
    message = <<-ERROR
Unexpected content found after self enclosing node on line
#{line} of input.
    ERROR
  when :self_enclosing_children
    message = <<-ERROR
Unexpected child elements found for self enclosing node on line
#{data[0] + 1} of input.
    ERROR
  when :include
    message = <<-ERROR
The included file #{data[0]} does not exist or an incorrect path
has been specified.
    ERROR
  when :include_dir
    message = <<-ERROR
The included file path #{data[0]} is a directory.
    ERROR
  when :include_end
    message = <<-ERROR
Missing argument for include on line #{line} of input.
    ERROR
  else
    message = <<-ERROR
Unexpected syntax error on line #{line} of input.
    ERROR
  end

  fail <<-OPULENT_ERROR
\n
[Opulent Parser] Runtime Error

#{message}
#{code[line - 1].chomp}
#{' ' * character}^

  OPULENT_ERROR
end

.red(text) ⇒ Object



23
24
25
# File 'lib/opulent/logger.rb', line 23

def red(text)
  colorize(text, "\e[31m")
end

.require_windows_libsObject

Require windows libraries for ANSI Console output



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

def require_windows_libs
  return unless RUBY_PLATFORM =~ /win32/

  begin
    require 'Win32/Console/ANSI'
  rescue LoadError
    raise 'You must run "gem install win32console" to use Opulent\'s
          error reporting on Windows.'
  end
end

.white(text) ⇒ Object



47
48
49
# File 'lib/opulent/logger.rb', line 47

def white(text)
  colorize(text, "\e[37m")
end

.yellow(text) ⇒ Object



31
32
33
# File 'lib/opulent/logger.rb', line 31

def yellow(text)
  colorize(text, "\e[33m")
end