Skip to content

Commit b266086

Browse files
authored
Add pymarkdown for Markdown linting (#4906)
Add support for pymarkdown Closes #4785
1 parent f0c8eb4 commit b266086

File tree

7 files changed

+236
-1
lines changed

7 files changed

+236
-1
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
2+
call ale#Set('markdown_pymarkdown_executable', 'pymarkdown')
3+
call ale#Set('markdown_pymarkdown_options', '')
4+
call ale#Set('markdown_pymarkdown_use_global', get(g:, 'ale_use_global_executables', 0))
5+
call ale#Set('markdown_pymarkdown_auto_pipenv', 0)
6+
call ale#Set('markdown_pymarkdown_auto_poetry', 0)
7+
call ale#Set('markdown_pymarkdown_auto_uv', 0)
8+
9+
function! ale_linters#markdown#pymarkdown#GetExecutable(buffer) abort
10+
if (ale#Var(a:buffer, 'python_auto_pipenv') || ale#Var(a:buffer, 'markdown_pymarkdown_auto_pipenv'))
11+
\ && ale#python#PipenvPresent(a:buffer)
12+
return 'pipenv'
13+
endif
14+
15+
if (ale#Var(a:buffer, 'python_auto_poetry') || ale#Var(a:buffer, 'markdown_pymarkdown_auto_poetry'))
16+
\ && ale#python#PoetryPresent(a:buffer)
17+
return 'poetry'
18+
endif
19+
20+
if (ale#Var(a:buffer, 'python_auto_uv') || ale#Var(a:buffer, 'markdown_pymarkdown_auto_uv'))
21+
\ && ale#python#UvPresent(a:buffer)
22+
return 'uv'
23+
endif
24+
25+
return ale#python#FindExecutable(a:buffer, 'markdown_pymarkdown', ['pymarkdown'])
26+
endfunction
27+
28+
function! ale_linters#markdown#pymarkdown#GetCommand(buffer) abort
29+
let l:executable = ale_linters#markdown#pymarkdown#GetExecutable(a:buffer)
30+
31+
let l:exec_args = l:executable =~? 'pipenv\|poetry\|uv$'
32+
\ ? ' run pymarkdown'
33+
\ : ''
34+
35+
return ale#Escape(l:executable) . l:exec_args
36+
\ . ' '
37+
\ . ale#Var(a:buffer, 'markdown_pymarkdown_options')
38+
\ . 'scan-stdin'
39+
endfunction
40+
41+
function! ale_linters#markdown#pymarkdown#Handle(buffer, lines) abort
42+
let l:pattern = '\v^(\S*):(\d+):(\d+): ([A-Z]+\d+): (.*)$'
43+
let l:output = []
44+
" lines are formatted as follows:
45+
" sample.md:1:1: MD022: Headings should be surrounded by blank lines. [Expected: 1; Actual: 0; Below] (blanks-around-headings,blanks-around-headers)
46+
47+
for l:match in ale#util#GetMatches(a:lines, l:pattern)
48+
if(l:match[4] is# 'MD009')
49+
\&& !ale#Var(a:buffer, 'warn_about_trailing_whitespace')
50+
" Skip warnings for trailing whitespace if the option is off.
51+
continue
52+
endif
53+
54+
let l:item = {
55+
\ 'lnum': l:match[2] + 0,
56+
\ 'col': l:match[3] + 0,
57+
\ 'type': l:match[4][0],
58+
\ 'text': l:match[5],
59+
\ 'code': l:match[4],
60+
\}
61+
62+
call add(l:output, l:item)
63+
endfor
64+
65+
return l:output
66+
endfunction
67+
68+
call ale#linter#Define('markdown', {
69+
\ 'name': 'pymarkdown',
70+
\ 'executable': function('ale_linters#markdown#pymarkdown#GetExecutable'),
71+
\ 'command': function('ale_linters#markdown#pymarkdown#GetCommand'),
72+
\ 'callback': 'ale_linters#markdown#pymarkdown#Handle',
73+
\})

doc/ale-markdown.txt

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,70 @@ g:ale_markdown_pandoc_options *g:ale_markdown_pandoc_options*
8383

8484
This variable can be set to change the default options passed to pandoc
8585

86-
8786
===============================================================================
8887
prettier *ale-markdown-prettier*
8988

9089
See |ale-javascript-prettier| for information about the available options.
9190

9291

92+
===============================================================================
93+
pymarkdown *ale-markdown-pymarkdown*
94+
95+
g:ale_markdown_pymarkdown_executable *g:ale_markdown_pymarkdown_executable*
96+
*b:ale_markdown_pymarkdown_executable*
97+
Type: |String|
98+
Default: `'pymarkdown'`
99+
100+
See |ale-integrations-local-executables|
101+
102+
Set this to `'pipenv'` to invoke `'pipenv` `run` `pymarkdown'`.
103+
Set this to `'poetry'` to invoke `'poetry` `run` `pymarkdown'`.
104+
105+
106+
g:ale_markdown_pymarkdown_options *g:ale_markdown_pymarkdown_options*
107+
*b:ale_markdown_pymarkdown_options*
108+
Type: |String|
109+
Default: `''`
110+
111+
This variable can be changed to add command-line arguments to the
112+
pymarkdown invocation.
113+
114+
115+
g:ale_markdown_pymarkdown_use_global *g:ale_markdown_pymarkdown_use_global*
116+
*b:ale_markdown_pymarkdown_use_global*
117+
Type: |Number|
118+
Default: `get(g:, 'ale_use_global_executables', 0)`
119+
120+
See |ale-integrations-local-executables|
121+
122+
123+
g:ale_markdown_pymarkdown_auto_pipenv *g:ale_markdown_pymarkdown_auto_pipenv*
124+
*b:ale_markdown_pymarkdown_auto_pipenv*
125+
Type: |Number|
126+
Default: `0`
127+
128+
Detect whether the file is inside a pipenv, and set the executable to `pipenv`
129+
if true. This is overridden by a manually-set executable.
130+
131+
132+
g:ale_markdown_pymarkdown_auto_poetry *g:ale_markdown_pymarkdown_auto_poetry*
133+
*b:ale_markdown_pymarkdown_auto_poetry*
134+
Type: |Number|
135+
Default: `0`
136+
137+
Detect whether the file is inside a poetry, and set the executable to `poetry`
138+
if true. This is overridden by a manually-set executable.
139+
140+
141+
g:ale_markdown_pymarkdown_auto_uv *g:ale_markdown_pymarkdown_auto_uv*
142+
*b:ale_markdown_pymarkdown_auto_uv*
143+
Type: |Number|
144+
Default: `0`
145+
146+
Set the executable to `uv` if true. This is overridden by a manually-set
147+
executable.
148+
149+
93150
===============================================================================
94151
remark-lint *ale-markdown-remark-lint*
95152

doc/ale-supported-languages-and-tools.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,7 @@ Notes:
391391
* `pandoc`
392392
* `prettier`
393393
* `proselint`
394+
* `pymarkdown`
394395
* `redpen`
395396
* `remark-lint`
396397
* `textlint`

doc/ale.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3209,6 +3209,7 @@ documented in additional help files.
32093209
mdl...................................|ale-markdown-mdl|
32103210
pandoc................................|ale-markdown-pandoc|
32113211
prettier..............................|ale-markdown-prettier|
3212+
pymarkdown............................|ale-markdown-pymarkdown|
32123213
remark-lint...........................|ale-markdown-remark-lint|
32133214
textlint..............................|ale-markdown-textlint|
32143215
write-good............................|ale-markdown-write-good|

supported-tools.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,7 @@ formatting.
400400
* [pandoc](https://pandoc.org)
401401
* [prettier](https://github.com/prettier/prettier)
402402
* [proselint](http://proselint.com/)
403+
* [pymarkdown](https://github.com/jackdewinter/pymarkdown)
403404
* [redpen](http://redpen.cc/)
404405
* [remark-lint](https://github.com/wooorm/remark-lint)
405406
* [textlint](https://textlint.github.io/)

test/linter/test_pymarkdown.vader

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
Before:
2+
call ale#assert#SetUpLinterTest('markdown', 'pymarkdown')
3+
4+
After:
5+
call ale#assert#TearDownLinterTest()
6+
7+
Execute(The pymarkdown command callback should return default string):
8+
AssertLinter 'pymarkdown', ale#Escape('pymarkdown') . ' scan-stdin'
9+
10+
Execute(The pycodestyle command callback should allow options):
11+
let g:markdown_pymarkdown_options = '--exclude=test*.py'
12+
13+
Execute(The pymarkdown executable should be configurable):
14+
let g:ale_markdown_pymarkdown_executable = '~/.local/bin/pymarkdown'
15+
16+
AssertLinter '~/.local/bin/pymarkdown',
17+
\ ale#Escape('~/.local/bin/pymarkdown') . ' scan-stdin'
18+
19+
Execute(Setting executable to 'pipenv' appends 'run pymarkdown'):
20+
let g:ale_markdown_pymarkdown_executable = 'path/to/pipenv'
21+
22+
AssertLinter 'path/to/pipenv',
23+
\ ale#Escape('path/to/pipenv') . ' run pymarkdown scan-stdin'
24+
25+
Execute(Pipenv is detected when markdown_pymarkdown_auto_pipenv is set):
26+
let g:ale_markdown_pymarkdown_auto_pipenv = 1
27+
call ale#test#SetFilename('../test-files/python/pipenv/whatever.py')
28+
29+
AssertLinter 'pipenv',
30+
\ ale#Escape('pipenv') . ' run pymarkdown scan-stdin'
31+
32+
Execute(Setting executable to 'poetry' appends 'run pymarkdown'):
33+
let g:ale_markdown_pymarkdown_executable = 'path/to/poetry'
34+
35+
AssertLinter 'path/to/poetry',
36+
\ ale#Escape('path/to/poetry') . ' run pymarkdown scan-stdin'
37+
38+
Execute(Poetry is detected when markdown_pymarkdown_auto_poetry is set):
39+
let g:ale_markdown_pymarkdown_auto_poetry = 1
40+
call ale#test#SetFilename('../test-files/python/poetry/whatever.py')
41+
42+
AssertLinter 'poetry',
43+
\ ale#Escape('poetry') . ' run pymarkdown scan-stdin'
44+
45+
Execute(uv is detected when markdown_pymarkdown_auto_uv is set):
46+
let g:ale_markdown_pymarkdown_auto_uv = 1
47+
call ale#test#SetFilename('../test-files/python/uv/whatever.py')
48+
49+
AssertLinter 'uv',
50+
\ ale#Escape('uv') . ' run pymarkdown scan-stdin'
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
Before:
2+
Save g:ale_warn_about_trailing_whitespace
3+
4+
let g:ale_warn_about_trailing_whitespace = 1
5+
6+
runtime ale_linters/markdown/pymarkdown.vim
7+
8+
After:
9+
Restore
10+
unlet! b:ale_warn_about_trailing_whitespace
11+
12+
call ale#linter#Reset()
13+
14+
Execute (Should parse error correctly):
15+
AssertEqual
16+
\ [
17+
\ {
18+
\ 'lnum': 1,
19+
\ 'col': 1,
20+
\ 'type': 'M',
21+
\ 'text': 'Headings should be surrounded by blank lines',
22+
\ 'code': 'MD022',
23+
\ }
24+
\ ],
25+
\ ale_linters#markdown#pymarkdown#Handle(bufnr(''), [
26+
\ 'foo.md:1:1: MD022: Headings should be surrounded by blank lines',
27+
\ ])
28+
29+
Execute(Warnings about trailing whitespace should be reported by default):
30+
AssertEqual
31+
\ [
32+
\ {
33+
\ 'lnum': 1,
34+
\ 'col': 1,
35+
\ 'type': 'M',
36+
\ 'text': 'who cares',
37+
\ 'code': 'MD009',
38+
\ },
39+
\ ],
40+
\ ale_linters#markdown#pymarkdown#Handle(bufnr(''), [
41+
\ 'foo.md:1:1: MD009: who cares',
42+
\ ])
43+
44+
Execute(Disabling trailing whitespace warnings should work):
45+
let b:ale_warn_about_trailing_whitespace = 0
46+
47+
AssertEqual
48+
\ [
49+
\ ],
50+
\ ale_linters#markdown#pymarkdown#Handle(bufnr(''), [
51+
\ 'foo.md:1:1: MD009: who cares',
52+
\ ])

0 commit comments

Comments
 (0)