Jump to content

Module:Track gauge/autodocument

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by DePiep (talk | contribs) at 12:25, 14 April 2014 (Create early auto-doc option). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

-- This is preliminary code (sandbox code) to give an early auto-documentation option.
-- In the near future, the autodoc functions will be incorporated in the main module:RailGauge code.
-- This code is tested for documentation page only (not for content space).
local p = {}
local gaugeDataAll = nil
local dataPageName = 'Module:RailGauge/data'

-----------------------------------------------------------------------------------
-- format_mm -- Pattern '0016.5 mm' for tablesort and catsort. To be wrapped in class="sortkey"
-----------------------------------------------------------------------------------
local function format_mm( id, suffix )
    return string.rep( '0', 4 - ( string.len( math.floor( tonumber( id ) ) ) ) ) ..
    id .. ' mm' .. ( suffix or '' )
end

-----------------------------------------------------------------------------------
-- anchors -- Target anchors (Here)
-----------------------------------------------------------------------------------
local function anchors( rgEntry )
    -- todo: distinct Here or There anchors; and imp or met
    local html = require('Module:HtmlBuilder')
    local anchImp = ''
    local anchMet = ''
    local ft = ''
    local frac = ''
    local inch = ''

    if rgEntry == nil then
        return ''
    end

    anchMet = rgEntry.id .. ' mm'

    if rgEntry.ft then
        ft = rgEntry.ft .. ' ft'
    end
    if rgEntry["in"] then
        inch = ' ' .. rgEntry["in"]
    end
    if rgEntry.num then
        frac = ' ' .. rgEntry.num .. '/' .. rgEntry.den .. ' in'
    elseif inch ~= '' then
        inch = inch .. ' in'
    end
    anchImp = ft .. inch .. frac

    local buildM = html.create()
        .tag('div').attr('id', anchMet).attr('style', 'vertical-align:top')
    local buildI = html.create()
        .tag('div').attr('id', anchImp).attr('style', 'vertical-align:top')

    return tostring(buildM) .. tostring(buildI)
end

-----------------------------------------------------------------------------------
-- catMentionsSize -- Category name for "mentions" categories
-----------------------------------------------------------------------------------
local function catMentionsSize( mmSize, catSort, pagename, show, plaintext )
    local cat

    -- Category page name. mmSize has format of id (like '16.5').
    cat = 'User:DePiep/sandbox' .. ' ' .. mmSize .. ' mm' --SANDBOX
    if plaintext then
        return cat
    elseif show then
        -- uses colon and wikilabel; no catsort
        local label = '|Ucat:Articles that mention ' .. mmSize .. ' mm' .. ' gauge'
        cat = '[[:'.. cat .. label .. ']]'
    else
        if ( catSort or '' ) ~= '' then
            catSort = '|' .. catSort -- todo: add pagename
        else
            catSort = ''
        end
        cat = '[['.. cat .. catSort .. ']]'
    end

    return cat
end

-----------------------------------------------------------------------------------
-- catMentionsSizeParent -- Parent of all "mentions" categories
-----------------------------------------------------------------------------------
local function catMentionsSizeParent( show )
    local cat

    -- Category page name
    cat = 'Category:Articles that mention a specific rail gauge'
    if show then
        -- uses colon and wikilabel; no catsort
        cat = '[[:'.. cat .. '|Cat:Articles mentioning RailGauge]]'
    else
        cat = '[['.. cat .. ']]'
    end

    return cat
end

