Class: ERB::Processor::LanguageCommenter

Inherits:
Object
  • Object
show all
Defined in:
lib/erb/processor/language_commenter.rb

Overview

Convert text to a comment depending on the target language

Constant Summary collapse

SPACE =
" "
NEWLINE =
"\n"
NO_COMMENT_MARKER =
[nil, nil].freeze
CSTYLE_COMMENT =
["/*", "*/"].freeze
LANGUAGE_COMMENTS =
{
  c: CSTYLE_COMMENT,
  cpp: CSTYLE_COMMENT,
  java: CSTYLE_COMMENT,
  js: CSTYLE_COMMENT,
  py: ["#", nil],
  rb: ["=begin", "=end"],
  html: ["<!--", "-->"],
  feature: ["#", nil]
}.freeze
FILE_EXTENSION_REGEX =
/\.([^.]+)$/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target_file_path, text_to_comment) ⇒ LanguageCommenter

Returns a new instance of LanguageCommenter.



14
15
16
17
# File 'lib/erb/processor/language_commenter.rb', line 14

def initialize(target_file_path, text_to_comment)
  @target_file_path = target_file_path
  @text_to_comment = text_to_comment
end

Instance Attribute Details

#target_file_pathObject (readonly)

Returns the value of attribute target_file_path.



12
13
14
# File 'lib/erb/processor/language_commenter.rb', line 12

def target_file_path
  @target_file_path
end

#text_to_commentObject (readonly)

Returns the value of attribute text_to_comment.



12
13
14
# File 'lib/erb/processor/language_commenter.rb', line 12

def text_to_comment
  @text_to_comment
end

Class Method Details

.comment_for(target_file_path, text_to_comment) ⇒ Object



10
# File 'lib/erb/processor/language_commenter.rb', line 10

def self.comment_for(target_file_path, text_to_comment); end

Instance Method Details

#commented_textObject



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/erb/processor/language_commenter.rb', line 35

def commented_text
  comment_start, comment_end = *LANGUAGE_COMMENTS.fetch(processed_language) { NO_COMMENT_MARKER }

  return text_to_comment unless comment_start

  if comment_end
    [comment_start, NEWLINE, text_to_comment, comment_end, NEWLINE].join
  else
    text_to_comment.lines.collect { |line| [comment_start, line].join(SPACE) }.join
  end
end

#processed_languageObject



48
49
50
51
52
53
54
# File 'lib/erb/processor/language_commenter.rb', line 48

def processed_language
  if target_file_path =~ FILE_EXTENSION_REGEX
    Regexp.last_match(1).intern
  else
    :unknown
  end
end