Module: Crosstest::Psychic::CodeHelper

Included in:
Script
Defined in:
lib/crosstest/psychic/code_helper.rb

Defined Under Namespace

Classes: Highlighter, MarkdownHelper, ReStructuredTextHelper

Instance Method Summary collapse

Instance Method Details

#absolute_source_fileObject


46
47
48
49
50
51
52
53
54
# File 'lib/crosstest/psychic/code_helper.rb', line 46

def absolute_source_file
  return nil if source_file.nil?

  if basedir
    File.expand_path source_file, basedir
  else
    source_file
  end
end

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


77
78
79
80
81
82
83
84
85
86
# File 'lib/crosstest/psychic/code_helper.rb', line 77

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

#detect_languageObject


68
69
70
# File 'lib/crosstest/psychic/code_helper.rb', line 68

def detect_language
  File.extname(source_file)
end

#highlighted_code(formatter = 'terminal256') ⇒ Object


72
73
74
75
# File 'lib/crosstest/psychic/code_helper.rb', line 72

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

#languageObject


64
65
66
# File 'lib/crosstest/psychic/code_helper.rb', line 64

def language
  @language ||= detect_language
end

#sourceObject


56
57
58
# File 'lib/crosstest/psychic/code_helper.rb', line 56

def source
  File.read absolute_source_file
end

#source?Boolean

Returns:

  • (Boolean)

60
61
62
# File 'lib/crosstest/psychic/code_helper.rb', line 60

def source?
  !absolute_source_file.nil?
end