-----------------------------------------------------------------------------------
-- prepareArgs -- Arguments coming from an #invoke or from a module
-----------------------------------------------------------------------------------
local function prepareArgs( frame )
    -- If called via #invoke, use the args passed into the invoking
    -- template, or the args passed to #invoke if any exist. Otherwise
    -- assume args are being passed directly in from the debug console
    -- or from another Lua module.
    local origArgs
    if frame == mw.getCurrentFrame() then
        origArgs = frame:getParent().args
        for k, v in pairs( frame.args ) do
            origArgs = frame.args
            break
        end
    else
        origArgs = frame
    end

    -- Trim whitespace, make lower-case and remove blank arguments for all arguments
    -- searchAlias is the cleaned value of [1]. [1] is kept as rawInput for error message
    local args = {}
    args["searchAlias"] = ''
    args["rawInput"] = origArgs[ 1 ] or ''

    for k, v in pairs( origArgs ) do
        v = mw.text.trim( v )
        if tonumber( k ) == nil then
            -- Named argment
            args[ k ] = mw.ustring.lower( v )
        else
            -- Unnamed argument, alias to be searched
            -- Into lowercase, remove all whitespace, commas to create the search key
            v = mw.ustring.lower( mw.ustring.gsub( v, '[%s%,]', '' ) )
            args[ k ] = v
            if k == 1 then
                args["searchAlias"] = v
            end
        end
    end

    return args
end

-----------------------------------------------------------------------------------
-- getGaugeDataSet -- Find entry data for a single gauge
-----------------------------------------------------------------------------------
local function getGaugeDataSet( searchAlias )
    if gaugeDataAll == nil then
        gaugeDataAll = mw.loadData( dataPageName )
    end
    local rgEntry = nil
    for i, rgEntry in ipairs( gaugeDataAll ) do
        for j, alias in ipairs( rgEntry.aliases ) do
            if alias == searchAlias then
                return rgEntry
            end
        end
    end
--todo: catch plain number then id
--not found
if gaugeDataAll[ tostring(searchAlias) ] == nil then
   return nil --gaugeDataAll[searchAlias]
else
    return gaugeDataAll["891"] --todo: what is this?
end

end

-----------------------------------------------------------------------------------
-- noWrap -- Add span tags to prevent a string from wrapping.
-----------------------------------------------------------------------------------
local function noWrap( s )
    return mw.ustring.format( '<span class="nowrap">%s</span>', s )
end

-----------------------------------------------------------------------------------
-- frac -- A slimmed-down version of the {{frac}} template (a nowrap to be added with the unit)
-----------------------------------------------------------------------------------
local function frac( whole, num, den )
    return mw.ustring.format(
        '<span class="frac">%s%s<sup>%s</sup>&frasl;<sub>%s</sub></span>',
        whole or '', whole and '<span class="visualhide">&nbsp;</span>' or '', num, den
        )
end

-----------------------------------------------------------------------------------
-- formatImp -- Formats imperial measurements into a single element
-----------------------------------------------------------------------------------
local function formatImp( gData, ulink, articleLink, pageName )
    local ret = {}
    local ft = gData.ft

    if ft then
        local ftlink = ulink and not articleLink and '[[Foot (unit)|ft]]' or 'ft'
        table.insert( ret, mw.ustring.format( '%s&nbsp;%s', ft, ftlink ) )
    end
    local inches = gData['in']
    local num = gData.num
    local den = gData.den
    if inches and not num and not den then
        table.insert( ret, inches )
    elseif num and den then
        table.insert( ret, frac( inches, num, den ) )
    end
    if inches or num and den then
        local incheslink = ulink and not articleLink and '[[inch|in]]' or 'in'
        table.insert( ret, incheslink )
    end
    local gaugeSize = noWrap( table.concat( ret, '&nbsp;' ) )

    if articleLink then
        return '[[' .. pageName .. '|' .. gaugeSize .. ']]'
    else
        return gaugeSize
    end
end

-----------------------------------------------------------------------------------
-- formatMet -- Formats metric measurements into a single formatted element
-----------------------------------------------------------------------------------
local function formatMet( gData, ulink, articleLink, pageName )
    local m = gData.m
    local gaugeSize

    if m then
        local munit = ulink and not articleLink and '[[metre|m]]' or 'm'
        gaugeSize = noWrap( mw.ustring.format( '%s&nbsp;%s', m, munit ) )
    else
        local mm = gData.mm
        mm = tonumber( mm )
        if mm then
            mm = mw.getContentLanguage():formatNum( mm )
        end
        local mmunit = ulink and not articleLink and '[[millimetre|mm]]' or 'mm'
        gaugeSize = noWrap( mw.ustring.format( '%s&nbsp;%s', mm, mmunit ) )
    end

    if articleLink then
        return '[[' .. pageName .. '|' .. gaugeSize .. ']]'
    else
        return gaugeSize
    end
