Module:User:Sarri.greek/toc2-hor

From Wiktionary, the free dictionary
Jump to navigation Jump to search

User:Sarri.greek (CAT) » Module toc2-hor doc :: style.css » Template:User:Sarri.greek/toc2-hor Assuming that the magic word TOC is withdrawn,

{{#invoke:User:Sarri.greek/toc2-hor|main}}
reconstruct a TOC version: horizontal, toclimit2, vertical, +/- L4s, or a look like wikt:el:Template:test-ol

Also test at wikt:el:Template:toc-test (wikt:el:Module:toc-test)


--[=[ test at [[wikt:en:Template:User:Sarri.greek/toc2-hor]]
2023.03.09. Get a TOC with parameters (break headings, make columns > TOC horizontal, limit 2)
Assume that the magic word TOC is withdrawn and not available.
[[wikt:en:Wiktionary:Grease_pit/2023/February#Asking_for_help_from_volunteer_programmers_for_TOC]]

3 make new columns for L2 -- TO DO
2 repetition of headers, Thank you  https://en.wiktionary.org/wiki/Special:Contributions/70.172.194.25
1 function make_table Thank you [[wikt:en:User:Erutuon]] for function table.concat


-- ---------- NEED a css with margin left to move it left
-- CSS also for fully numbered TOC, all levels
/* <nowiki> */


/* 	
	Table of Contents with numbered list
	in 2 or 3 columns
	If used at a page, write
		<noinclude><templatestyles src=".../style.css " /></noinclude>
source
https://stackoverflow.com/questions/4098195/can-ordered-list-produce-result-that-looks-like-1-1-1-2-1-3-instead-of-just-1
help. 
	*/

/* Fully numbered nested lists like 1, 1.1, 1,2, 1.2.1, 1.2.2... 
	To reset numbers, write at your page:
	make number 2 <li style="counter-reset: item 1;">
	make number 3 <li style="counter-reset: item 2;">
*/


.tocwikt ol li {display:inline;} /* hide original list counter */

/* display:inline ONLY for horizontal TOC, else display:block */
.tocwikt li { 
	font-size:13px;
	display:inline;
} 

/* For full tocs all lines must have equal height */
.tocwikt ol, li { 
	line-height:1.5em;
	padding-top: 0;
	padding-bottom: 0;
	margin-top: 0;
	margin-bottom: 0;
}

.tocwikt ol > li:first-child {counter-reset: item; } /* reset counter */
.tocwikt ol > li {counter-increment: item; } /* increment counter */
.tocwikt ol > li:before {content:counters(item, ".") " "; } /* print counter */

]=]--


---------  get headers
-- find the titles of sections by the number of equal  = = = symbols
-- there is no =header= with one equal symbol (this is the actual page)
-- if find in page "==header==" then get "header" 
-- repetitions of headers (not needed for Level2: language names)
-- headers in Talks may have templates -- TO DO
-- parameters to make columns for 2-5 Level2 language-headings) -- TO DO
-- add it in a table with styles at Template

local export = {}

function export.main(frame)
local args = frame:getParent().args	-- only for Templates
	local output = {}
	local page = mw.title.getCurrentTitle()
	local content = page:getContent() -- put this UNDER local page
	local header_numbered = {}
	local header_count = {}
	
-- for FULL toc
--[=[
	-- will not accept euqal symbol = in the header
	for start_equals, header, end_equals in content:gmatch("%f[^\n%z](=+)([^=\n]+)(=+)") do
		header = mw.text.trim(header)
]=]--

-- for toclimit2 toc
	for start_equals, header, end_equals in content:gmatch("%f[^\n%z](==)([^=\n]+)(==)") do
		header = mw.text.trim(header)
		-- mw.addWarning is just to demonstrate some of the possible error conditions.
		local start_level, end_level = #start_equals, #end_equals
		
		if start_level ~= end_level then
			mw.addWarning("Header " .. start_equals .. header .. end_equals .. " has mismatched equals")
		end
		if start_level == 1 then
			mw.addWarning("Header " .. start_equals .. header .. end_equals .. " has level 1, which is not expected in an entry")
		else
			
--TO DO levels (id=) how can i do <ol> and <li> with class and style args?
			local level =  args['level'] or ''
			local new_col = args['cols'] or ''
		
		if start_level == 2 then level = '#' end
		-- stop at level 2 for inline toclimit2
		
		if start_level == 3 then level = '#' end
		if start_level == 4 then level = '#' end
		if start_level == 5 then level = '#' end
		if start_level == 6 then level = '#' end

-- get headers
		local head = '' -- for header = head + numbered like Etymology 1, Etymology 2

-- -------------- as in Talks, Appendices, etc.
-- there could be xxxx [[zzz|xxx]] xxx
-- [[zzz|xxx]]
	if mw.ustring.find(header, "%[%[(.+[^%[%|%]])%|(.+[^%[%|%]])%]%]")
		then althead1 = mw.ustring.gsub(header, "%[%[(.+[^%[%|%]])%|", "")
			head = mw.ustring.gsub(althead1, "[]]", "")

-- there could be xxxx [[xxx]]] xxx
-- [[xxx]]	
	elseif mw.ustring.find(header, "%[%[(.+[^%|%]])%]%]")
		then head = mw.ustring.gsub(header, "[%[%]]", "")

-- ?# TO DO: headings with Templates, etc
	-- ..............
	
-- xxx xxx xxx 
	else
		head = header
	
	end -- close elseifs for head


-- for FULL TOC
-- header_numbered as in Etymology 1, Etymology 2, ...
	-- %s means whitespace, and %S means anything other than whitespace
	-- %d digit
	-- *  means zero or more times -- $ at the end
		local numbered = ''
		if mw.ustring.find(header, "%s(%d*)$") then
			numbered = mw.ustring.sub(mw.ustring.match(header, "%s(%d*)$"), 1, -1)
			header = head .. ' ' .. numbered
		elseif mw.ustring.find(header, "{{") and not mw.ustring.find(header, "%s(%d*)$") then
			header = head
		elseif not mw.ustring.find(header, "{{") and not mw.ustring.find(header, "%s(%d*)$") then
			header = head
		end
		
-- repetitions (e.g. Etymology 1 and again Etymology 1)
		header_count[header] = (header_count[header] or 0) + 1
		local number = ''
		if header_count[header] > 1 then
			number = '_' .. header_count[header]
		end

-- make headings linked ================ the ?# TO DO numbers are not linked, as they are to real TOC
	local heading = ''

		heading = '[[#' .. header .. number .. '|' ..  header .. ']]'
		
-- ?# how <td><ol><li style=args style> 
			-- Subtract 1 because the top-level header in an entry is level 2 (==).
			table.insert(output, (level):rep(start_level - 1)
				.. heading .. " &bull;\n")
		
		end -- close if start_level = 1
		
	end -- close for

-- TO DO: move it left!
return table.concat(output)

end -- close function


return export