add initial version of pandoc filter

This commit is contained in:
Rhiannon Morris 2020-08-18 18:33:25 +02:00
parent 22b4411641
commit d88c4c14b3
5 changed files with 64 additions and 2 deletions

View file

@ -0,0 +1,23 @@
cabal-version: 2.2
name: langfilter
version: 0.1.0
synopsis: filter for conlang stuff
license: AGPL-3.0-or-later
author: Rhiannon Morris <rhi@rhiannon.website>
maintainer: Rhiannon Morris <rhi@rhiannon.website>
executable langfilter
hs-source-dirs: .
main-is: langfilter.hs
ghc-options:
-Wall -threaded -rtsopts -with-rtsopts=-N
default-language: Haskell2010
default-extensions:
BlockArguments,
LambdaCase,
PatternSynonyms,
ViewPatterns
build-depends:
base ^>= 4.14.0.0,
pandoc-types == 1.17.*

27
langfilter/langfilter.hs Normal file
View file

@ -0,0 +1,27 @@
import Text.Pandoc.Definition
import Text.Pandoc.JSON
import Text.Pandoc.Builder
main :: IO ()
main = toJSONFilter \case
Code _ txt
| Just _ <- enclosed '/' '/' txt ->
Span (cls ["ipa", "ipa-broad"]) $ text' txt
| Just _ <- enclosed '[' ']' txt ->
Span (cls ["ipa", "ipa-narrow"]) $ text' txt
| Just txt' <- enclosed '{' '}' txt ->
Span (cls ["lang"]) $ text' txt'
i -> i
cls :: [String] -> Attr
cls cs = ("", cs, [])
text' :: String -> [Inline]
text' = toList . text
enclosed :: Char -> Char -> String -> Maybe String
enclosed o c txt
| length txt >= 2, head txt == o, last txt == c
= Just $ init $ tail txt
enclosed _ _ _ = Nothing