sskatex 0.9.20 → 0.9.23

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/sskatex.rb +70 -54
  3. data/test/test_all.rb +1 -2
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3b5feb74566bcc5031cb615e5bacf5e795c6e465
4
- data.tar.gz: a5163ab70c56af3d2aba6f7670883e5114348922
3
+ metadata.gz: e2480466e5a2d88c34e63efd8c390690826d19ea
4
+ data.tar.gz: 2546f31dd06272e182cbacba9670143ada7239f9
5
5
  SHA512:
6
- metadata.gz: 7d3e59e7a9d7005779e656528c09714b50d27c5def895baa2dd97560003e22a2342059698359043c548e222dc2d8d23fde368ba97e206de0878798f3a70476fb
7
- data.tar.gz: baa2384e98b60595061776f234fcc7be036c9ef51f2a586e294e9b3487f39d0d515cc0e7c48679e994558d78f00491ec09c8533956e4c7b386efc07522c9b5e7
6
+ metadata.gz: c5432c7f576e02bf636f938dda1dd891118c8e75b8abb530df630a6a5eb8bb7db0ceb01ae21c9a0b3efa88f3c2f78303a5e048c58cc42d4ec260c60dcad366fd
7
+ data.tar.gz: 06ac0855602e741da342d1186b2131cbacb2ff73de1fcba000bcb31efb88b2176518168f4d48305553786489c2bcc3cedc3c77b468869f9685191fc333b617fe
data/lib/sskatex.rb CHANGED
@@ -159,11 +159,10 @@ class SsKaTeX
159
159
  end
160
160
  end
161
161
 
162
- # This can be used for monitoring or debugging. When set to a
162
+ # This can be used for monitoring or debugging. Must be either +nil+ or a
163
163
  # proc {|level, &block| ...}
164
- # the #logger will be used internally as
165
- # logger.call(level) {msg}
166
- # with the log message constructed in the given block. _level_ is one of:
164
+ # where the _block_ is used for on-demand construction of the log message.
165
+ # _level_ is one of:
167
166
  #
168
167
  # +:verbose+::
169
168
  # For information about the effective engine configuration.
@@ -172,11 +171,30 @@ class SsKaTeX
172
171
  # For the Javascript expressions used when converting TeX.
173
172
  # Issued once per TeX snippet.
174
173
  #
175
- # For example, to ignore +:debug+ yet trace +:verbose+ messages, set
176
- # .logger = lambda {|level, &block| warn(block.call) if level == :verbose}
177
- # The default after construction is to log nothing.
174
+ # For example, to trace +:verbose+ but not +:debug+ messages, set #logger to
175
+ # lambda {|level, &block| warn(block.call) if level == :verbose}
176
+ # If #logger is +nil+, no logging will be done.
178
177
  attr_accessor :logger
179
178
 
179
+ protected
180
+
181
+ # Generic shortcut for logging
182
+ def log(logger = @logger, level, &msg)
183
+ logger.call(level, &msg) if logger
184
+ end
185
+
186
+ # Shortcut for logging debug-level messages
187
+ def logd(logger = @logger, &msg)
188
+ log(logger, :debug, &msg)
189
+ end
190
+
191
+ # Shortcut for logging verbose-level messages
192
+ def logv(logger = @logger, &msg)
193
+ log(logger, :verbose, &msg)
194
+ end
195
+
196
+ public
197
+
180
198
  # A dictionary with the used configuration options.
181
199
  # The resulting effective option values can be read from the same-named
182
200
  # attributes #katex_js, #katex_opts, #js_dir, #js_libs, #js_run.
@@ -203,44 +221,45 @@ class SsKaTeX
203
221
  @config = cfg
204
222
  end
205
223
 
