Module: Pseudocode

Defined in:
lib/pseudocode.rb,
lib/pseudocode/version.rb

Overview

Provides a Ruby wrapper for pseudocode server-side rendering.

Constant Summary collapse

VERSION =

Version of this gem

'0.1.1'
PSEUDOCODE_VERSION =

Version of the included Pseudocode.js

'2.2.0'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.caption_countObject

Emulated caption counter to allow manipulation from ruby



45
46
47
# File 'lib/pseudocode.rb', line 45

def caption_count
  @caption_count
end

.execjs_runtimeObject

The ExecJS runtime factory, default: ‘-> { ExecJS.runtime }`. Set this before calling any other methods to use a different runtime.

This proc is guaranteed to be called at most once.



42
43
44
# File 'lib/pseudocode.rb', line 42

def execjs_runtime
  @execjs_runtime
end

Class Method Details

.gem_pathObject

Return gem base path



68
69
70
71
# File 'lib/pseudocode.rb', line 68

def gem_path
  @gem_path ||=
    File.expand_path(File.join(File.dirname(__FILE__), '..'))
end

.pseudocode_contextObject

Return JS context, preloaded with KaTeX and Pseudocode.js



48
49
50
51
52
53
# File 'lib/pseudocode.rb', line 48

def pseudocode_context
  @load_context_mutex.synchronize do
    source = (File.read Katex.katex_js_path) + (File.read pseudocode_js_path)
    @context ||= @execjs_runtime.call.compile source
  end
end

.pseudocode_css_pathObject

Return path to included Pseudocode.js style file



62
63
64
65
# File 'lib/pseudocode.rb', line 62

def pseudocode_css_path
  File.expand_path File.join('vendor', 'pseudocode', 'pseudocode.min.css'),
                   gem_path
end

.pseudocode_js_pathObject

Return path to included Pseudocode.js script file



56
57
58
59
# File 'lib/pseudocode.rb', line 56

def pseudocode_js_path
  File.expand_path File.join('vendor', 'pseudocode', 'pseudocode.min.js'),
                   gem_path
end

.render(algorithm, line_number: false, **options) ⇒ String, Int

Note:

This method is thread-safe as long as your ExecJS runtime is thread-safe. MiniRacer is the recommended runtime.

Renders the given pseudocode algorithm to HTML via pseudocode.renderToString.

Parameters:

  • algorithm (String)

    The algorithm expression

  • line_number (Bool) (defaults to: false)

    Whether to include line numbers

  • options (Hash)

    Additional options for pseudocode.renderToString.

Returns:

  • (String, Int)

    HTML render and caption number



29
30
31
32
33
34
35
36
# File 'lib/pseudocode.rb', line 29

def render(algorithm, line_number: false, **options)
  return pseudocode_context.call('pseudocode.renderToString', algorithm,
                                 lineNumber: line_number,
                                 captionCount: @caption_count,
                                 **options), @caption_count += 1
rescue ExecJS::ProgramError => e
  raise e
end