﻿
	# 
	is_12star :={ remove_tags(card.level) == "************" }
	is_13star :={ remove_tags(card.level) == "*************" }	
	is_blmonster := {card.card_type == "dark synchro monster" or card.card_type == "xyz monster"}
	is_tw := { set.language == "TW" }
	is_cn := { set.language == "CN" }
	is_en := { set.language == "EN" }
	is_jp := { set.language == "JP" }
	is_ko := { set.language == "KO" }

	hide_gamecode := {set.hide_gamecode == "yes"}
	st_has_text :={ set.ST_mark_is_text=="yes"}
	
	get_sep := {if is_en() or is_ko() then " / " else if is_cn()then "<i>／</i>" else "／" }
	get_sepex := {
		class :=remove_tags(input);
		if class == "" then "" else get_sep()
	}
	get_lang :={ to_lower(get_lang2())}
	get_lang2 :={ set.language }
	get_lang_ex := { if is_tw() then "cn" else get_lang() }
	
	copyright_is_image := {set.copyright_images=="yes"}
	pend_image_is_small :={ set.pendulum_image_is_small=="yes"}
	no_corner := { set.no_corner=="yes"}
#	cannot_pendulum := {
#	card.card_type=="obelisk" 
#	or card.card_type=="ra" 
#	or card.card_type=="slifer" 
#	or is_blmonster()
#	or card.card_type=="spell card"
#	or card.card_type=="trap card"
#	or card.card_type=="token card"
#	or card.card_type=="transparent card"
#	}
	######################### jp small
	### auto jp monster type
	ap := {
		len := length(input);
		if len == 1 then "    "+input+"    " 
		else if len == 2 then "   "+input+"   "
		else if len == 3 then "  "+input+"  "
		else if len == 4 then " "+input+" "
		else input
	}
	small_type := {
		class :=remove_tags(input);
		if class == "効果"      then "  こう      か"
		else if class == "融合" then "  ゆう     ごう"
		else if class == "儀式" then "    ぎ     しき"
		else if class == "特殊召喚" then "   とく    しゅ    しょう    かん "
		else if class == "戦士族" then "  せん       し      ぞく"
		else if class == "アンデット族" then "                                                    ぞく"
		else if class == "悪魔族" then "   あく     ま 　   ぞく"
		else if class == "獣族" then " じゅう    ぞく"
		else if class == "獣戦士族" then " じゅう   せん       し      ぞく"
		else if class == "恐竜族" then " きょう  りゅう    ぞく"
		else if class == "魚族" then " さかな    ぞく"
		else if class == "機械族" then "   き       かい     ぞく"
		else if class == "水族" then "   みず     ぞく"
		else if class == "天使族" then "  てん      し      ぞく"
		else if class == "魔法使い族" then "   ま       ほう     つか              ぞく"
		else if class == "炎族" then " ほのお    ぞく"
		else if class == "雷族" then "いかずち   ぞく"
		else if class == "爬虫類族" then "   は     ちゅう    るい      ぞく"
		else if class == "植物族" then " しょく    ぶつ     ぞく"
		else if class == "昆虫族" then "  こん    ちゅう    ぞく"
		else if class == "岩石族" then "  がん     せき     ぞく"
		else if class == "海竜族" then "  かい    りゅう    ぞく"
		else if class == "鳥獣族" then " ちょう   じゅう    ぞく"
		else if class == "ドラゴン族" then "                                          ぞく"
		else if class == "サイキック族" then "                                                    ぞく"
		else if class == "幻神獣族" then "  げん     しん    じゅう    ぞく"
		else if class == "創造神族" then "  そう     ぞう     しん     ぞく"
		else if class == "幻竜族" then "  げん    りゅう    ぞく"
		else ""
	}
	####################################
	# 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
	name_filter := {
		if is_tw() then value #name_filter_tw(value)
		else value
	}
	name_filter_tw :=
		# step 1 : remove all automatic tags
		tag_remove_rule(tag: "<i>") 
		# step 2 : symbols
		+
		replace_rule(
			match: "[0-9|A-Z|，|。|！|？|鮟|鱇|・|]",
			replace: "<i>&</i>" 
			);
	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>" 
			);
	text_filter2 := 
		# 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>" 
		 	)+
		replace_rule(
			match: "[[:blank:]]",
			replace: "<sym-auto>^</sym-auto>" 
			);

	level_filter := 
		tag_remove_rule(tag: "<sym-auto>") +
		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 +
		{ lang:=get_lang2()
		"<word-list-"+lang+"race>{input}</word-list-"+lang+"race>" }
	
	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 +
		{  lang:=get_lang2()
		"<word-list-"+lang+"effect>{input}</word-list-"+lang+"effect>" }
	
	# Determine type of card
	card_type := {
		if	is_spell_card()						then "spell card"
		else if is_trap_card()							then "trap card"
		else if	contains(card.monster_type, match:"Fusion")						then "fusion monster"
		else if	contains(card.monster_type, match:"Ritual")						then "ritual monster"
		else if	contains(card.monster_type, match:"Dark Synchro")					then "dark synchro monster"
		else if	contains(card.monster_type, match:"Dark Tuner")						then "effect monster"
		else if	contains(card.monster_type, match:"Synchro")						then "synchro monster"
		else if	contains(card.monster_type, match:"Effect")						then "effect monster"
		else if	contains(card.monster_type, match:"Spirit")						then "effect monster"
		else if	contains(card.monster_type, match:"Toon")						then "effect monster"
		else if	contains(card.monster_type, match:"Tuner")						then "effect monster"
		else if	contains(card.monster_type, match:"Union")						then "effect monster"
		else                                         	 							"normal monster"
	}
	# Default 'attribute' of card
	attribute := {
		if is_spell_card() then "Spell"
		else if is_trap_card()  then "Trap"
		else                         "none"
	}
	# Default foil for a card
	foil := {
		if	card.rarity == "super rare"	then "holo image"
		else if card.rarity == "ultra rare"	then "holo image"
		else if card.rarity == "secret rare"	then "secret holo image"
		else if card.rarity == "ultimate rare"	then "holo image"
		else if card.rarity == "parallel rare"	then "parallel holo image"
		else if card.rarity == "gold tech"	then "gold tech holo image"
		else if card.rarity == "promo"		then "secret holo image"
		else if card.rarity == "star rare"	then "star holo image"
		else if card.rarity == "mosaic rare"	then "mosaic holo image"
		else					     "none"
	}
	
	############### Type of card
	
	nm_ext_querry := {
		if set.gods_have_effects == "no" then is_normal_monster_extended(value)
		else is_normal_monster(value)
	}
	is_nmonster := {
		card.card_type == "spell card" or
	    card.card_type == "trap card" or 
		card.card_type == "token card"
	}
	is_monster := {
		not is_nmonster()
	}
	is_normal_monster_extended := {
		card.card_type == "normal monster" or
		card.card_type == "obelisk"        or
		card.card_type == "slifer"	   or
		card.card_type == "ra"
	}
	is_normal_monster := {
		card.card_type == "normal monster"
	}
	is_spell_card := {
		card.card_type == "spell card"	or
		card.attribute == "spell"	or
		card.level     == "Spell"
	}
	is_trap_card := {
	    card.card_type == "trap card"	or
		card.attribute == "trap"	or
		card.level     == "Trap"
	}
	############### Determine Card Position
	
	type_sort := {
		type := card.card_type
		if chosen(choice:"normal monster", type) then "A"
		else if chosen(choice:"ra", type) then "B"
		else if chosen(choice:"effect monster", type) then "C"
		else if chosen(choice:"slifer", type) then "D"
		else if chosen(choice:"ritual monster", type) then "E"
		else if chosen(choice:"obelisk", type) then "F"
		else if chosen(choice:"fusion monster", type) then "G"
		else if chosen(choice:"synchro monster", type) then "H"
		else if chosen(choice:"dark synchro monster", type) then "I"
		else if chosen(choice:"token monster", type) then "J"
		else if chosen(choice:"token card", type) then "K"
		else if chosen(choice:"spell card", type) then "L"
		else if chosen(choice:"trap card", type) then "M"
		else if chosen(choice:"xyz monster", type) then "L"
		else "N"
	}
	
	pos_of_card := {
		position(
			of: card
			in: set
			order_by: {
				type_sort() + card.monster_type + card.name + card.attribute
			}) + 1
	}
