Difference between revisions of "Module:Check bibcode"

From blackwiki
Jump to navigation Jump to search
blackwiki>Trappist the monk
(create;)
 
blackwiki>Trappist the monk
(delete cs1|2 specific code;)
Line 1: Line 1:
-- This module is mostly a copy of the bibcode validation used in [[Module:Citation/CS1]]
 
 
 
require('Module:No globals');
 
require('Module:No globals');
 
local p = {}
 
local p = {}
Line 6: Line 4:
 
--[[--------------------------< B I B C O D E >--------------------------------------------------------------------
 
--[[--------------------------< B I B C O D E >--------------------------------------------------------------------
  
Validates (sort of) and formats a bibcode id.
+
Validates (sort of) a bibcode id.
  
 
Format for bibcodes is specified here: http://adsabs.harvard.edu/abs_doc/help_pages/data.html#bibcodes
 
Format for bibcodes is specified here: http://adsabs.harvard.edu/abs_doc/help_pages/data.html#bibcodes
Line 24: Line 22:
  
 
local function bibcode (id)
 
local function bibcode (id)
-- local handler = cfg.id_handlers['BIBCODE'];
 
 
local err_type;
 
local err_type;
 
local year;
 
local year;
  
-- local text = external_link_id({link=handler.link, label=handler.label,
 
-- prefix=handler.prefix, id=id, separator=handler.separator, encode=handler.encode});
 
 
 
if 19 ~= id:len() then
 
if 19 ~= id:len() then
 
err_type = 'length';
 
err_type = 'length';
Line 49: Line 43:
 
end
 
end
  
-- if is_set (err_type) then -- if there was an error detected
 
-- text = text .. ' ' .. set_error( 'bad_bibcode', {err_type});
 
-- end
 
-- return text;
 
 
return err_type;
 
return err_type;
 
end
 
end
  
  
--[[-------------------------< E N T R Y  P O I N T S >------------------------------------------------------
+
--[=[-------------------------< E N T R Y  P O I N T S >------------------------------------------------------
 +
 
 +
This module is mostly a copy of the bibcode validation used in [[Module:Citation/CS1]]
  
 
call this module with:
 
call this module with:
Line 63: Line 55:
 
where {{{1|}}} is the bibcode
 
where {{{1|}}} is the bibcode
  
]]
+
]=]
 +
 
 
function p.check_bibcode (frame)
 
function p.check_bibcode (frame)
 
local err_type = bibcode (frame.args[1]);
 
local err_type = bibcode (frame.args[1]);
 
if err_type then
 
if err_type then
return '<span style="font-size:100%" class="error citation-comment">Check bibcode: ' .. err_type .. ' ([[Template:Bibcode|help]])</span>';
+
return '<span style="font-size:100%" class="error citation-comment">Check bibcode: ' .. err_type .. ' ([[Template:Bibcode#Error_messages|help]])</span>';
 
end
 
end
 
return '';
 
return '';

Revision as of 19:46, 25 June 2016

Documentation for this module may be created at Module:Check bibcode/doc

require('Module:No globals');
local p = {}

--[[--------------------------< B I B C O D E >--------------------------------------------------------------------

Validates (sort of) a bibcode id.

Format for bibcodes is specified here: http://adsabs.harvard.edu/abs_doc/help_pages/data.html#bibcodes

But, this: 2015arXiv151206696F is apparently valid so apparently, the only things that really matter are length, 19 characters
and first four digits must be a year.  This function makes these tests:
	length must be 19 characters
	characters in position
		1–4 must be digits and must represent a year in the range of 1000 – next year
		5 must be a letter
		6 must be letter, ampersand, or dot (ampersand cannot directly precede a dot; &. )
		7–8 must be letter, digit, ampersand, or dot (ampersand cannot directly precede a dot; &. )
		9–18 must be letter, digit, or dot
		19 must be a letter or dot

]]

local function bibcode (id)
	local err_type;
	local year;

	if 19 ~= id:len() then
		err_type = 'length';
	else
		year = id:match ("^(%d%d%d%d)[%a][%a&%.][%a&%.%d][%a&%.%d][%a%d%.]+[%a%.]$")	-- 
		if not year then														-- if nil then no pattern match
			err_type = 'value';													-- so value error
		else
			local next_year = tonumber(os.date ('%Y'))+1;						-- get the current year as a number and add one for next year
			year = tonumber (year);												-- convert year portion of bibcode to a number
			if (1000 > year) or (year > next_year) then
				err_type = 'year';												-- year out of bounds
			end
			if id:find('&%.') then
				err_type = 'journal';											-- journal abbreviation must not have '&.' (if it does its missing a letter)
			end
		end
	end

	return err_type;
end


--[=[-------------------------< E N T R Y   P O I N T S >------------------------------------------------------

This module is mostly a copy of the bibcode validation used in [[Module:Citation/CS1]]

call this module with:
	{{#invoke:check bibcode|check_bibcode|{{{1|}}}}}
where {{{1|}}} is the bibcode

]=]

function p.check_bibcode (frame)
	local err_type = bibcode (frame.args[1]);
	if err_type then
		return '<span style="font-size:100%" class="error citation-comment">Check bibcode: ' .. err_type .. ' ([[Template:Bibcode#Error_messages|help]])</span>';
	end
	return '';
end

return p;