Commit b170443d authored by twanvl's avatar twanvl

Some tweaks of the regular expressions to improve performance (in_context is slow!)

parent 206868fd
...@@ -10,6 +10,8 @@ mana_sort := sort_text@(order: "XYZI[0123456789]S(WUBRG)") ...@@ -10,6 +10,8 @@ mana_sort := sort_text@(order: "XYZI[0123456789]S(WUBRG)")
# correctly sort guild mana # correctly sort guild mana
mana_sort_guild := sort_text@(order: "[XYZI01234567890SWUBRG/|]") + mana_sort_guild := sort_text@(order: "[XYZI01234567890SWUBRG/|]") +
replace@( replace@(
# No lookbehind :(
#match: "(?<!/)(./.|././.|./././.|.[|])(?!/)",
match: "./.|././.|./././.|.[|]", match: "./.|././.|./././.|.[|]",
in_context: "(^|[^/])<match>($|[^/])", in_context: "(^|[^/])<match>($|[^/])",
replace: {sort_text(order:"in_place((WUBRG)")} replace: {sort_text(order:"in_place((WUBRG)")}
...@@ -272,9 +274,12 @@ card_count := { ...@@ -272,9 +274,12 @@ card_count := {
############################################################## Utilities for keywords ############################################################## Utilities for keywords
# Replace spaces by a spacer
separate_words := remove_tags + trim + replace@(match:" ", replace: {spacer})
# replaces — correctly # replaces — correctly
add := "" # default is nothing add := "" # default is nothing
separate_words := remove_tags + trim + replace@(match:" ", replace: {spacer}) # If the 'input' parameter is a mana costs, then adds 'add'
for_mana_costs := format_cost := { for_mana_costs := format_cost := {
if input.separator_before == "—" and contains(input.param, match: " ") then ( if input.separator_before == "—" and contains(input.param, match: " ") then (
if contains(input.param, match:",") then ( if contains(input.param, match:",") then (
...@@ -286,15 +291,19 @@ for_mana_costs := format_cost := { ...@@ -286,15 +291,19 @@ for_mana_costs := format_cost := {
) else ) else
"{add}<param-mana>{input.param}</param-mana>" "{add}<param-mana>{input.param}</param-mana>"
} }
# Convert first character to lower case
alternative_cost := replace@(match:"^[A-Z]", replace: { to_lower() }) alternative_cost := replace@(match:"^[A-Z]", replace: { to_lower() })
#
combined_cost := replace@(match:", [A-Z]", replace: { to_lower() })+ combined_cost := replace@(match:", [A-Z]", replace: { to_lower() })+
replace@(match:",", replace:" and")+ replace@(match:",", replace:" and")+
replace@(match:"^[STQXYZIWUBRG0-9/|]", in_context: "(^|[[:space:]])<match>", replace: "<sym-auto>&</sym-auto>")+ replace@(match:"^[STQXYZIWUBRG0-9/|]", in_context: "(^|[[:space:]])<match>", replace: "<sym-auto>&</sym-auto>")+
replace@(match:"^[A-Z]", replace: { to_lower() }) replace@(match:"^[A-Z]", replace: { to_lower() })
long_dash := replace@(match:"-", replace:"—") long_dash := replace@(match:"-", replace:"—")
# Utilities for keywords # Utilities for keywords
has_cc := { card.casting_cost != "" } has_cc := { card.casting_cost != "" }
has_pt := { card.pt != "" } has_pt := { card.pt != "" }
contains_target := match@(match:"(?i)([^a-z]|^)target([^a-z]|$)") contains_target := match@(match:"(?i)([^a-z]|^)target([^a-z]|$)")
is_targeted := { contains_target(card.rule_text) } is_targeted := { contains_target(card.rule_text) }
...@@ -322,8 +331,8 @@ mana_context := ...@@ -322,8 +331,8 @@ mana_context :=
) )
) )
| <param-mana><match></param-mana> # keyword argument that is declared as mana | <param-mana><match></param-mana> # keyword argument that is declared as mana
| <param-cost>[ ]*<match></param-cost> # keyword argument that is declared as cost | <param-cost>[ ]*<match></param-cost> # keyword argument that is declared as cost
| <param-cost><match>, # keyword argument that is declared as cost | <param-cost><match>, # keyword argument that is declared as cost
"; ";
# truncates the name of legends # truncates the name of legends
legend_filter := replace@(match:"(, | of | the ).*", replace: "" ) legend_filter := replace@(match:"(, | of | the ).*", replace: "" )
...@@ -383,7 +392,7 @@ text_filter := ...@@ -383,7 +392,7 @@ text_filter :=
replace: {"<nosym>" + mana_filter_t() + "</nosym>"} ) + replace: {"<nosym>" + mana_filter_t() + "</nosym>"} ) +
# step 5 : add mana & tap symbols # step 5 : add mana & tap symbols
replace@( replace@(
match: "[STQXYZIWUBRG0-9/|]+", match: "\b[STQXYZIWUBRG0-9/|]+\b",
in_context: mana_context, in_context: mana_context,
replace: {"<sym-auto>" + mana_filter_t() + "</sym-auto>"} ) + replace: {"<sym-auto>" + mana_filter_t() + "</sym-auto>"} ) +
# step 5b : add explict mana symbols # step 5b : add explict mana symbols
...@@ -397,9 +406,10 @@ text_filter := ...@@ -397,9 +406,10 @@ text_filter :=
replace: "<i-auto>&</i-auto>") + replace: "<i-auto>&</i-auto>") +
# step 8 : automatic capitalization # step 8 : automatic capitalization
replace@( replace@(
match: "[a-z]", match: "([(](?:<param-[a-z]*>)?|[ ]*: <param-cost>|—| — )" # preceded by this
in_context: "[(](<param-[a-z]*>)?<match>[^)]|[ ]*: <param-cost><match>|—<match>| — <match>", + "([[:lower:]])" # match this
replace: to_upper) + + "(?![)])", # not followed by this
replace: { _1 + to_upper(_2) }) +
curly_quotes curly_quotes
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment