Skip to content

Commit

Permalink
Error messages improvement and arguments for prefixes
Browse files Browse the repository at this point in the history
- Error messages improved
- Added errors for multiple definitions with the same name
- All symbols allowed in prefixes
- Excluding `\` from allowed prefix' first-symbol 
- `@Suffix`, `@NoGroup` and `@OneLine` options added to prefixes
- Arguments for prefixes added
- New type of argument: `Math`
  • Loading branch information
Lev135 authored Jun 26, 2022
1 parent 91d6aa7 commit 4694b41
Show file tree
Hide file tree
Showing 14 changed files with 481 additions and 228 deletions.
3 changes: 3 additions & 0 deletions LaTeX-generator.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ executable texgen
TupleSections
LambdaCase
FlexibleContexts
ScopedTypeVariables
ConstraintKinds
RankNTypes
ghc-options:
-Wall
-Wno-name-shadowing
Expand Down
1 change: 0 additions & 1 deletion examples/example.tex
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

\subsection{Environments}


\subsection{Prefs}


Expand Down
5 changes: 5 additions & 0 deletions examples/examples-from-wiki/prefs/args.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
First example:
\begin{argument}
arg is a: some text here suffix a
arg is b: another text suffix b
\end{argument}
9 changes: 9 additions & 0 deletions examples/examples-from-wiki/prefs/args.ttex
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
@Define
@Prefs
|# (n : String) = @TexBeginEnd "argument" @Pref "arg is $n:" @Suf "suffix $n"

First example:
|# "a" some text here
|# "b" another text


3 changes: 3 additions & 0 deletions examples/examples-from-wiki/prefs/math-args.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Example:
$ a_1 \wedge a_2 \wedge \ldots \wedge v_n $ Some text here
$ b_1 + b_2 + \ldots + v_n $ Some text there
9 changes: 9 additions & 0 deletions examples/examples-from-wiki/prefs/math-args.ttex
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
@Define
@Prefs
## (v : String) (op : Math) = @Pref "$ $v_1 $op $v_2 $op \\ldots $op v_n $"
@MathCommands
/\ = "\\wedge"

Example:
## "a" `/\` Some text here
## "b" `+` Some text there
6 changes: 6 additions & 0 deletions examples/examples-from-wiki/prefs/nogroup-oneline.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Example:
Begin 1
Begin 2
1 End.
2 End.
Begin 1 2
13 changes: 13 additions & 0 deletions examples/examples-from-wiki/prefs/nogroup-oneline.ttex
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
@Define
@Prefs
|$ = @Begin "Begin" @NoGroup @OneLine
|> = @End "End." @NoGroup @OneLine
||$ = @Begin "Begin" @OneLine

Example:
|$ 1
|$ 2
|> 1
|> 2
||$ 1
||$ 2
Binary file modified examples/out/nibergall.pdf
Binary file not shown.
27 changes: 11 additions & 16 deletions src/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

module Main where

import Control.Monad.Except (ExceptT, runExceptT)
import Control.Monad.Writer (runWriter)
import Control.Monad.Except (runExceptT)
import Control.Monad.Writer (WriterT (runWriterT), runWriter)
import Data.ByteString (writeFile)
import Data.Char (isSpace)
import Data.List.Extra (spanEnd)
Expand All @@ -14,10 +14,10 @@ import Data.Void (Void)
import qualified IOUtils
import Options.Applicative (readerError)
import qualified Options.Applicative as Opt
import Parser (Definitions, DocElement, Parser, readDoc)
import Parser (Parser, readDoc)
import Prettyprinter
( LayoutOptions (..),
PageWidth (AvailablePerLine),
PageWidth (..),
defaultLayoutOptions,
layoutSmart,
)
Expand All @@ -29,8 +29,9 @@ import Printer
PrintOpts (PrintOpts),
texDoc,
)
import Processor (prettyError, processDoc)
import Processor (processDoc)
import Text.Megaparsec (MonadParsec (eof), errorBundlePretty, parse)
import Utils (renderErrors)
import Prelude hiding (writeFile)

parsePart :: Parser a -> Text -> Either String a
Expand All @@ -43,23 +44,17 @@ parseAll p = parsePart (p <* eof)

processFile :: Options -> IO ()
processFile Options {inpFile, outpFile, pageWidth, printOpts} = do
res <-
runExceptT
( readDoc inpFile ::
ExceptT String IO (Text, (Definitions, [DocElement]))
)
(res, srcs) <- runWriterT $ runExceptT $ readDoc inpFile
case res of
Left e -> IOUtils.putStrLn e
Right (file, (defs, docEls)) -> do
Left e -> IOUtils.putStrLn $ T.unpack $ renderErrors srcs [e]
Right (defs, docEls) -> do
let (els', errs) = runWriter $ processDoc defs docEls
case errs of
[] -> do
let stream = layoutSmart renderOpts $ texDoc printOpts els'
writeFile outpFile . encodeUtf8 . renderStrict . h $ stream
_ ->
IOUtils.putStrLn $
T.unpack $
T.unlines $ map (prettyError (T.lines file)) errs
IOUtils.putStrLn $ T.unpack $ renderErrors srcs errs
where
renderOpts = defaultLayoutOptions {layoutPageWidth = AvailablePerLine pageWidth 1.0}
h :: P.SimpleDocStream Ann -> P.SimpleDocStream Void
Expand Down Expand Up @@ -177,5 +172,5 @@ main = processFile . defaultOutp =<< Opt.execParser opts
| otherwise = opts
replExt ext' filePath =
case spanEnd (/= '.') filePath of
(_, []) -> filePath <> ext'
([], _) -> filePath <> "." <> ext'
(f', _) -> f' <> ext'
Loading

0 comments on commit 4694b41

Please sign in to comment.