Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Pandoc:ヌジョレーボーボーするLuaフィルタ
-- noujolais.lua
--
-- @copyright 2019 Takayuki YATO (aka. "ZR")
-- GitHub: https://github.com/zr-tex8r
-- Twitter: @zr_tex8r
-- This program is distributed under the MIT License.
--
---------------------------------------- ヌジョレーボーボー
-- 準備
math.randomseed(os.time())
local kana_type = {}
local function kana_type_assign(str, kt)
for p, uc in utf8.codes(str) do
kana_type[utf8.char(uc)] = kt
end
end
kana_type_assign('ぁぃぅぇぉゃゅょゎァィゥェォャュョヮ', 1)
kana_type_assign('ーっんッン', 2)
--- noujolais(str)
-- 文字列strに"ヌジョレーボーボー変換"を施した結果の文字列を返す.
local function noujolais(str)
local parts, idxs = {}, {}
for p, uc in utf8.codes(str) do
local ch = utf8.char(uc)
local kt = kana_type[ch] or 0
if #parts > 0 and kt == 1 then
parts[#parts] = parts[#parts]..ch
else
parts[#parts + 1] = ch
if kt ~= 2 then
idxs[#idxs + 1] = #parts
end
end
end
for k = #idxs, 1, -1 do
local p = math.random(k)
parts[idxs[p]], parts[idxs[k]] = parts[idxs[k]], parts[idxs[p]]
idxs[p], idxs[k] = idxs[k], idxs[p]
end
return table.concat(parts)
end
---------------------------------------- フィルタ定義
-- Span要素に対する処理.
function Span(span)
-- Spanがnoujolaisクラスつきならば
if span.classes:includes('noujolais', 1) then
-- 内容が単一のStrであるならば
if #span.content == 1 and span.content[1].t == 'Str' then
local str = span.content[1] -- Str要素
str.text = noujolais(str.text)
-- spanを返すことを忘れずに!
return span
else -- それ以外はエラー
error('bad noujolais span')
end
end
end
---------------------------------------- おしまい
-- EOF
「今年の[ボジョレーヌーボー]{.noujolais}は令和になって最高の出来栄え。」
<!DOCTYPE html>
<html><head>
<meta charset="UTF-8">
</head><body>
<ul>
<li>「今年の<span class="noujolais">ボジョレーヌーボー</span>は令和になって最高の出来栄え。」</li>
</ul>
</body></html>
@zr-tex8r

This comment has been minimized.

Copy link
Owner Author

zr-tex8r commented Dec 20, 2019

pandoc test1.md --lua-filter noujolais.lua -o test1.docx
pandoc test2.html --lua-filter noujolais.lua -o test2.tex
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.