end

-----------------------------------------------------------------------------------
-- compose -- Puts together the two metric, imperial measures into an output string.
-----------------------------------------------------------------------------------
local function compose( args, gData )
    local definition = gData.def1
    local pageName = gData.pagename or nil
    local imp = formatImp( gData, args.unitlink == 'on',
        args.lk=='on' and definition=='imp' and pageName, pageName)
    local met = formatMet( gData, args.unitlink == 'on',
        args.lk=='on' and definition=='met' and pageName, pageName)
    local first = args.first or gData.def1

    if first == 'met' or first == 'metric' then
        first = 'met'
    else
        first = 'imp'
    end
    local ret = {}
    if first == 'met' then
        table.insert( ret, met )
    else
        table.insert( ret, imp )
    end
    local disp = args.disp
    if disp ~= '1' then
        local formatText
        if disp == 's' or disp == '/' then
            formatText = '/&#x200b;%s'
        elseif disp == 'or' then
            formatText = ' or %s'
        elseif disp == '=' then
            formatText = ' = %s'
        else
            formatText = ' (%s)'
        end
        if first == 'met' then
            table.insert( ret, mw.ustring.format( formatText, imp ) )
        else
            table.insert( ret, mw.ustring.format( formatText, met ) )
        end
    end
    ret = table.concat( ret )

    if args.wrap == 'y' then
        return ret
    else
        return noWrap( ret )
    end
end

-----------------------------------------------------------------------------------
-- main -- MAIN: the basic flow of the module
-----------------------------------------------------------------------------------
function p.main( frame )
    local args = nil
    local gData = nil
    local title = mw.title.getCurrentTitle()

    args = prepareArgs( frame )
    -- Get the data entry for this gauge from /data subpage
    gData = getGaugeDataSet ( args.searchAlias )

    -- Categorise the page if no gauge information was found.
    if gData == nil then
        local category = ''
        if title:inNamespaces(0, 14) then --main=0,templ=10,cat=14 --SANDBOXcolon:cat
            category = mw.ustring.format(
               '[[:Category:Articles with template RailGauge with unrecognized input|%s, %s]]',
               args["rawInput"] or ' ', title.text
            )
        end
        return ( args["rawInput"] or '' ) .. category
    end

    -- Assemble the output.
    local ret = {}
    table.insert( ret, compose( args, gData ) )
    local gaugeName = gData.name
    local gaugeLink = gData.link
    if args.allk == 'on' and gaugeLink then
        table.insert( ret, ' ' .. noWrap( gaugeLink ) )
    elseif args.al == 'on' and gaugeName then
        table.insert( ret, ' ' .. noWrap( gaugeName ) )
    end

    local maintCatSortDepr = gData.maintcatsort --category to be DEPRECATED, see 'Categorize all' topic (3 April 2014)
    if maintCatSortDepr ~= nil then
        local addMaintCat = args.addcat
        if addMaintCat ~= 'off' and addMaintCat ~= 'no' then
            if title.namespace == 0 or title.namespace == 14 then
                local category = ''
                category = mw.ustring.format(
                    '[[Category:Articles with template RailGauge that may need attention' ..
                    ' |%s, %s]]',
                    maintCatSortDepr, title.text)
                table.insert( ret, ' ' .. category )
            end
        end
    end

    return table.concat( ret )
end

