Module: Polytrix::Documentation::Helpers::CodeHelper

Included in:
Challenge
Defined in:
lib/polytrix/documentation/helpers/code_helper.rb

Defined Under Namespace

Classes: MarkdownHelper, ReStructuredTextHelper

Instance Method Summary collapse

Instance Method Details

#code_block(source_code, language, opts = { format: :markdown }) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# File 'lib/polytrix/documentation/helpers/code_helper.rb', line 38

def code_block(source_code, language, opts = { format: :markdown })
  case opts[:format]
  when :rst
    ReStructuredTextHelper.code_block source_code, language
  when :markdown
    MarkdownHelper.code_block source_code, language
  else
    fail IllegalArgumentError, "Unknown format: #{format}"
  end
end

#initialize(*args) ⇒ Object



29
30
31
32
# File 'lib/polytrix/documentation/helpers/code_helper.rb', line 29

def initialize(*args)
  @segmenter = Polytrix::Documentation::CodeSegmenter.new
  super
end

#snippet_after(matcher) ⇒ Object

Loses proper indentation on comments



50
51
52
53
54
55
56
57
58
59
# File 'lib/polytrix/documentation/helpers/code_helper.rb', line 50

def snippet_after(matcher)
  segments = @segmenter.segment(source)
  buffer = StringIO.new
  segment = segments.find do |s|
    doc_segment_content = s.first.join
    doc_segment_content.match matcher
  end
  buffer.print segment[1].join "\n" if segment # return code segment
  buffer.string
end

#snippet_between(before_matcher, after_matcher) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/polytrix/documentation/helpers/code_helper.rb', line 61

def snippet_between(before_matcher, after_matcher)
  segments = @segmenter.segment(source)
  start_segment = find_segment_index segments, before_matcher
  end_segment   = find_segment_index segments, after_matcher
  buffer = StringIO.new
  if start_segment && end_segment
    segments[start_segment...end_segment].each do |segment|
      buffer.puts @segmenter.comment(segment[0]) unless segment == segments[start_segment]
      buffer.puts segment[1].join
    end
  end
  buffer.puts "\n"
  buffer.string
end

#sourceObject



34
35
36
# File 'lib/polytrix/documentation/helpers/code_helper.rb', line 34

def source
  File.read absolute_source_file
end