Class: MarkdownProofer

Inherits:
Object
  • Object
show all
Defined in:
lib/markdown_proofer.rb,
lib/markdown_proofer/version.rb,
lib/markdown_proofer/rake_task.rb

Defined Under Namespace

Modules: RakeTask

Constant Summary collapse

VERSION =
'0.1.1'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path: '.', excludes: [], html_proofer: {}) ⇒ MarkdownProofer

Returns a new instance of MarkdownProofer.



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/markdown_proofer.rb', line 15

def initialize(path: '.', excludes: [], html_proofer: {})
  @path = path
  @excludes = excludes
  @html_proofer = html_proofer

  self.reset_errors
  @pipeline = HTML::Pipeline.new [
    HTML::Pipeline::MarkdownFilter,
    HTML::Pipeline::TableOfContentsFilter
  ], gfm: true
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



13
14
15
# File 'lib/markdown_proofer.rb', line 13

def errors
  @errors
end

#excludesObject (readonly)

Returns the value of attribute excludes.



13
14
15
# File 'lib/markdown_proofer.rb', line 13

def excludes
  @excludes
end

#html_prooferObject (readonly)

Returns the value of attribute html_proofer.



13
14
15
# File 'lib/markdown_proofer.rb', line 13

def html_proofer
  @html_proofer
end

#pathObject (readonly)

Returns the value of attribute path.



13
14
15
# File 'lib/markdown_proofer.rb', line 13

def path
  @path
end

#pipelineObject (readonly)

Returns the value of attribute pipeline.



13
14
15
# File 'lib/markdown_proofer.rb', line 13

def pipeline
  @pipeline
end

Instance Method Details

#filesObject



27
28
29
30
31
32
33
34
# File 'lib/markdown_proofer.rb', line 27

def files
  if File.file?(self.path)
    [self.path]
  else # directory
    pattern = File.join(self.path, '**', '*.md')
    Dir.glob(pattern)
  end
end

#included_filesObject



36
37
38
39
40
41
42
# File 'lib/markdown_proofer.rb', line 36

def included_files
  self.files.reject do |file|
    self.excludes.any? do |exclude|
      file =~ exclude
    end
  end
end

#runObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/markdown_proofer.rb', line 44

def run
  self.reset_errors

  # iterate over files, and generate HTML from Markdown
  self.included_files.each do |file|
    # convert the Markdown to HTML
    contents = File.read(file)
    result = self.pipeline.call(contents)

    # save the HTML file next to the Markdown one
    output_file = file.sub(/\.md$/, '.html')
    begin
      File.open(output_file, 'w') do |file|
        file.write(result[:output].to_s)
      end

      # do validation on the file
      html_proofer = HTML::Proofer.new(output_file, self.html_proofer)
      self.capture_stderr { html_proofer.run }

      # TODO add getter in HTML::Proofer
      errors = html_proofer.instance_variable_get(:@failed_tests)
      self.errors.concat(errors)
    ensure
      # clean up the file
      FileUtils.rm(output_file)
    end
  end

  self.errors.empty?
end