-----------------------------------------------------------------------------------
-- checkData -- Public. Performs various checks on the /data subpage.
-----------------------------------------------------------------------------------
function p.checkData( frame )
    local dataPage = frame and frame.args and frame.args[1] or dataPageName
    local data = mw.loadData( dataPage )
    local exists, dupes, dupeSort, ret = {}, {}, {}, {}

    -- Check for duplicate aliases.
    for ti, t in ipairs( data ) do
        for ai, alias in ipairs( t.aliases or {} ) do
            if not exists[ alias ] then
                exists[ alias ] = { ti, ai }
            else
                if not dupes[ alias ] then
                    dupes[ alias ] = { exists[ alias ] }
                end
                table.insert( dupes[ alias ], { ti, ai } )
            end
        end
    end
    for alias in pairs( dupes ) do
        table.insert( dupeSort, alias )
    end
    table.sort( dupeSort )
    for i1, alias in ipairs( dupeSort ) do
        local positions = {}
        for i2, aliasKeys in ipairs( dupes[ alias ] ) do
            local position = mw.ustring.format( 'gauge %d, alias %d (gauge id: <code>%s</code>)', aliasKeys[ 1 ], aliasKeys[ 2 ], data[ aliasKeys[ 1 ] ].id or '' )
            table.insert( positions, position )
        end
        local aliasText = mw.ustring.format( 'Duplicate aliases "%s" detected at the following positions: %s.', alias, mw.text.listToText( positions, '; ' ) )
        table.insert( ret, aliasText )
    end
    -- Check for numerators without denominators.
    for ti, t in ipairs( data ) do
        local num = t.num
        local den = t.den
        if num and not den then
            table.insert( ret, mw.ustring.format( 'Numerator "%s" with no denominator detected at gauge %d (id: <code>%s</code>).', num, ti, t.id or '' ) )
        elseif den and not num then
            table.insert( ret, mw.ustring.format( 'Denominator "%s" with no numerator detected at gauge %d (id: <code>%s</code>).', den, ti, t.id or '' ) )
        end
    end
    -- Check for gauges with no imperial or no metric measurements.
    for ti, t in ipairs( data ) do
        if not ( t.ft or t['in'] or t.num or t.den ) then
            table.insert( ret, mw.ustring.format( 'No imperial measurements found for gauge %d (id: <code>%s</code>).', ti, t.id or '' ) )
        end
        if not ( t.m or t.mm ) then
            table.insert( ret, mw.ustring.format( 'No metric measurements found for gauge %d (id: <code>%s</code>).', ti, t.id or '' ) )
        end
    end
    -- Check for non-numeric measurements.
    local measurements = { 'ft', 'in', 'num', 'den', 'm', 'mm' }
    for ti, t in ipairs( data ) do
        for mi, measurement in ipairs( measurements ) do
            local measurementVal = t[ measurement ]
            if measurementVal and not tonumber( measurementVal ) then
                table.insert( ret, mw.ustring.format( 'Non-numeric <code>%s</code> measurement ("%s") found for gauge %d (id: <code>%s</code>).', measurement, measurementVal, ti, t.id or '' ) )
            end
        end
    end
    -- Check for gauges with no id.
    for ti, t in ipairs( data ) do
        if not t.id then
            local aliases = {}
            for i, alias in ipairs( t.aliases ) do
                table.insert( aliases, mw.ustring.format( '<code>%s</code>', alias ) )
            end
            aliases = mw.ustring.format( ' (aliases: %s)', mw.text.listToText( aliases ) )
            table.insert( ret, mw.ustring.format( 'No id found for gauge %d%s.', ti, aliases or '' ) )
        end
    end
    -- Check for gauges with no aliases.
    for ti, t in ipairs( data ) do
        if type( t.aliases ) ~= 'table' then
            table.insert( ret, mw.ustring.format( 'No aliases found for gauge %d (id: <code>%s</code>).', ti, t.id or '' ) )
        else
            local isAlias = false
            for ai, alias in ipairs( t.aliases ) do
                isAlias = true
                break
            end
            if not isAlias then
                table.insert( ret, mw.ustring.format( 'No aliases found for gauge %d (id: <code>%s</code>).', ti, t.id or '' ) )
            end
        end
    end
    -- Check for named gauges with no links and gauges with links but no names.
    for ti, t in ipairs( data ) do
        if t.name and not t.link then
            table.insert( ret, mw.ustring.format( 'No link found for the named gauge "%s" at position %d (id: <code>%s</code>).', t.name, ti, t.id or '' ) )
        elseif t.link and not t.name then
            table.insert( ret, mw.ustring.format( 'No name found for the gauge with link "%s" at position %d (id: <code>%s</code>).', t.link, ti, t.id or '' ) )
        end
    end
    -- Check for invalid def1 values.
    for ti, t in ipairs( data ) do
        local def1 = t.def1
        if def1 ~= 'imp' and def1 ~= 'met' then
            table.insert( ret, mw.ustring.format( 'Invalid def1 value "%s" found for gauge %d (id: <code>%s</code>).', def1 or '', ti, t.id or '' ) )
        end
    end
    -- Check for unwanted whitespace.
    for ti, t in ipairs( data ) do
        for tkey, tval in pairs( t ) do
            if tkey == 'aliases' and type( tval ) == 'table' then
                for ai, alias in ipairs( tval ) do
                    if mw.ustring.find( alias, '%s' ) then
                        table.insert( ret, mw.ustring.format( 'Unwanted whitespace detected in gauge %d alias %d ("%s", gauge id: <code>%s</code>).', ti, ai, alias, t.id or '' ) )
                    end
                end
            elseif tkey == 'name' or tkey == 'link' or tkey == 'pagename' then
                if tval ~= mw.text.trim( tval ) then
                    table.insert( ret, mw.ustring.format( 'Unwanted whitespace detected in <code>%s</code> field of gauge %d ("%s", gauge id: <code>%s</code>).', tkey, ti, tval, t.id or '' ) )
                end
            elseif mw.ustring.find( tval, '%s' ) then
                table.insert( ret, mw.ustring.format( 'Unwanted whitespace detected in <code>%s</code> field of gauge %d ("%s", gauge id: <code>%s</code>).', tkey, ti, tval, t.id or '' ) )
            end
        end
    end
    -- Added April 2014: alias should not double with another id (imp and mm not ambiguous)
    local self_id = ''
    local self_def1 = ''
    for ti, t in ipairs( data ) do
        self_id = t.id
        self_def1 = t.def1
        for iC, aliasCheck in ipairs( t.aliases ) do
            if tonumber( aliasCheck ) ~= nil then
                if self_id ~= aliasCheck then
                    for iTwo, tTwo in ipairs( data ) do
                        if aliasCheck == tTwo.id then
