diff --git a/langfilter/Spans.hs b/langfilter/Spans.hs index 29d722f..3da9a30 100644 --- a/langfilter/Spans.hs +++ b/langfilter/Spans.hs @@ -14,11 +14,12 @@ spans :: Vars => Inline -> IO Inline spans = \case Code attrs txt | Just ('\\', txt') <- Text.uncons txt -> pure $ Code attrs txt' - | Just _ <- enclosed '/' '/' txt -> pure $ ipaB txt - | Just txt' <- enclosed '⫽' '⫽' txt -> pure $ ipaA txt' - | Just _ <- enclosed '[' ']' txt -> pure $ ipaN txt - | Just txt' <- enclosed '{' '}' txt -> lang txt' - | Just txt' <- enclosed '!' '!' txt -> pure $ abbr txt' + | Just txt' <- enclosed "⫽" "⫽" txt -> pure $ ipaA txt' + | Just txt' <- enclosed "//" "//" txt -> pure $ ipaA txt' + | Just _ <- enclosed "/" "/" txt -> pure $ ipaB txt + | Just _ <- enclosed "[" "]" txt -> pure $ ipaN txt + | Just txt' <- enclosed "{" "}" txt -> lang txt' + | Just txt' <- enclosed "!" "!" txt -> pure $ abbr txt' i -> pure i ipaA, ipaB, ipaN, abbr :: Text -> Inline @@ -27,6 +28,9 @@ ipaB = Span (cls ["ipa", "ipa-broad"]) . text' ipaN = Span (cls ["ipa", "ipa-narrow"]) . text' abbr = Span (cls ["abbr"]) . text' . endash +surround :: Text -> Text -> Text +surround s txt = s <> txt <> s + text' :: Text -> [Inline] text' = toList . text @@ -60,12 +64,13 @@ cls :: [Text] -> Attr cls cs = ("", cs, []) -enclosed :: Char -> Char -> Text -> Maybe Text +enclosed :: Text -> Text -> Text -> Maybe Text enclosed o c txt - | Text.length txt >= 2, - Text.head txt == o, - Text.last txt == c - = Just $ Text.init $ Text.tail txt + | Text.length txt >= ℓo + ℓc, + Text.take ℓo txt == o, + Text.takeEnd ℓc txt == c + = Just $ Text.drop ℓo $ Text.dropEnd ℓc txt + where ℓo = Text.length o; ℓc = Text.length c enclosed _ _ _ = Nothing