Class: Danger::DangerProse
- Inherits:
-
Plugin
- Object
- Plugin
- Danger::DangerProse
- Defined in:
- lib/danger_plugin.rb
Overview
Lint markdown files inside your projects. This is done using the [proselint](proselint.com) python egg. Results are passed out as a table in markdown.
Instance Attribute Summary collapse
-
#disable_linters ⇒ Array<String>
Allows you to disable a collection of linters from running.
-
#ignored_words ⇒ Array<String>
Allows you to add a collection of words to skip in spellchecking.
Instance Method Summary collapse
-
#check_spelling(files = nil) ⇒ void
Runs a markdown-specific spell checker, against a corpus of ‘.markdown` and `.md` files.
-
#lint_files(files = nil) ⇒ void
Lints the globbed markdown files.
-
#mdspell_installed? ⇒ Bool
Determine if mdspell is currently installed in the system paths.
-
#proselint_installed? ⇒ Bool
Determine if proselint is currently installed in the system paths.
Instance Attribute Details
#disable_linters ⇒ Array<String>
Allows you to disable a collection of linters from running. Doesn’t work yet. You can get a list of [them here](github.com/amperser/proselint#checks) defaults to ‘[“misc.scare_quotes”, “typography.symbols”]` when it’s nil.
38 39 40 |
# File 'lib/danger_plugin.rb', line 38 def disable_linters @disable_linters end |
#ignored_words ⇒ Array<String>
Allows you to add a collection of words to skip in spellchecking. defaults to ‘[“”]` when it’s nil.
105 106 107 |
# File 'lib/danger_plugin.rb', line 105 def ignored_words @ignored_words end |
Instance Method Details
#check_spelling(files = nil) ⇒ void
This method returns an undefined value.
Runs a markdown-specific spell checker, against a corpus of ‘.markdown` and `.md` files.
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/danger_plugin.rb', line 114 def check_spelling(files = nil) # Installs my fork of the spell checker if needed # my fork has line numbers + indexes system "npm install -g orta/node-markdown-spellcheck" unless mdspell_installed? # Check that this is in the user's PATH after installing raise "mdspell is not in the user's PATH, or it failed to install" unless mdspell_installed? markdown_files = get_files files skip_words = ignored_words || [] File.write(".spelling", skip_words.join("\n")) result_texts = Hash[markdown_files.uniq.collect { |md| [md, `mdspell #{md} -r`.strip] }] spell_issues = result_texts.select { |path, output| output.include? "spelling errors found" } File.unlink(".spelling") # Get some metadata about the local setup current_slug = env.ci_source.repo_slug if spell_issues.count > 0 = "### Spell Checker found issues\n\n" spell_issues.each do |path, output| github_loc = "/#{current_slug}/tree/#{github.branch_for_head}/#{path}" << "#### [#{path}](#{github_loc})\n\n" << "Line | Typo |\n" << "| --- | ------ |\n" output.lines[1..-3].each do |line| index_info = line.strip.split("|").first index_line, index = index_info.split(":").map { |n| n.to_i } file = File.read(path) unknown_word = file[index..-1].split(" ").first error_text = line.strip.split("|")[1..-1].join("|").strip error = error_text.gsub(unknown_word, "**" + unknown_word + "**") << "#{index_line} | #{error} | \n" end markdown end end end |
#lint_files(files = nil) ⇒ void
This method returns an undefined value.
Lints the globbed markdown files. Will fail if ‘proselint` cannot be installed correctly. Generates a `markdown` list of warnings for the prose in a corpus of .markdown and .md files.
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 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/danger_plugin.rb', line 48 def lint_files(files = nil) # Installs a prose checker if needed system 'pip install --user proselint' unless proselint_installed? # Check that this is in the user's PATH after installing raise "proselint is not in the user's PATH, or it failed to install" unless proselint_installed? # Either use files provided, or use the modified + added markdown_files = get_files files proses = {} to_disable = disable_linters || ["misc.scare_quotes", "typography.symbols"] with_proselint_disabled(to_disable) do # Convert paths to proselint results result_jsons = Hash[markdown_files.uniq.collect { |v| [v, get_proselint_json(v)] }] proses = result_jsons.select { |_, prose| prose['data']['errors'].count } end # Get some metadata about the local setup current_slug = env.ci_source.repo_slug # We got some error reports back from proselint if proses.count > 0 = "### Proselint found issues\n\n" proses.each do |path, prose| github_loc = "/#{current_slug}/tree/#{github.branch_for_head}/#{path}" << "#### [#{path}](#{github_loc})\n\n" << "Line | Message | Severity |\n" << "| --- | ----- | ----- |\n" prose['data']['errors'].each do |error| << "#{error['line']} | #{error['message']} | #{error['severity']}\n" end end markdown end end |
#mdspell_installed? ⇒ Bool
Determine if mdspell is currently installed in the system paths.
98 99 100 |
# File 'lib/danger_plugin.rb', line 98 def mdspell_installed? `which mdspell`.strip.empty? == false end |
#proselint_installed? ⇒ Bool
Determine if proselint is currently installed in the system paths.
91 92 93 |
# File 'lib/danger_plugin.rb', line 91 def proselint_installed? `which proselint`.strip.empty? == false end |