Class: Qiita::Markdown::Filters::Code::Label

Inherits:
Object
  • Object
show all
Defined in:
lib/qiita/markdown/filters/code.rb

Overview

Detects language from code block label.

Instance Method Summary collapse

Constructor Details

#initialize(text) ⇒ Label

Returns a new instance of Label.

Parameters:

  • text (String, nil)


48
49
50
# File 'lib/qiita/markdown/filters/code.rb', line 48

def initialize(text)
  @text = text
end

Instance Method Details

#filenameString?

Returns:

  • (String, nil)


53
54
55
56
57
58
59
60
61
62
# File 'lib/qiita/markdown/filters/code.rb', line 53

def filename
  case
  when empty?
    nil
  when has_only_filename?
    sections[0]
  else
    sections[1]
  end
end

#languageString?

Examples:

Label.new(nil).language #=> nil
Label.new("ruby").language #=> "ruby"
Label.new("ruby:foo.rb").language #=> "ruby"
Label.new("foo.rb").language #=> "ruby"

Returns:

  • (String, nil)


70
71
72
73
74
75
76
77
78
79
# File 'lib/qiita/markdown/filters/code.rb', line 70

def language
  case
  when empty?
    nil
  when !has_only_filename?
    sections[0]
  when linguist_language
    linguist_language.default_alias_name
  end
end