Skip to content
Kward Search API index

Class: Kward::MarkdownCodeBlock

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

Overview

Finds fenced Markdown code blocks and manages their adjacent output fields.

Defined Under Namespace

Classes: Block, Fence

Class Method Summary collapse

Class Method Details

.for_cursor(source, line) ⇒ Object



59
60
61
62
63
# File 'lib/kward/prompt_interface/editor/markdown_code_block.rb', line 59

def for_cursor(source, line)
  parse(source).select do |block|
    block.open_line <= line && line <= block.close_line
  end.min_by { |block| block.close_line - block.open_line }
end

.for_selection(source, first_line, last_line) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'lib/kward/prompt_interface/editor/markdown_code_block.rb', line 49

def for_selection(source, first_line, last_line)
  blocks = parse(source)
  candidates = blocks.select do |block|
    full_fence = first_line == block.open_line && last_line == block.close_line
    body_only = block.open_line < first_line && last_line < block.close_line
    full_fence || body_only
  end
  candidates.min_by { |block| block.close_line - block.open_line }
end

.format_output(output, newline: "\n") ⇒ Object



129
130
131
132
133
# File 'lib/kward/prompt_interface/editor/markdown_code_block.rb', line 129

def format_output(output, newline: "\n")
  body = output.to_s.gsub(/\r\n?/, "\n").chomp
  body = body.gsub("\n", newline)
  body.empty? ? "<output>#{newline}</output>" : "<output>#{newline}#{body}#{newline}</output>"
end

.parse(source) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/kward/prompt_interface/editor/markdown_code_block.rb', line 65

def parse(source)
  source = source.to_s
  lines = source.lines
  fences = []
  open_fences = []
  offset = 0

  lines.each_with_index do |line, line_number|
    content = line.chomp
    if (match = content.match(/\A\s*(`{3,}|~{3,})(.*)\z/))
      marker = match[1][0]
      length = match[1].length
      rest = match[2]
      closing = rest.match?(/\A\s*\z/)

      if closing && open_fences.last&.marker == marker && length >= open_fences.last.length
        opening = open_fences.pop
        fences << Fence.new(
          line: line_number,
          start: offset,
          finish: offset + line.length,
          marker: marker,
          length: length,
          language: opening.language
        )
        opening.close_line = line_number
        opening.close_start = offset
        opening.close_end = offset + line.length
        offset += line.length
        next
      end

      next if closing

      opening = Fence.new(
        line: line_number,
        start: offset,
        finish: offset + line.length,
        marker: marker,
        length: length,
        language: rest.strip.split(/\s+/, 2).first
      )
      open_fences << opening
      fences << opening
    end
    offset += line.length
  end

  completed = fences.select { |fence| fence.close_line }
  completed.map do |opening|
    next_fence = fences.map(&:start).select { |start| start > opening.close_end }.min
    Block.new(
      source: source,
      open_line: opening.line,
      close_line: opening.close_line,
      language: opening.language,
      body_start: opening.finish,
      body_end: opening.close_start,
      close_end: opening.close_end,
      next_fence_start: next_fence
    )
  end
end