Skip to content
Kward Search API index

Class: Kward::MarkdownCodeBlock::Block

Inherits:
Struct
  • Object
show all
Defined in:
lib/kward/prompt_interface/editor/markdown_code_block.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#body_endObject

Returns the value of attribute body_end

Returns:

  • (Object)

    the current value of body_end



5
6
7
# File 'lib/kward/prompt_interface/editor/markdown_code_block.rb', line 5

def body_end
  @body_end
end

#body_startObject

Returns the value of attribute body_start

Returns:

  • (Object)

    the current value of body_start



5
6
7
# File 'lib/kward/prompt_interface/editor/markdown_code_block.rb', line 5

def body_start
  @body_start
end

#close_endObject

Returns the value of attribute close_end

Returns:

  • (Object)

    the current value of close_end



5
6
7
# File 'lib/kward/prompt_interface/editor/markdown_code_block.rb', line 5

def close_end
  @close_end
end

#close_lineObject

Returns the value of attribute close_line

Returns:

  • (Object)

    the current value of close_line



5
6
7
# File 'lib/kward/prompt_interface/editor/markdown_code_block.rb', line 5

def close_line
  @close_line
end

#languageObject

Returns the value of attribute language

Returns:

  • (Object)

    the current value of language



5
6
7
# File 'lib/kward/prompt_interface/editor/markdown_code_block.rb', line 5

def language
  @language
end

#next_fence_startObject

Returns the value of attribute next_fence_start

Returns:

  • (Object)

    the current value of next_fence_start



5
6
7
# File 'lib/kward/prompt_interface/editor/markdown_code_block.rb', line 5

def next_fence_start
  @next_fence_start
end

#open_lineObject

Returns the value of attribute open_line

Returns:

  • (Object)

    the current value of open_line



5
6
7
# File 'lib/kward/prompt_interface/editor/markdown_code_block.rb', line 5

def open_line
  @open_line
end

#sourceObject

Returns the value of attribute source

Returns:

  • (Object)

    the current value of source



5
6
7
# File 'lib/kward/prompt_interface/editor/markdown_code_block.rb', line 5

def source
  @source
end

Instance Method Details

#codeObject



9
10
11
# File 'lib/kward/prompt_interface/editor/markdown_code_block.rb', line 9

def code
  source[body_start...body_end].to_s
end

#output_edit(output) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/kward/prompt_interface/editor/markdown_code_block.rb', line 19

def output_edit(output)
  replacement = MarkdownCodeBlock.format_output(output, newline: newline)
  match = output_match
  if match
    [match.begin(0), match.end(0), replacement]
  else
    insertion = if source[close_end - newline.length, newline.length] == newline
                   "#{newline}#{replacement}#{newline}"
                 else
                   "#{newline}#{newline}#{replacement}#{newline}"
                 end
    [close_end, close_end, insertion]
  end
end

#output_matchObject



13
14
15
16
17
# File 'lib/kward/prompt_interface/editor/markdown_code_block.rb', line 13

def output_match
  limit = next_fence_start || source.length
  match = source.match(/<output\b[^>]*>.*?<\/output>/mi, close_end)
  match if match && match.begin(0) < limit && match.end(0) <= limit
end

#with_output(output) ⇒ Object



34
35
36
37
# File 'lib/kward/prompt_interface/editor/markdown_code_block.rb', line 34

def with_output(output)
  start_index, end_index, replacement = output_edit(output)
  source.dup.tap { |content| content[start_index...end_index] = replacement }
end