table.insert( ret, mw.ustring.format( 'Input alias %s (%s) from <code>id=%s mm</code> ambiguous with gauge id=<code>%s mm</code> (%s)', aliasCheck, self_def1, self_id, tTwo.id, tTwo.def1 ) )
                        end
                    end
                end
            end
        end
    end

    -- Return any errors found.
    for i, msg in ipairs( ret ) do
        ret[ i ] = mw.ustring.format( '<span class="error">%s</span>', msg )
    end

    if #ret > 0 then
        return mw.ustring.format( 'Found the following errors in %s:\n* %s', dataPageName, table.concat( ret, '\n* ' ) )
    else
        return mw.ustring.format( 'No errors found in %s.', dataPageName )
    end

end

-----------------------------------------------------------------------------------
-- documentHeader
-----------------------------------------------------------------------------------
local function documentHeader( numberOfEntries, docTitle, docState )
    local sortColHeaders = ''
    local sortClass = ''
    local catMparent = catMentionsSizeParent( true )

    if docTitle == '' then
        if ( numberOfEntries or 0 ) <= 1 then
            docTitle = 'Rail gauge'
        else
            docTitle = 'Rail gauges'
        end
    end

    if docState == '' then -- check nil
        docState = 'collapsible uncollapsed'
    else
        docState = 'collapsible ' .. collapsibleState
    end

    if ( numberOfEntries or 0 ) > 1 then
        sortClass = 'sortable'
        sortColHeaders = '' ..
        '\n|- ' ..
        '\n! style="line-height:90%;" | &nbsp; || || || || ||'
    end

    -- 6 columns
    return '' ..
    '\n{| class="wikitable ' .. sortClass .. ' ' .. docState .. '" '
    .. 'style="text-align:right; width:100%;" ' ..
    '\n|+ style="background:#d8d8d8;" | ' .. docTitle ..
    '\n|-' ..
    '\n!'
    .. ' style="background:#d8d8d8;" | Units' --1
    .. '\n! style="background:#d8d8d8;" | Input&nbsp;options (aliases)' --2
    .. '\n! style="background:#d8d8d8;" | Size (mm)' --3
    .. '\n! style="background:#d8d8d8;" | Size (ft,&nbsp;in)' --4
    .. '\n! style="background:#d8d8d8;" | Size (inches)' --5
    .. '\n! style="background:#d8d8d8;" | Link' --6
    .. sortColHeaders

    ---  .. '\n! style="background:thistle" | note'