206
- # Create a new instance configured with keyword arguments. Disable logging.
207
- # The keyword arguments can be retrieved as dictionary #config, and the
208
- # resulting effective option values can be read from the same-named attributes
209
- # #katex_js, #katex_opts, #js_dir, #js_libs, #js_run.
210
- def initialize(cfg = {})
211
- @logger = lambda {|level, &block|}
224
+ # Create a new instance configured with keyword arguments and optional logger.
225
+ # The arguments are just stored by reference; no further action is taken until
226
+ # parts of the configuration are actually needed in other method calls.
227
+ # The dictionary with the keyword arguments can be accessed as #config.
228
+ # The logger can be accessed as #logger.
229
+ def initialize(cfg = {}, &logger)
230
+ @logger = logger
212
231
  self.config = cfg
213
232
  end
214
233
 
215
- # A symbol for the Javascript engine to be used. Recognized identifiers
216
- # include: +:RubyRacer+, +:RubyRhino+, +:Duktape+, +:MiniRacer+, +:Node+,
234
+ # Identifies the Javascript engine to be used. Defined identifiers include:
235
+ # +:RubyRacer+, +:RubyRhino+, +:Duktape+, +:MiniRacer+, +:Node+,
217
236
  # +:JavaScriptCore+, +:Spidermonkey+, +:JScript+, +:V8+, and +:Disabled+;
218
237
  # that last one would raise an error on first run (by #js_context).
219
238
  # Which engines are actually available depends on your installation.
220
239
  #
221
- # #js_run is determined on demand as follows and then cached for reuse.
222
- # If #config[ +:js_run+ ] is not defined, the contents of the environment
223
- # variable +EXECJS_RUNTIME+ will be considered instead; and if that is not
224
- # defined, an automatic choice will be made. For more information, set the
225
- # #logger to show +:verbose+ messages and consult the documentation of
240
+ # #js_run is determined on first demand as follows, then logged and cached
241
+ # for reuse. If #config[ +:js_run+ ] is not defined, the contents of the
242
+ # environment variable +EXECJS_RUNTIME+ will be considered instead; and if
243
+ # that is not defined, an automatic choice will be made. For more information,
244
+ # use the logger to show +:verbose+ messages and consult the documentation of
226
245
  # ExecJS[https://github.com/rails/execjs#execjs].
227
- def js_run
246
+ # If a block is given, it is used instead of the logger set with #logger=.
247
+ def js_run(&logger)
228
248
  @js_run ||= begin
229
- log = lambda {|&block| @logger.call(:verbose, &block)}
230
-
231
- log.call {"Available JS runtimes: #{Utils.js_runtimes.join(', ')}"}
249
+ logger ||= @logger
250
+ logv(logger) {"Available JS runtimes: #{Utils.js_runtimes.join(', ')}"}
232
251
  jsrun = (@config[:js_run] ||
233
252
  ENV_EXECJS_RUNTIME ||
234
253
  Utils::JSRUN_TOSYM[ExecJS::Runtimes.best_available] ||
235
254
  'Disabled').to_s.to_sym
236
- log.call {"Selected JS runtime: #{jsrun}"}
255
+ logv(logger) {"Selected JS runtime: #{jsrun}"}
237
256
  jsrun
238
257
  end
239
258
  end
240
259
 
241
- # The +ExecJS::Runtime+ subclass to be used, corresponding to #js_run.
242
- def js_runtime
243
- @js_runtime ||= Utils::JSRUN_FROMSYM[js_run]
260
+ # The +ExecJS::Runtime+ subclass corresponding to #js_run(&logger).
261
+ def js_runtime(&logger)
262
+ @js_runtime ||= Utils::JSRUN_FROMSYM[js_run(&logger)]
244
263
  end
245
264
 
246
265
  # The path to a directory with Javascript helper files as specified by
@@ -304,51 +323,48 @@ class SsKaTeX
304
323
 
305
324
  # The concatenation of the contents of the files in #js_libs, in #katex_js,
306
325
  # and a JS variable definition for #katex_opts, each item followed by a
307
- # newline. Created at first use. Can be used to validate JS contents if used
308
- # before #js_context.
309
- def js_source
326
+ # newline. Created at first use, with filenames and #katex_opts logged.
327
+ # Can be used to validate JS contents before a #js_context is created.
328
+ # If a block is given, it is used instead of the logger set with #logger=.
329
+ def js_source(&logger)
310
330
  @js_source ||= begin
