2021-04-29 05:52:44 -04:00
|
|
|
|
module Glosses (glosses) where
|
|
|
|
|
|
|
|
|
|
import Lang
|
|
|
|
|
import LaantasImage
|
2021-05-20 16:15:24 -04:00
|
|
|
|
import Spans (abbrs)
|
|
|
|
|
import qualified Spans
|
2021-04-29 05:52:44 -04:00
|
|
|
|
|
|
|
|
|
import Text.Pandoc.Definition
|
|
|
|
|
import Text.Pandoc.Builder
|
|
|
|
|
import Text.Pandoc.Walk
|
|
|
|
|
import Data.Maybe
|
|
|
|
|
import Data.Text (Text)
|
2021-04-29 13:02:20 -04:00
|
|
|
|
import qualified Data.Text as Text
|
2021-04-29 05:52:44 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
glosses :: Vars => Block -> IO [Block]
|
|
|
|
|
glosses = \case
|
2021-04-29 13:02:20 -04:00
|
|
|
|
Div (i, cs, _) blocks | "glosses" `elem` cs -> do
|
2021-04-29 05:52:44 -04:00
|
|
|
|
tables <- traverse glossTable blocks
|
|
|
|
|
pure $
|
2021-04-29 13:02:20 -04:00
|
|
|
|
[RawBlock (Format "html") $ "<figure" <> id <> classes <> ">"] ++
|
2021-04-29 05:52:44 -04:00
|
|
|
|
catMaybes tables ++
|
|
|
|
|
[RawBlock (Format "html") "</figure>"]
|
2021-04-29 13:02:20 -04:00
|
|
|
|
where
|
|
|
|
|
id = if i == "" then "" else " id=\"" <> i <> "\""
|
|
|
|
|
classes = " class=\"" <> Text.unwords cs <> "\""
|
2021-04-29 05:52:44 -04:00
|
|
|
|
b -> pure [b]
|
|
|
|
|
|
|
|
|
|
pattern Gloss l g w t = BulletList [[Plain l], [Plain g], [Plain w], [Plain t]]
|
|
|
|
|
pattern PGloss l p g w t =
|
|
|
|
|
BulletList [[Plain l], [Plain p], [Plain g], [Plain w], [Plain t]]
|
2021-06-03 23:33:22 -04:00
|
|
|
|
pattern PNGloss l b n g w t =
|
|
|
|
|
BulletList [[Plain l], [Plain b], [Plain n], [Plain g], [Plain w], [Plain t]]
|
2021-04-29 05:52:44 -04:00
|
|
|
|
|
|
|
|
|
glossTable :: Vars => Block -> IO (Maybe Block)
|
|
|
|
|
glossTable = \case
|
2021-06-03 23:33:22 -04:00
|
|
|
|
Gloss l s g t -> Just <$> make l Nothing Nothing s g t
|
|
|
|
|
PGloss l p s g t -> Just <$> make l (Just p) Nothing s g t
|
|
|
|
|
PNGloss l b n s g t -> Just <$> make l (Just b) (Just n) s g t
|
|
|
|
|
HorizontalRule -> pure Nothing
|
|
|
|
|
b -> error $ "found " ++ show b ++ " in gloss section"
|
2021-04-29 05:52:44 -04:00
|
|
|
|
where
|
2021-06-03 23:33:22 -04:00
|
|
|
|
make l b n s g t = do
|
|
|
|
|
let ℓ = length $ splitInlines s
|
|
|
|
|
let colspecs = replicate ℓ (AlignDefault, ColWidthDefault)
|
2024-11-26 00:08:10 -05:00
|
|
|
|
let l' = cell1 ℓ $ underlines $ noHash l
|
|
|
|
|
b' = cell1 ℓ <$> b; n' = cell1 ℓ <$> n
|
2021-06-03 23:33:22 -04:00
|
|
|
|
let ss = cells s; gs = cells g; t' = cell1 ℓ t
|
2021-04-29 05:52:44 -04:00
|
|
|
|
img <- case ?lang of
|
|
|
|
|
Just Lántas ->
|
2021-06-03 23:33:22 -04:00
|
|
|
|
[Just $ cell1 ℓ [img] | img <- makeImage $ splitImage' $ stripInlines l]
|
2021-04-29 05:52:44 -04:00
|
|
|
|
Nothing -> pure Nothing
|
|
|
|
|
pure $ Table ("", ["gloss"], []) (Caption Nothing []) colspecs
|
|
|
|
|
(TableHead mempty [])
|
|
|
|
|
[TableBody mempty (RowHeadColumns 0) [] $ concat
|
|
|
|
|
[[row ["gloss-scr", "scr"] [i] | Just i <- [img]],
|
|
|
|
|
[row ["gloss-lang", "lang"] [l']],
|
2021-06-03 23:33:22 -04:00
|
|
|
|
[row ["gloss-pron", "ipa"] [b] | Just b <- [b']],
|
|
|
|
|
[row ["gloss-pron", "ipa"] [n] | Just n <- [n']],
|
2021-04-29 05:52:44 -04:00
|
|
|
|
[row ["gloss-split", "lang"] ss],
|
|
|
|
|
[row ["gloss-gloss"] gs],
|
|
|
|
|
[row ["gloss-trans"] [t']]]]
|
|
|
|
|
(TableFoot mempty [])
|
|
|
|
|
cell is = Cell mempty AlignDefault (RowSpan 1) (ColSpan 1) [Plain is]
|
2021-06-03 23:33:22 -04:00
|
|
|
|
cell1 ℓ is = Cell mempty AlignDefault (RowSpan 1) (ColSpan ℓ) [Plain is]
|
2021-04-29 05:52:44 -04:00
|
|
|
|
cells = map (cell . concatMap abbrs) . splitInlines
|
|
|
|
|
row c = Row ("", c, [])
|
|
|
|
|
|
|
|
|
|
stripInlines :: [Inline] -> Text
|
|
|
|
|
stripInlines = query \case
|
|
|
|
|
Str s -> s
|
|
|
|
|
Space -> " "
|
|
|
|
|
SoftBreak -> " "
|
|
|
|
|
LineBreak -> " "
|
|
|
|
|
_ -> ""
|
|
|
|
|
|
|
|
|
|
splitInlines :: [Inline] -> [Inlines]
|
|
|
|
|
splitInlines is = filter (not . null) $ go is where
|
|
|
|
|
go [] = []
|
|
|
|
|
go is =
|
|
|
|
|
let (is1, is') = break (== Space) is in
|
|
|
|
|
fromList is1 : splitInlines (dropWhile (== Space) is')
|
2021-05-20 16:15:24 -04:00
|
|
|
|
|
|
|
|
|
underlines :: [Inline] -> [Inline]
|
2024-06-02 22:07:09 -04:00
|
|
|
|
underlines = concatMap underlineStr . takeWhile (/= Str "|") where
|
|
|
|
|
underlineStr = \case
|
|
|
|
|
Str txt -> Spans.underlines txt
|
|
|
|
|
i -> [i]
|
2024-11-26 00:08:10 -05:00
|
|
|
|
|
|
|
|
|
noHash :: [Inline] -> [Inline]
|
|
|
|
|
noHash = walk \case
|
|
|
|
|
Str str -> Str $ Text.filter (/= '#') str
|
|
|
|
|
i -> i
|