-- | Custom Pandoc filters, like the ruby-text transform
module MathiasSM.Filters (processRubyText) where

import Data.Text qualified as T
import Text.Pandoc (Inline (Link, RawInline, Str))

{- | Change special syntax (overloading Links) into ruby-annotated text

Ex. @[飯](-はん)@ into @<ruby>飯<rt>はん</rt></ruby>@

NOTE: Uses "RawInline" since there's no native AST representation

NOTE: Author should manually add @{lang=ja/ko/zh/...}@ for accesibility and using correct CJK font
-}
processRubyText :: Inline -> Inline
processRubyText :: Inline -> Inline
processRubyText x :: Inline
x@(Link Attr
_ [Str Text
kanji] (Text
src, Text
_)) =
    case Text -> Maybe (Char, Text)
T.uncons Text
src of
        Just (Char
'-', Text
ruby) ->
            Format -> Text -> Inline
RawInline Format
"html" (Text -> Inline) -> Text -> Inline
forall a b. (a -> b) -> a -> b
$
                Text
"<ruby>" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
kanji Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"<rp>(</rp><rt>" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
ruby Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"</rt><rp>)</rp></ruby>"
        Maybe (Char, Text)
_ -> Inline
x
processRubyText Inline
x = Inline
x