Modul:Country extract

local p = {}
--
-- to enable us to replicate the current functioning of CountryAbbr and CountryAbbr2
-- We need to deal with 
-- 1 alternative names ISO 3166 should do that
-- 2 {{<name>}}
-- 3 [ [<name>] ]
-- 4 [ [<name>|<junk>] ]
-- 5 [ [image:flag of <country>.[svg|gif|png|jpg]|\d+px] ]
--

function p.extractCountry(frame)
   local string= mw.ustring.toNFC (frame.args[1])
   local match=nil;
   match = mw.ustring.match(string, "Bendera ([^\.]*)")
   if (match) then 
      return match
   end 
-- () for Cocos (Keeling) Islands
-- ' For  People's 
-- . for U.S. etc.
   match = mw.ustring.match(string, "(%u[%a\(\)\.' -]+)")
   if (match == "Image") then
       string = mw.ustring.gsub(string, match, "")
       match = mw.ustring.match(string, "[\|\[](%u[%a\(\)\.' -]+)")
   end
   if (match == "20px") then
       string = mw.ustring.gsub(string, match, "")
       match = mw.ustring.match(string, "\[(%u[%a\(\)\.' -]+)")
   end 
   if (match) then 
      return match
   end
   return  string
end

--[[ 


]]

function p.extractSubdivision(frame)
   local string= mw.ustring.toNFC (frame.args[1])
   local match=nil;
-- Needed for {{flag|Greenland}},
   match = mw.ustring.match(string, "Bendera ([^\.]*)")
   if (match) then 
      return match
   end 
-- . needed for Washington D.C. 
-- ' for cote d'azur
-- leading ' for 'Aden
-- ‘ and trailing ' for Şan‘ā'
   match = mw.ustring.match(string, "('?[%u][%a'‘ \.\,-]+[%a\.'\d]+)")
   if (match) then 
      return match
   end
   return  string
end

