From d017b6fd1d560996575edb5eb2afafe90a686b60 Mon Sep 17 00:00:00 2001 From: Rongsong Shen Date: Fri, 16 Sep 2022 16:59:10 +0800 Subject: [PATCH 01/14] add cjk option --- simplex.cabal | 1 + src/Simplex/CmdLineOpts.hs | 9 +++++++-- src/Simplex/ConfigData.hs | 2 ++ src/Simplex/ToTeX.hs | 13 +++++++++++-- src/simplex.hs | 20 +++++++++++++------- 5 files changed, 34 insertions(+), 11 deletions(-) diff --git a/simplex.cabal b/simplex.cabal index c0710fa..bb1a562 100644 --- a/simplex.cabal +++ b/simplex.cabal @@ -77,5 +77,6 @@ Executable simplex , mtl >= 2.0.1 , time >= 1.4 , random >= 1.0 + , text >= 1.0 diff --git a/src/Simplex/CmdLineOpts.hs b/src/Simplex/CmdLineOpts.hs index 033a5b4..865a04d 100644 --- a/src/Simplex/CmdLineOpts.hs +++ b/src/Simplex/CmdLineOpts.hs @@ -17,6 +17,7 @@ data Flag = Help | Verbose | Print | NoClean | Density Int | Quality Int | Crop | Convert String | Force | Version | ListSymbols String | ThreeTimes + | CJK deriving (Show, Eq) data Opts = Opts { @@ -39,7 +40,8 @@ data Opts = Opts { optPdfcrop :: String, optGraphviz :: String, optGnuplot :: String, - optConvert :: String + optConvert :: String, + optCJK :: Bool } defOpts = Opts { @@ -52,6 +54,7 @@ defOpts = Opts { optDryRun = False, optCrop = False, optForce = False, + optCJK = False, optThreeTimes = False, optListSymbols = Nothing, optWatch = Nothing, @@ -75,8 +78,9 @@ cmdOpts = [ Option "c" ["crop"] (NoArg Crop) "Crops the document so that no margins are left.", Option "f" ["force"] (NoArg Force) "Forces the creation of output files.", Option "tT" ["type"] (ReqArg Type "") "Specify type of output (pdf, png, tex)", - Option "x" ["pdflatex"] (ReqArg Pdflatex "") "Path to `pdflatex' executable", + Option "x" ["pdflatex/xelatex"] (ReqArg Pdflatex "") "Path to `pdflatex/xelatex' executable", Option "k" ["pdfcrop"] (ReqArg Pdfcrop "") "Path to `pdfcrop'", + Option "" ["cjk"] (NoArg CJK) "Enable CJK language support(xelatex will be used)", Option "z" ["graphviz"] (ReqArg Graphviz "") "Path to `dot' (graphviz)", Option "g" ["gnuplot"] (ReqArg Gnuplot "") "Path to `gnuplot'", Option "m" ["convert"] (ReqArg Convert "") "Path to `convert' (ImageMagick)", @@ -117,6 +121,7 @@ parseArgs = do Graphviz c -> opts { optGraphviz = c } Gnuplot c -> opts { optGnuplot = c } Convert c -> opts { optConvert = c } + CJK -> opts { optCJK = True } parseOpts opts _ = opts diff --git a/src/Simplex/ConfigData.hs b/src/Simplex/ConfigData.hs index 0075468..c6b7560 100644 --- a/src/Simplex/ConfigData.hs +++ b/src/Simplex/ConfigData.hs @@ -12,6 +12,7 @@ data Config = Config { oFigure :: Bool, oStandalone :: Bool, oLetter :: Bool, + oCJK :: Bool, oLetterClosing :: String, oImageWidth :: Maybe String, @@ -30,6 +31,7 @@ defaultConfig = Config { oFigure = False, oStandalone = False, oLetter = False, + oCJK = False, oLetterClosing = "", oImageWidth = Nothing, diff --git a/src/Simplex/ToTeX.hs b/src/Simplex/ToTeX.hs index 49de622..f41c0f5 100644 --- a/src/Simplex/ToTeX.hs +++ b/src/Simplex/ToTeX.hs @@ -75,6 +75,10 @@ documentClass cfg props : "]{" : articleType props : ["}\n"] +optionInCfg option cfg v1 v2 + | option cfg = v1 + | otherwise = v2 + packages = [("inputenc", "\\usepackage[utf8]{inputenc}\n"), ("fancyhdr", "\\usepackage{fancyhdr}\n"), ("tabularx", "\\usepackage{tabularx}\n"), @@ -121,7 +125,12 @@ toTeX cfg doc@(Document blocks props) = concat $ preamble $ toTeX' cfg' $ blocks preamble xs = documentClass cfg' props - : "\\usepackage[utf8]{inputenc}\n" + : optionInCfg oCJK cfg' + ("\\usepackage[AutoFakeBold,AutoFakeSlant]{xeCJK}\n" ++ + "\\setCJKmainfont[BoldFont=simhei.ttf, SlantedFont=simkai.ttf]{simsun.ttc}\n" ++ + "\\setCJKsansfont[AutoFakeSlant=false, BoldFont=simhei.ttf, SlantedFont=simkai.ttf]{simsun.ttc}\n" ++ + "\\setCJKmonofont[ItalicFont=simkai.ttf]{simsun.ttc}\n") + "\\usepackage[utf8]{inputenc}\n" : maybe "" (\x -> "\\usepackage[" ++ x ++ "]{babel}\n") @@ -131,7 +140,7 @@ toTeX cfg doc@(Document blocks props) = concat $ preamble $ toTeX' cfg' $ blocks : "\\usepackage{tabularx}\n" : "\\usepackage{eurosym}\n" - : "\\DeclareUnicodeCharacter{20AC}{\\euro{}}\n" + : optionInCfg oCJK cfg' "" "\\DeclareUnicodeCharacter{20AC}{\\euro{}}\n" : "\\usepackage{amsmath}\n" : "\\usepackage{amsfonts}\n" diff --git a/src/simplex.hs b/src/simplex.hs index bb54bf3..a220e68 100644 --- a/src/simplex.hs +++ b/src/simplex.hs @@ -15,6 +15,7 @@ import Text.Printf import Data.List (sort) import Data.Maybe import Data.Time +import Data.Text (replace, pack, unpack) import System.Console.GetOpt (usageInfo) import System.Directory @@ -119,6 +120,10 @@ process opts file exit = do pdflatex = optPdflatex opts pdfcrop = optPdfcrop opts convert = optConvert opts + cjk = optCJK opts + latex = if cjk + then unpack (replace (pack "pdflatex") (pack "xelatex") (pack pdflatex)) + else pdflatex pdfopts = ["-interaction=nonstopmode", "-file-line-error"] @@ -149,7 +154,8 @@ process opts file exit = do tok <- liftIO $ loadIncludes True (lex c) >>= loadHashbangs print' "." - let cfg = defaultConfig { oStandalone = optType opts == "png" } + let cfg = defaultConfig { oStandalone = optType opts == "png", + oCJK = cjk } (spec, tok') <- liftIO $ if (optDryRun opts) then (return $ (newSpec, parse tok)) @@ -165,17 +171,17 @@ process opts file exit = do print' "." unless (optDryRun opts || optType opts == "tex") $ do - -- run pdflatex - r <- liftIO $ exec verbose pdflatex (pdfopts ++ [filename ++ ".tex"]) + -- run pdflatex/xelatex + r <- liftIO $ exec verbose latex (pdfopts ++ [filename ++ ".tex"]) _ <- either (throw' spec . Err . snd) (return . const Ok) r print' "." - -- run pdflatex a second time - _ <- liftIO $ exec verbose pdflatex (pdfopts ++ [filename ++ ".tex"]) + -- run pdflatex/xelatex a second time + _ <- liftIO $ exec verbose latex (pdfopts ++ [filename ++ ".tex"]) print' "." - -- run pdflatex a third time if desired - when (optThreeTimes opts) $ do { liftIO $ exec verbose pdflatex (pdfopts ++ [filename ++ ".tex"]) + -- run pdflatex/xelatex a third time if desired + when (optThreeTimes opts) $ do { liftIO $ exec verbose latex (pdfopts ++ [filename ++ ".tex"]) ; print' "." } -- clean files From a7b4460b448fcb6fc4c3305053efd1f1bbffe152 Mon Sep 17 00:00:00 2001 From: Rongsong Shen Date: Fri, 16 Sep 2022 18:02:38 +0800 Subject: [PATCH 02/14] use @xeCJK to support chinese(CJK) --- src/Simplex/CmdLineOpts.hs | 9 ++------- src/Simplex/ConfigData.hs | 2 -- src/Simplex/ToTeX.hs | 26 ++++++++++++++------------ src/simplex.hs | 13 +++++++------ 4 files changed, 23 insertions(+), 27 deletions(-) diff --git a/src/Simplex/CmdLineOpts.hs b/src/Simplex/CmdLineOpts.hs index 865a04d..033a5b4 100644 --- a/src/Simplex/CmdLineOpts.hs +++ b/src/Simplex/CmdLineOpts.hs @@ -17,7 +17,6 @@ data Flag = Help | Verbose | Print | NoClean | Density Int | Quality Int | Crop | Convert String | Force | Version | ListSymbols String | ThreeTimes - | CJK deriving (Show, Eq) data Opts = Opts { @@ -40,8 +39,7 @@ data Opts = Opts { optPdfcrop :: String, optGraphviz :: String, optGnuplot :: String, - optConvert :: String, - optCJK :: Bool + optConvert :: String } defOpts = Opts { @@ -54,7 +52,6 @@ defOpts = Opts { optDryRun = False, optCrop = False, optForce = False, - optCJK = False, optThreeTimes = False, optListSymbols = Nothing, optWatch = Nothing, @@ -78,9 +75,8 @@ cmdOpts = [ Option "c" ["crop"] (NoArg Crop) "Crops the document so that no margins are left.", Option "f" ["force"] (NoArg Force) "Forces the creation of output files.", Option "tT" ["type"] (ReqArg Type "") "Specify type of output (pdf, png, tex)", - Option "x" ["pdflatex/xelatex"] (ReqArg Pdflatex "") "Path to `pdflatex/xelatex' executable", + Option "x" ["pdflatex"] (ReqArg Pdflatex "") "Path to `pdflatex' executable", Option "k" ["pdfcrop"] (ReqArg Pdfcrop "") "Path to `pdfcrop'", - Option "" ["cjk"] (NoArg CJK) "Enable CJK language support(xelatex will be used)", Option "z" ["graphviz"] (ReqArg Graphviz "") "Path to `dot' (graphviz)", Option "g" ["gnuplot"] (ReqArg Gnuplot "") "Path to `gnuplot'", Option "m" ["convert"] (ReqArg Convert "") "Path to `convert' (ImageMagick)", @@ -121,7 +117,6 @@ parseArgs = do Graphviz c -> opts { optGraphviz = c } Gnuplot c -> opts { optGnuplot = c } Convert c -> opts { optConvert = c } - CJK -> opts { optCJK = True } parseOpts opts _ = opts diff --git a/src/Simplex/ConfigData.hs b/src/Simplex/ConfigData.hs index c6b7560..0075468 100644 --- a/src/Simplex/ConfigData.hs +++ b/src/Simplex/ConfigData.hs @@ -12,7 +12,6 @@ data Config = Config { oFigure :: Bool, oStandalone :: Bool, oLetter :: Bool, - oCJK :: Bool, oLetterClosing :: String, oImageWidth :: Maybe String, @@ -31,7 +30,6 @@ defaultConfig = Config { oFigure = False, oStandalone = False, oLetter = False, - oCJK = False, oLetterClosing = "", oImageWidth = Nothing, diff --git a/src/Simplex/ToTeX.hs b/src/Simplex/ToTeX.hs index f41c0f5..8fb1b69 100644 --- a/src/Simplex/ToTeX.hs +++ b/src/Simplex/ToTeX.hs @@ -1,6 +1,6 @@ {-# LANGUAGE Haskell2010 #-} -module Simplex.ToTeX (toTeX) where +module Simplex.ToTeX (toTeX, docProps) where import Simplex.Parser import Simplex.Config @@ -75,10 +75,6 @@ documentClass cfg props : "]{" : articleType props : ["}\n"] -optionInCfg option cfg v1 v2 - | option cfg = v1 - | otherwise = v2 - packages = [("inputenc", "\\usepackage[utf8]{inputenc}\n"), ("fancyhdr", "\\usepackage{fancyhdr}\n"), ("tabularx", "\\usepackage{tabularx}\n"), @@ -119,18 +115,21 @@ packages = [("inputenc", "\\usepackage[utf8]{inputenc}\n"), ] +docProps doc@(Document blocks props) = props + toTeX cfg doc@(Document blocks props) = concat $ preamble $ toTeX' cfg' $ blocks where cfg' = config cfg doc preamble xs = documentClass cfg' props - : optionInCfg oCJK cfg' - ("\\usepackage[AutoFakeBold,AutoFakeSlant]{xeCJK}\n" ++ - "\\setCJKmainfont[BoldFont=simhei.ttf, SlantedFont=simkai.ttf]{simsun.ttc}\n" ++ - "\\setCJKsansfont[AutoFakeSlant=false, BoldFont=simhei.ttf, SlantedFont=simkai.ttf]{simsun.ttc}\n" ++ - "\\setCJKmonofont[ItalicFont=simkai.ttf]{simsun.ttc}\n") - "\\usepackage[utf8]{inputenc}\n" + : maybe + "\\usepackage[utf8]{inputenc}\n" + (\x -> "\\usepackage[AutoFakeBold,AutoFakeSlant]{xeCJK}\n" ++ + "\\setCJKmainfont[BoldFont=simhei.ttf, SlantedFont=simkai.ttf]{simsun.ttc}\n" ++ + "\\setCJKsansfont[AutoFakeSlant=false, BoldFont=simhei.ttf, SlantedFont=simkai.ttf]{simsun.ttc}\n" ++ + "\\setCJKmonofont[ItalicFont=simkai.ttf]{simsun.ttc}\n") + (lookup "xeCJK" props) : maybe "" (\x -> "\\usepackage[" ++ x ++ "]{babel}\n") @@ -140,7 +139,10 @@ toTeX cfg doc@(Document blocks props) = concat $ preamble $ toTeX' cfg' $ blocks : "\\usepackage{tabularx}\n" : "\\usepackage{eurosym}\n" - : optionInCfg oCJK cfg' "" "\\DeclareUnicodeCharacter{20AC}{\\euro{}}\n" + : maybe + "\\DeclareUnicodeCharacter{20AC}{\\euro{}}\n" + (\x -> "") + (lookup "xeCJK" props) : "\\usepackage{amsmath}\n" : "\\usepackage{amsfonts}\n" diff --git a/src/simplex.hs b/src/simplex.hs index a220e68..8ecb385 100644 --- a/src/simplex.hs +++ b/src/simplex.hs @@ -120,10 +120,7 @@ process opts file exit = do pdflatex = optPdflatex opts pdfcrop = optPdfcrop opts convert = optConvert opts - cjk = optCJK opts - latex = if cjk - then unpack (replace (pack "pdflatex") (pack "xelatex") (pack pdflatex)) - else pdflatex + xelatex = unpack (replace (pack "pdflatex") (pack "xelatex") (pack pdflatex)) pdfopts = ["-interaction=nonstopmode", "-file-line-error"] @@ -154,14 +151,18 @@ process opts file exit = do tok <- liftIO $ loadIncludes True (lex c) >>= loadHashbangs print' "." - let cfg = defaultConfig { oStandalone = optType opts == "png", - oCJK = cjk } + let cfg = defaultConfig { oStandalone = optType opts == "png" } (spec, tok') <- liftIO $ if (optDryRun opts) then (return $ (newSpec, parse tok)) else (processSpecials opts newSpec $ parse tok) let tex = toTeX cfg tok' + + let latex = maybe + latex + (\x -> xelatex) + (lookup "xeCJK" (docProps tok') ) print' "." when (optType opts == "tex" || not (optDryRun opts)) $ do From 451f73c94fbf1dafee292d08d9ca88d3eaf192cf Mon Sep 17 00:00:00 2001 From: Rongsong Shen Date: Mon, 19 Sep 2022 11:50:05 +0800 Subject: [PATCH 03/14] fix @address & no xeCJK doc --- src/Simplex/ToTeX.hs | 4 ++++ src/simplex.hs | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Simplex/ToTeX.hs b/src/Simplex/ToTeX.hs index 8fb1b69..8ac98ba 100644 --- a/src/Simplex/ToTeX.hs +++ b/src/Simplex/ToTeX.hs @@ -262,6 +262,10 @@ toTeX cfg doc@(Document blocks props) = concat $ preamble $ toTeX' cfg' $ blocks (\x -> "\\setcounter{tocdepth}{" ++ x ++ "}\n") (lookup "tocdepth" props) + : maybe + "\\newcommand{\\setkomavar}[2]{} %% FIXME: later\n" + (\x -> "") + (lookup "letter" props) : maybe "" (\x -> "\\setkomavar{fromaddress}{" ++ escapeTeX' "}\n" x) diff --git a/src/simplex.hs b/src/simplex.hs index 8ecb385..dd4611f 100644 --- a/src/simplex.hs +++ b/src/simplex.hs @@ -160,7 +160,7 @@ process opts file exit = do let tex = toTeX cfg tok' let latex = maybe - latex + pdflatex (\x -> xelatex) (lookup "xeCJK" (docProps tok') ) print' "." From abec7222ce1f923f17d43a9dc05c1495b8bd95ef Mon Sep 17 00:00:00 2001 From: Rongsong Shen Date: Mon, 19 Sep 2022 18:20:12 +0800 Subject: [PATCH 04/14] use MissingH replace --- simplex.cabal | 3 +-- src/simplex.hs | 5 +++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/simplex.cabal b/simplex.cabal index bb1a562..5caa818 100644 --- a/simplex.cabal +++ b/simplex.cabal @@ -78,5 +78,4 @@ Executable simplex , time >= 1.4 , random >= 1.0 , text >= 1.0 - - + , MissingH >= 1.5.0 diff --git a/src/simplex.hs b/src/simplex.hs index dd4611f..0978919 100644 --- a/src/simplex.hs +++ b/src/simplex.hs @@ -15,7 +15,8 @@ import Text.Printf import Data.List (sort) import Data.Maybe import Data.Time -import Data.Text (replace, pack, unpack) +import Data.Text (pack, unpack) +import Data.List.Utils (replace) import System.Console.GetOpt (usageInfo) import System.Directory @@ -120,7 +121,7 @@ process opts file exit = do pdflatex = optPdflatex opts pdfcrop = optPdfcrop opts convert = optConvert opts - xelatex = unpack (replace (pack "pdflatex") (pack "xelatex") (pack pdflatex)) + xelatex = replace "pdflatex" "xelatex" pdflatex pdfopts = ["-interaction=nonstopmode", "-file-line-error"] From 4ac8f893f9a7bbffc323c1963b8cb3425d912753 Mon Sep 17 00:00:00 2001 From: Rongsong Shen Date: Fri, 7 Oct 2022 21:35:15 +0800 Subject: [PATCH 05/14] add alias command for chapter/part/section/subsection/subsubsection --- src/Simplex/Parser.hs | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/Simplex/Parser.hs b/src/Simplex/Parser.hs index dc47364..52e20dd 100644 --- a/src/Simplex/Parser.hs +++ b/src/Simplex/Parser.hs @@ -82,6 +82,16 @@ data State = SStart | SNewline | SControl | SControlAfter | SSymbol | SSpace | S isBlock (TBlock _) = True isBlock _ = False +convertAlias :: String -> String +convertAlias s = + case s of + ".chapter" -> "!!" + ".part" -> "!!!" + ".section" -> "=" + ".subsection" -> "==" + ".subsubsection" -> "===" + _ -> s + parse :: [Token] -> Document parse = parse' $ Document [] [] @@ -327,10 +337,10 @@ lex' l c m SCommand s@(x:xs) | otherwise = lex' l (c+1) (x:m) SCommand xs lex' l c m SControl s@(x:xs) - | x == ' ' && c < 4 = TControl (reverse m) : lex' l (c+1) [] SControlAfter xs - | x == ' ' = TControl (reverse m) : lex' l (c+1) [] SSymbol xs - | x == '\t' = TControl (reverse m) : lex' l (c+1) [] SSymbol xs - | x == '\n' = TControl (reverse m) : lex' (l+1) 0 [] SNewline xs + | x == ' ' && c < 4 = TControl (convertAlias (reverse m)) : lex' l (c+1) [] SControlAfter xs + | x == ' ' = TControl (convertAlias (reverse m)) : lex' l (c+1) [] SSymbol xs + | x == '\t' = TControl (convertAlias (reverse m)) : lex' l (c+1) [] SSymbol xs + | x == '\n' = TControl (convertAlias (reverse m)) : lex' (l+1) 0 [] SNewline xs | otherwise = lex' l (c+1) (x:m) SControl xs lex' l c m SControlAfter s@(x:xs) From 9ce4441b2b4f37776662f007e2dff7d3f69cfbea Mon Sep 17 00:00:00 2001 From: Rongsong Shen Date: Fri, 7 Oct 2022 21:36:10 +0800 Subject: [PATCH 06/14] use @preamble to choose fonts in xeCJK --- src/Simplex/ToTeX.hs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/Simplex/ToTeX.hs b/src/Simplex/ToTeX.hs index 8ac98ba..7477d7a 100644 --- a/src/Simplex/ToTeX.hs +++ b/src/Simplex/ToTeX.hs @@ -125,10 +125,7 @@ toTeX cfg doc@(Document blocks props) = concat $ preamble $ toTeX' cfg' $ blocks : maybe "\\usepackage[utf8]{inputenc}\n" - (\x -> "\\usepackage[AutoFakeBold,AutoFakeSlant]{xeCJK}\n" ++ - "\\setCJKmainfont[BoldFont=simhei.ttf, SlantedFont=simkai.ttf]{simsun.ttc}\n" ++ - "\\setCJKsansfont[AutoFakeSlant=false, BoldFont=simhei.ttf, SlantedFont=simkai.ttf]{simsun.ttc}\n" ++ - "\\setCJKmonofont[ItalicFont=simkai.ttf]{simsun.ttc}\n") + (\x -> "\\usepackage[AutoFakeBold,AutoFakeSlant]{xeCJK}\n") (lookup "xeCJK" props) : maybe "" From e906332baec37fe89a423e312c2efa829a087688 Mon Sep 17 00:00:00 2001 From: Rongsong Shen Date: Fri, 7 Oct 2022 21:36:50 +0800 Subject: [PATCH 07/14] add stub for gnuplot support --- src/Simplex/Specials.hs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/Simplex/Specials.hs b/src/Simplex/Specials.hs index 0e42012..50be424 100644 --- a/src/Simplex/Specials.hs +++ b/src/Simplex/Specials.hs @@ -78,10 +78,21 @@ randomString n = do mkGraph e g opts spec c = do file <- randomString 10 + case e of + "dot" -> mkGraphDot e file g opts spec c + "neato" -> mkGraphDot e file g opts spec c + "gnuplot" -> mkGraphGnuPlot e file g opts spec c + _ -> return (spec, []) + + +mkGraphDot e file g opts spec c = do let spec' = spec { sRemoveFiles = (file ++ ".pdf") : (file ++ ".dot") : sRemoveFiles spec } writeFile (file ++ ".dot") (if null g then c else g ++ " G {\n" ++ c ++ "\n}\n") r <- exec (optVerbose opts) (optGraphviz opts) ["-Tpdf", "-K" ++ e, file ++ ".dot", "-o" ++ file ++ ".pdf"] - return (spec', (either (const "") (const $ file ++ ".pdf") r)) + return (spec', (either (const "") (const $ file ++ ".pdf") r)) +mkGraphGnuPlot e file g opts spec c = do + let spec' = spec { sRemoveFiles = (file ++ ".pdf") : (file ++ ".gp") : sRemoveFiles spec } + return (spec', []) From e8c77d826dac46e35697eab699a22478cfbc7312 Mon Sep 17 00:00:00 2001 From: Rongsong Shen Date: Sat, 8 Oct 2022 17:10:18 +0800 Subject: [PATCH 08/14] support gnuplot --- src/Simplex/Specials.hs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/Simplex/Specials.hs b/src/Simplex/Specials.hs index 50be424..e55996b 100644 --- a/src/Simplex/Specials.hs +++ b/src/Simplex/Specials.hs @@ -62,6 +62,13 @@ processSpecials' opts spec (BVerbatim "dot" b : xs) = do then BVerbatim "error" "Graphviz .dot failed" else BCommand "image" [pdf]) : rest) +processSpecials' opts spec (BVerbatim "gnuplot" b : xs) = do + (spec', pdf) <- mkGraph "gnuplot" "" opts spec b + (spec'', rest) <- processSpecials' opts spec' xs + return (spec'', (if null pdf + then BVerbatim "error" "Gnuplot .gp failed" + else BCommand "image" [pdf]) : rest) + processSpecials' opts spec (x : xs) = do (spec', rest) <- processSpecials' opts spec xs return (spec', x : rest) @@ -95,4 +102,7 @@ mkGraphDot e file g opts spec c = do mkGraphGnuPlot e file g opts spec c = do let spec' = spec { sRemoveFiles = (file ++ ".pdf") : (file ++ ".gp") : sRemoveFiles spec } - return (spec', []) + writeFile (file ++ ".gp") ("set terminal pdf\n" ++ "set output \"" ++ file ++ ".pdf" ++ "\"\n" ++c ++ "\n") + + r <- exec (optVerbose opts) (optGnuplot opts) [file ++ ".gp"] + return (spec', (either (const "") (const $ file ++ ".pdf") r)) From 4a0824091eb8b5f73ccafb15e770093ee9f3a96b Mon Sep 17 00:00:00 2001 From: Rongsong Shen Date: Fri, 14 Oct 2022 16:56:15 +0800 Subject: [PATCH 09/14] add support of ditaa/mermaid/plantuml --- src/Simplex/CmdLineOpts.hs | 18 ++++++++++++++ src/Simplex/Specials.hs | 49 +++++++++++++++++++++++++++++++++++++- 2 files changed, 66 insertions(+), 1 deletion(-) diff --git a/src/Simplex/CmdLineOpts.hs b/src/Simplex/CmdLineOpts.hs index 033a5b4..cb85cae 100644 --- a/src/Simplex/CmdLineOpts.hs +++ b/src/Simplex/CmdLineOpts.hs @@ -13,6 +13,8 @@ import Data.Maybe data Flag = Help | Verbose | Print | NoClean | Pdflatex String | Pdfcrop String | Graphviz String | Gnuplot String + | Ditaa String | PlantUML String + | Mermaid String | Java String | Watch Int | DryRun | Type String | Density Int | Quality Int | Crop | Convert String | Force | Version @@ -38,7 +40,11 @@ data Opts = Opts { optPdflatex :: String, optPdfcrop :: String, optGraphviz :: String, + optJava :: String, optGnuplot :: String, + optDitaa :: String, + optPlantUML :: String, + optMermaid :: String, optConvert :: String } @@ -62,6 +68,10 @@ defOpts = Opts { optPdfcrop = "pdfcrop", optGraphviz = "dot", optGnuplot = "gnuplot", + optJava = "java", + optDitaa = "ditaa.jar", + optPlantUML = "plantuml.jar", + optMermaid = "mmdc", optConvert = "convert" } @@ -80,6 +90,10 @@ cmdOpts = [ Option "z" ["graphviz"] (ReqArg Graphviz "") "Path to `dot' (graphviz)", Option "g" ["gnuplot"] (ReqArg Gnuplot "") "Path to `gnuplot'", Option "m" ["convert"] (ReqArg Convert "") "Path to `convert' (ImageMagick)", + Option "" ["java"] (ReqArg Java "") "Path to `java'", + Option "" ["mmdc"] (ReqArg Mermaid "") "Path to `mmdc'", + Option "" ["ditaa"] (ReqArg Ditaa "") "Path to ditaai.jar", + Option "" ["plantuml"] (ReqArg PlantUML "") "Path to plantuml.jar", Option "w" ["watch"] (OptArg (Watch . read . fromMaybe "2000") "") "Watch files or folder (optionally amount of time in ms)", Option "3" ["three-times"] (NoArg ThreeTimes) "Execute `pdflatex' three times instead of the default two times.", Option "s" ["symbols"] (OptArg (ListSymbols . fromMaybe "\\%s\n") "") "Show a list of symboles known to simplex.", @@ -115,6 +129,10 @@ parseArgs = do Pdflatex c -> opts { optPdflatex = c } Pdfcrop c -> opts { optPdfcrop = c } Graphviz c -> opts { optGraphviz = c } + Java c -> opts { optJava = c } + Mermaid c -> opts { optMermaid = c } + Ditaa c -> opts { optDitaa = c } + PlantUML c -> opts { optPlantUML = c } Gnuplot c -> opts { optGnuplot = c } Convert c -> opts { optConvert = c } parseOpts opts _ = opts diff --git a/src/Simplex/Specials.hs b/src/Simplex/Specials.hs index e55996b..9cfa659 100644 --- a/src/Simplex/Specials.hs +++ b/src/Simplex/Specials.hs @@ -69,6 +69,27 @@ processSpecials' opts spec (BVerbatim "gnuplot" b : xs) = do then BVerbatim "error" "Gnuplot .gp failed" else BCommand "image" [pdf]) : rest) +processSpecials' opts spec (BVerbatim "mermaid" b : xs) = do + (spec', pdf) <- mkGraph "mermaid" "" opts spec b + (spec'', rest) <- processSpecials' opts spec' xs + return (spec'', (if null pdf + then BVerbatim "error" "Mermaid .mmd failed" + else BCommand "image" [pdf]) : rest ) + +processSpecials' opts spec (BVerbatim "ditaa" b : xs) = do + (spec', png) <- mkGraph "ditaa" "" opts spec b + (spec'', rest) <- processSpecials' opts spec' xs + return (spec'', (if null png + then BVerbatim "error" "ditaa .ditaa failed" + else BCommand "image" [png]) : rest ) + +processSpecials' opts spec (BVerbatim "plantuml" b : xs) = do + (spec', pdf) <- mkGraph "plantuml" "" opts spec b + (spec'', rest) <- processSpecials' opts spec' xs + return (spec'', (if null pdf + then BVerbatim "error" "plantuml .plantuml failed" + else BCommand "image" [pdf]) : rest ) + processSpecials' opts spec (x : xs) = do (spec', rest) <- processSpecials' opts spec xs return (spec', x : rest) @@ -89,10 +110,12 @@ mkGraph e g opts spec c = do "dot" -> mkGraphDot e file g opts spec c "neato" -> mkGraphDot e file g opts spec c "gnuplot" -> mkGraphGnuPlot e file g opts spec c + "mermaid" -> mkGraphMermaid e file g opts spec c + "ditaa" -> mkGraphDitaa e file g opts spec c + "plantuml" -> mkGraphPlantUML e file g opts spec c _ -> return (spec, []) - mkGraphDot e file g opts spec c = do let spec' = spec { sRemoveFiles = (file ++ ".pdf") : (file ++ ".dot") : sRemoveFiles spec } writeFile (file ++ ".dot") (if null g then c else g ++ " G {\n" ++ c ++ "\n}\n") @@ -106,3 +129,27 @@ mkGraphGnuPlot e file g opts spec c = do r <- exec (optVerbose opts) (optGnuplot opts) [file ++ ".gp"] return (spec', (either (const "") (const $ file ++ ".pdf") r)) + +mkGraphMermaid e file g opts spec c = do + let spec' = spec { sRemoveFiles = (file ++ ".pdf") : (file ++ ".mmd") : sRemoveFiles spec} + writeFile (file ++ ".mmd") (c ++ "\n") + + r <- exec (optVerbose opts) (optMermaid opts) ["-i", file ++ ".mmd" , "-o", file ++ ".pdf"] + + return (spec', (either (const "") (const $ file ++ ".pdf") r)) + +mkGraphDitaa e file g opts spec c = do + let spec' = spec { sRemoveFiles = (file ++ ".png") : (file ++ ".ditaa") : sRemoveFiles spec} + writeFile (file ++ ".ditaa") (c ++ "\n") + + r <- exec (optVerbose opts) (optJava opts) ["-jar", (optDitaa opts),file ++ ".ditaa", file ++ ".png"] + + return (spec', (either (const "") (const $ file ++ ".png") r)) + +mkGraphPlantUML e file g opts spec c = do + let spec' = spec { sRemoveFiles = (file ++ ".png") : (file ++ ".plantuml") : sRemoveFiles spec } + writeFile (file ++ ".plantuml") ("@startuml\n" ++ c ++ "\n@enduml\n") + + r <- exec (optVerbose opts) (optJava opts) ["-jar", (optPlantUML opts), file ++ ".plantuml"] + + return (spec', (either (const "") (const $ file ++ ".png") r)) From ac729380def97054af04b636ac7a7e7c3efec8d6 Mon Sep 17 00:00:00 2001 From: Rongsong Shen Date: Fri, 28 Oct 2022 11:56:15 +0800 Subject: [PATCH 10/14] use ctex to replace xeCJK & use lwarp to support html --- src/Simplex/CmdLineOpts.hs | 2 +- src/Simplex/Parser.hs | 2 +- src/Simplex/ToTeX.hs | 34 ++++++++++++++++++++++------------ src/simplex.hs | 20 +++++++++++++++++--- 4 files changed, 41 insertions(+), 17 deletions(-) diff --git a/src/Simplex/CmdLineOpts.hs b/src/Simplex/CmdLineOpts.hs index cb85cae..eb091c9 100644 --- a/src/Simplex/CmdLineOpts.hs +++ b/src/Simplex/CmdLineOpts.hs @@ -84,7 +84,7 @@ cmdOpts = [ Option "p" ["print"] (NoArg Print) "Print processed tex to stdout.", Option "c" ["crop"] (NoArg Crop) "Crops the document so that no margins are left.", Option "f" ["force"] (NoArg Force) "Forces the creation of output files.", - Option "tT" ["type"] (ReqArg Type "") "Specify type of output (pdf, png, tex)", + Option "tT" ["type"] (ReqArg Type "") "Specify type of output (pdf, png, tex, html)", Option "x" ["pdflatex"] (ReqArg Pdflatex "") "Path to `pdflatex' executable", Option "k" ["pdfcrop"] (ReqArg Pdfcrop "") "Path to `pdfcrop'", Option "z" ["graphviz"] (ReqArg Graphviz "") "Path to `dot' (graphviz)", diff --git a/src/Simplex/Parser.hs b/src/Simplex/Parser.hs index 52e20dd..64fe022 100644 --- a/src/Simplex/Parser.hs +++ b/src/Simplex/Parser.hs @@ -324,7 +324,7 @@ lex' l c m SStart s@(x:xs) | otherwise = lex' l (c+1) [x] SControl xs lex' l c m SNewline s@(x:xs) - | x == '\n' = mkBlock m : lex' (l+1) 0 m SStart xs + | x == '\n' = mkBlock m : lex' (l+1) 0 [] SStart xs | x == ' ' = lex' l (c+1) m SSpace xs | x == '\t' = lex' l (c+1) ('\n':m) SSymbol xs | isAlpha x = mkBlock m : lex' l (c+1) [x] SCommand xs diff --git a/src/Simplex/ToTeX.hs b/src/Simplex/ToTeX.hs index 7477d7a..fc8713b 100644 --- a/src/Simplex/ToTeX.hs +++ b/src/Simplex/ToTeX.hs @@ -123,10 +123,20 @@ toTeX cfg doc@(Document blocks props) = concat $ preamble $ toTeX' cfg' $ blocks preamble xs = documentClass cfg' props + : "\\usepackage{iftex}\n" + : "\\ifPDFTeX" + : "\\usepackage{lmodern} % pdflatex or dvi latex\n" + : "\\usepackage[T1]{fontenc}\n" + : "\\usepackage[utf8]{inputenc}\n" + : "\\else\n" + : "\\usepackage{fontspec} % XeLaTeX or LuaLaTeX\n" + : "\\fi\n" : maybe - "\\usepackage[utf8]{inputenc}\n" - (\x -> "\\usepackage[AutoFakeBold,AutoFakeSlant]{xeCJK}\n") - (lookup "xeCJK" props) + "" + (\x -> "\\usepackage[heading = true ]{ctex}\n") + (lookup "cjk" props) + : "\\usepackage[\n" + : "]{lwarp}" : maybe "" (\x -> "\\usepackage[" ++ x ++ "]{babel}\n") @@ -135,31 +145,31 @@ toTeX cfg doc@(Document blocks props) = concat $ preamble $ toTeX' cfg' $ blocks : "\\usepackage{fancyhdr}\n" : "\\usepackage{tabularx}\n" - : "\\usepackage{eurosym}\n" + : "%% \\usepackage{eurosym}\n" : maybe - "\\DeclareUnicodeCharacter{20AC}{\\euro{}}\n" + "%% \\DeclareUnicodeCharacter{20AC}{\\euro{}}\n" (\x -> "") - (lookup "xeCJK" props) + (lookup "cjk" props) : "\\usepackage{amsmath}\n" : "\\usepackage{amsfonts}\n" : "\\usepackage{amssymb}\n" : "\\usepackage{stmaryrd}\n" - : "\\usepackage{wasysym}\n" + : "%% \\usepackage{wasysym}\n" - : "\\let\\EUR\\undefined\n" - : "\\usepackage{marvosym}\n" + : "%% \\let\\EUR\\undefined\n" + : "%% \\usepackage{marvosym}\n" : "\\usepackage{verbatim}\n" : "\\usepackage{listings}\n" : "\\usepackage{multicol}\n" : "\\usepackage[usenames,dvipsnames]{color}\n" - : "\\usepackage[table]{xcolor}\n" - : "\\usepackage{multirow}\n" + : "%% \\usepackage[table]{xcolor}\n" + : "%% \\usepackage{multirow}\n" - : "\\usepackage{lastpage}\n" + : "%% \\usepackage{lastpage}\n" : "\\usepackage{graphicx}\n" : maybe "\\usepackage[section]{placeins}\n" diff --git a/src/simplex.hs b/src/simplex.hs index 0978919..78f7e9e 100644 --- a/src/simplex.hs +++ b/src/simplex.hs @@ -122,6 +122,7 @@ process opts file exit = do pdfcrop = optPdfcrop opts convert = optConvert opts xelatex = replace "pdflatex" "xelatex" pdflatex + lwarpmk = replace "pdflatex" "lwarpmk" pdflatex pdfopts = ["-interaction=nonstopmode", "-file-line-error"] @@ -163,16 +164,29 @@ process opts file exit = do let latex = maybe pdflatex (\x -> xelatex) - (lookup "xeCJK" (docProps tok') ) + (lookup "cjk" (docProps tok') ) print' "." - when (optType opts == "tex" || not (optDryRun opts)) $ do + when (optType opts == "tex" || optType opts == "html" || not (optDryRun opts)) $ do -- write tex-file r <- liftIO $ try (writeFile (filename ++ ".tex") tex) _ <- either (throw . Exc) (return . const Ok) r print' "." - unless (optDryRun opts || optType opts == "tex") $ do + when ( optType opts == "html" && not (optDryRun opts )) $ do + r <- liftIO $ exec verbose latex (pdfopts ++ [filename ++ ".tex"]) + _ <- either (throw' spec . Err . snd) (return . const Ok) r + print' "." + + r <- liftIO $ exec verbose lwarpmk (["print"]) + _ <- either (throw' spec . Err . snd) (return . const Ok) r + print' "." + + r <- liftIO $ exec verbose lwarpmk (["html"]) + _ <- either (throw' spec . Err . snd) (return . const Ok) r + print' "." + + unless (optDryRun opts || optType opts == "tex" || optType opts == "html" ) $ do -- run pdflatex/xelatex r <- liftIO $ exec verbose latex (pdfopts ++ [filename ++ ".tex"]) _ <- either (throw' spec . Err . snd) (return . const Ok) r From d59b4341513385c98b199461a8bdc0662a2b0625 Mon Sep 17 00:00:00 2001 From: Rongsong Shen Date: Tue, 1 Nov 2022 17:06:28 +0800 Subject: [PATCH 11/14] do not use pdf as generated image format --- src/Simplex/Specials.hs | 61 +++++++++++++++++++++-------------------- src/Simplex/Util.hs | 1 + src/simplex.hs | 61 ++++++++++++++++++++++++----------------- 3 files changed, 68 insertions(+), 55 deletions(-) diff --git a/src/Simplex/Specials.hs b/src/Simplex/Specials.hs index 9cfa659..b1656be 100644 --- a/src/Simplex/Specials.hs +++ b/src/Simplex/Specials.hs @@ -35,46 +35,46 @@ processSpecials o s (Document b m) = do processSpecials' :: Opts -> Spec -> [Block] -> IO (Spec, [Block]) processSpecials' opts spec (BVerbatim "digraph" b : xs) = do - (spec', pdf) <- mkGraph "dot" "digraph" opts spec b + (spec', g) <- mkGraph "dot" "digraph" opts spec b (spec'', rest) <- processSpecials' opts spec' xs - return (spec'', (if null pdf + return (spec'', (if null g then BVerbatim "error" "Graphviz .digraph failed" - else BCommand "image" [pdf]) : rest) + else BCommand "image" [g]) : rest) processSpecials' opts spec (BVerbatim "graph" b : xs) = do - (spec', pdf) <- mkGraph "neato" "graph" opts spec b + (spec', g) <- mkGraph "neato" "graph" opts spec b (spec'', rest) <- processSpecials' opts spec' xs - return (spec'', (if null pdf + return (spec'', (if null g then BVerbatim "error" "Graphviz .graph failed" - else BCommand "image" [pdf]) : rest) + else BCommand "image" [g]) : rest) processSpecials' opts spec (BVerbatim "neato" b : xs) = do - (spec', pdf) <- mkGraph "neato" "" opts spec b + (spec', g) <- mkGraph "neato" "" opts spec b (spec'', rest) <- processSpecials' opts spec' xs - return (spec'', (if null pdf + return (spec'', (if null g then BVerbatim "error" "Graphviz .neato failed" - else BCommand "image" [pdf]) : rest) + else BCommand "image" [g]) : rest) processSpecials' opts spec (BVerbatim "dot" b : xs) = do - (spec', pdf) <- mkGraph "dot" "" opts spec b + (spec', g) <- mkGraph "dot" "" opts spec b (spec'', rest) <- processSpecials' opts spec' xs - return (spec'', (if null pdf + return (spec'', (if null g then BVerbatim "error" "Graphviz .dot failed" - else BCommand "image" [pdf]) : rest) + else BCommand "image" [g]) : rest) processSpecials' opts spec (BVerbatim "gnuplot" b : xs) = do - (spec', pdf) <- mkGraph "gnuplot" "" opts spec b + (spec', g) <- mkGraph "gnuplot" "" opts spec b (spec'', rest) <- processSpecials' opts spec' xs - return (spec'', (if null pdf + return (spec'', (if null g then BVerbatim "error" "Gnuplot .gp failed" - else BCommand "image" [pdf]) : rest) + else BCommand "image" [g]) : rest) processSpecials' opts spec (BVerbatim "mermaid" b : xs) = do - (spec', pdf) <- mkGraph "mermaid" "" opts spec b + (spec', g) <- mkGraph "mermaid" "" opts spec b (spec'', rest) <- processSpecials' opts spec' xs - return (spec'', (if null pdf + return (spec'', (if null g then BVerbatim "error" "Mermaid .mmd failed" - else BCommand "image" [pdf]) : rest ) + else BCommand "image" [g]) : rest ) processSpecials' opts spec (BVerbatim "ditaa" b : xs) = do (spec', png) <- mkGraph "ditaa" "" opts spec b @@ -84,11 +84,11 @@ processSpecials' opts spec (BVerbatim "ditaa" b : xs) = do else BCommand "image" [png]) : rest ) processSpecials' opts spec (BVerbatim "plantuml" b : xs) = do - (spec', pdf) <- mkGraph "plantuml" "" opts spec b + (spec', g) <- mkGraph "plantuml" "" opts spec b (spec'', rest) <- processSpecials' opts spec' xs - return (spec'', (if null pdf + return (spec'', (if null g then BVerbatim "error" "plantuml .plantuml failed" - else BCommand "image" [pdf]) : rest ) + else BCommand "image" [g]) : rest ) processSpecials' opts spec (x : xs) = do (spec', rest) <- processSpecials' opts spec xs @@ -106,6 +106,7 @@ randomString n = do mkGraph e g opts spec c = do file <- randomString 10 + print $ file ++ "." ++ e case e of "dot" -> mkGraphDot e file g opts spec c "neato" -> mkGraphDot e file g opts spec c @@ -117,26 +118,26 @@ mkGraph e g opts spec c = do mkGraphDot e file g opts spec c = do - let spec' = spec { sRemoveFiles = (file ++ ".pdf") : (file ++ ".dot") : sRemoveFiles spec } + let spec' = spec { sRemoveFiles = (file ++ ".jpeg") : (file ++ ".dot") : sRemoveFiles spec } writeFile (file ++ ".dot") (if null g then c else g ++ " G {\n" ++ c ++ "\n}\n") - r <- exec (optVerbose opts) (optGraphviz opts) ["-Tpdf", "-K" ++ e, file ++ ".dot", "-o" ++ file ++ ".pdf"] - return (spec', (either (const "") (const $ file ++ ".pdf") r)) + r <- exec (optVerbose opts) (optGraphviz opts) ["-Tjpeg", "-K" ++ e, file ++ ".dot", "-o" ++ file ++ ".jpeg"] + return (spec', (either (const "") (const $ file ++ ".jpeg") r)) mkGraphGnuPlot e file g opts spec c = do - let spec' = spec { sRemoveFiles = (file ++ ".pdf") : (file ++ ".gp") : sRemoveFiles spec } - writeFile (file ++ ".gp") ("set terminal pdf\n" ++ "set output \"" ++ file ++ ".pdf" ++ "\"\n" ++c ++ "\n") + let spec' = spec { sRemoveFiles = (file ++ ".jpeg") : (file ++ ".gp") : sRemoveFiles spec } + writeFile (file ++ ".gp") ("set terminal jpeg\n" ++ "set output \"" ++ file ++ ".jpeg" ++ "\"\n" ++c ++ "\n") r <- exec (optVerbose opts) (optGnuplot opts) [file ++ ".gp"] - return (spec', (either (const "") (const $ file ++ ".pdf") r)) + return (spec', (either (const "") (const $ file ++ ".jpeg") r)) mkGraphMermaid e file g opts spec c = do - let spec' = spec { sRemoveFiles = (file ++ ".pdf") : (file ++ ".mmd") : sRemoveFiles spec} + let spec' = spec { sRemoveFiles = (file ++ ".png") : (file ++ ".mmd") : sRemoveFiles spec} writeFile (file ++ ".mmd") (c ++ "\n") - r <- exec (optVerbose opts) (optMermaid opts) ["-i", file ++ ".mmd" , "-o", file ++ ".pdf"] + r <- exec (optVerbose opts) (optMermaid opts) ["-i", file ++ ".mmd" , "-o", file ++ ".png"] - return (spec', (either (const "") (const $ file ++ ".pdf") r)) + return (spec', (either (const "") (const $ file ++ ".png") r)) mkGraphDitaa e file g opts spec c = do let spec' = spec { sRemoveFiles = (file ++ ".png") : (file ++ ".ditaa") : sRemoveFiles spec} diff --git a/src/Simplex/Util.hs b/src/Simplex/Util.hs index 00a8343..f21881f 100644 --- a/src/Simplex/Util.hs +++ b/src/Simplex/Util.hs @@ -70,6 +70,7 @@ skipOneSpace s = s removeIfExists :: String -> IO () removeIfExists file = do + print $ "removing file " ++ file exists <- doesFileExist file when exists (removeFile file) diff --git a/src/simplex.hs b/src/simplex.hs index 78f7e9e..c5bb54a 100644 --- a/src/simplex.hs +++ b/src/simplex.hs @@ -173,32 +173,40 @@ process opts file exit = do _ <- either (throw . Exc) (return . const Ok) r print' "." - when ( optType opts == "html" && not (optDryRun opts )) $ do - r <- liftIO $ exec verbose latex (pdfopts ++ [filename ++ ".tex"]) - _ <- either (throw' spec . Err . snd) (return . const Ok) r - print' "." - - r <- liftIO $ exec verbose lwarpmk (["print"]) - _ <- either (throw' spec . Err . snd) (return . const Ok) r - print' "." - - r <- liftIO $ exec verbose lwarpmk (["html"]) - _ <- either (throw' spec . Err . snd) (return . const Ok) r - print' "." + when ( optType opts == "html" ) $ do + r <- liftIO $ exec verbose latex (pdfopts ++ [filename ++ ".tex"]) + _ <- either (throw' spec . Err . snd) (return . const Ok) r + print' "." + + r <- liftIO $ exec verbose lwarpmk (["print"]) + _ <- either (throw' spec . Err . snd) (return . const Ok) r + print' "." + + r <- liftIO $ exec verbose lwarpmk (["html1"]) + _ <- either (throw' spec . Err . snd) (return . const Ok) r + print' "." + + r <- liftIO $ exec verbose lwarpmk (["limages"]) + _ <- either (throw' spec . Err . snd) (return . const Ok) r + print' "." + + r <- liftIO $ exec verbose lwarpmk (["html"]) + _ <- either (throw' spec . Err . snd) (return . const Ok) r + print' "." - unless (optDryRun opts || optType opts == "tex" || optType opts == "html" ) $ do - -- run pdflatex/xelatex - r <- liftIO $ exec verbose latex (pdfopts ++ [filename ++ ".tex"]) - _ <- either (throw' spec . Err . snd) (return . const Ok) r - print' "." - - -- run pdflatex/xelatex a second time - _ <- liftIO $ exec verbose latex (pdfopts ++ [filename ++ ".tex"]) - print' "." - - -- run pdflatex/xelatex a third time if desired - when (optThreeTimes opts) $ do { liftIO $ exec verbose latex (pdfopts ++ [filename ++ ".tex"]) - ; print' "." } + unless ( optType opts == "tex" || optType opts == "html" ) $ do + -- run pdflatex/xelatex + r <- liftIO $ exec verbose latex (pdfopts ++ [filename ++ ".tex"]) + _ <- either (throw' spec . Err . snd) (return . const Ok) r + print' "." + + -- run pdflatex/xelatex a second time + _ <- liftIO $ exec verbose latex (pdfopts ++ [filename ++ ".tex"]) + print' "." + + -- run pdflatex/xelatex a third time if desired + when (optThreeTimes opts) $ do { liftIO $ exec verbose latex (pdfopts ++ [filename ++ ".tex"]) + ; print' "." } -- clean files unless (optNoClean opts) (liftIO $ do { mapM_ removeIfExists (prepend dirtyExts) @@ -221,6 +229,9 @@ process opts file exit = do _ <- either (throw . Err . snd) (return . const Ok) r return () + when (filetype == "html") $ do + return () + print' " OK\n" when (optPrint opts) $ do From 0bf5dfa1df0a5ec4b69287e9e060e9f61f229fab Mon Sep 17 00:00:00 2001 From: Rongsong Shen Date: Wed, 2 Nov 2022 14:45:52 +0800 Subject: [PATCH 12/14] keep generate images for html output --- src/Simplex/Specials.hs | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/Simplex/Specials.hs b/src/Simplex/Specials.hs index b1656be..2dc5d9a 100644 --- a/src/Simplex/Specials.hs +++ b/src/Simplex/Specials.hs @@ -106,7 +106,6 @@ randomString n = do mkGraph e g opts spec c = do file <- randomString 10 - print $ file ++ "." ++ e case e of "dot" -> mkGraphDot e file g opts spec c "neato" -> mkGraphDot e file g opts spec c @@ -118,21 +117,27 @@ mkGraph e g opts spec c = do mkGraphDot e file g opts spec c = do - let spec' = spec { sRemoveFiles = (file ++ ".jpeg") : (file ++ ".dot") : sRemoveFiles spec } + let spec' = if (optType opts == "html") + then spec { sRemoveFiles = (file ++ ".dot") : sRemoveFiles spec } + else spec { sRemoveFiles = (file ++ ".jpeg") : (file ++ ".dot") : sRemoveFiles spec} writeFile (file ++ ".dot") (if null g then c else g ++ " G {\n" ++ c ++ "\n}\n") r <- exec (optVerbose opts) (optGraphviz opts) ["-Tjpeg", "-K" ++ e, file ++ ".dot", "-o" ++ file ++ ".jpeg"] return (spec', (either (const "") (const $ file ++ ".jpeg") r)) mkGraphGnuPlot e file g opts spec c = do - let spec' = spec { sRemoveFiles = (file ++ ".jpeg") : (file ++ ".gp") : sRemoveFiles spec } + let spec' = if (optType opts == "html" ) + then spec { sRemoveFiles = (file ++ ".gp") : sRemoveFiles spec } + else spec { sRemoveFiles = (file ++ ".jpeg") : (file ++ ".gp") : sRemoveFiles spec } writeFile (file ++ ".gp") ("set terminal jpeg\n" ++ "set output \"" ++ file ++ ".jpeg" ++ "\"\n" ++c ++ "\n") r <- exec (optVerbose opts) (optGnuplot opts) [file ++ ".gp"] return (spec', (either (const "") (const $ file ++ ".jpeg") r)) mkGraphMermaid e file g opts spec c = do - let spec' = spec { sRemoveFiles = (file ++ ".png") : (file ++ ".mmd") : sRemoveFiles spec} + let spec' = if (optType opts == "html") + then spec { sRemoveFiles = (file ++ ".mmd") : sRemoveFiles spec} + else spec { sRemoveFiles = (file ++ ".png") : (file ++ ".mmd") : sRemoveFiles spec} writeFile (file ++ ".mmd") (c ++ "\n") r <- exec (optVerbose opts) (optMermaid opts) ["-i", file ++ ".mmd" , "-o", file ++ ".png"] @@ -140,7 +145,9 @@ mkGraphMermaid e file g opts spec c = do return (spec', (either (const "") (const $ file ++ ".png") r)) mkGraphDitaa e file g opts spec c = do - let spec' = spec { sRemoveFiles = (file ++ ".png") : (file ++ ".ditaa") : sRemoveFiles spec} + let spec' = if (optType opts == "html") + then spec { sRemoveFiles = (file ++ ".ditaa") : sRemoveFiles spec} + else spec { sRemoveFiles = (file ++ ".png") : (file ++ ".ditaa") : sRemoveFiles spec} writeFile (file ++ ".ditaa") (c ++ "\n") r <- exec (optVerbose opts) (optJava opts) ["-jar", (optDitaa opts),file ++ ".ditaa", file ++ ".png"] @@ -148,7 +155,9 @@ mkGraphDitaa e file g opts spec c = do return (spec', (either (const "") (const $ file ++ ".png") r)) mkGraphPlantUML e file g opts spec c = do - let spec' = spec { sRemoveFiles = (file ++ ".png") : (file ++ ".plantuml") : sRemoveFiles spec } + let spec' = if (optType opts == "html") + then spec { sRemoveFiles = (file ++ ".plantuml") : sRemoveFiles spec } + else spec { sRemoveFiles = (file ++ ".png") : (file ++ ".plantuml") : sRemoveFiles spec } writeFile (file ++ ".plantuml") ("@startuml\n" ++ c ++ "\n@enduml\n") r <- exec (optVerbose opts) (optJava opts) ["-jar", (optPlantUML opts), file ++ ".plantuml"] From d1e6bf1796e4cb95d057a600933afc389355fb20 Mon Sep 17 00:00:00 2001 From: Rongsong Shen Date: Thu, 10 Nov 2022 17:55:27 +0800 Subject: [PATCH 13/14] keep generate images for html output --- src/Simplex/ToTeX.hs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Simplex/ToTeX.hs b/src/Simplex/ToTeX.hs index fc8713b..b22a324 100644 --- a/src/Simplex/ToTeX.hs +++ b/src/Simplex/ToTeX.hs @@ -136,7 +136,11 @@ toTeX cfg doc@(Document blocks props) = concat $ preamble $ toTeX' cfg' $ blocks (\x -> "\\usepackage[heading = true ]{ctex}\n") (lookup "cjk" props) : "\\usepackage[\n" - : "]{lwarp}" + : "]{lwarp}\n" + : "\\setcounter{FileDepth}{0}\n" + : "\\setcounter{SideTOCDepth}{2}\n" + : "\\boolfalse{FileSectionNames}\n" + : "\\CSSFilename{sample_project.css}\n" : maybe "" (\x -> "\\usepackage[" ++ x ++ "]{babel}\n") From 75f9b800d8962f7be0cd77140c90f354db1eecfc Mon Sep 17 00:00:00 2001 From: Rongsong Shen Date: Fri, 11 Nov 2022 15:40:04 +0800 Subject: [PATCH 14/14] use latexmk & lwarp settings --- src/Simplex/ToTeX.hs | 33 +++++++++++++++++++++++---------- src/simplex.hs | 35 ++++++++++++++--------------------- 2 files changed, 37 insertions(+), 31 deletions(-) diff --git a/src/Simplex/ToTeX.hs b/src/Simplex/ToTeX.hs index b22a324..3abb2e9 100644 --- a/src/Simplex/ToTeX.hs +++ b/src/Simplex/ToTeX.hs @@ -125,22 +125,35 @@ toTeX cfg doc@(Document blocks props) = concat $ preamble $ toTeX' cfg' $ blocks : "\\usepackage{iftex}\n" : "\\ifPDFTeX" - : "\\usepackage{lmodern} % pdflatex or dvi latex\n" - : "\\usepackage[T1]{fontenc}\n" - : "\\usepackage[utf8]{inputenc}\n" - : "\\else\n" - : "\\usepackage{fontspec} % XeLaTeX or LuaLaTeX\n" - : "\\fi\n" + : "\\usepackage{lmodern} % pdflatex or dvi latex\n" + : "\\usepackage[T1]{fontenc}\n" + : "\\usepackage[utf8]{inputenc}\n" + : "\\else\n" + : "\\usepackage{fontspec} % XeLaTeX or LuaLaTeX\n" + : "\\fi\n" : maybe "" (\x -> "\\usepackage[heading = true ]{ctex}\n") (lookup "cjk" props) : "\\usepackage[\n" + : "latexmk\n" : "]{lwarp}\n" - : "\\setcounter{FileDepth}{0}\n" - : "\\setcounter{SideTOCDepth}{2}\n" - : "\\boolfalse{FileSectionNames}\n" - : "\\CSSFilename{sample_project.css}\n" + : maybe + "" + (\x -> "\\setcounter{FileDepth}{" ++ x ++ "}\n") + (lookup "filedepth" props) + : maybe + "" + (\x -> "\\setcounter{SideTOCDepth}{" ++ x ++ "}\n") + (lookup "sidetocdepth" props) + : maybe + "" + (\x -> "\\boolfalse{FileSectionNames}\n") + (lookup "nofilesectionnames" props) + : maybe + "" + (\x -> "\\CSSFilename{" ++ x ++ "}\n") + (lookup "css" props) : maybe "" (\x -> "\\usepackage[" ++ x ++ "]{babel}\n") diff --git a/src/simplex.hs b/src/simplex.hs index c5bb54a..87c11e2 100644 --- a/src/simplex.hs +++ b/src/simplex.hs @@ -123,6 +123,7 @@ process opts file exit = do convert = optConvert opts xelatex = replace "pdflatex" "xelatex" pdflatex lwarpmk = replace "pdflatex" "lwarpmk" pdflatex + latexmk = replace "pdflatex" "latexmk" pdflatex pdfopts = ["-interaction=nonstopmode", "-file-line-error"] @@ -161,10 +162,14 @@ process opts file exit = do let tex = toTeX cfg tok' + let latexopt = maybe + "-latex" + (\x -> "-xelatex") + (lookup "cjk" (docProps tok')) let latex = maybe - pdflatex - (\x -> xelatex) - (lookup "cjk" (docProps tok') ) + pdflatex + (\x -> "xelatex") + (lookup "cjk" (docProps tok')) print' "." when (optType opts == "tex" || optType opts == "html" || not (optDryRun opts)) $ do @@ -174,26 +179,14 @@ process opts file exit = do print' "." when ( optType opts == "html" ) $ do - r <- liftIO $ exec verbose latex (pdfopts ++ [filename ++ ".tex"]) - _ <- either (throw' spec . Err . snd) (return . const Ok) r - print' "." + r <- liftIO $ exec verbose latexmk ["-pdf", latexopt, filename ++ ".tex"] + _ <- either (throw' spec . Err . snd) (return . const Ok) r + print' "." - r <- liftIO $ exec verbose lwarpmk (["print"]) - _ <- either (throw' spec . Err . snd) (return . const Ok) r - print' "." + r <- liftIO $ exec verbose lwarpmk ["html1"] + _ <- either (throw' spec . Err . snd) (return . const Ok) r + print' "." - r <- liftIO $ exec verbose lwarpmk (["html1"]) - _ <- either (throw' spec . Err . snd) (return . const Ok) r - print' "." - - r <- liftIO $ exec verbose lwarpmk (["limages"]) - _ <- either (throw' spec . Err . snd) (return . const Ok) r - print' "." - - r <- liftIO $ exec verbose lwarpmk (["html"]) - _ <- either (throw' spec . Err . snd) (return . const Ok) r - print' "." - unless ( optType opts == "tex" || optType opts == "html" ) $ do -- run pdflatex/xelatex r <- liftIO $ exec verbose latex (pdfopts ++ [filename ++ ".tex"])