Skip to content

Commit e7c6f9d

Browse files
marcarlclaude
andcommitted
Update frontmatter sorting to support CELEX lists
The frontmatter sorter now properly handles CELEX numbers in amendments: - Add 'celex' to AMENDMENT_ORDER for proper field ordering - Parse nested YAML lists (e.g., multiple CELEX numbers) - Generate correct YAML output for both single values and lists This completes the CELEX number support by ensuring that frontmatter sorting preserves CELEX data in the correct format. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent 0645a26 commit e7c6f9d

1 file changed

Lines changed: 88 additions & 39 deletions

File tree

formatters/sort_frontmatter.py

Lines changed: 88 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@
1414
def sort_amendments_list(amendment_lines: list) -> str:
1515
"""
1616
Sorterar innehållet i en andringsforfattningar-lista.
17-
17+
1818
Args:
1919
amendment_lines: Lista med rader som representerar andringsforfattningar
20-
20+
2121
Returns:
2222
str: Sorterad YAML-representation av andringsforfattningar
2323
"""
24-
AMENDMENT_ORDER = ['beteckning', 'rubrik', 'ikraft_datum', 'anteckningar']
24+
AMENDMENT_ORDER = ['beteckning', 'rubrik', 'ikraft_datum', 'celex', 'anteckningar']
2525

2626
# Hantera det felaktiga formatet där första raden börjar direkt efter kolon
2727
processed_lines = []
@@ -35,33 +35,51 @@ def sort_amendments_list(amendment_lines: list) -> str:
3535
# Parsa amendment items
3636
amendments = []
3737
current_amendment = {}
38-
38+
current_list_key = None # Track if we're parsing a nested list
39+
3940
for line in processed_lines:
4041
stripped = line.strip()
41-
42+
4243
# Ny amendment item (börjar med -)
4344
if stripped.startswith('-'):
44-
# Spara föregående amendment om den finns
45-
if current_amendment:
46-
amendments.append(current_amendment)
47-
48-
# Starta ny amendment
49-
current_amendment = {}
50-
51-
# Kolla om det finns data på samma rad som -
52-
if ':' in stripped:
53-
parts = stripped[1:].split(':', 1) # Ta bort - först
54-
key = parts[0].strip()
55-
value = parts[1].strip() if len(parts) > 1 else ''
56-
current_amendment[key] = value
57-
45+
# Check if this is a list item within a nested list (indented with 6 spaces)
46+
if line.startswith(' -'):
47+
# This is a nested list item (e.g., for celex)
48+
if current_list_key:
49+
list_value = stripped[1:].strip() # Remove the '-' and trim
50+
if current_list_key not in current_amendment:
51+
current_amendment[current_list_key] = []
52+
current_amendment[current_list_key].append(list_value)
53+
else:
54+
# This is a new amendment item
55+
# Spara föregående amendment om den finns
56+
if current_amendment:
57+
amendments.append(current_amendment)
58+
59+
# Starta ny amendment
60+
current_amendment = {}
61+
current_list_key = None
62+
63+
# Kolla om det finns data på samma rad som -
64+
if ':' in stripped:
65+
parts = stripped[1:].split(':', 1) # Ta bort - först
66+
key = parts[0].strip()
67+
value = parts[1].strip() if len(parts) > 1 else ''
68+
current_amendment[key] = value
69+
5870
# Property inom amendment item
5971
elif ':' in line and (line.startswith(' ') or line.startswith(' ')):
6072
parts = line.strip().split(':', 1)
6173
key = parts[0].strip()
6274
value = parts[1].strip() if len(parts) > 1 else ''
6375
if key:
64-
current_amendment[key] = value
76+
if value:
77+
# Simple key-value pair
78+
current_amendment[key] = value
79+
current_list_key = None
80+
else:
81+
# Empty value, might be start of a nested list
82+
current_list_key = key
6583

6684
# Spara sista amendment
6785
if current_amendment:
@@ -70,39 +88,70 @@ def sort_amendments_list(amendment_lines: list) -> str:
7088
# Bygg sorterad YAML med korrekt indentation
7189
if not amendments:
7290
return ''
73-
91+
7492
result_lines = []
7593
for i, amendment in enumerate(amendments):
7694
# Lägg till första property med - prefix
7795
first_prop = True
7896
for prop in AMENDMENT_ORDER:
7997
if prop in amendment:
8098
value = amendment[prop]
81-
# Lägg till citattecken runt värden som innehåller kolon eller speciella tecken
82-
if ':' in value or value.startswith('"') or '"' in value:
83-
if not (value.startswith('"') and value.endswith('"')):
84-
value = f'"{value}"'
85-
86-
if first_prop:
87-
result_lines.append(f" - {prop}: {value}")
88-
first_prop = False
99+
100+
# Handle lists (e.g., celex with multiple values)
101+
if isinstance(value, list):
102+
if first_prop:
103+
result_lines.append(f" - {prop}:")
104+
first_prop = False
105+
else:
106+
result_lines.append(f" {prop}:")
107+
108+
for item in value:
109+
# Add quotes if needed
110+
if ':' in str(item) or (isinstance(item, str) and (item.startswith('"') or '"' in item)):
111+
if not (str(item).startswith('"') and str(item).endswith('"')):
112+
item = f'"{item}"'
113+
result_lines.append(f" - {item}")
89114
else:
90-
result_lines.append(f" {prop}: {value}")
115+
# Lägg till citattecken runt värden som innehåller kolon eller speciella tecken
116+
if ':' in str(value) or (isinstance(value, str) and (value.startswith('"') or '"' in value)):
117+
if not (str(value).startswith('"') and str(value).endswith('"')):
118+
value = f'"{value}"'
119+
120+
if first_prop:
121+
result_lines.append(f" - {prop}: {value}")
122+
first_prop = False
123+
else:
124+
result_lines.append(f" {prop}: {value}")
91125

92126
# Lägg till okända properties sist
93127
unknown_props = [k for k in amendment.keys() if k not in AMENDMENT_ORDER]
94128
for prop in unknown_props:
95129
value = amendment[prop]
96-
# Lägg till citattecken runt värden som innehåller kolon eller speciella tecken
97-
if ':' in value or value.startswith('"') or '"' in value:
98-
if not (value.startswith('"') and value.endswith('"')):
99-
value = f'"{value}"'
100-
101-
if first_prop:
102-
result_lines.append(f" - {prop}: {value}")
103-
first_prop = False
130+
131+
# Handle lists
132+
if isinstance(value, list):
133+
if first_prop:
134+
result_lines.append(f" - {prop}:")
135+
first_prop = False
136+
else:
137+
result_lines.append(f" {prop}:")
138+
139+
for item in value:
140+
if ':' in str(item) or (isinstance(item, str) and (item.startswith('"') or '"' in item)):
141+
if not (str(item).startswith('"') and str(item).endswith('"')):
142+
item = f'"{item}"'
143+
result_lines.append(f" - {item}")
104144
else:
105-
result_lines.append(f" {prop}: {value}")
145+
# Lägg till citattecken runt värden som innehåller kolon eller speciella tecken
146+
if ':' in str(value) or (isinstance(value, str) and (value.startswith('"') or '"' in value)):
147+
if not (str(value).startswith('"') and str(value).endswith('"')):
148+
value = f'"{value}"'
149+
150+
if first_prop:
151+
result_lines.append(f" - {prop}: {value}")
152+
first_prop = False
153+
else:
154+
result_lines.append(f" {prop}: {value}")
106155

107156
return '\n' + '\n'.join(result_lines)
108157

0 commit comments

Comments
 (0)