311
- log = lambda {|&block| @logger.call(:verbose, &block)}
331
+ logger ||= @logger
312
332
 
313
- # Concatenate sources
314
333
  js = ''
315
334
  js_libs.each do |libfile|
316
335
  absfile = File.expand_path(libfile, js_dir)
317
- log.call {"Loading JS file: #{absfile}"}
336
+ logv(logger) {"Loading JS file: #{absfile}"}
318
337
  js << IO.read(absfile, external_encoding: Encoding::UTF_8) << "\n"
319
338
  end
320
- log.call {"Loading KaTeX JS file: #{katex_js}"}
339
+ logv(logger) {"Loading KaTeX JS file: #{katex_js}"}
321
340
  js << IO.read(katex_js, external_encoding: Encoding::UTF_8) << "\n"
322
341
 
323
- # Initialize JS variable katex_opts
324
342
  js_katex_opts = "var katex_opts = #{katex_opts.to_json}"
325
- log.call {"JS eval: #{js_katex_opts}"}
343
+ logv(logger) {"JS eval: #{js_katex_opts}"}
326
344
  js << js_katex_opts << "\n"
327
345
  end
328
346
  end
329
347
 
330
- # The JS engine context resulting from compilation of #js_source by the
331
- # #js_runtime selected with #js_run. Created at first use e.g. by #call.
332
- def js_context
333
- @js_context ||= js_runtime.compile(js_source)
348
+ # The JS engine context obtained by letting the #js_runtime(&logger) compile
349
+ # the #js_source(&logger). Created at first use e.g. by #call.
350
+ def js_context(&logger)
351
+ @js_context ||= js_runtime(&logger).compile(js_source(&logger))
334
352
  end
335
353
 
336
- # Given a TeX math fragment _tex_ as well as a boolean _display_mode_ (true
337
- # for block, false for inline), run the JS engine (using #js_context) and let
338
- # KaTeX compile the math fragment. Return the resulting HTML string.
354
+ # Given a TeX math fragment _tex_ and a boolean _display_mode_ (+true+ for
355
+ # block, default +false+ for inline), run the JS engine (using #js_context)
356
+ # and let KaTeX compile the math fragment. Return the resulting HTML string.
339
357
  # Can raise errors if something in the process fails.
340
- def call(tex, display_mode)
341
- ctx = js_context
358
+ # If a block is given, it is used instead of the logger set with #logger=.
359
+ def call(tex, display_mode = false, &logger)
360
+ logger ||= @logger
361
+ ctx = js_context(&logger)
342
362
  js = "tex_to_html(#{Utils.js_quote(tex)}, #{display_mode.to_json}, katex_opts)"
343
- @logger.call(:debug) {"JS eval: #{js}"}
363
+ logd(logger) {"JS eval: #{js}"}
344
364
  ans = ctx.eval(js)
345
- raise (<<MSG) unless ans && ans.start_with?('<') && ans.end_with?('>')
346
- KaTeX conversion failed!
347
- Input:
348
- #{tex}
349
- Output:
350
- #{ans}
351
- MSG
365
+ unless ans && ans.start_with?('<') && ans.end_with?('>')
366
+ raise "KaTeX conversion failed!\nInput:\n#{tex}\nOutput:\n#{ans}"
367
+ end
352
368
  ans
353
369
  end
354
370
  end
data/test/test_all.rb CHANGED
@@ -11,8 +11,7 @@ require 'sskatex'
11
11
 
12
12
  class SsKaTeXTest < Minitest::Test
13
13
  def setup
14
- @tex2html = SsKaTeX.new
15
- @tex2html.logger = lambda do |level, &block|
14
+ @tex2html = SsKaTeX.new do |level, &block|
16
15
  # warn(block.call) if level == :verbose
17
16
  end
18
17
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sskatex
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.20
4
+ version: 0.9.23
5
5
  platform: ruby
6
6
  authors:
7
7
  - Christian Cornelssen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-11-20 00:00:00.000000000 Z
11
+ date: 2017-11-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: execjs