refactor: Simplified extension parsers.

This commit is contained in:
Spencer Brower
2026-08-04 14:15:01 -04:00
parent 876f767548
commit fde54c1f94
2 changed files with 35 additions and 30 deletions
+1
View File
@@ -30,3 +30,4 @@ test_emoji_skips_invalid_shortcodes :: proc(t: ^testing.T) {
testing.expect_value(t, expand_emoji(":Smile:"), ":Smile:") testing.expect_value(t, expand_emoji(":Smile:"), ":Smile:")
testing.expect_value(t, expand_emoji(": not real :"), ": not real :") testing.expect_value(t, expand_emoji(": not real :"), ": not real :")
} }
+34 -30
View File
@@ -62,22 +62,11 @@ process :: proc(
parse_extension_list :: proc(s: string) -> (result: bit_set[Extension]) { parse_extension_list :: proc(s: string) -> (result: bit_set[Extension]) {
for part in strings.split(s, ",", allocator = context.temp_allocator) { for part in strings.split(s, ",", allocator = context.temp_allocator) {
name := strings.to_lower(strings.trim_space(part), allocator = context.temp_allocator) name := strings.to_lower(strings.trim_space(part), allocator = context.temp_allocator)
switch name { e, ok := extension_from_name(name)
case "emoji": if !ok && name != "" {
result += {.Emoji} panic("!ok") // TODO: handle this
case "sidenotes":
result += {.Sidenotes}
case "alerts":
result += {.Alerts}
case "highlight":
result += {.Highlight}
case "sections":
result += {.Sections}
case "heading_ids":
result += {.HeadingIDs}
case "deflists":
result += {.DefLists}
} }
result += {e}
} }
return result return result
} }
@@ -87,22 +76,37 @@ apply_extension_config :: proc(ext: ^bit_set[Extension], config: json.Object) {
for name, val in config { for name, val in config {
// TODO: Silently discards invalid values. // TODO: Silently discards invalid values.
enabled := val.(json.Boolean) or_continue enabled := val.(json.Boolean) or_continue
switch name { e := extension_from_name(name) or_continue
case "emoji":
if enabled {ext^ += {.Emoji}} else {ext^ -= {.Emoji}} if enabled {
case "sidenotes": ext^ += {e}
if enabled {ext^ += {.Sidenotes}} else {ext^ -= {.Sidenotes}} } else {
case "alerts": ext^ -= {e}
if enabled {ext^ += {.Alerts}} else {ext^ -= {.Alerts}}
case "highlight":
if enabled {ext^ += {.Highlight}} else {ext^ -= {.Highlight}}
case "sections":
if enabled {ext^ += {.Sections}} else {ext^ -= {.Sections}}
case "heading_ids":
if enabled {ext^ += {.HeadingIDs}} else {ext^ -= {.HeadingIDs}}
case "deflists":
if enabled {ext^ += {.DefLists}} else {ext^ -= {.DefLists}}
} }
} }
} }
extension_from_name :: proc(name: string) -> (e: Extension, ok: bool) {
switch name {
case "emoji":
e = .Emoji
ok = true
case "sidenotes":
e = .Sidenotes
case "alerts":
e = .Alerts
case "highlight":
e = .Highlight
case "sections":
e = .Sections
case "heading_ids":
e = .HeadingIDs
case "deflists":
e = .DefLists
case:
// Do nothing
}
return e, ok || e != .Emoji
}