mirror of
https://github.com/sbrow/thor.git
synced 2026-08-26 11:23:32 -04:00
refactor: Simplified extension parsers.
This commit is contained in:
@@ -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
@@ -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
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user