Class: Rundoc::CodeSection

Inherits:
Object
  • Object
show all
Defined in:
lib/rundoc/code_section.rb

Overview

holds code, parses and creates CodeCommand

Defined Under Namespace

Classes: ParseError

Constant Summary collapse

COMMAND_REGEX =

todo: move whole thing

Rundoc::Parser::COMMAND_REGEX

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(match, options = {}) ⇒ CodeSection

Returns a new instance of CodeSection.



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

def initialize(match, options = {})
  @original = match.to_s
  @commands = []
  @stack    = []
  @keyword  = options[:keyword] or raise "keyword is required"
  @document_path = options[:document_path]
  @fence    = match[:fence]
  @lang     = match[:lang]
  @code     = match[:contents]
  parse_code_command
end

Instance Attribute Details

#codeObject

Returns the value of attribute code.



28
29
30
# File 'lib/rundoc/code_section.rb', line 28

def code
  @code
end

#commandsObject

Returns the value of attribute commands.



28
29
30
# File 'lib/rundoc/code_section.rb', line 28

def commands
  @commands
end

#fenceObject

Returns the value of attribute fence.



28
29
30
# File 'lib/rundoc/code_section.rb', line 28

def fence
  @fence
end

#keywordObject

Returns the value of attribute keyword.



28
29
30
# File 'lib/rundoc/code_section.rb', line 28

def keyword
  @keyword
end

#langObject

Returns the value of attribute lang.



28
29
30
# File 'lib/rundoc/code_section.rb', line 28

def lang
  @lang
end

#originalObject

Returns the value of attribute original.



28
29
30
# File 'lib/rundoc/code_section.rb', line 28

def original
  @original
end

Instance Method Details

#hidden?Boolean

all of the commands are hidden

Returns:

  • (Boolean)


84
85
86
# File 'lib/rundoc/code_section.rb', line 84

def hidden?
  !not_hidden?
end

#not_hidden?Boolean

one or more of the commands are not hidden

Returns:

  • (Boolean)


89
90
91
92
# File 'lib/rundoc/code_section.rb', line 89

def not_hidden?
  return true if commands.empty?
  commands.map(&:not_hidden?).detect {|c| c }
end

#parse_code_commandObject



94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/rundoc/code_section.rb', line 94

def parse_code_command
  parser = Rundoc::PegParser.new.code_block
  tree = parser.parse(@code)
  actual = Rundoc::PegTransformer.new.apply(tree)
  actual = [actual] unless actual.is_a?(Array)
  actual.each do |code_command|
    @stack   << "\n" if commands.last.is_a?(Rundoc::CodeCommand)
    @stack   << code_command
    commands << code_command
  end
rescue ::Parslet::ParseFailed => e
  raise "Could not compile code:\n#{@code}\nReason: #{e.message}"
end

#renderObject



42
43
44
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
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/rundoc/code_section.rb', line 42

def render
  result = []
  env    = {}
  env[:commands] = []
  env[:before]   = String.new("#{fence}#{lang}")
  env[:after]    = String.new(fence)
  env[:document_path] = @document_path

  @stack.each do |s|
    unless s.respond_to?(:call)
      result << s
      next
    end

    code_command = s
    code_output  = code_command.call(env)  || ""
    code_line    = code_command.to_md(env) || ""

    env[:commands] << { object: code_command, output: code_output, command: code_line }

    tmp_result = []
    tmp_result << code_line   if code_command.render_command?
    tmp_result << code_output if code_command.render_result?

    result << tmp_result unless code_command.hidden?
    result
  end

  return env[:replace] if env[:replace]

  return "" if hidden?

  array = [env[:before], result, env[:after]]
  array.flatten!
  array.compact!
  array.map!(&:rstrip)
  array.reject!(&:empty?)

  return array.join("\n") << "\n"
end