local countries = {
	["Andorra"] = "AD",
	["Uni Emirat Arab"] = "AE",
	["UAE"] = "AE",
	["Afganistan"] = "AF",
	["Antigua dan Barbuda"] = "AG",
	["Anguilla"] = "AI",
	["Albania"] = "AL",
	["Armenia"] = "AM",
	["Angola"] = "AO",
	["Antarktika"] = "AQ",
	["Argentina"] = "AR",
	["Samoa Amerika"] = "AS",
	["Austria"] = "AT",
	["Australia"] = "AU",
	["Aruba"] = "AW",
	["Kepulauan Åland"] = "AX",
	["Kepulauan Aland"] = "AX",
	["Azerbaijan"] = "AZ",
	["Nagorno-Karabakh"] = "AZ", -- disputed
	["Republik Nagorno-Karabakh"] = "AZ", -- disputed
	["Bosnia dan Herzegovina"] = "BA",
	["Bosnia"] = "BA",
	["Barbados"] = "BB",
	["Bangladesh"] = "BD",
	["Belgia"] = "BE",
	["Burkina Faso"] = "BF",
	["Bulgaria"] = "BG",
	["Bahrain"] = "BH",
	["Burundi"] = "BI",
	["Benin"] = "BJ",
	["Saint Barthélemy"] = "BL",
	["Saint Barthelemy"] = "BL",
	["St. Barthélemy"] = "BL",
	["St. Barthelemy"] = "BL",
	["St Barthélemy"] = "BL",
	["St Barthelemy"] = "BL",
	["Bermuda"] = "BM",
	["Brunei"] = "BN",
	["Brunei Darussalam"] = "BN",
	["Bolivia"] = "BO",
	["Negara Plurinasional Bolivia"] = "BO",
	["Bonaire, Sint Eustatius dan Saba"] = "BQ",
	["Brasil"] = "BR",
	["Bahama"] = "BS",
	["The Bahamas"] = "BS",
	["the Bahamas"] = "BS",
	["Bhutan"] = "BT",
	["Pulau Bouvet"] = "BV",
	["Botswana"] = "BW",
	["Belarusia"] = "BY",
	["Belize"] = "BZ",
	["Kanada"] = "CA",
	["Kepulauan Cocos (Keeling)"] = "CC",
	["Kepulauan Cocos"] = "CC",
	["Kepulauan Keeling"] = "CC",
	["Republik Demokratik Kongo"] = "CD",
	["Republik Demokratik Congo"] = "CD",
	["DR Congo"] = "CD",
	["Congo-Kinshasa"] = "CD",
	["DROC"] = "CD",
	["DRC"] = "CD",
	["Republik Afrika Tengah"] = "CF",
	["CAR"] = "CF",
	["Congo"] = "CG",
	["Republik Kongo"] = "CG",
	["Congo Republic"] = "CG",
	["Congo-Brazzaville"] = "CG",
	["Kongo Barat"] = "CG",
	["Swiss"] = "CH",
	["Côte d'Ivoire"] = "CI",
	["Pantai Gading"] = "CI",
	["Kepulauan Cook"] = "CK",
	["Chili"] = "CL",
	["Kamerun"] = "CM",
	["China"] = "CN",
	["RRT"] = "CN",
	["Republik Rakyat Tiongkok"] = "CN",
	["Kolombia"] = "CO",
	["Kosta Rika"] = "CR",
	["Kuba"] = "CU",
	["Tanjung Verde"] = "CV",
	["Curaçao"] = "CW",
	["Curacao"] = "CW",
	["Pulau Natal"] = "CX",
	["Siprus"] = "CY",
	["Republik Cyprus"] = "CY",
	["Siprus Utara"] = "CY", -- disputed
	["Republik Turki Siprus Utara"] = "CY", -- disputed
	["Republik Ceko"] = "CZ",
	["Jerman"] = "DE",
	["Djibouti"] = "DJ",
	["Denmark"] = "DK",
	["Dominica"] = "DM",
	["Dominican Republic"] = "DO",
	["Algeria"] = "DZ",
	["Ecuador"] = "EC",
	["Estonia"] = "EE",
	["Mesir"] = "EG",
	["Sahara Barat"] = "EH",
	["Eritrea"] = "ER",
	["Spanyol"] = "ES",
	["Ethiopia"] = "ET",
	["Finlandia"] = "FI",
	["Fiji"] = "FJ",
	["Kepulauan Falkland (Malvinas)"] = "FK",
	["Falklands"] = "FK",
	["Kepulauan Falkland"] = "FK",
	["Malvinas"] = "FK",
	["Kepulauan Malvinas"] = "FK",
	["Federasi Mikronesia"] = "FM",
	["Mikronesia"] = "FM",
	["Kepulauan Faroe"] = "FO",
	["Perancis"] = "FR",
	["Gabon"] = "GA",
	["Britania Raya"] = "GB",
	["the United Kingdom"] = "GB",
	["U. K."] = "GB",
	["U.K."] = "GB",
	["the U.K."] = "GB",
	["UK"] = "GB",
	["the UK"] = "GB",
	["Kerajaan Persatuan"] = "GB",
	["Inggris"] = "GB-ENG",
	["Skotlandia"] = "GB-SCT",
	["Wales"] = "GB-WLS",
	["Irlanda Utara"] = "GB-NIR",
	["Inggris dan Wales"] = "GB-EAW",
	["Grenada"] = "GD",
	["Georgia"] = "GE",
	["Ossetia Selatan"] = "GE", -- disputed
	["Wilayah Tskhinvali"] = "GE", -- disputed
	["Abkhazia"] = "GE-AB", -- disputed
	["Republik Abkhazia"] = "GE-AB", -- disputed
	["Guyana Perancis"] = "GF",
	["Guernsey"] = "GG",
	["Ghana"] = "GH",
	["Gibraltar"] = "GI",
	["Greenland"] = "GL",
	["Gambia"] = "GM",
	["Guinea"] = "GN",
	["Guadeloupe"] = "GP",
	["Guinea Ekuator"] = "GQ",
	["Yunani"] = "GR",
	["Kepulauan Georgia Selatan dan Sandwich Selatan"] = "GS",
	["Guatemala"] = "GT",
	["Guam"] = "GU",
	["Guinea-Bissau"] = "GW",
	["Guyana"] = "GY",
	["Hong Kong"] = "HK",
	["Pulau Heard dan Kepulauan McDonald"] = "HM",
	["Kepulauan Heard dan McDonald"] = "HM",
	["Honduras"] = "HN",
	["Kroasia"] = "HR",
	["Haiti"] = "HT",
	["Hongaria"] = "HU",
	["Indonesia"] = "ID",
	["Irlanda"] = "IE",
	["Israel"] = "IL",
	["Pulau Man"] = "IM",
	["India"] = "IN",
	["Wilayah Samudra Hindia Britania"] = "IO",
	["Irak"] = "IQ",
	["Republik Islam Iran"] = "IR",
	["Iran"] = "IR",
	["Islandia"] = "IS",
	["Italia"] = "IT",
	["Jersey"] = "JE",
	["Jamaika"] = "JM",
	["Yordana"] = "JO",
	["Jepang"] = "JP",
	["Kenya"] = "KE",
	["Kirgistan"] = "KG",
	["Kamboja"] = "KH",
	["Kiribati"] = "KI",
	["Komoro"] = "KM",
	["The Comoros"] = "KM",
	["the Comoros"] = "KM",
	["Saint Kitts dan Nevis"] = "KN",
	["St. Kitts and Nevis"] = "KN",
	["St Kitts and Nevis"] = "KN",
	["Korea, Democratic People's Republic of"] = "KP",
	["Republik Rakyat Demokratik Korea"] = "KP",
	["Korea Utara"] = "KP",
	["Korea, Republic of"] = "KR",
	["Republik Korea"] = "KR",
	["Korea Selatan"] = "KR",
	["Kuwait"] = "KW",
	["Kepulauan Cayman"] = "KY",
	["The Cayman Islands"] = "KY",
	["the Cayman Islands"] = "KY",
	["Kazakhstan"] = "KZ",
	["Republik Rakyat Demokratik Laos"] = "LA",
	["Laos"] = "LA",
	["Lao"] = "LA",
	["Lebanon"] = "LB",
	["Saint Lucia"] = "LC",
	["St. Lucia"] = "LC",
	["St Lucia"] = "LC",
	["Liechtenstein"] = "LI",
	["Sri Lanka"] = "LK",
	["Liberia"] = "LR",
	["Lesotho"] = "LS",
	["Lithuania"] = "LT",
	["Luksemburg"] = "LU",
	["Latvia"] = "LV",
	["Libyan Arab Jamahiriya"] = "LY",
	["Libya"] = "LY",
	["Maroko"] = "MA",
	["Monako"] = "MC",
	["Republik Moldova"] = "MD",
	["Moldova"] = "MD",
	["Transnistria"] = "MD", -- disputed
	["Montenegro"] = "ME",
	["Saint Martin (bagian Perancis)"] = "MF",
	["Saint Martin (Perancis)"] = "MF",
	["Kolektivitas Saint Martin"] = "MF",
	["Collectivity of St. Martin"] = "MF",
	["Collectivity of St Martin"] = "MF",
	["Saint-Martin"] = "MF",
	["Madagaskar"] = "MG",
	["Kepulauan Marshall"] = "MH",
	["Makedonia"] = "MK",
	["Mali"] = "ML",
	["Myanmar"] = "MM",
	["Burma"] = "MM",
	["Mongolia"] = "MN",
	["Makau"] = "MO",
	["Macao"] = "MO",
	["Kepulauan Mariana Utara"] = "MP",
	["Martinik"] = "MQ",
	["Mauritania"] = "MR",
	["Montserrat"] = "MS",
	["Malta"] = "MT",
	["Mauritius"] = "MU",
	["Maladewa"] = "MV",
	["Malawi"] = "MW",
	["Meksiko"] = "MX",
	["Malaysia"] = "MY",
	["Mozambik"] = "MZ",
	["Namibia"] = "NA",
	["Kaledonia Baru"] = "NC",
	["Niger"] = "NE",
	["Pulau Norfolk"] = "NF",
	["Nigeria"] = "NG",
	["Nikaragua"] = "NI",
	["Belanda"] = "NL",
	["The Netherlands"] = "NL",
	["the Netherlands"] = "NL",
	["Norwegia"] = "NO",
	["Nepal"] = "NP",
	["Nauru"] = "NR",
	["Niue"] = "NU",
	["Selandia Baru"] = "NZ",
	["Aotearoa"] = "NZ",
	["Oman"] = "OM",
	["Panama"] = "PA",
	["Peru"] = "PE",
	["Polinesia Perancis"] = "PF",
	["Papua Nugini"] = "PG",
	["Filipina"] = "PH",
	["The Philippines"] = "PH",
	["the Philippines"] = "PH",
	["Pakistan"] = "PK",
	["Polanda"] = "PL",
	["Saint Pierre dan Miquelon"] = "PM",
	["St. Pierre and Miquelon"] = "PM",
	["St Pierre and Miquelon"] = "PM",
	["Pitcairn"] = "PN",
	["Puerto Riko"] = "PR",
	["Negara Palestina"] = "PS",
	["Palestina"] = "PS",
	["Portugal"] = "PT",
	["Palau"] = "PW",
	["Paraguay"] = "PY",
	["Qatar"] = "QA",
	["Réunion"] = "RE",
	["Reunion"] = "RE",
	["Rumania"] = "RO",
	["Serbia"] = "RS",
	["Kosovo"] = "RS", -- disputed
	["Federasi Rusia"] = "RU",
	["Rusia"] = "RU",
	["Rwanda"] = "RW",
	["Arab Saudi"] = "SA",
	["Kepualauan Solomon"] = "SB",
	["Seychelles"] = "SC",
	["Sudan"] = "SD",
	["Swedia"] = "SE",
	["Singapura"] = "SG",
	["Saint Helena, Ascension dan Tristan da Cunha"] = "SH",
	["Slovenia"] = "SI",
	["Svalbard dan Jan Mayen"] = "SJ",
	["Slovakia"] = "SK",
	["Sierra Leone"] = "SL",
	["San Marino"] = "SM",
	["Senegal"] = "SN",
	["Somalia"] = "SO",
	["Somaliland"] = "SO", -- disputed
	["Suriname"] = "SR",
	["Sudan Selatan"] = "SS",
	["São Tomé dan Príncipe"] = "ST",
	["Sao Tome dan Principe"] = "ST",
	["El Salvador"] = "SV",
	["Sint Maarten"] = "SX",
	["Saint Martin (Dutch part)"] = "SX",
	["Saint Martin (Netherlands)"] = "SX",
	["Republik Arab Suriah"] = "SY",
	["Suriah"] = "SY",
	["Eswatini"] = "SZ",
	["Kepulauan Turks dan Caicos"] = "TC",
	["Chad"] = "TD",
	["Wilayah Selatan Perancis"] = "TF",
	["Togo"] = "TG",
	["Thailand"] = "TH",
	["Tajikistan"] = "TJ",
	["Tokelau"] = "TK",
	["Timor-Leste"] = "TL",
	["Timor Timur"] = "TL",
	["Turkmenistan"] = "TM",
	["Tunisia"] = "TN",
	["Tonga"] = "TO",
	["Turki"] = "TR",
	["Trinidad dan Tobago"] = "TT",
	["Tuvalu"] = "TV",
	["Taiwan"] = "TW",
	["Republik Tiongkok"] = "TW",
	["Republik Persatuan Tanzania"] = "TZ",
	["Tanzania"] = "TZ",
	["Ukraina"] = "UA",
	["The Ukraine"] = "UA",
	["the Ukraine"] = "UA",
	["Uganda"] = "UG",
	["Kepulauan Terluar Kecil Amerika Serikat"] = "UM",
	["Amerika Serikat"] = "US",
	["the United States"] = "US",
	["United States of America"] = "US",
	["the United States of America"] = "US",
	["U. S."] = "US",
	["U.S."] = "US",
	["the U.S."] = "US",
	["US"] = "US",
	["the US"] = "US",
	["Uruguay"] = "UY",
	["Uzbekistan"] = "UZ",
	["Tahta Suci (Negara Kota Vatikan)"] = "VA",
	["Tahta Suci"] = "VA",
	["Negara Kota Vatikan"] = "VA",
	["Kota Vatikan"] = "VA",
	["Saint Vincent dan Grenadines"] = "VC",
	["St. Vincent and the Grenadines"] = "VC",
	["St Vincent and the Grenadines"] = "VC",
	["Republik Bolivar Venezuela"] = "VE",
	["Venezuela"] = "VE",
	["Virgin Islands, British"] = "VG",
	["Kepulauan Virgin Britania Raya"] = "VG",
	["UK Virgin Islands"] = "VG",
	["Virgin Islands of the United Kingdom"] = "VG",
	["Virgin Islands of the UK"] = "VG",
	["Virgin Islands, U. S."] = "VI",
	["Virgin Islands, U.S."] = "VI",
	["Virgin Islands, US"] = "VI",
	["Kepulauan Virgin Amerika Serikat"] = "VI",
	["American Virgin Islands"] = "VI",
	["U. S. Virgin Islands"] = "VI",
	["U.S. Virgin Islands"] = "VI",
	["US Virgin Islands"] = "VI",
	["Virgin Islands of the United States"] = "VI",
	["Virgin Islands of the U.S."] = "VI",
	["Virgin Islands of the US"] = "VI",
	["Vietnam"] = "VN",
	["Viet Nam"] = "VN",
	["Vanuatu"] = "VU",
	["Wallis dan Futuna"] = "WF",
	["Samoa"] = "WS",
	["Yaman"] = "YE",
	["Mayotte"] = "YT",
	["Afrika Selatan"] = "ZA",
	["Zambia"] = "ZM",
	["Zimbabwe"] = "ZW",
	["ABW"] = "AW",
  	["AFG"] = "AF",
  	["AGO"] = "AO",
  	["AIA"] = "AI",
  	["ALA"] = "AX",
  	["ALB"] = "AL",
  	["AND"] = "AD",
  	["ARE"] = "AE",
  	["ARG"] = "AR",
  	["ARM"] = "AM",
  	["ASM"] = "AS",
  	["ATA"] = "AQ",
  	["ATF"] = "TF",
  	["ATG"] = "AG",
  	["AUS"] = "AU",
  	["AUT"] = "AT",
  	["AZE"] = "AZ",
  	["BDI"] = "BI",
  	["BEL"] = "BE",
  	["BEN"] = "BJ",
  	["BFA"] = "BF",
  	["BGD"] = "BD",
  	["BGR"] = "BG",
  	["BHR"] = "BH",
  	["BHS"] = "BS",
  	["BIH"] = "BA",
  	["BLM"] = "BL",
  	["BLR"] = "BY",
  	["BLZ"] = "BZ",
  	["BMU"] = "BM",
  	["BOL"] = "BO",
  	["BRA"] = "BR",
  	["BRB"] = "BB",
  	["BRN"] = "BN",
  	["BTN"] = "BT",
  	["BVT"] = "BV",
  	["BWA"] = "BW",
  	["CAF"] = "CF",
  	["CAN"] = "CA",
  	["CCK"] = "CC",
  	["CHE"] = "CH",
  	["CHL"] = "CL",
  	["CHN"] = "CN",
  	["CIV"] = "CI",
  	["CMR"] = "CM",
  	["COD"] = "CG",
  	["COG"] = "CG",
  	["COK"] = "CK",
  	["COL"] = "CO",
  	["COM"] = "KM",
  	["CRI"] = "CR",
  	["CUB"] = "CU",
  	["CUW"] = "CW",
  	["CXR"] = "CX",
  	["CYM"] = "KY",
  	["CYP"] = "CY",
  	["CZE"] = "CZ",
  	["DEU"] = "DE",
  	["DJI"] = "DJ",
  	["DMA"] = "DM",
  	["DNK"] = "DK",
  	["DOM"] = "DO",
  	["DZA"] = "DZ",
  	["ECU"] = "EC",
  	["EGY"] = "EG",
  	["ERI"] = "ER",
  	["ESH"] = "EH",
  	["ESP"] = "ES",
  	["EST"] = "EE",
  	["ETH"] = "ET",
  	["FIN"] = "FI",
  	["FJI"] = "FJ",
  	["FLK"] = "FK",
  	["FRA"] = "FR",
  	["FRO"] = "FO",
  	["FSM"] = "FM",
  	["GAB"] = "GA",
  	["GBR"] = "GB",
  	["GEO"] = "GE",
  	["GGY"] = "GG",
  	["GHA"] = "GH",
  	["GIB"] = "GI",
  	["GIN"] = "GN",
  	["GLP"] = "GP",
  	["GMB"] = "GM",
  	["GNB"] = "GW",
  	["GNQ"] = "GQ",
  	["GRC"] = "GR",
  	["GRD"] = "GD",
  	["GRL"] = "GL",
  	["GTM"] = "GT",
  	["GUF"] = "GF",
  	["GUM"] = "GU",
  	["GUY"] = "GY",
  	["HKG"] = "HK",
  	["HMD"] = "HM",
  	["HND"] = "HN",
  	["HRV"] = "HR",
  	["HTI"] = "HT",
  	["HUN"] = "HU",
  	["IDN"] = "ID",
  	["IMN"] = "IM",
  	["IND"] = "IN",
  	["IOT"] = "IO",
  	["IRL"] = "IE",
  	["IRN"] = "IR",
  	["IRQ"] = "IQ",
  	["ISL"] = "IS",
  	["ISR"] = "IL",
  	["ITA"] = "IT",
  	["JAM"] = "JM",
  	["JEY"] = "JE",
  	["JOR"] = "JO",
  	["JPN"] = "JP",
  	["KAZ"] = "KZ",
  	["KEN"] = "KE",
  	["KGZ"] = "KG",
  	["KHM"] = "KH",
  	["KIR"] = "KI",
  	["KNA"] = "KN",
  	["KWT"] = "KW",
  	["LAO"] = "LA",
  	["LBN"] = "LB",
  	["LBR"] = "LR",
  	["LBY"] = "LY",
  	["LCA"] = "LC",
  	["LIE"] = "LI",
  	["LKA"] = "LK",
  	["LSO"] = "LS",
  	["LTU"] = "LT",
  	["LUX"] = "LU",
  	["LVA"] = "LV",
  	["MAC"] = "MO",
  	["MAF"] = "MF",
  	["MAR"] = "MA",
  	["MCO"] = "MC",
  	["MDA"] = "MD",
  	["MDG"] = "MG",
  	["MDV"] = "MV",
  	["MEX"] = "MX",
  	["MHL"] = "MH",
  	["MKD"] = "MK",
  	["MLI"] = "ML",
  	["MLT"] = "MT",
  	["MMR"] = "MM",
  	["MNE"] = "ME",
  	["MNG"] = "MN",
  	["MNP"] = "MP",
  	["MOZ"] = "MZ",
  	["MRT"] = "MR",
  	["MSR"] = "MS",
  	["MTQ"] = "MQ",
  	["MUS"] = "MU",
  	["MWI"] = "MW",
  	["MYS"] = "MY",
  	["MYT"] = "YT",
  	["NAM"] = "NA",
  	["NCL"] = "NC",
  	["NER"] = "NE",
  	["NFK"] = "NF",
  	["NGA"] = "NG",
  	["NIC"] = "NI",
  	["NIU"] = "NU",
  	["NLD"] = "NL",
  	["NOR"] = "NO",
  	["NPL"] = "NP",
  	["NRU"] = "NR",
  	["NZL"] = "NZ",
  	["OMN"] = "OM",
  	["PAK"] = "PK",
  	["PAN"] = "PA",
  	["PCN"] = "PN",
  	["PER"] = "PE",
  	["PHL"] = "PH",
  	["PLW"] = "PW",
  	["PNG"] = "PG",
  	["POL"] = "PL",
  	["PRI"] = "PR",
  	["PRT"] = "PT",
  	["PRY"] = "PY",
  	["PSE"] = "PS",
  	["PYF"] = "PF",
  	["QAT"] = "QA",
  	["REU"] = "RE",
  	["ROU"] = "RO",
  	["RUS"] = "RU",
  	["RWA"] = "RW",
  	["SAU"] = "SA",
  	["SDN"] = "SD",
  	["SEN"] = "SN",
  	["SGP"] = "SG",
  	["SGS"] = "GS",
  	["SJM"] = "SJ",
  	["SLB"] = "SB",
  	["SLE"] = "SL",
  	["SLV"] = "SV",
  	["SMR"] = "SM",
  	["SOM"] = "SO",
  	["SPM"] = "PM",
  	["SRB"] = "RS",
  	["SSD"] = "SS",
  	["STP"] = "ST",
  	["SUR"] = "SR",
  	["SVK"] = "SK",
  	["SVN"] = "SI",
  	["SWE"] = "SE",
  	["SWZ"] = "SZ",
  	["SXM"] = "SX",
  	["SYC"] = "SC",
  	["SYR"] = "SY",
  	["TCA"] = "TC",
  	["TCD"] = "TD",
  	["TGO"] = "TG",
  	["THA"] = "TH",
  	["TJK"] = "TJ",
  	["TKL"] = "TK",
  	["TKM"] = "TM",
  	["TLS"] = "TL",
  	["TON"] = "TO",
  	["TTO"] = "TT",
  	["TUN"] = "TN",
  	["TUR"] = "TR",
  	["TUV"] = "TV",
  	["TWN"] = "TW",
  	["TZA"] = "TZ",
  	["UGA"] = "UG",
  	["UKR"] = "UA",
  	["UMI"] = "UM",
  	["URY"] = "UY",
  	["USA"] = "US",
  	["UZB"] = "UZ",
  	["VAT"] = "VA",
  	["VCT"] = "VC",
  	["VEN"] = "VE",
  	["VNM"] = "VN",
  	["VUT"] = "VU",
  	["WLF"] = "WF",
  	["WSM"] = "WS",
  	["YEM"] = "YE",
  	["ZAF"] = "ZA",
  	["ZMB"] = "ZM",
  	["ZWE"] = "ZW"
	}

