<commit_before>module MB.Tikz
    ( tikzProcessor
    )
where

import Data.List
    ( intercalate
    )
import Data.Digest.Pure.SHA
    ( showDigest
    , sha1
    )
import Data.ByteString.Lazy.Char8
    ( pack
    )
import System.Process
    ( readProcessWithExitCode
    )
import System.Exit
    ( ExitCode(..)
    )
import qualified Text.Pandoc as Pandoc
import MB.Types
import qualified MB.Files as Files

tikzProcessor :: Processor
tikzProcessor =
    nullProcessor { preProcessPost = Just renderTikz
                  }

renderTikz :: Blog -> Post -> IO Post
renderTikz config post = do
  let Pandoc.Pandoc m blocks = postAst post
  newBlocks <- mapM (renderTikzScript config) blocks
  return $ post { postAst = Pandoc.Pandoc m newBlocks }

renderTikzScript :: Blog
                 -> Pandoc.Block
                 -> IO Pandoc.Block
renderTikzScript config blk@(Pandoc.CodeBlock ("tikz", classes, _) rawScript) = do
  putStrLn "Rendering equation graph (tikz)"

  let digestInput = rawScript

      -- Generate an image name in the images/ directory of the blog
      -- data directory.  Use a hash of the preamble name and script
      -- contents so we can avoid rendering the image again if it
      -- already exists.
      hash = showDigest $ sha1 $ pack digestInput
      imageFilename = "tikz-" ++ hash ++ ".png"
      imagePath = Files.imageFilename config imageFilename
      preamble = unlines [ "\\documentclass{article}"
                         , "\\usepackage{tikz}"
                         , "\\usepackage{pgfplots}"
                         , "\\pgfrealjobname{tmp}"

                         , "\\begin{document}"
                         , "\\begin{figure}"
                         , "\\beginpgfgraphicnamed{testfigure}"
                         , "\\begin{tikzpicture}"
                         ]
      postamble = unlines [ "\\end{tikzpicture}"
                          , "\\endpgfgraphicnamed"
                          , "\\end{figure}"
                          , "\\end{document}"
                          ]

      latexSource = preamble ++ rawScript ++ postamble

  writeFile "tmp.tex" latexSource

  (s1, out1, _) <- readProcessWithExitCode "pdflatex" ["tmp.tex"] ""
  case s1 of
    ExitFailure _ -> putStrLn out1 >> return blk
    ExitSuccess -> do
            (s2, out2, _) <- readProcessWithExitCode "pdflatex" ["--jobname", "testfigure", "tmp.tex"] ""
            case s2 of
              ExitFailure _ -> putStrLn out2 >> return blk
              ExitSuccess -> do
                      -- Convert the temporary file to a PNG.
                      (s3, out3, err) <- readProcessWithExitCode "convert" [ "-density", "120"
                                                                           , "-quality", "100"
                                                                           , "testfigure.pdf"
                                                                           , imagePath
                                                                           ] ""

                      case s3 of
                        ExitFailure _ -> do
                                putStrLn "Could not render Tikz picture:"
                                putStrLn "Equation was:"
                                putStrLn latexSource
                                putStrLn "dvipng output:"
                                putStrLn out3
                                putStrLn err
                                return blk
                        ExitSuccess ->
                            return $ Pandoc.Para [Pandoc.RawInline "html" $
                                                        concat [ "<img src=\"/generated-images/"
                                                               , imageFilename
                                                               , "\" class=\""
                                                               , intercalate " " classes
                                                               , "\">"
                                                               ]
                                                 ]
renderTikzScript _ b = return b<commit_msg>Fix tex output directory, remove unnecessary processing step<commit_after>module MB.Tikz
    ( tikzProcessor
    )
where

import Data.List
    ( intercalate
    )
import Data.Digest.Pure.SHA
    ( showDigest
    , sha1
    )
import Data.ByteString.Lazy.Char8
    ( pack
    )
import System.Process
    ( readProcessWithExitCode
    )
import System.Exit
    ( ExitCode(..)
    )
import qualified Text.Pandoc as Pandoc
import MB.Types
import qualified MB.Files as Files

tikzProcessor :: Processor
tikzProcessor =
    nullProcessor { preProcessPost = Just renderTikz
                  }

renderTikz :: Blog -> Post -> IO Post
renderTikz config post = do
  let Pandoc.Pandoc m blocks = postAst post
  newBlocks <- mapM (renderTikzScript config) blocks
  return $ post { postAst = Pandoc.Pandoc m newBlocks }

renderTikzScript :: Blog
                 -> Pandoc.Block
                 -> IO Pandoc.Block
renderTikzScript config blk@(Pandoc.CodeBlock ("tikz", classes, _) rawScript) = do
  putStrLn "Rendering equation graph (tikz)"

  let digestInput = rawScript

      -- Generate an image name in the images/ directory of the blog
      -- data directory.  Use a hash of the preamble name and script
      -- contents so we can avoid rendering the image again if it
      -- already exists.
      hash = showDigest $ sha1 $ pack digestInput
      imageFilename = "tikz-" ++ hash ++ ".png"
      imagePath = Files.imageFilename config imageFilename
      preamble = unlines [ "\\documentclass{article}"
                         , "\\usepackage{tikz}"
                         , "\\usepackage{pgfplots}"
                         , "\\pgfrealjobname{tmp}"

                         , "\\begin{document}"
                         , "\\begin{figure}"
                         , "\\beginpgfgraphicnamed{testfigure}"
                         , "\\begin{tikzpicture}"
                         ]
      postamble = unlines [ "\\end{tikzpicture}"
                          , "\\endpgfgraphicnamed"
                          , "\\end{figure}"
                          , "\\end{document}"
                          ]

      latexSource = preamble ++ rawScript ++ postamble

  writeFile "/tmp/tmp.tex" latexSource

  (s1, out1, _) <- readProcessWithExitCode "pdflatex" [ "-output-directory", "/tmp"
                                                      , "--jobname", "testfigure", "/tmp/tmp.tex"] ""
  case s1 of
    ExitFailure _ -> putStrLn out1 >> return blk
    ExitSuccess -> do
            -- Convert the temporary file to a PNG.
            (s2, out2, err) <- readProcessWithExitCode "convert" [ "-density", "120"
                                                                 , "-quality", "100"
                                                                 , "/tmp/testfigure.pdf"
                                                                 , imagePath
                                                                 ] ""

            case s2 of
              ExitFailure _ -> do
                      putStrLn "Could not render Tikz picture:"
                      putStrLn "Equation was:"
                      putStrLn latexSource
                      putStrLn "dvipng output:"
                      putStrLn out2
                      putStrLn err
                      return blk
              ExitSuccess ->
                  return $ Pandoc.Para [Pandoc.RawInline "html" $
                                              concat [ "<img src=\"/generated-images/"
                                                     , imageFilename
                                                     , "\" class=\""
                                                     , intercalate " " classes
                                                     , "\">"
                                                     ]
                                       ]
renderTikzScript _ b = return b
{'fraction_non_alphanumeric': 0.08291894579811039, 'fraction_numerical': 0.004724017901541522, 'mean_word_length': 1.0455123315535215, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 50, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '863180', 'n_tokens_mistral': 1914, 'n_tokens_neox': 1783, 'n_words': 487}