@@ -114,46 +114,60 @@ def get_title_heading(self) -> SectionHeading:
114
114
"First heading in the skeleton was not the title." )
115
115
return self .headings [0 ]
116
116
117
- def check_if_topic_file_matches (self , topic_file_path : Path ) -> bool :
117
+ def check_if_topic_file_matches (self , topic_file : tp . TextIO ) -> bool :
118
118
"""
119
119
Checks if the topic file headings and meta text matches the skeleton.
120
120
Prints the differences between the topic file and the skeleton, if a
121
121
miss mach is detected.
122
122
123
123
Args:
124
- topic_file_path: path to the topic file to check
124
+ topic_file: the topic markdown file to update
125
125
126
126
Returns: `True` if the topic matches, otherwise, `False`
127
127
"""
128
- with open ( topic_file_path , "r" ) as topic_file :
129
- current_heading_iter = iter ( self . headings )
130
- current_heading = None
131
- expected_meta_text : tp .List [str ] = []
128
+ current_heading_iter = iter ( self . headings )
129
+ current_heading = None
130
+ processing_meta_text = False
131
+ expected_meta_text : tp .List [str ] = []
132
132
133
- for line in topic_file .readlines ():
134
- if line .startswith ("#" ):
135
- if current_heading is not None and expected_meta_text :
136
- print ("Found missing italics:" )
137
- print (f"Expected: { expected_meta_text [0 ]} " )
138
- return False
139
-
140
- current_heading = next (current_heading_iter )
141
- expected_meta_text = copy (current_heading .meta_text )
142
- if line .startswith ("_" ):
143
- if not expected_meta_text :
144
- print ("Did not expect further italics but found:" )
145
- print (line )
146
- return False
147
-
148
- if line == expected_meta_text [0 ]:
149
- expected_meta_text .pop (0 )
150
- else :
151
- print ("Found italics text did not match the skeleton:" )
152
- print (f"Found:\n { line } " )
153
- print (f"Expected:\n { expected_meta_text [0 ]} " )
154
- return False
155
-
156
- return True
133
+ for line in topic_file .readlines ():
134
+ if line .startswith ("#" ):
135
+ if current_heading and expected_meta_text :
136
+ print ("Found missing italics:" )
137
+ print (f"Expected: { expected_meta_text [0 ]} " )
138
+ return False
139
+
140
+ current_heading = next (current_heading_iter )
141
+ expected_meta_text = copy (
142
+ self .lookup_heading (current_heading .header_text ).meta_text )
143
+ processing_meta_text = False
144
+
145
+ # Check if the required section headings are present
146
+ if line .startswith ("##" ) and current_heading :
147
+ if current_heading .header_text .split (":" )[0 ] != line .split (
148
+ ":" )[0 ].strip ():
149
+ print ("Found wrong section title:" )
150
+ print (f"Found:\n { line .strip ()} " )
151
+ print (f"Expected:\n { current_heading .header_text } " )
152
+ return False
153
+
154
+ # Check if the correct meta text is below the section heading
155
+ if line .startswith ("_" ) or processing_meta_text :
156
+ if not expected_meta_text :
157
+ print ("Did not expect further italics but found:" )
158
+ print (line )
159
+ return False
160
+
161
+ if line == expected_meta_text [0 ]:
162
+ processing_meta_text = not line .strip ().endswith ("_" )
163
+ expected_meta_text .pop (0 )
164
+ else :
165
+ print ("Found italics text did not match the skeleton:" )
166
+ print (f"Found:\n { line } " )
167
+ print (f"Expected:\n { expected_meta_text [0 ]} " )
168
+ return False
169
+
170
+ return True
157
171
158
172
def update_topic_meta_text (self , topic_file : tp .TextIO ) -> tp .List [str ]:
159
173
"""
@@ -292,17 +306,25 @@ def __str__(self) -> str:
292
306
293
307
294
308
def check_skeletons (skeleton : Skeleton ,
295
- topic_paths : tp .Iterator [Path ]) -> None :
309
+ topic_paths : tp .Iterator [Path ]) -> bool :
296
310
"""
297
311
Check of the topics files match the skeleton.
298
312
299
313
Args:
300
314
skeleton: base skeleton to compare the topics against
301
315
topic_paths: list of paths to topic files
316
+
317
+ Returns: True, if all topic files matched the skeleton
302
318
"""
319
+ all_files_matched = True
303
320
for topic_path in topic_paths :
304
- if skeleton .check_if_topic_file_matches (topic_path ):
305
- print (f"All meta-text in { topic_path } matched the skeleton." )
321
+ with open (topic_path , "r" ) as topic_file :
322
+ if skeleton .check_if_topic_file_matches (topic_file ):
323
+ print (f"All meta-text in { topic_path } matched the skeleton." )
324
+ else :
325
+ all_files_matched = False
326
+
327
+ return all_files_matched
306
328
307
329
308
330
def update_skeletons (skeleton : Skeleton ,
0 commit comments