Ajout inner-parens-to-brackets

This commit is contained in:
Bastien Dumont 2024-05-24 15:26:52 +02:00
parent 57b3a8e9bc
commit 1578254301
6 changed files with 246 additions and 0 deletions

View File

@ -0,0 +1,23 @@
(undo-tree-save-format-version . 1)
"199a3ff653aaa566301c36c7025bac4c9f3e90ee"
[nil nil nil nil (26192 38250 630077 19000) 0 nil]
([nil nil ((70 . 72) (t 26192 38113 372753 271000)) nil (26192 38250 630075 659000) 0 nil])
([nil nil ((72 . 93)) nil (26192 38250 630074 858000) 0 nil])
([nil nil ((93 . 95)) nil (26192 38250 630074 227000) 0 nil])
([nil nil ((73 . 77)) nil (26192 38250 630073 396000) 0 nil])
([nil nil ((#(" " 0 1 (fontified t)) . -73) (undo-tree-id5 . -1) (#("d" 0 1 (fontified t)) . -74) (undo-tree-id6 . -1) (#("o" 0 1 (fontified t)) . -75) (undo-tree-id7 . -1) (#("u" 0 1 (fontified t)) . -76) (undo-tree-id8 . -1) 77) nil (26192 38250 630071 779000) 0 nil])
([nil nil ((73 . 79)) nil (26192 38250 630066 446000) 0 nil])
([nil nil ((#("t" 0 1 (fontified t)) . 79)) nil (26192 38250 630065 485000) 0 nil])
([nil nil ((79 . 89)) nil (26192 38250 630064 813000) 0 nil])
([nil nil ((#(" is" 0 3 (fontified t)) . 89) (undo-tree-id4 . -3)) nil (26192 38250 630063 630000) 0 nil])
([nil nil ((#(" useful" 0 7 (fontified t)) . 96) (undo-tree-id1 . -5) (undo-tree-id2 . -5) (undo-tree-id3 . -7)) nil (26192 38250 630060 718000) 0 nil])
([nil nil ((#(" to" 0 3 (fontified t)) . 96) (undo-tree-id0 . -3)) nil (26192 38250 630054 177000) 0 nil])
([nil nil ((96 . 109)) nil (26192 38250 630027 658000) 0 nil])
([nil nil ((109 . 129)) nil (26192 38250 630026 149000) 0 nil])
([nil nil ((#(" " 0 1 (fontified t)) . 129)) nil (26192 38250 630018 883000) 0 nil])
([nil nil ((129 . 130) (t 26192 38250 635843 580000)) nil (26192 38252 536863 795000) 0 nil])
([nil nil ((#(" found" 0 6 (fontified t)) . 73) (undo-tree-id11 . -6) (t 26192 38252 541830 950000)) nil (26192 38280 42489 849000) 0 nil])
([nil nil ((#(" it" 0 3 (fontified t)) . 73) (undo-tree-id10 . -3)) nil (26192 38280 42486 886000) 0 nil])
([nil nil ((#("ful" 0 3 (fontified t)) . 77) (undo-tree-id9 . -3)) nil (26192 38280 42482 714000) 0 nil])
([nil current ((77 . 80)) nil (26192 38280 42461 997000) 0 nil])
nil

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,4 @@
This filter converts nested parentheses to brackets (and conversely).
I use it mostly to post-process citeproc output.

View File

@ -0,0 +1 @@
This filter converts nested parentheses to brackets (and conversely).

View File

@ -0,0 +1,91 @@
-- inner-parens-to-brackets.lua
-- A Pandoc Lua filter that converts nested parentheses to brackets
-- (and conversely).
-- Copyright 2024 Bastien Dumont (bastien.dumont [at] posteo.net)
-- This file is under the MIT License: see LICENSE for more details
local previously_signalled = {}
local function signal_if_new(id, msg)
if not previously_signalled[id] then
io.stdout:write('INFO: ' .. msg .. '\n')
previously_signalled[id] = true
end
end
function Para(para)
local whole_para = pandoc.utils.stringify(para)
local parens_trace = {}
local i_str = 0
local isolate_parens = {
-- In case there are several level of parentheses in the same Str object
-- (could theoretically happen due to non-breaking spaces).
-- Also simplifies check_and_replace() below.
Str = function(str)
local text = str.text
if string.match(text, '[][()]') then
local substrings = {}
for a, b, c in string.gmatch(text, '([^][()]*)([][()]?)([^][()]*)') do
for _, sub in ipairs({ a, b, c }) do
if sub ~= '' then table.insert(substrings, pandoc.Str(sub)) end
end
end
return substrings
end
end
}
local check_and_replace = {
traverse = 'topdown',
Str = function(str)
i_str = i_str + 1
local text = str.text
if text == '(' then
if #parens_trace > 0 then
if parens_trace[#parens_trace] == '(' then
str.text = '['
signal_if_new(whole_para .. i_str,
'Replacing a left parenthesis with a bracket ' ..
'in the following item: ' .. whole_para)
end
end
table.insert(parens_trace, str.text)
elseif text == ')' then
if #parens_trace > 0 then
if parens_trace[#parens_trace] == '[' then
str.text = ']'
signal_if_new(whole_para .. i_str,
'Replacing a right parenthesis with a bracket ' ..
'in the following item: ' .. whole_para)
end
end
table.remove(parens_trace)
elseif text == '[' then
if #parens_trace > 0 then
if parens_trace[#parens_trace] == '[' then
str.text = '('
signal_if_new(whole_para .. i_str,
'Replacing a left bracket with a parenthesis ' ..
'in the following item: ' .. whole_para)
end
end
table.insert(parens_trace, str.text)
elseif text == ']' then
if #parens_trace > 0 then
if parens_trace[#parens_trace] == '(' then
str.text = ')'
signal_if_new(whole_para .. i_str,
'Replacing a right bracket with a parenthesis ' ..
'in the following item: ' .. whole_para)
end
end
table.remove(parens_trace)
end
return str
end
}
local processed_para = para:walk(isolate_parens):walk(check_and_replace)
if #parens_trace > 0 then
io.stdout:write('WARNING: Unbalanced parentheses or brackets found in ' .. whole_para .. '\n')
end
return processed_para
end

View File

@ -0,0 +1,85 @@
local previously_signalled = {}
local function signal_if_new(id, msg)
if not previously_signalled[id] then
io.stdout:write('INFO: ' .. msg .. '\n')
previously_signalled[id] = true
end
end
function Para(para)
local whole_para = pandoc.utils.stringify(para)
local parens_trace = {}
local i_str = 0
local isolate_parens = {
-- In case there are several level of parentheses in the same Str object
-- (could theoretically happen due to non-breaking spaces).
-- Also simplifies check_and_replace() below.
Str = function(str)
local text = str.text
if string.match(text, '[][()]') then
local substrings = {}
for a, b, c in string.gmatch(text, '([^][()]*)([][()]?)([^][()]*)') do
for _, sub in ipairs({ a, b, c }) do
if sub ~= '' then table.insert(substrings, pandoc.Str(sub)) end
end
end
return substrings
end
end
}
local check_and_replace = {
traverse = 'topdown',
Str = function(str)
i_str = i_str + 1
local text = str.text
if text == '(' then
if #parens_trace > 0 then
if parens_trace[#parens_trace] == '(' then
str.text = '['
signal_if_new(whole_para .. i_str,
'Replacing a left parenthesis with a bracket ' ..
'in the following item: ' .. whole_para)
end
end
table.insert(parens_trace, str.text)
elseif text == ')' then
if #parens_trace > 0 then
if parens_trace[#parens_trace] == '[' then
str.text = ']'
signal_if_new(whole_para .. i_str,
'Replacing a right parenthesis with a bracket ' ..
'in the following item: ' .. whole_para)
end
end
table.remove(parens_trace)
elseif text == '[' then
if #parens_trace > 0 then
if parens_trace[#parens_trace] == '[' then
str.text = '('
signal_if_new(whole_para .. i_str,
'Replacing a left bracket with a parenthesis ' ..
'in the following item: ' .. whole_para)
end
end
table.insert(parens_trace, str.text)
elseif text == ']' then
if #parens_trace > 0 then
if parens_trace[#parens_trace] == '(' then
str.text = ')'
signal_if_new(whole_para .. i_str,
'Replacing a right bracket with a parenthesis ' ..
'in the following item: ' .. whole_para)
end
end
table.remove(parens_trace)
end
return str
end
}
local processed_para = para:walk(isolate_parens):walk(check_and_replace)
if #parens_trace > 0 then
io.stdout:write('WARNING: Unbalanced parentheses or brackets found in ' .. whole_para .. '\n')
end
return processed_para
end