Skip to content

Commit

Permalink
LaTeX reader: support ams proof environment.
Browse files Browse the repository at this point in the history
  • Loading branch information
jgm committed Jul 22, 2020
1 parent 7faa9d9 commit 65865b3
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/Text/Pandoc/Readers/LaTeX.hs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import Data.List (intercalate)
import qualified Data.Map as M
import Data.Maybe (fromMaybe, maybeToList)
import qualified Data.Set as Set
import qualified Data.Sequence as Seq
import Data.Text (Text)
import qualified Data.Text as T
import System.FilePath (addExtension, replaceExtension, takeExtension)
Expand Down Expand Up @@ -1845,6 +1846,8 @@ environments = M.fromList
, ("tikzcd", rawVerbEnv "tikzcd")
, ("lilypond", rawVerbEnv "lilypond")
, ("ly", rawVerbEnv "ly")
-- amsthm
, ("proof", amsProof)
-- etoolbox
, ("ifstrequal", ifstrequal)
, ("newtoggle", braced >>= newToggle)
Expand All @@ -1853,6 +1856,30 @@ environments = M.fromList
, ("iftoggle", try $ ifToggle >> block)
]

amsProof :: PandocMonad m => LP m Blocks
amsProof = do
title <- option (B.text "Proof") opt
bs <- env "proof" blocks
return $
B.divWith ("", ["proof"], []) $
addQed $ addTitle (B.emph (title <> ".")) $ bs

addTitle :: Inlines -> Blocks -> Blocks
addTitle ils bs =
case B.toList bs of
(Para xs : rest)
-> B.fromList (Para (B.toList ils ++ (Space : xs)) : rest)
_ -> B.para ils <> bs

addQed :: Blocks -> Blocks
addQed bs =
case Seq.viewr (B.unMany bs) of
s Seq.:> Para ils
-> B.Many (s Seq.|> Para (ils ++ B.toList qedSign))
_ -> bs <> B.para qedSign
where
qedSign = B.spanWith ("",["qed"],[]) "\x220E"

environment :: PandocMonad m => LP m Blocks
environment = try $ do
controlSeq "begin"
Expand Down
10 changes: 10 additions & 0 deletions src/Text/Pandoc/Readers/LaTeX/Parsing.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ module Text.Pandoc.Readers.LaTeX.Parsing
( DottedNum(..)
, renderDottedNum
, incrementDottedNum
, TheoremSpec(..)
, LaTeXState(..)
, defaultLaTeXState
, LP
Expand Down Expand Up @@ -114,6 +115,13 @@ incrementDottedNum level (DottedNum ns) = DottedNum $
(x:xs) -> reverse (x+1 : xs)
[] -> [] -- shouldn't happen

data TheoremSpec =
TheoremSpec
{ theoremName :: Text
, theoremSeries :: Maybe Text
, theoremLastNum :: DottedNum }
deriving (Show)

data LaTeXState = LaTeXState{ sOptions :: ReaderOptions
, sMeta :: Meta
, sQuoteContext :: QuoteContext
Expand All @@ -128,6 +136,7 @@ data LaTeXState = LaTeXState{ sOptions :: ReaderOptions
, sLastHeaderNum :: DottedNum
, sLastFigureNum :: DottedNum
, sLastTableNum :: DottedNum
, sTheoremMap :: M.Map Text TheoremSpec
, sLastLabel :: Maybe Text
, sLabels :: M.Map Text [Inline]
, sHasChapters :: Bool
Expand All @@ -151,6 +160,7 @@ defaultLaTeXState = LaTeXState{ sOptions = def
, sLastHeaderNum = DottedNum []
, sLastFigureNum = DottedNum []
, sLastTableNum = DottedNum []
, sTheoremMap = M.empty
, sLastLabel = Nothing
, sLabels = M.empty
, sHasChapters = False
Expand Down

0 comments on commit 65865b3

Please sign in to comment.