aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJustin Bedo <cu@cua0.org>2022-02-13 08:11:34 +1100
committerJustin Bedo <cu@cua0.org>2022-02-13 08:11:34 +1100
commitfe80e94838d3701f83eef1f3cb45c087bf9130ac (patch)
treea62e96bf11bf35b24030799e270ab36d3cfb30d8
parentb1b65c7a3a864a1607d1be144e5eb186cd78a5cb (diff)
Passthrough maths but change delimiters
-rw-r--r--README3
-rw-r--r--latexfmt.hs18
2 files changed, 18 insertions, 3 deletions
diff --git a/README b/README
index cf1d4fd..665b7c4 100644
--- a/README
+++ b/README
@@ -1,2 +1,3 @@
This is an idempotent latex formatter. Sentences are broken on new lines to
-limit conflicts. Reads from STDIN and writes formatted output to STDOUT.
+limit conflicts. Reads from STDIN and writes formatted output to STDOUT. Maths
+delimeters are converted to LaTeX and passed through without formatting.
diff --git a/latexfmt.hs b/latexfmt.hs
index 1dd6a88..86c17a3 100644
--- a/latexfmt.hs
+++ b/latexfmt.hs
@@ -13,7 +13,7 @@ import Prelude hiding ( print
, takeWhile
)
-data Tokens = Wrd {unwrd :: T.Text} | Cmd T.Text | BSq [Tokens] | BCr [Tokens] | NL | Cmt T.Text | Nobs
+data Tokens = Wrd {unwrd :: T.Text} | Cmd T.Text | BSq [Tokens] | BCr [Tokens] | NL | Cmt T.Text | Nobs | IMath T.Text | Math T.Text
deriving Show
isCtrl x = not $ isAlpha x || isDigit x
@@ -53,11 +53,20 @@ toStr (Cmt c) = do
toStr Nobs = do
print' "~"
suppress
+toStr (Math m) = do
+ printnl
+ print "\\["
+ print' m
+ println "\\]"
+toStr (IMath m) = do
+ print "\\("
+ print' m
+ print' "\\)"
-- Tokeniser
ws = inClass " \t\n"
pad = takeWhile ws
-token = nl <|> pad *> (nobs <|> cmd <|> bsq <|> bcr <|> cmt <|> wrd)
+token = nl <|> pad *> (nobs <|> math <|> cmd <|> bsq <|> bcr <|> cmt <|> wrd)
nobs = string "~" *> pure Nobs
nl = string "\n" *> takeWhile (inClass " \t") *> string "\n" *> pure NL
cmd :: Parser Tokens
@@ -66,6 +75,11 @@ bsq = BSq <$> (string "[" *> many' token) <* pad <* string "]"
bcr = BCr <$> (string "{" *> many' token) <* pad <* string "}"
wrd = Wrd <$> takeWhile1 (notInClass " \t\n[]{}\\~")
cmt = Cmt <$> (takeWhile1 (== '%') *> takeWhile (/= '\n'))
+math = oldmath <|> newmath <|> oldimath <|> newimath
+oldmath = Math <$> (string "$$" *> takeWhile (/= '$')) <* string "$$"
+newmath = Math . T.pack <$> (string "\\[" *> manyTill' anyChar (string "\\]"))
+oldimath = IMath <$> (string "$" *> takeWhile (/= '$')) <* string "$"
+newimath = IMath . T.pack <$> (string "\\(" *> manyTill' anyChar (string "\\)"))
-- indented printer
type Beginning = Bool