Mudanças entre as edições de "Módulo:MapaJson"
Ir para navegação
Ir para pesquisar
(Página substituída por '-- Módulo:MapaJson -- Carrega JSON de uma página e retorna URL encoded local p = {} function p.loadAndEncode(frame) local pageName = frame.args[1] or ''...') Etiqueta: Substituído |
|||
| Linha 1: | Linha 1: | ||
-- Módulo: | -- Módulo:JSONLoader | ||
-- Carrega JSON de uma página e retorna URL encoded | -- Carrega JSON de uma página e retorna URL encoded corretamente | ||
local p = {} | local p = {} | ||
| Linha 19: | Linha 19: | ||
local jsonContent = title:getContent() or '' | local jsonContent = title:getContent() or '' | ||
-- | -- Remover espaços extras e quebras de linha | ||
jsonContent = jsonContent:gsub("\n", "") | jsonContent = jsonContent:gsub("\n", "") | ||
jsonContent = jsonContent:gsub("\r", "") | jsonContent = jsonContent:gsub("\r", "") | ||
| Linha 25: | Linha 25: | ||
jsonContent = jsonContent:gsub(" +", " ") | jsonContent = jsonContent:gsub(" +", " ") | ||
-- Codificar para URL | -- Codificar para URL corretamente (RFC 3986) | ||
local encoded = mw.uri.encode(jsonContent, ' | -- Usar função personalizada para evitar conversão de espaços para _ | ||
local encoded = mw.uri.encode(jsonContent, 'PATH') | |||
return encoded | return encoded | ||
end | |||
-- Versão alternativa que codifica tudo (recomendado) | |||
function p.loadAndEncodeFull(frame) | |||
local pageName = frame.args[1] or '' | |||
if pageName == '' then | |||
return '' | |||
end | |||
local title = mw.title.new(pageName) | |||
if not title or not title.exists then | |||
return '' | |||
end | |||
local jsonContent = title:getContent() or '' | |||
-- Remover espaços extras e quebras de linha | |||
jsonContent = jsonContent:gsub("\n", "") | |||
jsonContent = jsonContent:gsub("\r", "") | |||
jsonContent = jsonContent:gsub("\t", " ") | |||
jsonContent = jsonContent:gsub(" +", " ") | |||
-- Codificar tudo (espaços viram %20, não _) | |||
local encoded = mw.uri.encode(jsonContent, 'COMPONENT') | |||
return encoded | |||
end | |||
-- Função que retorna o JSON limpo sem codificação (para debug) | |||
function p.loadRaw(frame) | |||
local pageName = frame.args[1] or '' | |||
if pageName == '' then | |||
return '' | |||
end | |||
local title = mw.title.new(pageName) | |||
if not title or not title.exists then | |||
return '' | |||
end | |||
local jsonContent = title:getContent() or '' | |||
-- Apenas remover espaços extras | |||
jsonContent = jsonContent:gsub("\n", "") | |||
jsonContent = jsonContent:gsub("\r", "") | |||
jsonContent = jsonContent:gsub("\t", " ") | |||
jsonContent = jsonContent:gsub(" +", " ") | |||
return jsonContent | |||
end | end | ||
return p | return p | ||
Edição das 20h56min de 9 de abril de 2026
A documentação para este módulo pode ser criada em Módulo:MapaJson/doc
-- Módulo:JSONLoader
-- Carrega JSON de uma página e retorna URL encoded corretamente
local p = {}
function p.loadAndEncode(frame)
local pageName = frame.args[1] or ''
if pageName == '' then
return ''
end
-- Buscar o conteúdo da página
local title = mw.title.new(pageName)
if not title or not title.exists then
return ''
end
local jsonContent = title:getContent() or ''
-- Remover espaços extras e quebras de linha
jsonContent = jsonContent:gsub("\n", "")
jsonContent = jsonContent:gsub("\r", "")
jsonContent = jsonContent:gsub("\t", " ")
jsonContent = jsonContent:gsub(" +", " ")
-- Codificar para URL corretamente (RFC 3986)
-- Usar função personalizada para evitar conversão de espaços para _
local encoded = mw.uri.encode(jsonContent, 'PATH')
return encoded
end
-- Versão alternativa que codifica tudo (recomendado)
function p.loadAndEncodeFull(frame)
local pageName = frame.args[1] or ''
if pageName == '' then
return ''
end
local title = mw.title.new(pageName)
if not title or not title.exists then
return ''
end
local jsonContent = title:getContent() or ''
-- Remover espaços extras e quebras de linha
jsonContent = jsonContent:gsub("\n", "")
jsonContent = jsonContent:gsub("\r", "")
jsonContent = jsonContent:gsub("\t", " ")
jsonContent = jsonContent:gsub(" +", " ")
-- Codificar tudo (espaços viram %20, não _)
local encoded = mw.uri.encode(jsonContent, 'COMPONENT')
return encoded
end
-- Função que retorna o JSON limpo sem codificação (para debug)
function p.loadRaw(frame)
local pageName = frame.args[1] or ''
if pageName == '' then
return ''
end
local title = mw.title.new(pageName)
if not title or not title.exists then
return ''
end
local jsonContent = title:getContent() or ''
-- Apenas remover espaços extras
jsonContent = jsonContent:gsub("\n", "")
jsonContent = jsonContent:gsub("\r", "")
jsonContent = jsonContent:gsub("\t", " ")
jsonContent = jsonContent:gsub(" +", " ")
return jsonContent
end
return p