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

Included in:
Challenge, Command::Generate::Documentation
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



47
48
49
50
51
52
53
54
55
56
# File 'lib/polytrix/documentation/helpers/code_helper.rb', line 47

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

#highlighted_code(formatter = 'terminal256') ⇒ Object



43
44
45
# File 'lib/polytrix/documentation/helpers/code_helper.rb', line 43

def highlighted_code(formatter = 'terminal256')
  highlight(source, language: implementor.language, filename: absolute_source_file, formatter: formatter)
end

#initialize(*args) ⇒ Object



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

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

#snippet_after(matcher) ⇒ Object

Loses proper indentation on comments



59
60
61
62
63
64
65
66
67
68
# File 'lib/polytrix/documentation/helpers/code_helper.rb', line 59

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



70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/polytrix/documentation/helpers/code_helper.rb', line 70

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



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

def source
  File.read absolute_source_file
end

#source?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/polytrix/documentation/helpers/code_helper.rb', line 39

def source?
  !absolute_source_file.nil?
end