Module: Djot::JavaScript

Defined in:
lib/djot/javascript.rb

Overview

Functionalities of djot.js

Constant Summary collapse

PATH =

:nodoc:

path
VERSION =

:nodoc:

version

Class Method Summary collapse

Class Method Details

.call(name, *args) ⇒ Object

:nodoc:



142
143
144
# File 'lib/djot/javascript.rb', line 142

def self.call(name, *args) # :nodoc:
  context.eval("djot.#{name}.apply(this, #{::JSON.generate(args)})")
end

.contextObject

:nodoc:



19
20
21
22
23
24
25
26
# File 'lib/djot/javascript.rb', line 19

def self.context # :nodoc:
  return @context if @context

  context = MiniRacer::Context.new
  context.eval("let args, result")
  context.eval(source)
  @context = context
end

.from_pandoc(pandoc, warn: nil) ⇒ Object

Correspond to djot.fromPandoc (github.com/jgm/djot.js#pandoc-interoperability)

CAUTION: warn options hasn’t yet tested.



121
122
123
124
125
126
127
128
# File 'lib/djot/javascript.rb', line 121

def self.from_pandoc(pandoc, warn: nil)
  context.eval("args = #{JSON.generate([pandoc, {}])}")
  if warn
    context.attach("warn", warn)
    context.eval('args[1]["warn"] = warn')
  end
  context.eval("djot.fromPandoc.apply(this, args)")
end

.parse(input, source_positions: nil, warn: nil) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/djot/javascript.rb', line 30

def self.parse(input, source_positions: nil, warn: nil)
  args = [input]
  options = {}
  options["sourcePositions"] = source_positions if source_positions
  args << options
  context.eval("args = #{JSON.generate(args)}")
  if warn
    context.attach("warn", warn)
    context.eval('args[1]["warn"] = warn')
  end
  context.eval("djot.parse.apply(this, args)")
end

.parse_events(input, warn: nil, &block) ⇒ Object



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
# File 'lib/djot/javascript.rb', line 45

def self.parse_events(input, warn: nil, &block)
  context.eval("args = #{JSON.generate([input, {}])}")
  if warn
    context.attach("warn", warn)
    context.eval('args[1]["warn"] = warn')
  end
  source = if block_given?
             context.attach("fun", block)
             <<~END_JAVASCRIPT
               for (let event of djot.parseEvents(...args)) {
                 fun(event)
               }
             END_JAVASCRIPT
           else
             # TODO: Use enum_for
             <<~END_JAVASCRIPT
               events = []
               for (let event of djot.parseEvents(...args)) {
                 events.push(event)
               }
               events
             END_JAVASCRIPT
           end
  context.eval(source)
end

.pathObject

:nodoc:



7
8
9
# File 'lib/djot/javascript.rb', line 7

def self.path # :nodoc:
  @path ||= Pathname(__dir__ || (raise Error)) / ".." / "js" / "djot.js"
end

.render_ast(doc, source_positions: false) ⇒ Object



73
74
75
76
77
# File 'lib/djot/javascript.rb', line 73

def self.render_ast(doc, source_positions: false)
  args = [doc]
  args << { "sourcePositions" => source_positions } if source_positions
  context.eval("djot.renderAST.apply(this, #{JSON.generate(args)})")
end

.render_djot(doc, wrap_width: nil) ⇒ Object

Correspond to djot.renderDjot (github.com/jgm/djot.js#rendering-djot)



94
95
96
97
98
99
# File 'lib/djot/javascript.rb', line 94

def self.render_djot(doc, wrap_width: nil)
  options = {}
  options["wrapWidth"] = wrap_width if wrap_width

  call("renderDjot", doc, options)
end

.render_html(doc, warn: nil) ⇒ Object

Correspond to djot.renderHTML (github.com/jgm/djot.js#rendering-the-djot-ast-to-html)

TODO: support overrides option



83
84
85
86
87
88
89
90
# File 'lib/djot/javascript.rb', line 83

def self.render_html(doc, warn: nil)
  context.eval("args = #{JSON.generate([doc, {}])}")
  if warn
    context.attach("warn", warn)
    context.eval('args[1]["warn"] = warn')
  end
  context.eval("djot.renderHTML.apply(this, args)")
end

.sourceObject

:nodoc:



15
16
17
# File 'lib/djot/javascript.rb', line 15

def self.source # :nodoc:
  @source ||= PATH.read
end

.to_pandoc(doc, warn: nil, smart_punctuation_map: nil) ⇒ Object

Correspond to djot.toPandoc (github.com/jgm/djot.js#pandoc-interoperability)

CAUTION: warn option hasn’t yet tested. There may be bugs.



106
107
108
109
110
111
112
113
114
115
# File 'lib/djot/javascript.rb', line 106

def self.to_pandoc(doc, warn: nil, smart_punctuation_map: nil)
  options = {}
  options["smartPunctuationMap"] = smart_punctuation_map if smart_punctuation_map
  context.eval("args = #{JSON.generate([doc, options])}")
  if warn
    context.attach("warn", warn)
    context.eval('args[1]["warn"] = warn')
  end
  context.eval("djot.toPandoc.apply(this, args)")
end

.versionObject

Correspond to djot.version (github.com/jgm/djot.js#getting-the-version)



134
135
136
# File 'lib/djot/javascript.rb', line 134

def self.version
  @version ||= context.eval("djot.version")
end