@@ -12,7 +12,7 @@ module Render (
12
12
Render (render ), concatRender , url , renderTab , renderFig , simpleRender , simpleRender2 , squareAbbr ,
13
13
linkToSection , secnum , SectionFileStyle (.. ), applySectionFileStyle , Page (.. ), parentLink ,
14
14
fileContent , Link (.. ), outputDir , linkToRemoteTable , defaultRenderContext , isSectionPage ,
15
- abbrAsPath , abbreviations , RenderContext (.. ), renderLatexParas
15
+ abbrAsPath , abbreviations , RenderContext (.. ), renderLatexParas , makeMathMap , MathMap , extractMath
16
16
) where
17
17
18
18
import Load14882 (parseIndex ) -- todo: bad
@@ -420,7 +420,7 @@ instance Render LaTeXUnit where
420
420
render env@ (TeXEnv e _ t)
421
421
| e `elem` makeSpan = spanTag (Text. pack e) . render t
422
422
| e `elem` makeDiv = xml " div" [(" class" , Text. pack e)] . render t
423
- | isMath env && isComplexMath [env] = return $ TextBuilder. fromText $ renderComplexMath [env]
423
+ | isMath env && isComplexMath [env] = TextBuilder. fromText . renderComplexMath [env]
424
424
| isCodeblock env = renderCodeblock env
425
425
| e == " minipage" , [cb@ (TeXEnv " codeblock" [] _)] <- trim t =
426
426
xml " div" [(" class" , " minipage" )] . renderCodeblock cb
@@ -644,7 +644,8 @@ data RenderContext = RenderContext
644
644
, inCodeBlock :: Bool -- in codeblocks, some commands like \tcode have a different meaning
645
645
, replXmlChars :: Bool -- replace < with <, etc
646
646
, extraIndentation :: Int -- in em
647
- , idPrefix :: Text }
647
+ , idPrefix :: Text
648
+ , mathMap :: MathMap }
648
649
649
650
defaultRenderContext :: RenderContext
650
651
defaultRenderContext = RenderContext
@@ -659,7 +660,8 @@ defaultRenderContext = RenderContext
659
660
, inCodeBlock = False
660
661
, replXmlChars = True
661
662
, extraIndentation = 0
662
- , idPrefix = " " }
663
+ , idPrefix = " "
664
+ , mathMap = Map. empty }
663
665
664
666
squareAbbr :: Render a => a -> TextBuilder. Builder
665
667
squareAbbr x = " [" ++ simpleRender2 x ++ " ]"
@@ -700,14 +702,39 @@ abbrHref abbr RenderContext{..}
700
702
_ -> url abbr
701
703
| otherwise = linkToSectionHref SectionToSection abbr
702
704
705
+ extractMath :: LaTeX -> Maybe (String , Bool )
706
+ extractMath [TeXMath Dollar (TeXComm " text" [(FixArg , stuff)] : more)] = extractMath [TeXMath Dollar more]
707
+ extractMath [TeXMath Dollar (c@ (TeXComm " tcode" _) : more)] = extractMath [TeXMath Dollar more]
708
+ extractMath m | not (isComplexMath m) = Nothing
709
+ extractMath m = if isComplexMath m then Just (mathKey m) else Nothing
710
+
711
+ prepMath :: LaTeX -> String
712
+ prepMath = Text. unpack . renderLaTeX . (>>= cleanup)
713
+ where
714
+ cleanup :: LaTeXUnit -> LaTeX
715
+ cleanup (TeXComm " tcode" x) = [TeXComm " texttt" (map (second (>>= cleanup)) x)]
716
+ cleanup (TeXComm " ensuremath" [(FixArg , x)]) = x >>= cleanup
717
+ cleanup (TeXComm " discretionary" _) = []
718
+ cleanup (TeXComm " hfill" [] ) = []
719
+ cleanup (TeXComm " break" [] ) = []
720
+ cleanup (TeXComm " br" [] ) = []
721
+ cleanup (TeXComm " -" [] ) = []
722
+ cleanup (TeXComm " quad " [] ) = [TeXRaw " " ] -- because MathJax does not support \quad
723
+ cleanup (TeXComm x y) = [TeXComm x (map (second (>>= cleanup)) y)]
724
+ cleanup x@ (TeXRaw _) = [x]
725
+ cleanup (TeXBraces x) = [TeXBraces (x >>= cleanup)]
726
+ cleanup (TeXEnv x y z) = [TeXEnv x (map (second (>>= cleanup)) y) (z >>= cleanup)]
727
+ cleanup (TeXMath x y) = [TeXMath x (y >>= cleanup)]
728
+ cleanup x@ TeXLineBreak = [x]
729
+
703
730
renderMath :: LaTeX -> RenderContext -> TextBuilder. Builder
704
731
renderMath [TeXMath Dollar (TeXComm " text" [(FixArg , stuff)] : more)] ctx =
705
732
render stuff ctx ++ renderMath [TeXMath Dollar more] ctx
706
733
renderMath [TeXMath Dollar (c@ (TeXComm " tcode" _) : more)] ctx =
707
734
render c ctx ++ renderMath [TeXMath Dollar more] ctx
708
- renderMath m sec
709
- | isComplexMath m = TextBuilder. fromText $ renderComplexMath m
710
- | otherwise = spanTag s $ renderSimpleMath m sec
735
+ renderMath m ctx
736
+ | isComplexMath m = TextBuilder. fromText $ renderComplexMath m ctx
737
+ | otherwise = spanTag s $ renderSimpleMath m ctx
711
738
where
712
739
s = mathKind m
713
740
mathKind [TeXMath Square _] = " mathblock"
@@ -775,46 +802,53 @@ renderSimpleMathUnit (TeXMath Dollar m) sec = renderSimpleMath (trim m) sec
775
802
renderSimpleMathUnit (TeXMath _ m) sec = renderSimpleMath m sec
776
803
renderSimpleMathUnit other sec = render other sec
777
804
778
- renderComplexMath :: LaTeX -> Text
779
- renderComplexMath m = case m of
780
- [TeXMath kind t] -> memodRenderMath (prepMath t) (kind == Dollar )
781
- [TeXEnv " eqnarray*" [] _] -> memodRenderMath (prepMath m) False
782
- _ -> memodRenderMath (prepMath m) True
783
- where
784
- prepMath = Text. unpack . renderLaTeX . (>>= cleanup)
785
- cleanup :: LaTeXUnit -> LaTeX
786
- cleanup (TeXComm " tcode" x) = [TeXComm " texttt" (map (second (>>= cleanup)) x)]
787
- cleanup (TeXComm " ensuremath" [(FixArg , x)]) = x >>= cleanup
788
- cleanup (TeXComm " discretionary" _) = []
789
- cleanup (TeXComm " hfill" [] ) = []
790
- cleanup (TeXComm " break" [] ) = []
791
- cleanup (TeXComm " br" [] ) = []
792
- cleanup (TeXComm " -" [] ) = []
793
- cleanup (TeXComm " quad " [] ) = [TeXRaw " " ] -- because MathJax does not support \quad
794
- cleanup (TeXComm x y) = [TeXComm x (map (second (>>= cleanup)) y)]
795
- cleanup x@ (TeXRaw _) = [x]
796
- cleanup (TeXBraces x) = [TeXBraces (x >>= cleanup)]
797
- cleanup (TeXEnv x y z) = [TeXEnv x (map (second (>>= cleanup)) y) (z >>= cleanup)]
798
- cleanup (TeXMath x y) = [TeXMath x (y >>= cleanup)]
799
- cleanup x@ TeXLineBreak = [x]
805
+ mathKey :: LaTeX -> (String , Bool )
806
+ mathKey m = case m of
807
+ [TeXMath kind t] -> (prepMath t, kind == Dollar )
808
+ [TeXEnv " eqnarray*" [] _] -> (prepMath m, False )
809
+ _ -> (prepMath m, True )
810
+
811
+ renderComplexMath :: LaTeX -> RenderContext -> Text
812
+ renderComplexMath math ctx
813
+ | inline = html
814
+ | otherwise = " </p><p style='text-align:center'>" ++ html ++ " </p><p>"
815
+ where
816
+ k@ (_, inline) = mathKey math
817
+ html = fromJust $ Map. lookup k $ mathMap ctx
800
818
801
819
rmTrailingNewline :: Text -> Text
802
820
rmTrailingNewline (Text. stripSuffix " \n " -> Just x) = x
803
821
rmTrailingNewline x = x
804
822
805
- memodRenderMath :: String -> Bool -> Text
806
- memodRenderMath = memo2 $ \ s inline -> unsafePerformIO $ do
807
- let args = [" --inline" | inline] ++ [" --" , s]
808
- formula <- Text. replace " focusable=\" false\" " " "
809
- . rmTrailingNewline -- Prevents artifacts in [rand.adapt.ibits]#4
810
- . Text. pack
811
- . rm " id=\" (MJXc|MathJax)-[0-9A-Za-z-]+\" "
812
- . rm " style=\"\" "
813
- . readProcess " tex2html" args " "
814
- return $ if inline then formula else " </p><p style='text-align:center'>" ++ formula ++ " </p><p>"
815
- where
816
- rm r s = subRegex (mkRegex r) s " "
817
-
823
+ batchedRenderMath :: [(String , Bool )] -> IO [Text ]
824
+ batchedRenderMath formulas = do
825
+ out <- readProcess " ./mathjax-batch" [] stdin
826
+ return
827
+ $ splitResults " "
828
+ $ Text. lines
829
+ $ Text. replace " focusable=\" false\" " " "
830
+ $ rmTrailingNewline -- Prevents artifacts in [rand.adapt.ibits]#4
831
+ $ Text. pack
832
+ $ rm " id=\" (MJXc|MathJax)-[0-9A-Za-z-]+\" "
833
+ $ rm " style=\"\" "
834
+ $ out
835
+ where
836
+ rm r s = subRegex (mkRegex r) s " "
837
+ stdinLines :: [String ]
838
+ stdinLines = concatMap (\ (f, inline) -> [f] ++ [if inline then " INLINE" else " NONINLINE" ]) formulas
839
+ stdin :: String
840
+ stdin = unlines stdinLines
841
+
842
+ splitResults :: Text -> [Text ] -> [Text ]
843
+ splitResults _ [] = []
844
+ splitResults x (a: b)
845
+ | a == " DONE" = x : splitResults " " b
846
+ | otherwise = splitResults (if x == " " then a else x ++ " \n " ++ a) b
847
+
848
+ type MathMap = Map. Map (String , Bool ) Text
849
+
850
+ makeMathMap :: [(String , Bool )] -> IO MathMap
851
+ makeMathMap formulas = Map. fromList . zip formulas . batchedRenderMath formulas
818
852
819
853
renderTable :: LaTeX -> [Row [TeXPara ]] -> RenderContext -> TextBuilder. Builder
820
854
renderTable colspec a sec =
0 commit comments