Created
December 20, 2019 14:15
-
-
Save zr-tex8r/884826a99342cee7ec301d6dfa2be1c7 to your computer and use it in GitHub Desktop.
Pandoc:ヌジョレーボーボーするLuaフィルタ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
「今年の[ボジョレーヌーボー]{.noujolais}は令和になって最高の出来栄え。」 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html><head> | |
<meta charset="UTF-8"> | |
</head><body> | |
<ul> | |
<li>「今年の<span class="noujolais">ボジョレーヌーボー</span>は令和になって最高の出来栄え。」</li> | |
</ul> | |
</body></html> |
Author
zr-tex8r
commented
Dec 20, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment