Class: MarkdownLint::Style

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

Overview

defines a style

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(all_rules) ⇒ Style

Returns a new instance of Style.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/mdl/style.rb', line 8

def initialize(all_rules)
  @tagged_rules = {}
  @aliases = {}
  all_rules.each do |id, r|
    r.tags.each do |t|
      @tagged_rules[t] ||= Set.new
      @tagged_rules[t] << id
    end
    r.aliases.each do |a|
      @aliases[a] = id
    end
  end
  @all_rules = all_rules
  @rules = Set.new
end

Instance Attribute Details

#rulesObject (readonly)

Returns the value of attribute rules.



6
7
8
# File 'lib/mdl/style.rb', line 6

def rules
  @rules
end

Class Method Details

.load(style_file, rules) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/mdl/style.rb', line 54

def self.load(style_file, rules)
  unless style_file.include?('/') || style_file.end_with?('.rb')
    tmp = File.expand_path("../styles/#{style_file}.rb", __FILE__)
    unless File.exist?(tmp)
      warn "#{style_file} does not appear to be a built-in style." +
           ' If you meant to pass in your own style file, it must contain' +
           " a '/' or end in '.rb'. See https://github.com/markdownlint/" +
           'markdownlint/blob/master/docs/configuration.md'
      exit(1)
    end
    style_file = tmp
  end

  unless File.exist?(style_file)
    warn "Style '#{style_file}' does not exist."
    exit(1)
  end

  style = new(rules)
  style.instance_eval(File.read(style_file), style_file)
  rules.select! { |r| style.rules.include?(r) }
  style
end

Instance Method Details

#allObject



24
25
26
# File 'lib/mdl/style.rb', line 24

def all
  @rules.merge(@all_rules.keys)
end

#exclude_rule(id) ⇒ Object



41
42
43
44
# File 'lib/mdl/style.rb', line 41

def exclude_rule(id)
  id = @aliases[id] if @aliases[id]
  @rules.delete(id)
end

#exclude_tag(tag) ⇒ Object



50
51
52
# File 'lib/mdl/style.rb', line 50

def exclude_tag(tag)
  @rules.subtract(@tagged_rules[tag])
end

#rule(id, params = {}) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/mdl/style.rb', line 28

def rule(id, params = {})
  if block_given?
    raise '"rule" does not take a block. Should this definition go in a ' +
          'ruleset instead?'
  end

  id = @aliases[id] if @aliases[id]
  raise "No such rule: #{id}" unless @all_rules[id]

  @rules << id
  @all_rules[id].params(params)
end

#tag(tag) ⇒ Object



46
47
48
# File 'lib/mdl/style.rb', line 46

def tag(tag)
  @rules.merge(@tagged_rules[tag])
end