function p.main(frame)
	local country = mw.ustring.gsub(mw.ustring.toNFC(frame.args[1] or ''), '^%s*(.-)%s*$', '%1')
	local match1 = ''
	if country and country ~= '' then
		match1 = mw.ustring.match(country, "Bendera ([^\.]*)") or mw.ustring.match(country, "Flag of ([^\.]*)") or mw.ustring.match(country, "^[Tt]he %A*(%u[%a%(%)%.' %-]+)") or mw.ustring.match(country, "(%u[%a%(%)%.' %-]+)") or ""
		if (match1 == "Image") or (match1 == "File") then
			country = mw.ustring.gsub(country, match1, "")
			match1 = mw.ustring.match(country, "[%|%[](%u[%a%(%)%.' %-]+)") or ""
		end
		if mw.ustring.match(match1, "^%d*x?%d+px$") then
			country = mw.ustring.gsub(country, match1, "")
			match1 = mw.ustring.match(country, "%[(%u[%a%(%)%.' %-]+)") or ""
		end
	end
	local code = countries[match1] or countries[mw.ustring.gsub(match1, " %(.*%)$", "")] or countries[mw.ustring.gsub(match1, ",.*$", "")] or countries[mw.ustring.gsub(match1, " %(.*$", "")]
	if not code then
		if match1 ~= '' and mw.title.new('Template:ISO 3166 code ' .. match1).exists then
			code = frame:expandTemplate{ title = 'Template:ISO 3166 code ' .. match1 }
		else
			if frame.args.nocat and frame.args.nocat == 'true' then
				return ''
			end
			local ns = mw.title.getCurrentTitle().namespace
			if ns == 0 then
				return '[[Kategori:Halaman Wikipedia dengan negara yang tidak jelas]]'
			else
				return '[[Kategori:Halaman Wikipedia dengan negara yang tidak jelas|'.. frame:expandTemplate{ title = 'Namespace Greek' } ..']]'
			end
		end
	end
	local subdivision = mw.ustring.gsub(mw.ustring.toNFC(frame.args[2] or ''), '^%s*(.-)%s*$', '%1')
	if not subdivision or subdivision == '' then return code end
	local countrytables = {
		["AD"] = true,
		["AE"] = true,
		["AF"] = true,
		["AG"] = true,
		["AL"] = true,
		["AM"] = true,
		["AO"] = true,
		["AR"] = true,
		["AT"] = true,
		["AU"] = true,
		["AZ"] = true,
		["BA"] = true,
		["BB"] = true,
		["BD"] = true,
		["BE"] = true,
		["BF"] = true,
		["BG"] = true,
		["BH"] = true,
		["BI"] = true,
		["BJ"] = true,
		["BN"] = true,
		["BO"] = true,
		["BQ"] = true,
		["BR"] = true,
		["BS"] = true,
		["BT"] = true,
		["BW"] = true,
		["BY"] = true,
		["BZ"] = true,
		["CA"] = true,
		["CD"] = true,
		["CF"] = true,
		["CG"] = true,
		["CH"] = true,
		["CI"] = true,
		["CL"] = true,
		["CM"] = true,
		["CN"] = true,
		["CO"] = true,
		["CR"] = true,
		["CU"] = true,
		["CV"] = true,
		["CY"] = true,
		["CZ"] = true,
		["DE"] = true,
		["DJ"] = true,
		["DK"] = true,
		["DM"] = true,
		["DO"] = true,
		["DZ"] = true,
		["EC"] = true,
		["EE"] = true,
		["EG"] = true,
		["ER"] = true,
		["ES"] = true,
		["ET"] = true,
		["FI"] = true,
		["FJ"] = true,
		["FM"] = true,
		["FR"] = true,
		["GA"] = true,
		["GB"] = true,
		["GD"] = true,
		["GE"] = true,
		["GH"] = true,
		["GL"] = true,
		["GM"] = true,
		["GN"] = true,
		["GQ"] = true,
		["GR"] = true,
		["GT"] = true,
		["GW"] = true,
		["GY"] = true,
		["HN"] = true,
		["HR"] = true,
		["HT"] = true,
		["HU"] = true,
		["ID"] = true,
		["IE"] = true,
		["IL"] = true,
		["IN"] = true,
		["IQ"] = true,
		["IR"] = true,
		["IS"] = true,
		["IT"] = true,
		["JM"] = true,
		["JO"] = true,
		["JP"] = true,
		["KE"] = true,
		["KG"] = true,
		["KH"] = true,
		["KI"] = true,
		["KM"] = true,
		["KN"] = true,
		["KP"] = true,
		["KR"] = true,
		["KW"] = true,
		["KZ"] = true,
		["LA"] = true,
		["LB"] = true,
		["LC"] = true,
		["LI"] = true,
		["LK"] = true,
		["LR"] = true,
		["LS"] = true,
		["LT"] = true,
		["LU"] = true,
		["LV"] = true,
		["LY"] = true,
		["MA"] = true,
		["MC"] = true,
		["MD"] = true,
		["ME"] = true,
		["MG"] = true,
		["MH"] = true,
		["MK"] = true,
		["ML"] = true,
		["MM"] = true,
		["MN"] = true,
		["MR"] = true,
		["MT"] = true,
		["MU"] = true,
		["MV"] = true,
		["MW"] = true,
		["MX"] = true,
		["MY"] = true,
		["MZ"] = true,
		["NA"] = true,
		["NE"] = true,
		["NG"] = true,
		["NI"] = true,
		["NL"] = true,
		["NO"] = true,
		["NP"] = true,
		["NR"] = true,
		["NZ"] = true,
		["OM"] = true,
		["PA"] = true,
		["PE"] = true,
		["PG"] = true,
		["PH"] = true,
		["PK"] = true,
		["PL"] = true,
		["PS"] = true,
		["PT"] = true,
		["PW"] = true,
		["PY"] = true,
		["QA"] = true,
		["RO"] = true,
		["RS"] = true,
		["RU"] = true,
		["RW"] = true,
		["SA"] = true,
		["SB"] = true,
		["SC"] = true,
		["SD"] = true,
		["SE"] = true,
		["SG"] = true,
		["SH"] = true,
		["SI"] = true,
		["SK"] = true,
		["SL"] = true,
		["SM"] = true,
		["SN"] = true,
		["SO"] = true,
		["SR"] = true,
		["SS"] = true,
		["ST"] = true,
		["SV"] = true,
		["SY"] = true,
		["SZ"] = true,
		["TD"] = true,
		["TG"] = true,
		["TH"] = true,
		["TJ"] = true,
		["TL"] = true,
		["TM"] = true,
		["TN"] = true,
		["TO"] = true,
		["TR"] = true,
		["TT"] = true,
		["TV"] = true,
		["TW"] = true,
		["TZ"] = true,
		["UA"] = true,
		["UG"] = true,
		["UM"] = true,
		["US"] = true,
		["UY"] = true,
		["UZ"] = true,
		["VC"] = true,
		["VE"] = true,
		["VN"] = true,
		["VU"] = true,
		["WF"] = true,
		["WS"] = true,
		["YE"] = true,
		["ZA"] = true,
		["ZM"] = true,
		["ZW"] = true,
		}
	local match2 = mw.ustring.match(subdivision, "Bendera ([^\.]*)") or mw.ustring.match(subdivision, "Flag of ([^\.]*)") or mw.ustring.match(subdivision, "[Tt]he %A*([ǁ'‘ʻ]?%u[%aZ̧z̄'‘‘ %.%,%-]+[%a%.'%d]+)") or mw.ustring.match(subdivision, "([ǁ'‘ʻ]?%u[%aZ̧z̄'‘‘ %.%,%-]+[%a%.'%d]+)") or ""
	if (match2 == "Image") or (match2 == "File") then
		subdivision = mw.ustring.gsub(subdivision, match2, "")
		match2 = mw.ustring.match(subdivision, "[\|\[]([ǁ'‘ʻ]?%u[%aZ̧z̄'‘ʻ %.%,%-]+[%a%.'%d]+)") or ""
	end
	if mw.ustring.match(match2, "^%d*x?%d+px$") then
		subdivision = mw.ustring.gsub(subdivision, match2, "")
		match2 = mw.ustring.match2(subdivision, "%[([ǁ'‘ʻ]?%u[%aZ̧z̄'‘ʻ %.%,%-]+[%a%.'%d]+)") or ""
	end
	local code1 = mw.ustring.sub(code, 1, 2)
	if countrytables[code1] then
		local tmp = mw.loadData('Module:Country extract/' .. code1)
		local code2 = tmp[match2] or tmp[mw.ustring.gsub(match2, " %(.*%)$", "")] or tmp[mw.ustring.gsub(match2, ",.*$", "")] or tmp[mw.ustring.gsub(match2, " %(.*$", "")]
		if code2 then return code2 end
		
		local legacytemplate = 'Template:ISO 3166 code ' .. match1
		if mw.title.new(legacytemplate).exists then
			tmp = nil
			for k, v in ipairs({
				frame:expandTemplate{ title = legacytemplate, args = { mw.ustring.gsub(match2, " %(.*%)$", "") } },
				frame:expandTemplate{ title = legacytemplate, args = { mw.ustring.gsub(match2, ",.*$", "") } },
				frame:expandTemplate{ title = legacytemplate, args = { mw.ustring.gsub(match2, " %(.*$", "") } },
				frame:expandTemplate{ title = legacytemplate, args = { match2 } }
			}) do
				if v ~= code and v ~= '' then
					tmp = v
					break
				end
			end
			if tmp then return tmp end
		end
		if frame.args.nocat and frame.args.nocat == 'true' then
			return code
		end
		local ns = mw.title.getCurrentTitle().namespace
		if ns == 0 then
			return code .. '[[Kategori:Halaman Wikipedia dengan negara atau subdivisi yang tidak jelas]]'
		else
			return code .. '[[Kategori:Halaman Wikipedia dengan negara atau subdivisi yang tidak jelas|'.. frame:expandTemplate{ title = 'Namespace Greek' } ..']]'
		end
	end
	return code
end

return p