{-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE OverloadedStrings #-} module Text.Pandoc.SideNote (usingSideNotes) where import Data.List (intercalate) import Data.Text (append, pack) import Control.Monad.State import Text.Pandoc.JSON import Text.Pandoc.Walk (walk, walkM) data NoteType = SideNote | MarginNote | FootNote deriving (Show, Eq) getFirstStr :: [Block] -> (NoteType, [Block]) getFirstStr blocks@(block:blocks') = case block of Plain ((Str "{-}"):Space:rest) -> (MarginNote, (Plain rest):blocks') Plain ((Str "{.}"):Space:rest) -> (FootNote, (Plain rest):blocks') Para ((Str "{-}"):Space:rest) -> (MarginNote, (Para rest):blocks') Para ((Str "{.}"):Space:rest) -> (FootNote, (Para rest):blocks') LineBlock (((Str "{-}"):Space:rest):rest') -> (MarginNote, (LineBlock (rest:rest')):blocks') LineBlock (((Str "{.}"):Space:rest):rest') -> (FootNote, (LineBlock (rest:rest')):blocks') _ -> (SideNote, blocks) getFirstStr blocks = (SideNote, blocks) newline :: [Inline] newline = [LineBreak, LineBreak] -- This could be implemented more concisely, but I think this is more clear. getThenIncr :: State Int Int getThenIncr = do i <- get put (i + 1) return i -- Extract inlines from blocks. Note has a [Block], but Span needs [Inline]. coerceToInline :: [Block] -> [Inline] coerceToInline = concatMap deBlock . walk deNote where deBlock :: Block -> [Inline] deBlock (Plain ls ) = ls -- Simulate paragraphs with double LineBreak deBlock (Para ls ) = ls ++ newline -- See extension: line_blocks deBlock (LineBlock lss ) = intercalate [LineBreak] lss ++ newline -- Pretend RawBlock is RawInline (might not work!) -- Consider: raw