11import re
22import json
33
4+
45class ReadmeFormatException (Exception ):
56 """Custom exception for errors in the README format."""
67
8+
79def camel_case (s ):
810 """Converts a string to camelCase."""
911 s = re .sub (r"(\s|_|-)+" , " " , s ).title ().replace (" " , "" )
1012 return s [0 ].lower () + s [1 :] if s else ""
1113
14+
1215def expand_special_keys (key , value ):
1316 """Expand special keys into their structured format (affiliation, nameIdentifiers)."""
1417 if key == "affiliation" :
15- return [
16- {
17- "affiliationIdentifier" : value ,
18- "affiliationIdentifierScheme" : "ROR"
19- }
20- ]
18+ return [{"affiliationIdentifier" : value , "affiliationIdentifierScheme" : "ROR" }]
2119 elif key == "nameIdentifiers" :
2220 return [
2321 {
2422 "nameIdentifier" : value ,
2523 "nameIdentifierScheme" : "ORCID" ,
26- "schemeUri" : f"https://orcid.org/{ value } "
24+ "schemeUri" : f"https://orcid.org/{ value } " ,
2725 }
2826 ]
2927 return value
3028
29+
3130def parse_readme_to_json (readme_path ):
3231 try :
33- with open (readme_path , 'r' ) as file :
34- lines = file .read ().split (' \n ' )
32+ with open (readme_path , "r" ) as file :
33+ lines = file .read ().split (" \n " )
3534 except IOError as e :
3635 raise ReadmeFormatException (f"Failed to open or read the file: { e } " )
3736
3837 json_data = {}
3938 current_section = None
4039 current_object = {}
4140
42- section_pattern = re .compile (r' ^##\s+(.*)$' )
43- key_value_pattern = re .compile (r' ^-\s+(.*?):\s+(.*)$' )
44- link_pattern = re .compile (r' \[.*?\]\((.*?)\)' )
41+ section_pattern = re .compile (r" ^##\s+(.*)$" )
42+ key_value_pattern = re .compile (r" ^-\s+(.*?):\s+(.*)$" )
43+ link_pattern = re .compile (r" \[.*?\]\((.*?)\)" )
4544
4645 for line_number , line in enumerate (lines , 1 ):
4746 if not line .strip ():
@@ -93,7 +92,9 @@ def parse_readme_to_json(readme_path):
9392 current_object [key ] = value
9493
9594 elif line .strip () and not section_match :
96- raise ReadmeFormatException (f"Incorrect format detected at line { line_number } : { line } " )
95+ raise ReadmeFormatException (
96+ f"Incorrect format detected at line { line_number } : { line } "
97+ )
9798
9899 if current_section and current_object :
99100 if current_section == "types" :
@@ -109,11 +110,12 @@ def parse_readme_to_json(readme_path):
109110
110111 return json_data
111112
112- readme_path = '/Users/elizabethwon/downloads/exampleREADME.md'
113+
114+ readme_path = "/Users/elizabethwon/downloads/exampleREADME.md"
113115try :
114116 json_data = parse_readme_to_json (readme_path )
115- output_json_path = ' output1.json'
116- with open (output_json_path , 'w' ) as json_file :
117+ output_json_path = " output1.json"
118+ with open (output_json_path , "w" ) as json_file :
117119 json .dump (json_data , json_file , indent = 4 )
118120 print (f"Converted JSON saved to { output_json_path } " )
119121except ReadmeFormatException as e :
0 commit comments