@@ -166,12 +166,44 @@ def update_topic_meta_text(self, topic_file: tp.TextIO) -> tp.List[str]:
166
166
Returns: updated topic lines
167
167
"""
168
168
updated_topic_lines = []
169
- headings_iter = iter (self .headings )
169
+ skeleton_headings_iter = iter (self .headings )
170
170
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 = []
171
202
emitting_doc_text = True
203
+
172
204
for line in topic_file .readlines ():
173
205
if line .startswith ("##" ):
174
- next_heading = next (headings_iter )
206
+ next_heading = next (skeleton_headings_iter )
175
207
current_heading = self .lookup_heading (line .split (":" )[0 ])
176
208
177
209
# Add headers that are completely missing
@@ -185,7 +217,7 @@ def update_topic_meta_text(self, topic_file: tp.TextIO) -> tp.List[str]:
185
217
updated_topic_lines .extend (
186
218
next_heading .convert_meta_text_to_lines ())
187
219
188
- next_heading = next (headings_iter )
220
+ next_heading = next (skeleton_headings_iter )
189
221
190
222
emitting_doc_text = False
191
223
@@ -197,33 +229,48 @@ def update_topic_meta_text(self, topic_file: tp.TextIO) -> tp.List[str]:
197
229
elif line .startswith ("#" ):
198
230
# Verify that the title heading has correct meta text
199
231
emitting_doc_text = False
200
- next_heading = next (headings_iter )
232
+ next_heading = next (skeleton_headings_iter )
201
233
updated_topic_lines .append (line )
202
234
updated_topic_lines .extend (
203
235
self .get_title_heading ().convert_meta_text_to_lines ())
204
236
elif line .startswith ("_" ) or line .strip ().endswith ("_" ):
205
237
# 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.
206
241
continue
207
242
elif emitting_doc_text or line != "\n " :
208
243
# Skip new lines if we aren't emitting normal document text
209
244
emitting_doc_text = True
210
245
updated_topic_lines .append (line )
211
246
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 ] = []
213
264
try :
214
265
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 (
219
269
next_heading .convert_meta_text_to_lines ())
220
270
except StopIteration :
221
271
pass
222
272
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
227
274
228
275
def __parse_headings (self ) -> None :
229
276
with open (self .__skeleton_file_path , "r" ) as skeleton_file :
0 commit comments