User:CharlesTGillingham/Lua code for author-date IDs

These are sketches of new functions for Module:Citation/CS 1 that creates an author-date ID. This code is not tested or debugged --- it probably won't even compile. This is only a feasibility study.

It requires almost no refactoring. I chose to refactor out the code that is exclusively for producing the anchor, because it was in two places. I also assumed that this code would not report errors, because the intended usage is when there is also a citation template, so errors would be reported there.

If Meta-Wiki intends to duplicate this algorithm on their side, they will have to take into account all the details that this code does. Note this code calls subroutines (such as extract_names, or validate.dates) which are themselves very complex.

--[[--------------------< A U T H O R _ D A T E _ I D >-----------------------

]]

local function make_author_date_id (refparam, reforigin, a, e, c, year)

    local ref = is_valid_parameter_value (refparam, reforigin, cfg.keywords_lists['ref'], nil, true);	-- nil when |ref=harv; A['Ref'] else  
 	if 'none' == cfg.keywords_xlate[(ref.val and ref.val:lower()) or ''] then
        return nil;
    elseif ref then
        return ref;
    end
       
    local author_date = '';
  
	local namelist_t = {};								-- holds selected contributor, author, editor name list

	if #c > 0 then										-- if there is a contributor list
		namelist_t = c;									-- select it
	elseif #a > 0 then									-- or an author list
		namelist_t = a;
	elseif #e > 0 then									-- or an editor list
		namelist_t = e;
	else
        return '';
    end

    local names={};					                    -- a table for the one to four names and year
    for i,v in ipairs (namelist_t) do	                -- loop through the list and take up to the first four last names
        names[i] = v.last
        if i == 4 then break 
    end			                                        -- if four then done
        
    table.insert (names, year);				            -- add the year at the end
    author_date = table.concat(names);			        -- concatenate names and year
			
    if mw.uri.anchorEncode (author_date) == ((ref and mw.uri.anchorEncode (ref)) or '') then -- Ref may already be encoded (by {{sfnref}}) so citeref_id must be encoded before comparison
	    utilities.set_message ('maint_ref_duplicates_default');
	end
 	return author_date;
end

--[[--------------------< A U T H O R _ D A T E _ 0 >-----------------------

Create an author-date ID from the parameters passed to CS 1 templates. 

]]

local function authordate0( config, args )
	--[[ 
	Load Input Parameters
	The argument_wrapper facilitates the mapping of multiple aliases to single internal variable.
	]]
	local A = argument_wrapper ( args );

	--[[ AUTHOR ]]---------------------------------------------------------
	local a	= {};																
	do																			
	    local author_etal;
		local selected = select_author_editor_source (A['Vauthors'], A['Authors'], args, 'AuthorList');
		if 1 == selected then
			a, author_etal = extract_names (args, 'AuthorList');			
		elseif 2 == selected then
			a, author_etal = parse_vauthors_veditors (args, A['Vauthors'], 'AuthorList');
		end
	end
    
    --[[ EDITOR ]]---------------------------------------------------------
	local e	= {};														
	do																			
        editor_etal; --- not used
		local selected = select_author_editor_source (A['Veditors'], nil, args, 'EditorList');	
		if 1 == selected then
			e, editor_etal = extract_names (args, 'EditorList');		
		elseif 2 == selected then
			e, editor_etal = parse_vauthors_veditors (args, args.veditors, 'EditorList');
		end
	end

    --[[ CONTRIBUTOR ]]-----------------------------------------------------
	local c = {};			
	if utilities.in_array (config.CitationClass, {"book", "citation"}) and not utilities.is_set (A['Periodical']) then
		c = extract_names (args, 'ContributorList');							
	end

    --[[ DATE AND YEAR ]]---------------------------------------------------
    local anchor_year;
    do
        local Date = A['Date'];
	    local Year = A['Year'];		
	    if not utilities.is_set (Date) then
		    Date = Year;
            Year = nil;
        end															

     	local date_parameters_list = {
			['date'] = {val = Date, name = A:ORIGIN ('Date')},
			['year'] = {val = Year, name = A:ORIGIN ('Year')},
		};

	    local COinS_date = {};	-- not used
        local error_list = {};  -- not used
        local Embargo; --- not used
    	anchor_year, Embargo = validation.dates(date_parameters_list, COinS_date, error_list);

	    anchor_year = first_set ({Year, anchor_year}, 2);		                    -- Year first for legacy citations 
    end

    --[[ AUTHOR-DATE ID ]]----------------------------------------
	local author_date;
    author_date = make_author_date_id (A['Ref'], A:ORIGIN ('Ref'), a, e, c, anchor_year);

     -- Clear the errors?

    return author_date;
end

And this refactor expects that citation0 would (eventually) use this. And of course, citation0 could refactor a few other things to avoid the duplicate code.


-- Now enclose the whole thing in a <cite> element
	local options_t = {};
	options_t.class = cite_class_attribute_make (config.CitationClass, Mode);

	local author_date = make_author_date_id (A['Ref'], A:ORIGIN ('Ref'), a, e, c, anchor_year);
    if author_date then 
         options_t.id =  "CITEREF" .. author_date;
    end

	if string.len (text:gsub('%b<>', '')) <= 2 then								-- remove html and html-like tags; then get length of what

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.