end

-----------------------------------------------------------------------------------
-- documentFooter
-----------------------------------------------------------------------------------
local function documentFooter( msg )
    return '\n|}' ..
    '\n' .. (msg or '') --dev
end

-----------------------------------------------------------------------------------
-- createCatMentionsSize -- Create and preload control
-----------------------------------------------------------------------------------
function createCatMentionsSize( id )
local catM
local preloadUrl = 'action=edit&preload=Template:RailGauge/preload-categorypage-railgauge-mentionings'

    --catMentionsSize( mmSize, catSort, pagename, show, plaintext )
    catM = catMentionsSize( id, nil, nil, nil, true )
    local qry = mw.uri.parseQueryString( preloadUrl )

    return tostring( mw.uri.fullUrl( catM, qry ) )
end

-----------------------------------------------------------------------------------
-- fromInputToId -- Used cleaned Alias as searchkey
-----------------------------------------------------------------------------------
local function fromInputToId( searchAlias )
    if gaugeDataAll == nil then
        gaugeDataAll = mw.loadData( dataPageName )
    end
    for i, rgEntry in ipairs( gaugeDataAll ) do
        for j, alias in ipairs( rgEntry.aliases ) do
            if alias == searchAlias then
                return rgEntry.id
            end
        end
    end

    return nil
end

-----------------------------------------------------------------------------------
-- documentInchCount -- Gives the number of inches in decimals.
-----------------------------------------------------------------------------------
local function documentInchCount( rgEntry )
    local inches = 0
    if rgEntry["num"] ~= nil then
    	inches = tonumber( ( rgEntry["num"] or 0 ) / ( rgEntry["den"] or 1 ) )
    end
    inches = tostring( inches + ( tonumber( rgEntry["ft"] or 0 ) * 12)
             + tonumber( rgEntry["in"] or 0 ) )

    return inches
end

-----------------------------------------------------------------------------------
-- documentInchToMm -- Not tested lately
-----------------------------------------------------------------------------------
local function documentInchToMm( inchCount )
    return tonumber(inchCount or 0) * 25.4
end

