Class: MarkdownLint::Rule
- Inherits:
-
Object
- Object
- MarkdownLint::Rule
- Defined in:
- lib/mdl/ruleset.rb
Overview
defines a single rule
Instance Attribute Summary collapse
-
#description ⇒ Object
Returns the value of attribute description.
-
#id ⇒ Object
Returns the value of attribute id.
Instance Method Summary collapse
- #aliases(*aliases) ⇒ Object
- #check(&block) ⇒ Object
- #docs(url = nil, &block) ⇒ Object
- #docs_url ⇒ Object
-
#get_table_rows(lines, pos) ⇒ Array<String>
This method returns all the rows of a table.
-
#initialize(id, description, fallback_docs: nil, &block) ⇒ Rule
constructor
A new instance of Rule.
-
#number_of_columns_in_a_table_row(table_row) ⇒ Numeric
This method calculates the number of columns in a table row.
- #params(params = nil) ⇒ Object
- #tags(*tags) ⇒ Object
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
#description ⇒ Object
Returns the value of attribute description.
4 5 6 |
# File 'lib/mdl/ruleset.rb', line 4 def description @description end |
#id ⇒ Object
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_url ⇒ Object
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
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
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 = .flatten.map(&:to_sym) unless .empty? @tags end |