Skip to content

Commit bd06835

Browse files
vuldercjdb
authored andcommitted
Restructures update code into different functions
1 parent e4d555e commit bd06835

File tree

1 file changed

+60
-13
lines changed

1 file changed

+60
-13
lines changed

topic_updater.py

Lines changed: 60 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -166,12 +166,44 @@ def update_topic_meta_text(self, topic_file: tp.TextIO) -> tp.List[str]:
166166
Returns: updated topic lines
167167
"""
168168
updated_topic_lines = []
169-
headings_iter = iter(self.headings)
169+
skeleton_headings_iter = iter(self.headings)
170170

171+
updated_topic_lines.extend(
172+
self.__process_existing_topic_content(topic_file,
173+
skeleton_headings_iter))
174+
175+
# Add missing section headings at the end
176+
updated_topic_lines.extend(
177+
self.__get_remaining_section_headings(skeleton_headings_iter))
178+
179+
# Remove excessive newlines
180+
updated_topic_lines[-1] = updated_topic_lines[-1].rstrip() + os.linesep
181+
182+
return updated_topic_lines
183+
184+
def __process_existing_topic_content(
185+
self, topic_file: tp.TextIO,
186+
skeleton_headings_iter: tp.Iterator[SectionHeading]
187+
) -> tp.List[str]:
188+
"""
189+
This method checks that all heading related lines in the topic file
190+
correspond correctly to the skeleton. If lines are missing, the user is
191+
asked if the missing heading should be added.
192+
193+
Args:
194+
topic_file: the topic markdown file to update
195+
skeleton_headings_iter: iterator that points to the next expected
196+
topic heading
197+
198+
Returns: existing topic lines, where headings were updated according to
199+
the skeleton
200+
"""
201+
updated_topic_lines = []
171202
emitting_doc_text = True
203+
172204
for line in topic_file.readlines():
173205
if line.startswith("##"):
174-
next_heading = next(headings_iter)
206+
next_heading = next(skeleton_headings_iter)
175207
current_heading = self.lookup_heading(line.split(":")[0])
176208

177209
# Add headers that are completely missing
@@ -185,7 +217,7 @@ def update_topic_meta_text(self, topic_file: tp.TextIO) -> tp.List[str]:
185217
updated_topic_lines.extend(
186218
next_heading.convert_meta_text_to_lines())
187219

188-
next_heading = next(headings_iter)
220+
next_heading = next(skeleton_headings_iter)
189221

190222
emitting_doc_text = False
191223

@@ -197,33 +229,48 @@ def update_topic_meta_text(self, topic_file: tp.TextIO) -> tp.List[str]:
197229
elif line.startswith("#"):
198230
# Verify that the title heading has correct meta text
199231
emitting_doc_text = False
200-
next_heading = next(headings_iter)
232+
next_heading = next(skeleton_headings_iter)
201233
updated_topic_lines.append(line)
202234
updated_topic_lines.extend(
203235
self.get_title_heading().convert_meta_text_to_lines())
204236
elif line.startswith("_") or line.strip().endswith("_"):
205237
# Ignore meta lines
238+
# Meta lines are not allowed to contain modifications by the
239+
# topic writer and are always inserted with the title heading
240+
# from the skeleton.
206241
continue
207242
elif emitting_doc_text or line != "\n":
208243
# Skip new lines if we aren't emitting normal document text
209244
emitting_doc_text = True
210245
updated_topic_lines.append(line)
211246

212-
# Add missing section headings at the end
247+
return updated_topic_lines
248+
249+
@staticmethod
250+
def __get_remaining_section_headings(
251+
skeleton_headings_iter: tp.Iterator[SectionHeading]
252+
) -> tp.List[str]:
253+
"""
254+
Returns a list of all `SectionHeading`s that are still missing and
255+
should be added at the end of the topic.
256+
257+
Args:
258+
skeleton_headings_iter: iterator that points to the next expected
259+
topic heading
260+
Returns: missing topic lines, which are not present and have not been
261+
added up to now
262+
"""
263+
missing_headings: tp.List[str] = []
213264
try:
214265
while True:
215-
next_heading = next(headings_iter)
216-
updated_topic_lines.append(next_heading.header_text +
217-
os.linesep)
218-
updated_topic_lines.extend(
266+
next_heading = next(skeleton_headings_iter)
267+
missing_headings.append(next_heading.header_text + os.linesep)
268+
missing_headings.extend(
219269
next_heading.convert_meta_text_to_lines())
220270
except StopIteration:
221271
pass
222272

223-
# Remove excessive newlines
224-
updated_topic_lines[-1] = updated_topic_lines[-1].rstrip() + os.linesep
225-
226-
return updated_topic_lines
273+
return missing_headings
227274

228275
def __parse_headings(self) -> None:
229276
with open(self.__skeleton_file_path, "r") as skeleton_file:

0 commit comments

Comments
 (0)