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)


44
45
46
# File 'lib/qiita/markdown/filters/code.rb', line 44

def initialize(text)
  @text = text
end

Instance Method Details

#filenameString?

Returns:

  • (String, nil)


49
50
51
52
53
54
55
56
57
58
# File 'lib/qiita/markdown/filters/code.rb', line 49

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)


66
67
68
69
70
71
72
73
74
75
# File 'lib/qiita/markdown/filters/code.rb', line 66

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