Class: MarkdownLint::Rule

Inherits:
Object
  • Object
show all
Defined in:
lib/mdl/ruleset.rb

Overview

defines a single rule

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, description, fallback_docs: nil, &block) ⇒ Rule

Returns a new instance of Rule.



6
7
8
9
10
11
12
13
14
15
# File 'lib/mdl/ruleset.rb', line 6

def initialize(id, description, fallback_docs: nil, &block)
  @id = id
  @description = description
  @generate_docs = fallback_docs
  @docs_overridden = false
  @aliases = []
  @tags = []
  @params = {}
  instance_eval(&block)
end

Instance Attribute Details

#descriptionObject

Returns the value of attribute description.



4
5
6
# File 'lib/mdl/ruleset.rb', line 4

def description
  @description
end

#idObject

Returns the value of attribute id.



4
5
6
# File 'lib/mdl/ruleset.rb', line 4

def id
  @id
end

Instance Method Details

#aliases(*aliases) ⇒ Object



27
28
29
30
# File 'lib/mdl/ruleset.rb', line 27

def aliases(*aliases)
  @aliases.concat(aliases)
  @aliases
end

#check(&block) ⇒ Object



17
18
19
20
# File 'lib/mdl/ruleset.rb', line 17

def check(&block)
  @check = block unless block.nil?
  @check
end

#docs(url = nil, &block) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/mdl/ruleset.rb', line 37

def docs(url = nil, &block)
  if block_given? != url.nil?
    raise ArgumentError, 'Give either a URL or a block, not both'
  end

  raise 'A docs url is already set within this rule' if @docs_overridden

  @generate_docs = block_given? ? block : lambda { |_, _| url }
  @docs_overridden = true
end

#docs_urlObject



48
49
50
# File 'lib/mdl/ruleset.rb', line 48

def docs_url
  @generate_docs&.call(id, description)
end

#get_table_rows(lines, pos) ⇒ Array<String>

This method returns all the rows of a table

Parameters:

  • lines (Array<String>)

    Lines of a doc as an array

  • pos (Numeric)

    Position/index of the table in the array

Returns:

  • (Array<String>)

    Rows of the table in an array



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/mdl/ruleset.rb', line 89

def get_table_rows(lines, pos)
  table_rows = []
  while pos < lines.length
    line = lines[pos]

    # If the previous line is a table and the current line
    #   1) includes pipe character
    #   2) does not start with code block identifiers
    #     a) >= 4 spaces
    #     b) < 4 spaces and ``` right after
    #
    # it is possibly a table row
    unless line.include?('|') && !line.start_with?('    ') &&
           !line.strip.start_with?('```')
      break
    end

    table_rows << line
    pos += 1
  end

  table_rows
end

#number_of_columns_in_a_table_row(table_row) ⇒ Numeric

This method calculates the number of columns in a table row

Parameters:

  • table_row (String)

    A row of the table in question.

Returns:

  • (Numeric)

    Number of columns in the row



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/mdl/ruleset.rb', line 56

def number_of_columns_in_a_table_row(table_row)
  columns = table_row.strip.split('|')

  if columns.empty?
    # The stripped line consists of zero or more pipe characters
    # and nothing more.
    #
    # Examples of stripped rows:
    #   '||' --> one column
    #   '|||' --> two columns
    #   '|' --> zero columns
    [0, table_row.count('|') - 1].max
  else
    # Number of columns is the number of splited
    # segments with pipe separator. The first segment
    # is ignored when it's empty string because
    # someting like '|1|2|' is split into ['', '1', '2']
    # when using split('|') function.
    #
    # Some examples:
    #   '|foo|bar|' --> two columns
    #   '  |foo|bar|' --> two columns
    #   '|foo|bar' --> two columns
    #   'foo|bar' --> two columns
    columns.size - (columns[0].empty? ? 1 : 0)
  end
end

#params(params = nil) ⇒ Object



32
33
34
35
# File 'lib/mdl/ruleset.rb', line 32

def params(params = nil)
  @params.update(params) unless params.nil?
  @params
end

#tags(*tags) ⇒ Object



22
23
24
25
# File 'lib/mdl/ruleset.rb', line 22

def tags(*tags)
  @tags = tags.flatten.map(&:to_sym) unless tags.empty?
  @tags
end