« Module:LxTerm » : différence entre les versions

De Adadov.net wiki
Aucun résumé des modifications
Aucun résumé des modifications
 
(133 versions intermédiaires par le même utilisateur non affichées)
Ligne 1 : Ligne 1 :
local documentation = require('Module:Documentation').main
local documentation = require('Module:Documentation').main
local getArgs = require('Module:Arguments').getArgs
local randomString = require('Module:RandomString').randomString
local p = {}


local p = {}
local function _definePrompt(frame)
--[{{{user|user}}}@linux] {{#ifeq: {{{user}}}|root|# |$ }}
local args = getArgs(frame)
local userArg = args.user or nil
local prompt = "$ "
if not userArg then
userArg = "user"
elseif userArg == "root" then
prompt = "# "
end
return "["..userArg.."@linux] "..prompt
end


function p.main(frame)
function p.main(frame)
local text = frame.args.text
local text = frame.args.text
local prompt = frame.args.prompt
local prompt = _definePrompt(frame)
local result = ""
local result = ""
local sameCommand = false
local sameCommand = false
local ret = ''
local firstLine = true
 
local html = mw.html.create('table')
html:cssText('width: 100%')
:addClass('lxterm-code')
 
for line in text:gmatch("[^\n]+") do
for line in text:gmatch("[^\n]+") do
line = line:gsub("\n","")
line = line:gsub('\n+', '')
line = line:gsub("<br>","")
ret = ret..line.."\n"
 
local tr = html:tag('tr')
local newline
 
if sameCommand then
if sameCommand then
line = "> "..line
local td = tr:tag('td')
td:addClass('prompt')
:css('text-align', 'right')
:wikitext(" > ")
:done()
sameCommand = false
sameCommand = false
else
else
line = prompt..line
local td = tr:tag('td')
td:addClass('prompt')
td:wikitext(prompt)
:done()
end
end
local td = tr:tag('td')
if not firstLine then
td:attr('colspan', 2)
end
td:wikitext(line)
:done()
if line:find("\\") then
if firstLine then
local td = tr:tag('td')
td:addClass('switch')
td:wikitext('dblclick to copy')
:done()
firstLine = false
end
if line:find("\\\s*$") then
sameCommand = true
sameCommand = true
end
end
result = result..line.."\n"
tr:done()
end
-- result = result..tostring(html).."\n"
local pre=mw.html.create('pre')
:addClass('lxterm-code')
:wikitext(result)
:allDone()
local hidden = mw.html.create('div')
hidden:addClass('lxterm-txt')
:css('display', 'none')
:cssText('width: 100%; color: black;')
:wikitext('<nowiki>'..ret..'</nowiki>')
:allDone()
 
local out = mw.html.create('div')
out:wikitext(tostring(html))
:attr('title', 'Double-click pour copier facilement')
:addClass('js-lxterm')
:wikitext(tostring(hidden))
:allDone()
return tostring(out)
end
 
function p.mulCommands(frame)
local args = getArgs(frame)
local prompt = _definePrompt(frame)
local text = frame.args.text
 
local nbcmd = 0
local ret = ''
local nbs = ''
local linenb = true
-- Numéro de commande demandé
if args.cmdnum then
linenb = true
end
 
local html = mw.html.create('table')
html:addClass('lxterm')
mw.logObject(args)
for txtline in text:gmatch("[^\n]+") do
local v = txtline
ret = ret..txtline.."\n"
if linenb == true then --Compte les commandes si nécessaire
nbcmd = nbcmd+1
end
 
local line=mw.html.create('tr')
if linenb == true then -- Cellule pour le numéro de commande
line:tag('td')
:addClass('lxterm-linenb')
:wikitext(nbcmd)
:done()
end
local l=0
local linetmp=''
 
v = v:gsub('([^\n]+)\n', '%1 <br /> > ') -- On met les retour à la ligne pour les commandes sur plusieurs lignes
v = v:gsub('\\ <br />', '<br />') -- On supprime les \ présents
v = v:gsub('<br />', '\\ <br />') -- Pour en ajouter partout
v = v:gsub('\\ <br /> > $', '')
 
-- Création de la cellule pour la commande
line:tag('td')
:addClass('lxterm-cmd')
:wikitext(prompt.." "..v)
:allDone()
 
html:wikitext(tostring(line))
end
end
html:allDone()
local txt = mw.html.create('pre')
:addClass('lxterm-txt')
:addClass('hide')
:wikitext(ret)
:allDone()
return tostring(html)..tostring(txt)
end
function p.singleCommand(frame)
local args = getArgs(frame)
local prompt = _definePrompt(frame)
local sameCommand = false
local v = args.text or frame.args[1]
local result = ""
v = v:gsub('([^\n]+)\n', '%1 <br /> > ') -- On met les retour à la ligne pour les commandes sur plusieurs lignes
v = v:gsub('\\ <br />', '<br />') -- On supprime les \ présents
v = v:gsub('<br />', '\\ <br />') -- Pour en ajouter partout
v = v:gsub('\\ <br /> > $', '')
local script = mw.html.create('script')
script:wikitext('')
local line=mw.html.create('table')
line:attr('id','test')
:addClass('lxterm-switch')
:tag('tr')
:tag('td')
:addClass('lxterm-cmd')
:wikitext(prompt..v)
:allDone()
local hidtxt = v:gsub(' > ', '')
hidtxt = hidtxt:gsub('<br />', '\n')
local hidden = mw.html.create('div')
hidden:attr('id', 'test-hid')
:addClass('lxterm-txt')
:cssText('border:1px solid black; background-color: white; width: 70%; margin: 10px auto; padding: 10px; color: black;')
:wikitext(hidtxt)
:wikitext('<a href="" id="test-btn">switch</a>')
:allDone()


return result
return tostring(line)..tostring(hidden)
end
end


return p
return p

Dernière version du 23 janvier 2023 à 17:35

Documentation icon Documentation Module[voir] [éditer] [historique] [purger]

Ce module implémente {{LxTerm}}, il met en forme les lignes de commande et ajoute les prompt.

{{#invoke:LxTerm|main|cmdnum=1|user=root|text=test \
test2}}
[root@linux] # test \dblclick to copy
> test2
{{#invoke:LxTerm|main|cmdnum=1|user=root|text=test
test2}}
[root@linux] # testdblclick to copy
[root@linux] # test2
{{#invoke:LxTerm|main|user=nobody|text=test2}}
[nobody@linux] $ test2dblclick to copy
{{#invoke:LxTerm|main|text=test3}}
[user@linux] $ test3dblclick to copy

local documentation = require('Module:Documentation').main
local getArgs = require('Module:Arguments').getArgs
local randomString = require('Module:RandomString').randomString
local p = {}

local function _definePrompt(frame)
	--[{{{user|user}}}@linux] {{#ifeq: {{{user}}}|root|# |$ }}
	local args = getArgs(frame)
	local userArg = args.user or nil
	local prompt = "$ "
	if not userArg then
		userArg = "user"
	elseif userArg == "root" then
		prompt = "# "
	end
	return "["..userArg.."@linux] "..prompt
end

function p.main(frame)
	local text = frame.args.text
	local prompt = _definePrompt(frame)
	local result = ""
	local sameCommand = false
	local ret = ''
	local firstLine = true

	local html = mw.html.create('table')
	html:cssText('width: 100%')
		:addClass('lxterm-code')

	for line in text:gmatch("[^\n]+") do
		line = line:gsub('\n+', '')
		ret = ret..line.."\n"

		local tr = html:tag('tr')
		local newline

		if sameCommand then
			local td = tr:tag('td')
			td:addClass('prompt')
				:css('text-align', 'right')
				:wikitext(" > ")
				:done()
			sameCommand = false
		else
			local td = tr:tag('td')
			td:addClass('prompt')
			td:wikitext(prompt)
			:done()
		end
		local td = tr:tag('td')
		if not firstLine then
			td:attr('colspan', 2)
		end
		td:wikitext(line)
			:done()
		
		if firstLine then
			local td = tr:tag('td')
			td:addClass('switch')
			td:wikitext('dblclick to copy')
			:done()
			firstLine = false
		end			
		
		if line:find("\\\s*$") then
			sameCommand = true
		end
		tr:done()
	end
--	result = result..tostring(html).."\n"
	
	
	local pre=mw.html.create('pre')
		:addClass('lxterm-code')
		:wikitext(result)
		:allDone()
		
	local hidden = mw.html.create('div')
	hidden:addClass('lxterm-txt')
		:css('display', 'none')
		:cssText('width: 100%; color: black;')
		:wikitext('<nowiki>'..ret..'</nowiki>')
		:allDone()

	local out = mw.html.create('div')
	out:wikitext(tostring(html))
		:attr('title', 'Double-click pour copier facilement')
		:addClass('js-lxterm')
		:wikitext(tostring(hidden))
		:allDone()
	return tostring(out)
end

function p.mulCommands(frame)
	local args = getArgs(frame)
	local prompt = _definePrompt(frame)
	local text = frame.args.text

	local nbcmd = 0
	local ret = ''
	local nbs = ''
	local linenb = true
	
	-- Numéro de commande demandé
	if args.cmdnum then
		linenb = true
	end

	local html = mw.html.create('table')
	html:addClass('lxterm')
	mw.logObject(args)
	for txtline in text:gmatch("[^\n]+") do
		local v = txtline
		ret = ret..txtline.."\n"
		
		if linenb == true then --Compte les commandes si nécessaire
			nbcmd = nbcmd+1
		end

		local line=mw.html.create('tr')
		if linenb == true then -- Cellule pour le numéro de commande
			line:tag('td')
				:addClass('lxterm-linenb')
				:wikitext(nbcmd)
				:done()
		end
		local l=0
		local linetmp=''

		v = v:gsub('([^\n]+)\n', '%1 <br /> > ') -- On met les retour à la ligne pour les commandes sur plusieurs lignes
		v = v:gsub('\\ <br />', '<br />') -- On supprime les \ présents
		v = v:gsub('<br />', '\\ <br />') -- Pour en ajouter partout
		v = v:gsub('\\ <br /> > $', '')

		-- Création de la cellule pour la commande
		line:tag('td')
			:addClass('lxterm-cmd')
			:wikitext(prompt.." "..v)
			:allDone()

		html:wikitext(tostring(line))
	end
	html:allDone()

	local txt = mw.html.create('pre')
	:addClass('lxterm-txt')
	:addClass('hide')
	:wikitext(ret)
	:allDone()

	return tostring(html)..tostring(txt)
end

function p.singleCommand(frame)
	local args = getArgs(frame)
	local prompt = _definePrompt(frame)
	local sameCommand = false
	local v = args.text or frame.args[1]

	local result = ""
	
	v = v:gsub('([^\n]+)\n', '%1 <br /> > ') -- On met les retour à la ligne pour les commandes sur plusieurs lignes
	v = v:gsub('\\ <br />', '<br />') -- On supprime les \ présents
	v = v:gsub('<br />', '\\ <br />') -- Pour en ajouter partout
	v = v:gsub('\\ <br /> > $', '')

	local script = mw.html.create('script')
	script:wikitext('')

	local line=mw.html.create('table')
	line:attr('id','test')
		:addClass('lxterm-switch')
		:tag('tr')
		:tag('td')
		:addClass('lxterm-cmd')
		:wikitext(prompt..v)
		:allDone()

	local hidtxt = v:gsub(' > ', '')
	hidtxt = hidtxt:gsub('<br />', '\n')
	local hidden = mw.html.create('div')
	hidden:attr('id', 'test-hid')
		:addClass('lxterm-txt')
		:cssText('border:1px solid black; background-color: white; width: 70%; margin: 10px auto; padding: 10px; color: black;')
		:wikitext(hidtxt)
		:wikitext('<a href="" id="test-btn">switch</a>')
		:allDone()

	return tostring(line)..tostring(hidden)
end

return p