-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathprocess.py
More file actions
643 lines (550 loc) · 19.9 KB
/
process.py
File metadata and controls
643 lines (550 loc) · 19.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
"""Utilities for string processing."""
from __future__ import annotations
import functools
import io
import os
import re
import stat
from collections.abc import Callable, Iterator
from typing import TYPE_CHECKING
if TYPE_CHECKING: # pragma: no cover
from typing import Any
from mkdocs_include_markdown_plugin.cache import Cache
# Markdown regular expressions. Taken from the original Markdown.pl by John
# Gruber, and modified to work in Python
# Matches markdown links.
# e.g. [scikit-learn](https://github.com/scikit-learn/scikit-learn)
#
# The next Regex can raise a catastrophic backtracking, but with the current
# implementation of the plugin it is not very much likely to reach the case.
# Can be checked with dlint:
# python3 -m dlint.redos --pattern '\[(?:(?:\[[^\[\]]+\])*)?\]'
#
# In the original Markdown.pl, the nested brackets are enclosed by an atomic
# group (?>...), but atomic groups are not supported by Python in versions
# previous to Python3.11. Also, these nested brackets can be recursive in the
# Perl implementation but this doesn't seem possible in Python, the current
# implementation only reaches two levels.
MARKDOWN_LINK_REGEX = re.compile( # noqa: DUO138
r'''
( # wrap whole match in $1
(?<!!) # don't match images - negative lookbehind
\[
( # link text = $2
(?:
[^\[\]]+ # not bracket
(?:
\[[^\[\]]+\] # another level of nested bracket
# with something inside
[^\[\]]* # not bracket
)*
)? # allow for empty link text
)
\]
\( # literal paren
\s*
<?(.*?)>? # href = $3
\s*
( # $4
(['"]) # quote char = $5
(.*?) # Title = $6
\5 # matching quote
)? # title is optional
\)
)
''',
flags=re.VERBOSE,
)
# Matches markdown inline images.
# e.g. 
MARKDOWN_IMAGE_REGEX = re.compile(
r'''
( # wrap whole match in $1
!\[
(.*?) # alt text = $2
\]
\( # literal paren
[ \t]*
<?(\S+?)>? # src url = $3
[ \t]*
( # $4
(['"]) # quote char = $5
(.*?) # title = $6
\5 # matching quote
[ \t]*
)? # title is optional
\)
)
''',
flags=re.VERBOSE,
)
# Matches markdown link definitions.
# e.g. [scikit-learn]: https://github.com/scikit-learn/scikit-learn
MARKDOWN_LINK_DEFINITION_REGEX = re.compile(
r'''
^[ ]{0,4}\[(.+)\]: # id = $1
[ \t]*
\n? # maybe *one* newline
[ \t]*
<?(\S+?)>? # url = $2
[ \t]*
\n? # maybe one newline
[ \t]*
(?:
(?<=\s) # lookbehind for whitespace
["(]
(.+?) # title = $3
[")]
[ \t]*
)? # title is optional
(?:\n+|\Z)
''',
flags=re.VERBOSE | re.MULTILINE,
)
# Matched html image and source definition.
# e.g. <img src="path/to/image.png" alt="alt-text">
# e.g. <source src="path/to/image.png" alt="alt-text">
MARKDOWN_HTML_IMAGE_REGEX = re.compile(
r'''
<(?:img|source) # img or source
(?:\s+ # More than one whitespace
(?!src=) # Not src=
[\w-]+ # attribute name
(?:\s*=\s*)? # arbitrary whitespace (optional)
(?:
"[^"]*" # Quoted value (double quote)
|
'[^']*' # Quoted value (single quote)
)?
)* # Other attributes are repeated 0 or more times
\s+ # More than one whitespace
src=["'](\S+?)["'] # src = $1 (double quote or single quote)
''',
flags=re.VERBOSE | re.MULTILINE,
)
# Matched html anchor definition.
# e.g. <a href="https://example.com">example</a>
MARKDOWN_HTML_ANCHOR_DEFINITION_REGEX = re.compile(
r'''
<a
(?:\s+ # More than one whitespace
(?!href=) # Not href=
[\w-]+ # attribute name
(?:\s*=\s*)? # arbitrary whitespace (optional)
(?:
"[^"]*" # Quoted value (double quote)
|
'[^']*' # Quoted value (single quote)
)?
)* # Other attributes are repeated 0 or more times
\s+ # More than one whitespace
href=["'](\S+?)["']# href = $1 (double quote or single quote)
''',
flags=re.VERBOSE | re.MULTILINE,
)
def transform_p_by_p_skipping_codeblocks( # noqa: PLR0912, PLR0915
markdown: str,
func: Callable[[str], str],
) -> str:
"""Apply a transformation paragraph by paragraph in a Markdown text.
Apply a transformation paragraph by paragraph in a Markdown using a
function. Skip indented and fenced codeblock lines, where the
transformation is never applied.
"""
# current fenced codeblock delimiter
_current_fcodeblock_delimiter = ''
# inside indented codeblock
_maybe_icodeblock_lines: list[str] = []
_previous_line_was_empty = False
lines, current_paragraph = ([], '')
def process_current_paragraph() -> None:
lines.extend(func(current_paragraph).splitlines(keepends=True))
# The next implementation takes into account that indented code
# blocks must be surrounded by newlines as per the CommonMark
# specification. See https://spec.commonmark.org/0.28/#indented-code-blocks
#
# However, note that ambiguities with list items are not handled.
for line in io.StringIO(markdown):
if not _current_fcodeblock_delimiter:
lstripped_line = line.lstrip()
if lstripped_line.startswith(('```', '~~~')):
_current_fcodeblock_delimiter = lstripped_line[:3]
process_current_paragraph()
current_paragraph = ''
lines.append(line)
elif line.startswith(' '):
if not lstripped_line or _maybe_icodeblock_lines:
# maybe enter indented codeblock
_maybe_icodeblock_lines.append(line)
else:
current_paragraph += line
elif _maybe_icodeblock_lines:
process_current_paragraph()
current_paragraph = ''
if not _previous_line_was_empty:
# wasn't an indented code block
for line_ in _maybe_icodeblock_lines:
current_paragraph += line_
_maybe_icodeblock_lines = []
current_paragraph += line
process_current_paragraph()
current_paragraph = ''
else:
# exit indented codeblock
for line_ in _maybe_icodeblock_lines:
lines.append(line_)
_maybe_icodeblock_lines = []
lines.append(line)
else:
current_paragraph += line
_previous_line_was_empty = not lstripped_line
else:
lines.append(line)
lstripped_line = line.lstrip()
if lstripped_line.startswith(_current_fcodeblock_delimiter):
_current_fcodeblock_delimiter = ''
_previous_line_was_empty = not lstripped_line
if _maybe_icodeblock_lines:
if not _previous_line_was_empty:
# at EOF
process_current_paragraph()
current_paragraph = ''
for line_ in _maybe_icodeblock_lines:
current_paragraph += line_
process_current_paragraph()
current_paragraph = ''
else:
process_current_paragraph()
current_paragraph = ''
for line_ in _maybe_icodeblock_lines:
lines.append(line_)
else:
process_current_paragraph()
return ''.join(lines)
def transform_line_by_line_skipping_codeblocks(
markdown: str,
func: Callable[[str], str],
) -> str:
"""Apply a transformation line by line in a Markdown text using a function,.
Skip fenced codeblock lines and empty lines, where the transformation
is never applied.
Indented codeblocks are not taken into account because in the practice
this function is only used for transformations of heading prefixes. See
the PR https://github.com/mondeja/mkdocs-include-markdown-plugin/pull/95
to recover the implementation handling indented codeblocks.
"""
# current fenced codeblock delimiter
_current_fcodeblock_delimiter = ''
lines = []
for line in io.StringIO(markdown):
lstripped_line = line.lstrip()
if not _current_fcodeblock_delimiter:
if lstripped_line.startswith('```'):
_current_fcodeblock_delimiter = '```'
elif lstripped_line.startswith('~~~'):
_current_fcodeblock_delimiter = '~~~'
else:
line = func(line) # noqa: PLW2901
elif lstripped_line.startswith(_current_fcodeblock_delimiter):
_current_fcodeblock_delimiter = ''
lines.append(line)
return ''.join(lines)
def rewrite_relative_urls(
markdown: str,
source_path: str,
destination_path: str,
) -> str:
"""Rewrite relative URLs in a Markdown text.
Rewrites markdown so that relative links that were written at
``source_path`` will still work when inserted into a file at
``destination_path``.
"""
def rewrite_url(url: str) -> str:
if is_url(url) or is_absolute_path(url):
return url
new_path = os.path.relpath(
os.path.join(os.path.dirname(source_path), url),
os.path.dirname(destination_path),
)
# ensure forward slashes are used, on Windows
new_path = new_path.replace('\\', '/').replace('//', '/')
try:
if url[-1] == '/':
# the above operation removes a trailing slash,
# so add it back if it was present in the input
new_path += '/'
except IndexError: # pragma: no cover
pass
return new_path
def found_href(m: re.Match[str], url_group_index: int = -1) -> str:
match_start, match_end = m.span(0)
href = m[url_group_index]
href_start, href_end = m.span(url_group_index)
rewritten_url = rewrite_url(href)
return (
m.string[match_start:href_start]
+ rewritten_url
+ m.string[href_end:match_end]
)
found_href_url_group_index_3 = functools.partial(
found_href,
url_group_index=3,
)
def transform(paragraph: str) -> str:
paragraph = MARKDOWN_LINK_REGEX.sub(
found_href_url_group_index_3,
paragraph,
)
paragraph = MARKDOWN_IMAGE_REGEX.sub(
found_href_url_group_index_3,
paragraph,
)
paragraph = MARKDOWN_LINK_DEFINITION_REGEX.sub(
functools.partial(found_href, url_group_index=2),
paragraph,
)
paragraph = MARKDOWN_HTML_IMAGE_REGEX.sub(
functools.partial(found_href, url_group_index=1),
paragraph,
)
return MARKDOWN_HTML_ANCHOR_DEFINITION_REGEX.sub(
functools.partial(found_href, url_group_index=1),
paragraph,
)
return transform_p_by_p_skipping_codeblocks(
markdown,
transform,
)
def interpret_escapes(value: str) -> str:
"""Interpret Python literal escapes in a string.
Replaces any standard escape sequences in value with their usual
meanings as in ordinary Python string literals.
"""
return value.encode('latin-1', 'backslashreplace').decode('unicode_escape')
def filter_inclusions( # noqa: PLR0912
start: str | None,
end: str | None,
text_to_include: str,
) -> tuple[str, bool, bool]:
"""Filter inclusions in a text.
Manages inclusions from files using ``start`` and ``end`` directive
arguments.
"""
expected_start_not_found, expected_end_not_found = (False, False)
new_text_to_include = ''
if start is not None and end is None:
start = interpret_escapes(start)
if start not in text_to_include:
expected_start_not_found = True
else:
new_text_to_include = text_to_include.split(
start,
maxsplit=1,
)[1]
elif start is None and end is not None:
end = interpret_escapes(end)
if end not in text_to_include:
expected_end_not_found = True
new_text_to_include = text_to_include
else:
new_text_to_include = text_to_include.split(
end,
maxsplit=1,
)[0]
elif start is not None and end is not None:
start, end = interpret_escapes(start), interpret_escapes(end)
if start not in text_to_include:
expected_start_not_found = True
if end not in text_to_include:
expected_end_not_found = True
start_split = text_to_include.split(start)
text_parts = (
start_split[1:]
if len(start_split) > 1 else [text_to_include]
)
for start_text in text_parts:
for i, end_text in enumerate(start_text.split(end)):
if not i % 2:
new_text_to_include += end_text
else: # pragma: no cover
new_text_to_include = text_to_include
return (
new_text_to_include,
expected_start_not_found,
expected_end_not_found,
)
def _transform_negative_offset_func_factory(
offset: int,
) -> Callable[[str], str]:
heading_prefix = '#' * abs(offset)
def transform(line: str) -> str:
try:
if line[0] != '#':
return line
except IndexError: # pragma: no cover
return line
if line.startswith(heading_prefix):
return heading_prefix + line.lstrip('#')
return '#' + line.lstrip('#')
return transform
def _transform_positive_offset_func_factory(
offset: int,
) -> Callable[[str], str]:
heading_prefix = '#' * offset
def transform(line: str) -> str:
try:
prefix = line[0]
except IndexError: # pragma: no cover
return line
else:
return heading_prefix + line if prefix == '#' else line
return transform
def increase_headings_offset(markdown: str, offset: int = 0) -> str:
"""Increases the headings depth of a snippet of Makdown content."""
if not offset: # pragma: no cover
return markdown
return transform_line_by_line_skipping_codeblocks(
markdown,
_transform_positive_offset_func_factory(offset) if offset > 0
else _transform_negative_offset_func_factory(offset),
)
def rstrip_trailing_newlines(content: str) -> str:
"""Removes trailing newlines from a string."""
while content.endswith(('\n', '\r')):
content = content.rstrip('\r\n')
return content
def filter_paths(
filepaths: Iterator[str] | list[str],
ignore_paths: list[str],
) -> list[str]:
"""Filters a list of paths removing those defined in other list of paths.
The paths to filter can be defined in the list of paths to ignore in
several forms:
- The same string.
- Only the file name.
- Only their direct directory name.
- Their direct directory full path.
Args:
filepaths (list): Set of source paths to filter.
ignore_paths (list): Paths that must not be included in the response.
Returns:
list: Non filtered paths ordered alphabetically.
"""
response = []
for filepath in filepaths:
# ignore by filepath
if filepath in ignore_paths:
continue
# ignore by dirpath (relative or absolute)
fp_split = filepath.split(os.sep)
fp_split.pop()
if (os.sep).join(fp_split) in ignore_paths:
continue
# ignore if is a directory
try:
if not stat.S_ISDIR(os.stat(filepath).st_mode):
response.append(filepath)
except (FileNotFoundError, OSError): # pragma: no cover
continue
response.sort()
return response
def _is_valid_url_scheme_char(c: str) -> bool:
"""Determine is a character is a valid URL scheme character.
Valid characters are:
```
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+-.
```
"""
codepoint = ord(c)
A = 65
Z = 90
a = 97
z = 122
zero = 48
nine = 57
dot = 46
plus = 43
minus = 45
return (
A <= codepoint <= Z
or a <= codepoint <= z
or zero <= codepoint <= nine
or codepoint in (plus, minus, dot)
)
def is_url(string: str) -> bool:
"""Determine if a string is an URL.
The implementation has been adapted from `urllib.urlparse`.
"""
i = string.find(':')
if i <= 1: # noqa: PLR2004 -> exclude C: or D: on Windows
return False
try:
return all(_is_valid_url_scheme_char(string[j]) for j in range(i))
except (IndexError, ValueError): # pragma: no cover
return False
def is_relative_path(string: str) -> bool:
"""Check if a string looks like a relative path."""
try:
return (
string[0] == '.'
and (
string[1] == '/'
or (string[1] == '.' and string[2] == '/')
)
)
except IndexError: # pragma: no cover
return False
def is_absolute_path(string: str) -> bool:
"""Check if a string looks like an absolute path."""
try:
return string[0] == '/' or string[0] == os.sep
except IndexError: # pragma: no cover
return False
def read_file(file_path: str, encoding: str) -> str:
"""Read a file and return its content."""
f = open(file_path, encoding=encoding) # noqa: SIM115
content = f.read()
f.close()
return content
def read_url(
url: str,
http_cache: Cache | None,
encoding: str = 'utf-8',
) -> Any:
"""Read an HTTP location and return its content."""
from urllib.request import Request, urlopen
if http_cache is not None:
cached_content = http_cache.get_(url, encoding)
if cached_content is not None:
return cached_content
with urlopen(Request(url)) as response:
content = response.read().decode(encoding)
if http_cache is not None:
http_cache.set_(url, content, encoding)
return content
def safe_os_path_relpath(path: str, start: str) -> str:
"""Return the relative path of a file from a start directory.
Safe version of `os.path.relpath` that catches possible `ValueError`
exceptions and returns the original path in case of error.
On Windows, `ValueError` is raised when `path` and `start` are on
different drives.
"""
try:
return os.path.relpath(path, start)
except ValueError: # pragma: no cover
return path
def file_lineno_message(
page_src_path: str | None,
docs_dir: str,
lineno: int,
) -> str:
"""Return a message with the file path and line number."""
if page_src_path is None: # pragma: no cover
return f'generated page content (line {lineno})'
return (
f'{safe_os_path_relpath(page_src_path, docs_dir)}'
f':{lineno}'
)
def lineno_from_content_start(content: str, start: int) -> int:
"""Return the line number of the first line of ``start`` in ``content``."""
return content[:start].count('\n') + 1