﻿	# the rule text filter
	#  - adds continuous symbols
	#  - adds counter symbols
	#  - adds equipment symbols
	#  - adds field symbols
	#  - adds quickplay symbols
	#  - adds ritual symbols
	#  - adds level symbols
	#  - adds infinity symbols
	#  - makes text in parentheses italic
	########卡片规划
	text_filter := 
		# step 1 : remove all automatic tags
		tag_remove_rule(tag: "<sym-auto>") +
		# step 2 : expand shortcut words ~ and CARDNAME
		replace_rule(
			match: "CARDNAME",
			in_context: "(^|[[:space:]]|\\()<match>", # TODO: Allow any punctuation before
			replace: "<atom-cardname></atom-cardname>"
			) +
		# step 3 : fill in the cardname atom field
		tag_contents_rule(
			tag: "<atom-cardname>",
			contents: { if card.name=="" then "CARDNAME" else card.name }
			) +
		# step 4 : symbols
		replace_rule(
			match: "[|@|%|?|^|]",
			replace: "<sym-auto>&</sym-auto>" );
	level_filter := 
		# step 1 : remove all automatic tags
		tag_remove_rule(tag: "<sym-auto>") +
		# step 2 : symbols
		replace_rule(
			match: "[|%|!|+|&|$|#|*|^|]",
			replace: "<sym-auto>&</sym-auto>" );
	a_and_d_filter := 
		# step 1 : remove all automatic tags
		tag_remove_rule(tag: "<sym-auto>") +
		# step 2 : infinity
		replace_rule(
			match: "@",
			replace: "<sym-auto>&</sym-auto>" ) +
		# step 3 : question mark
		replace_rule(
			match: "\\?",
			replace: "<sym-auto>&</sym-auto>" );
	
	# the flavor text filter
	flavor_text_filter :=
		# step 1 : remove italic tags
		tag_remove_rule(tag: "<i-flavor>") +
		# step 2 : surround by <i> tags
		{ "<i-flavor>" + input + "</i-flavor>" };
	
	type_over_list := replace_rule(match:" ?/", replace:"")
	
	monster_type_filter :=
		tag_remove_rule(tag: "<word-list-") +
		type_over_list +
		{ "<word-list-monster>{input}</word-list-monster>" }
	monster_type_filter2 :=
		tag_remove_rule(tag: "<word-list-") +
		type_over_list +
		{ "<word-list-monster2>{input}</word-list-monster2>" }	
	monster_type_filter3 :=
		tag_remove_rule(tag: "<word-list-") +
		type_over_list +
		{ "<word-list-monster3>{input}</word-list-monster3>" }		
	monster_type_filter4 :=
		tag_remove_rule(tag: "<word-list-") +
		type_over_list +
		{ "<word-list-monster4>{input}</word-list-monster4>" }
		
	space_to_wltags := replace_rule(match:"( +|<soft> </soft>)",
	                                replace:{"</word-list-{list_type}>{_1}<word-list-{list_type}>"})
	
	card_class_filter :=
		tag_remove_rule(tag: "<word-list-") +
		type_over_list +
		{ "<word-list-card>{input}</word-list-card>" }
	card_class_filter2 :=
		tag_remove_rule(tag: "<word-list-") +
		type_over_list +
		{ "<word-list-card2>{input}</word-list-card2>" }
	card_class_filter3 :=
		tag_remove_rule(tag: "<word-list-") +
		type_over_list +
		{ "<word-list-card3>{input}</word-list-card3>" }
	card_class_filter4 :=
		tag_remove_rule(tag: "<word-list-") +
		type_over_list +
		{ "<word-list-card4>{input}</word-list-card4>" }		
	card_c_filter :=
		tag_remove_rule(tag: "<word-list-") +
		type_over_list +
		{ "<word-list-c>{input}</word-list-c>" }
	card_c_filter2 :=
		tag_remove_rule(tag: "<word-list-") +
		type_over_list +
		{ "<word-list-c2>{input}</word-list-c2>" }
	card_c_filter3 :=
		tag_remove_rule(tag: "<word-list-") +
		type_over_list +
		{ "<word-list-c3>{input}</word-list-c3>" }
	card_c_filter4 :=
		tag_remove_rule(tag: "<word-list-") +
		type_over_list +
		{ "<word-list-c4>{input}</word-list-c4>" }	
		
	type_list:={
		if styling.卡片语言=="日文" then monster_type_filter4(value)
		else if styling.卡片语言=="简体" then monster_type_filter2(value)
		else if styling.卡片语言=="繁体" then monster_type_filter3(value)
		else monster_type_filter(value)
		}
	class_list:= {
		if styling.卡片语言=="日文" then card_class_filter4(value)
		else if styling.卡片语言=="简体" then card_class_filter2(value)
		else if styling.卡片语言=="繁体" then card_class_filter3(value)
		else card_class_filter(value)
	}	
	c_list:= {
		if styling.卡片语言=="日文" then card_c_filter4(value)
		else if styling.卡片语言=="简体" then card_c_filter2(value)
		else if styling.卡片语言=="繁体" then card_c_filter3(value)
		else card_c_filter(value)
	}
	typecombined:= {
		if styling.卡片语言=="英文" then typecd(value)
		else typecd2(value)
		}
	typecd:={
		combined_editor(
			field1: card.type_1,
			separator1: " / ",
			field2: card.type_2,
			separator2: " / ",
			field3: card.type_3,
			soft_before_empty: true,
			hide_when_empty: true
		)
	}

	typecd2:={
				combined_editor(
			field1: card.type_1,
			separator1: "・",
			field2: card.type_2,
			separator2: "／",
			field3: card.type_3,
			soft_before_empty: true,
			hide_when_empty: true
		)
	}
		
	# Determine type of card
	# 选择效果，自动换卡片类型
	card_type := {
		if	card.attribute	== "魔法" or card.attribute	=="spell"								then "魔法"
		else if	contains(card.level, match:"魔法")						then "魔法"
		else if card.attribute	== "魔法" or card.attribute	=="trap"								then "陷阱"
		else if	contains(card.level, match:"陷阱")							then "陷阱"
		else if card.type_1	== "<word-list-monster>幻神兽族</word-list-monster>"		then "巨神兵"
		else if card.type_1	== "<word-list-monster>传说之龙</word-list-monster>"		then "传说之龙"
		else if	contains(card.monster_type, match:"融合")						then "融合怪兽"
		else if	contains(card.monster_type, match:"仪式")						then "仪式怪兽"
		else if	contains(card.monster_type, match:"同调")						then "同调怪兽"
		else if	contains(card.monster_type, match:"黑暗同调")					then "黑暗同调怪兽"
		else if	contains(card.monster_type, match:"超量")						then "超量怪兽"
		else if	contains(card.monster_type, match:"黑暗调整")						then "效果怪兽"
		else if	contains(card.monster_type, match:"效果")						then "效果怪兽"
		else if	contains(card.monster_type, match:"调整")						then "效果怪兽"
		else if	contains(card.monster_type, match:"灵魂")						then "效果怪兽"
		else if	contains(card.monster_type, match:"卡通")						then "效果怪兽"
		else if	contains(card.monster_type, match:"二重")						then "效果怪兽"
		else if	contains(card.monster_type, match:"同盟")						then "效果怪兽"
		else                                         	 							"通常怪兽"
	}
	# Default 'attribute' of card
	attribute := {
		if      is_spell_card() then "魔法"
		else if is_trap_card()  then "陷阱"
		else                         "none"
	}

	# Default foil for a card
	# 罕见度的图片选择
	foil := {
		if	styling.罕见度 == "闪图" then "holo image"
		else if styling.罕见度 == "斜碎"	then "secret holo image"
		else if styling.罕见度 == "DT闪图"	then "parallel holo image"
		else if styling.罕见度 == "黄金闪图"	then "gold tech holo image"
		else					     "none"
	}
	zkuohao :={
	if styling.卡片语言=="英文" then "lbracket"
	else "lbracket2"
	}
	ykuohao :={
	if styling.卡片语言=="英文" then "rbracket"
	else "rbracket2"
	}
	
	############### Type of card
	##卡片类型自动替换配置
	
	nm_ext_querry := {
		if styling.自动效果符号 == "no" then is_normal_monster_extended(value)
		else is_normal_monster(value)
	}
	is_nmonster := {
		card.card_type == "魔法" or
	        card.card_type == "陷阱"
	}

	is_monster := {
		card.card_type == "通常怪兽" or
	        card.card_type == "效果怪兽" or
	        card.card_type == "仪式怪兽" or
		card.card_type == "黑暗同调怪兽" or
	        card.card_type == "同调怪兽" or
	        card.card_type == "超量怪兽"or
	        card.card_type == "融合怪兽" or
	        card.card_type == "衍生物" or
		card.card_type == "巨神兵"        or
		card.card_type == "天空龙"	   or
		card.card_type == "翼神龙"		   or
		card.card_type == "传说之龙"
	}
	is_normal_monster_extended := {
		card.card_type == "通常怪兽" or
		card.card_type == "巨神兵"        or
		card.card_type == "天空龙"	   or
		card.card_type == "翼神龙"		   or
		card.card_type == "传说之龙"
	}
	is_normal_monster := {
		card.card_type == "通常怪兽"
	}
	is_spell_card := {
		card.card_type == "魔法"	or
		card.attribute == "魔法"	or
		card.level     == "魔法"
	}
	is_trap_card := {
	        card.card_type == "陷阱"	or
		card.attribute == "陷阱"	or
		card.level     == "陷阱"
	}

	############### Determine Card Position
	
	type_sort := {
		type := card.card_type
		if chosen(choice:"通常怪兽", type) then "A"
		else if chosen(choice:"翼神龙", type) then "B"
		else if chosen(choice:"效果怪兽", type) then "C"
		else if chosen(choice:"天空龙", type) then "D"
		else if chosen(choice:"仪式怪兽", type) then "E"
		else if chosen(choice:"巨神兵", type) then "F"
		else if chosen(choice:"融合怪兽", type) then "G"
		else if chosen(choice:"同调", type) then "H"
	    else if chosen(choice:"超量怪兽", type) then "O"
		else if chosen(choice:"黑暗同调怪兽", type) then "I"
		else if chosen(choice:"衍生物", type) then "J"
		else if chosen(choice:"传说之龙", type) then "K"
		else if chosen(choice:"魔法", type) then "L"
		else if chosen(choice:"魔法", type) then "M"
		else "N"
	}
	
	pos_of_card := {
		position(
			of: card
			in: set
			order_by: {
				type_sort() + card.monster_type + card.name + card.attribute
			}) + 1
	}