-----------------------------------------------------------------------------------
--  fromIdToEntrySet
-- From one id,  make the set withall one-two-three-more entries (met, inp, variants)
-----------------------------------------------------------------------------------
local function fromIdToEntrySet( id, searchedAlias )
local TableTools = require('Module:TableTools')
local html = require('Module:HtmlBuilder')
local htmlString = ''
local rowSplit = '<div style="border-top:1px solid #ccc; height:1px;"/>'
local inchCount = ''

    -- From the id, build the set of existing entries (met, imp, and variants)
    local entry = {}
    for i, rgEntry in ipairs( gaugeDataAll ) do
        if id == rgEntry["id"] then
            if rgEntry["def1"] == 'met' and entry[ 1 ] == nil then
                entry[ 1 ] = rgEntry
            elseif rgEntry["def1"] == 'imp' and entry[ 2 ] == nil then
                entry[ 2 ] = rgEntry
            else
                entry[ 3 + TableTools.size( entry ) ] = rgEntry
            end
            entry = TableTools.compressSparseArray( entry )
            -- Result: the entry (table) with entries present in data,
            -- in sequence (met, imp, any extra)
        end
    end
    -- Entry set is now complete & clean

    -- Check
    if entry[ 1 ] == nil then
        -- A non-existent id is entered? Unexpected here.
        return '' ..
        '\n|-' ..
        '\n| colspan=7 style="color:red; text-align:left;" |' ..
        'Error using [[Template:RailGauge/autodocumentation|RailGauge/autodocumentation]]:' ..
        ' No rail gauge defined for: "' .. ( searchedAlias or '' ) .. '"'
     end

    -- Build row from data (all entries)
    -- String together various data elements

    -- Values independent of met/imp def1:
    local title = mw.title.getCurrentTitle()
    local anchors = anchors( entry[ 1 ] )
    local sortKey = format_mm( id )
    htmlString = html.create()
        .tag('div').addClass('sortkey').wikitext( sortKey )
    sortKey = tostring( htmlString )
    inchCount = documentInchCount( entry[ 1 ] )
    ---local catSort = title.text --?

    -- aliases -- listing the input options
    local aliasList = {}
    for i, e in ipairs( entry ) do
        local alis = {}
        for j, v in ipairs( e.aliases ) do
            if tonumber( v ) == nil then -- No plain numbers
                table.insert( alis, tostring( v ) )
            end
        end
        -- cannot sort alis here (?)
        for j, v in ipairs( alis ) do
            if string.match( v, '^%d' ) == nil then -- textual so to italic. Todo: better uc
                local txt = v
                -- txt = mw.ustring.upper(mw.ustring.sub(v, 1, 1)) .. mw.ustring.sub(v, 2)
                htmlString = html.create()
                    .tag('span').wikitext( txt ).css('font-style', 'italic')
                alis[ j ] = tostring( htmlString )
            end
        end
        table.insert ( aliasList, table.concat( alis, '; ' ) )
    end

    -- def1 -- Definition unit
    local def1 = {} -- definition  code ('met' or 'imp')
    local def1text = {}
    for i, v in ipairs ( entry ) do
        table.insert( def1, v.def1 )
        if v.def1 == 'imp' then
            table.insert( def1text, 'imperial' )
        else
            table.insert( def1text, 'metric' )
        end
    end

    -- mm; ft in -- Measurement (number & unit; met and imp)
    -- todo: check for >=3 entries (915mm)
    local measure = {}
    measure[ 1 ] = formatMet( entry[ 1 ] )
    measure[ 2 ] = formatImp( entry[ 1 ] ) -- both met and imp from entry[ 1 ]
    if def1[ 1 ] == 'met' then
        htmlString = html.create()
            .tag('div').wikitext(measure[ 1 ]).css('font-weight', 'bold')
        measure[ 1 ] = tostring( htmlString )
    end
    if ( def1[ 1 ] == 'imp' ) or ( def1[ 2 ] == 'imp' ) then
        htmlString = html.create()
            .tag('div').wikitext( measure[ 2 ] ).css('font-weight', 'bold')
        measure[ 2 ] = tostring( htmlString )
    end

    -- pagename -- Linked page
    local linkPage = {}
    for i, e in ipairs( entry ) do
        table.insert( linkPage, e.pagename )
    end
    if #linkPage == 2 then
        if linkPage[ 1 ] == linkPage[ 2 ] then
            linkPage[ 2 ] = nil
        end
    end
    for i, lp in ipairs( linkPage ) do
        local fmtLp = ''
        if string.len( lp ) > 25 then
            fmtLp = '[[' .. lp .. ']]'
        else
            htmlString = html.create()
                .tag('span').wikitext( lp ).addClass('nowrap')
            htmlString = tostring( htmlString )
            fmtLp = '[[' .. lp .. '|' .. htmlString .. ']]'
        end
        htmlString = html.create()
            .tag('div').css('text-align', 'left').wikitext( fmtLp )
        linkPage[ i ] = tostring(htmlString)
    end

    -- mentions categories -- switched off for now (now gaugedoc only)
    if false then
        local catM = catMentionsSize( id, catSort, title, true )
        htmlString = html.create()
            .tag('div').css('text-align', 'left').wikitext(catM)
        catM = tostring( htmlString )
    end

    --createcat
    --local createCat = createCatMentionsSize( id )

    -- Compose the row with all entries
    local row = {}
    table.insert( row, table.concat( def1text, rowSplit ) ) --1
    table.insert( row, table.concat( aliasList, rowSplit ) ) --2
    table.insert( row, anchors .. sortKey .. measure[ 1 ] ) --3
    table.insert( row, sortKey .. measure[ 2 ] ) --4
    table.insert( row, sortKey .. inchCount ) --5
    table.insert( row, table.concat( linkPage, rowSplit ) ) --6
    --- table.insert( row, catM ) --
    --- table.insert( row, '[' .. createCat .. ' create]' ) --7

    return '' ..
    '\n|- style="background:#e8e8e8; border-top:2px solid #aaa;" | ' ..
    '\n|' .. table.concat( row, ' || ' )
