sskatex 0.9.20 → 0.9.23
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/sskatex.rb +70 -54
- data/test/test_all.rb +1 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e2480466e5a2d88c34e63efd8c390690826d19ea
|
4
|
+
data.tar.gz: 2546f31dd06272e182cbacba9670143ada7239f9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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.
|
162
|
+
# This can be used for monitoring or debugging. Must be either +nil+ or a
|
163
163
|
# proc {|level, &block| ...}
|
164
|
-
# the
|
165
|
-
#
|
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
|
176
|
-
#
|
177
|
-
#
|
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
|
207
|
-
# The
|
208
|
-
#
|
209
|
-
#
|
210
|
-
|
211
|
-
|
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
|
-
#
|
216
|
-
#
|
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
|
222
|
-
# If #config[ +:js_run+ ] is not defined, the contents of the
|
223
|
-
# variable +EXECJS_RUNTIME+ will be considered instead; and if
|
224
|
-
# defined, an automatic choice will be made. For more information,
|
225
|
-
#
|
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
|
-
|
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
|
-
|
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
|
-
|
255
|
+
logv(logger) {"Selected JS runtime: #{jsrun}"}
|
237
256
|
jsrun
|
238
257
|
end
|
239
258
|
end
|
240
259
|
|
241
|
-
# The +ExecJS::Runtime+ subclass
|
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
|
308
|
-
# before #js_context.
|
309
|
-
|
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
|
-
|
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
|
-
|
336
|
+
logv(logger) {"Loading JS file: #{absfile}"}
|
318
337
|
js << IO.read(absfile, external_encoding: Encoding::UTF_8) << "\n"
|
319
338
|
end
|
320
|
-
|
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
|
-
|
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
|
331
|
-
#
|
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_
|
337
|
-
#
|
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
|
-
|
341
|
-
|
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
|
-
|
363
|
+
logd(logger) {"JS eval: #{js}"}
|
344
364
|
ans = ctx.eval(js)
|
345
|
-
|
346
|
-
KaTeX conversion failed
|
347
|
-
|
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
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.
|
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-
|
11
|
+
date: 2017-11-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: execjs
|