Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issues related to LaTeX #598

Merged
merged 4 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 39 additions & 17 deletions eo-phi-normalizer/app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ import Data.List (intercalate, isPrefixOf)
import Data.Maybe (fromMaybe)
import Data.Text.Internal.Builder (toLazyText)
import Data.Text.Lazy as TL (unpack)
import Data.Text.Lazy.Manipulate (toOrdinal)
import Data.Version (showVersion)
import Data.Yaml (decodeFileThrow, decodeThrow)
import GHC.Generics (Generic)
Expand Down Expand Up @@ -597,7 +598,7 @@ main = withUtf8 do
Nothing -> do
ruleSet :: RuleSet <- decodeThrow $(embedFileRelative "test/eo/phi/rules/new.yaml")
return (False, ruleSet.title, convertRuleNamed <$> ruleSet.rules)
unless (single || json || (chain && latex)) $ logStrLn ruleSetTitle
unless (single || json || latex) $ logStrLn ruleSetTitle
bindingsWithDeps <- case deepMergePrograms (program' : deps) of
Left err -> throw (CouldNotMergeDependencies err)
Right (Program bindingsWithDeps) -> return bindingsWithDeps
Expand All @@ -611,6 +612,23 @@ main = withUtf8 do
limits = ApplicationLimits maxDepth (maxGrowthFactor * objectSize (Formation bindings))
ctx = (defaultContext rules (Formation bindingsWithDeps)){builtinRules = builtin} -- IMPORTANT: context contains dependencies!
totalResults = length uniqueResults
inLatexDocument :: IO () -> IO ()
inLatexDocument logContent = do
logStrLn
[fmtTrim|
% {ruleSetTitle}

\\documentclass{{article}}
\\usepackage{{eolang}}
\\begin{{document}}
|]
logContent
logStrLn "\n\\end{document}"
inPhiEquation :: String -> IO ()
inPhiEquation phiExpr = do
logStrLn "\\begin{phiquation*}"
logStrLn [fmtTrim|{phiExpr}|]
logStrLn "\\end{phiquation*}"
when (null uniqueResults || null (head uniqueResults)) (throw CouldNotNormalize)
if
| single && json ->
Expand All @@ -630,25 +648,29 @@ main = withUtf8 do
{ input = printTree program'
, output = (logEntryToPair . fmap printAsProgramOrAsObject <$>) <$> uniqueResults
}
| chain && latex -> do
logStrLn
[fmtTrim|
% {ruleSetTitle}

\\documentclass{{article}}
\\usepackage{{eolang}}
\\begin{{document}}
|]
forM_ uniqueResults $ \steps -> do
| chain && latex -> inLatexDocument $ do
forM_ (zip [1 ..] uniqueResults) $ \(index, steps) -> do
let latexLines = toLatexString (Formation bindings) : (toLatexString . (.logEntryLog) <$> steps)
transitions :: [String] = ((\x -> [fmt| \\trans_{{\\rulename{{{logEntryMessage x}}}}} \n|]) <$> steps) <> ["."]
linesCombined :: String = fold $ zipWith (\latexLine transition -> [fmt|{latexLine}{transition}|]) latexLines transitions
logStrLn "\\begin{phiquation*}"
logStrLn [fmtTrim|{linesCombined}|]
logStrLn "\\end{phiquation*}"
logStrLn "\n\\end{document}"
trailingTransitions :: [String] = "" : repeat [fmt| \\trans |]
linesCombined :: String =
fold $
zipWith3
( \trailingTrans latexLine transition ->
[fmt|{trailingTrans}{latexLine}{transition}|]
)
trailingTransitions
latexLines
transitions
unless (length uniqueResults == 1) $
logStrLn
[fmt|\nThis is the {unpack (toOrdinal index)} possible chain of normalizing rewritings:\n|]
inPhiEquation linesCombined
| latex ->
logStrLn . toLatexString $ logEntryLog (head (head uniqueResults))
inLatexDocument $
inPhiEquation $
toLatexString $
logEntryLog (head (head uniqueResults))
| otherwise -> do
logStrLn "Input:"
logStrLn (printTree program')
Expand Down
4 changes: 4 additions & 0 deletions eo-phi-normalizer/eo-phi-normalizer.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ library
, scientific
, template-haskell
, text
, text-manipulate
, unordered-containers
, yaml
default-language: Haskell2010
Expand Down Expand Up @@ -313,6 +314,7 @@ executable eo-phi-normalizer
, scientific
, template-haskell
, text
, text-manipulate
, unordered-containers
, with-utf8
, yaml
Expand Down Expand Up @@ -387,6 +389,7 @@ test-suite doctests
, scientific
, template-haskell
, text
, text-manipulate
, unordered-containers
, yaml
default-language: Haskell2010
Expand Down Expand Up @@ -439,6 +442,7 @@ test-suite spec
, scientific
, template-haskell
, text
, text-manipulate
, unordered-containers
, with-utf8
, yaml
Expand Down
1 change: 1 addition & 0 deletions eo-phi-normalizer/package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ dependencies:
- hspec-core
- text
- template-haskell
- text-manipulate
- blaze-html
- blaze-markup
- scientific
Expand Down
Loading