end

-----------------------------------------------------------------------------------
-- documentGauge -- Selfdocument gauge data (one, multiple, range, all)
-----------------------------------------------------------------------------------
function p.documentGauge( frame )
    local TableTools = require('Module:TableTools')
    local rgList = {}

    local args = {}
    args = prepareArgs( frame )
    gaugeDataAll = mw.loadData( dataPageName )

    -- Series from the list
    -- idFrom and idTo are numerical
    local idFrom = -1
    local idTo = -1
    for i, v in ipairs( args ) do
        if v == 'all' then
            idFrom = -math.huge
            idTo = math.huge
            break
        end
    end

    if args.docfrom ~= nil then
        idFrom = tonumber( fromInputToId( args.docfrom )
                 or mw.ustring.gsub( '0' .. args.docfrom, 'mm', '' ) )
        idTo = math.huge
    end
    if args.docto ~= nil then
        idTo = tonumber( fromInputToId( args.docto )
               or mw.ustring.gsub( '0' .. args.docto, 'mm', '' ) )
    end

    if idTo > 0 then -- some list is requested from the whole data set
        if idFrom > idTo then
            local dummy = idFrom
            idFrom = idTo
            idTo = dummy
        end
        for i, rgEntry in ipairs( gaugeDataAll ) do
            if ( tonumber( rgEntry.id ) >= idFrom ) and ( tonumber( rgEntry.id ) <= idTo ) then
                table.insert( rgList, tonumber( rgEntry.id ) )
            end
        end
        rgList = TableTools.removeDuplicates( rgList )
        table.sort( rgList )
    end

    -- Individual entries can be mentioned in args (all unnamed = numbered params)
    -- Need a straight table to keep sequence right
    local argsAliases = TableTools.compressSparseArray(args)
    for i, argAlias in ipairs( argsAliases ) do
        id = fromInputToId( argsAliases )
        if id ~= nil then
            -- Add to the top
            table.insert( rgList, i, tonumber( id ) )
        end
    end

    local maintReportArgs = 'args: '
    for k, v in pairs( args ) do
        maintReportArgs = maintReportArgs .. k .. '='.. v .. '; '
    end
    if maintReportArgs == 'args: ' then
        maintReportArgs = 'no args' --todo: what is this?
    end
    maintReportArgs = '<tt>' .. maintReportArgs .. '</tt><br>'

    -- Now loop through the prepared rgList[id] and add rows to result table
    -- One row contains all available entries for the id (met, imp, a third variant)
    local rowRGid = {}
    for i, numId in ipairs( rgList ) do
        table.insert( rowRGid, fromIdToEntrySet( tostring( numId ) ) )
    end

    return documentHeader( #rgList, args.docTitle or '', args.docState or '') ..
        table.concat( rowRGid, '' ) ..
        documentFooter() -- no maintReportArgs 
end

-----------------------------------------------------------------------------------
-- gaugeSizeFromTitle
-----------------------------------------------------------------------------------
function p.gaugeSizeFromTitle()
-- Currently finds "1620 mm" when at end of title, then returns "1620". Blank when not found.
-- Used for Mentions  cat pages.
    local title = mw.title.getCurrentTitle()
    return string.match( title.text, '%s(%d+%.?%d*)%smm$' ) or ''
end

return p