Skip to content

Commit 39df609

Browse files
authored
Optimize string concatenation across multiple files (#288)
1 parent 39fb543 commit 39df609

File tree

4 files changed

+32
-30
lines changed

4 files changed

+32
-30
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "mkdocs-include-markdown-plugin"
3-
version = "7.2.0"
3+
version = "7.2.1"
44
description = "Mkdocs Markdown includer plugin."
55
readme = "README.md"
66
license = "Apache-2.0"

src/mkdocs_include_markdown_plugin/directive.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ def _maybe_arguments_iter(arguments_string: str) -> Iterable[str]:
140140
inside_string = False
141141
escaping = False
142142
opening_argument = False # whether we are at the beginning of an argument
143-
current_value = ''
143+
current_value: list[str] = []
144144

145145
for c in arguments_string:
146146
if inside_string:
@@ -153,24 +153,25 @@ def _maybe_arguments_iter(arguments_string: str) -> Iterable[str]:
153153
else:
154154
escaping = False
155155
elif c == '=':
156+
current_value_str = ''.join(current_value)
156157
new_current_value = ''
157-
for ch in reversed(current_value):
158+
for ch in reversed(current_value_str):
158159
if ch in string.whitespace:
159-
current_value = new_current_value[::-1]
160+
current_value_str = new_current_value[::-1]
160161
break
161162
new_current_value += ch
162-
yield current_value
163-
current_value = ''
163+
yield current_value_str
164+
current_value = []
164165
opening_argument = True
165166
elif opening_argument:
166167
opening_argument = False
167168
if c in ('"', "'"):
168169
current_string_opening = c
169170
inside_string = True
170-
current_value += c
171-
current_value += c
171+
current_value.append(c)
172+
current_value.append(c)
172173
else:
173-
current_value += c
174+
current_value.append(c)
174175

175176

176177
def warn_invalid_directive_arguments(

src/mkdocs_include_markdown_plugin/event.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,7 @@ def found_include_markdown_tag( # noqa: PLR0912, PLR0915
545545
# but they have been specified, so the warning(s) must be raised
546546
expected_but_any_found = [start is not None, end is not None]
547547

548-
text_to_include = ''
548+
text_to_include_parts = []
549549
for file_path in file_paths_to_include:
550550
if process.is_url(filename):
551551
new_text_to_include = process.read_url(
@@ -621,19 +621,20 @@ def found_include_markdown_tag( # noqa: PLR0912, PLR0915
621621
new_text_to_include
622622
):
623623
lines = new_text_to_include.splitlines(keepends=True)
624-
new_text_to_include = lines[0]
624+
indented_lines = [lines[0]]
625625
for i in range(1, len(lines)):
626-
new_text_to_include += (
627-
filled_includer_indent + lines[i]
628-
)
626+
indented_lines.append(filled_includer_indent + lines[i])
627+
new_text_to_include = ''.join(indented_lines)
629628

630629
if offset:
631630
new_text_to_include = process.increase_headings_offset(
632631
new_text_to_include,
633632
offset=offset + cumulative_heading_offset,
634633
)
635634

636-
text_to_include += new_text_to_include
635+
text_to_include_parts.append(new_text_to_include)
636+
637+
text_to_include = ''.join(text_to_include_parts)
637638

638639
# warn if expected start or ends haven't been found in included content
639640
for i, delimiter_name in enumerate(['start', 'end']):

src/mkdocs_include_markdown_plugin/process.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -174,10 +174,12 @@ def transform_p_by_p_skipping_codeblocks( # noqa: PLR0912, PLR0915
174174
_maybe_icodeblock_lines: list[str] = []
175175
_previous_line_was_empty = False
176176

177-
lines, current_paragraph = ([], '')
177+
lines = []
178+
current_paragraph_lines: list[str] = []
178179

179180
def process_current_paragraph() -> None:
180-
lines.extend(func(current_paragraph).splitlines(keepends=True))
181+
lines.extend(func(''.join(current_paragraph_lines),
182+
).splitlines(keepends=True))
181183

182184
# The next implementation takes into account that indented code
183185
# blocks must be surrounded by newlines as per the CommonMark
@@ -191,33 +193,32 @@ def process_current_paragraph() -> None:
191193
if lstripped_line.startswith(('```', '~~~')):
192194
_current_fcodeblock_delimiter = lstripped_line[:3]
193195
process_current_paragraph()
194-
current_paragraph = ''
196+
current_paragraph_lines = []
195197
lines.append(line)
196198
elif line.startswith(' '):
197199
if not lstripped_line or _maybe_icodeblock_lines:
198200
# maybe enter indented codeblock
199201
_maybe_icodeblock_lines.append(line)
200202
else:
201-
current_paragraph += line
203+
current_paragraph_lines.append(line)
202204
elif _maybe_icodeblock_lines:
203205
process_current_paragraph()
204-
current_paragraph = ''
206+
current_paragraph_lines = []
205207
if not _previous_line_was_empty:
206208
# wasn't an indented code block
207-
for line_ in _maybe_icodeblock_lines:
208-
current_paragraph += line_
209+
current_paragraph_lines.extend(_maybe_icodeblock_lines)
209210
_maybe_icodeblock_lines = []
210-
current_paragraph += line
211+
current_paragraph_lines.append(line)
211212
process_current_paragraph()
212-
current_paragraph = ''
213+
current_paragraph_lines = []
213214
else:
214215
# exit indented codeblock
215216
for line_ in _maybe_icodeblock_lines:
216217
lines.append(line_)
217218
_maybe_icodeblock_lines = []
218219
lines.append(line)
219220
else:
220-
current_paragraph += line
221+
current_paragraph_lines.append(line)
221222
_previous_line_was_empty = not lstripped_line
222223
else:
223224
lines.append(line)
@@ -230,14 +231,13 @@ def process_current_paragraph() -> None:
230231
if not _previous_line_was_empty:
231232
# at EOF
232233
process_current_paragraph()
233-
current_paragraph = ''
234-
for line_ in _maybe_icodeblock_lines:
235-
current_paragraph += line_
234+
current_paragraph_lines = []
235+
current_paragraph_lines.extend(_maybe_icodeblock_lines)
236236
process_current_paragraph()
237-
current_paragraph = ''
237+
current_paragraph_lines = []
238238
else:
239239
process_current_paragraph()
240-
current_paragraph = ''
240+
current_paragraph_lines = []
241241
for line_ in _maybe_icodeblock_lines:
242242
lines.append(line_)
243243
else:

0 commit comments

Comments
 (0)