وحدة:NVR/rules

NVR url transformations used by Module:NVR

Transformations are stored in 2 tables:

  • p.ShipRules — Table of rules for transforming links to a ship in the Naval Vessel Register.
  • p.ServiceCraftRules — Table of rules for transforming links to a service craft in the Naval Vessel Register.

Each table has two entries:

  • urls - a table of named URL strings that can be referenced by the rules. The first occurrence of **** in the url will be replaced by the results of applying the rules.
  • rules — a table of rules to use to transform an NVR 'id'. The 'id' was originally the 'file name' portion of the url path (typically the ship's hull designation) without the .HTM/.HTML extension.

The heart of each transformation table is the rules parameter that specifies the rules to apply when processing an NVR 'id'. Each rule is checked in order until a matching rule is found. Only the first rule that is matched is applied. Each entry in the rules table is itself a table, of which three varieties are supported, determined by a method parameter in these tables. Supported values for method include:

  • 'literal' — if 'id' is identical to the match parameter then it is set it to the value of the replace parameter. If replace is not specified, the 'id' is not changed. The url parameter specifies which URL from the table of URLs to use.
  • 'pattern' — if 'id' matches the pattern in the match parameter then the occurrences of the pattern in the pattern parameter is replaced with the pattern in the replace parameter. If replace is not specified, the 'id' is not changed. The url parameter specifies which URL from the table of URLs to use.
  • 'all' — Matches all 'id's, and the 'id' is not changed. The url parameter specifies which URL from the table of URLs to use. In most cases each rules table should end with a rule using the 'all' method.



local p = {}

-- rules for transforming links to a ship in the Naval Vessel Register
p.ShipRules = {
	urls = {
		['old'] = 'http://www.nvr.navy.mil/NVRSHIPS/DETAILS/****.HTM',
		['new'] = 'http://www.nvr.navy.mil/SHIPDETAILS/SHIPSDETAIL_****.HTML',
	},
	rules = {
		{
			method = 'literal',
			match = 'MAINE',
			replace = '939',
			url = 'new',
		},
		{
			method = 'literal',
			match = 'MARCOS',
			replace = '940',
			url = 'new',
		},
		{
			method = 'literal',
			match = 'OLDIRON',
			replace = '1315',
			url = 'new',
		},
		{
			method = 'literal',
			match = 'AFSB15',
			replace = 'AFSB_(I)_15',
			url = 'new',
		},
		{
			method = 'pattern',
			match = '^[A-Z]+[0-9]+$',
			pattern = '^([A-Z]+)([0-9]+)$',
			replace = '%1_%2',
			url = 'new',
		},
		{
			method = 'all',
			url = 'new',
		},
	}
}

-- rules for transforming links to a service craft in the Naval Vessel Register
p.ServiceCraftRules = {
	urls = {
		['old'] = 'http://www.nvr.navy.mil/NVRSERVICECRAFT/DETAILS/****.HTM',
		['new'] = 'http://www.nvr.navy.mil/SHIPDETAILS/SHIPSDETAIL_****.HTML',
	},
	rules = {
		{
			method = 'literal',
			match = 'AFDB7_1',
			replace = 'AFDB_7',
			url = 'new',
		},
		{
			method = 'literal',
			match = 'AFDB1_1',
			url = 'old',
		},
		{
			method = 'literal',
			match = 'AFDB1_3',
			url = 'old',
		},
		{
			method = 'literal',
			match = 'AFDB1_4',
			url = 'old',
		},
		{
			method = 'literal',
			match = 'AFDB7_2',
			url = 'old',
		},
		{
			method = 'pattern',
			match = '^[A-Z]+[0-9]+$',
			pattern = '^([A-Z]+)([0-9]+)$',
			replace = '%1_%2',
			url = 'new',
		},
		{
			method = 'all',
			url = 'new',
		},
	}
}
 
 
return p