<commit_before>{-# LANGUAGE LambdaCase        #-}
{-# LANGUAGE OverloadedStrings #-}

module Documentation.RunAsciiDoctor
  ( manTxtToDocbook
  ) where

import           Data.Semigroup
import           Data.Text
import qualified Data.Text               as T
import qualified Data.Text.Lazy          as T (toStrict)
import qualified Data.List as L
import           Data.Text.Lazy.Encoding
import           Say
import           System.Environment
import           System.Exit
import           System.FilePath
import           System.Process.Typed

-- | Convert a man page from the Vulkan-Docs repo into docbook format using
-- 'asciidoctor'
--
-- This function also applies a few fixes to the output to make it more
-- friendly for Pandoc.
manTxtToDocbook
  :: [Text]
  -- ^ A list of extension names to enable
  -> FilePath
  -- ^ The 'Vulkan-Docs' directory, necessary to find plugins
  -> FilePath
  -- ^ The path to the man page to translate
  -> IO (Either Text Text)
  -- ^ Either an error if something went wrong, or the docbook xml
manTxtToDocbook extensions vkPath manTxt =
  fmap fixupDocbookOutput <$> asciidoctor extensions vkPath manTxt

asciidoctor
  :: [Text]
  -- ^ Extension names
  -> FilePath
  -- ^ The 'Vulkan-Docs' directory, necessary to find plugins
  -> FilePath
  -- ^ The path to the man page to translate
  -> IO (Either Text Text)
asciidoctor extensions vkPath manTxt = do
  let asciidoctorPath = "asciidoctor"
      -- This is mimicing the Makefile in Vulkan-Docs
      extAttribs      = preceedAll "-a" (T.unpack <$> extensions)
      -- TODO: Version information here
      attribOpts      = extAttribs
      noteOpts        = []
      adocExts =
        [ "-r"
        , vkPath </> "config/vulkan-macros.rb"
        , "-r"
        , vkPath </> "config/tilde_open_block.rb"
        ]
      adocOpts = attribOpts ++ noteOpts ++ adocExts
      args =
        adocOpts
          ++ [ "-r"
             , "asciidoctor-mathematical"
             , "-r"
             , vkPath </> "config/asciidoctor-mathematical-ext.rb"
             , "-a"
             , "mathematical-format=svg"
             , "--backend"
             , "docbook5"
             , manTxt
             , "--out-file"
             , "-"
             ]
      p = setStdin closed $ proc asciidoctorPath args
  (exitCode, out, err) <- readProcess p
  case exitCode of
    ExitFailure e ->
      pure<commit_msg>Better png generation<commit_after>{-# LANGUAGE LambdaCase        #-}
{-# LANGUAGE OverloadedStrings #-}

module Documentation.RunAsciiDoctor
  ( manTxtToDocbook
  ) where

import           Data.Semigroup
import           Data.Text
import qualified Data.Text               as T
import qualified Data.Text.Lazy          as T (toStrict)
import qualified Data.List as L
import           Data.Text.Lazy.Encoding
import           Say
import           System.Environment
import           System.Exit
import           System.FilePath
import           System.Process.Typed

-- | Convert a man page from the Vulkan-Docs repo into docbook format using
-- 'asciidoctor'
--
-- This function also applies a few fixes to the output to make it more
-- friendly for Pandoc.
manTxtToDocbook
  :: [Text]
  -- ^ A list of extension names to enable
  -> FilePath
  -- ^ The 'Vulkan-Docs' directory, necessary to find plugins
  -> FilePath
  -- ^ The path to the man page to translate
  -> IO (Either Text Text)
  -- ^ Either an error if something went wrong, or the docbook xml
manTxtToDocbook extensions vkPath manTxt =
  fmap fixupDocbookOutput <$> asciidoctor extensions vkPath manTxt

asciidoctor
  :: [Text]
  -- ^ Extension names
  -> FilePath
  -- ^ The 'Vulkan-Docs' directory, necessary to find plugins
  -> FilePath
  -- ^ The path to the man page to translate
  -> IO (Either Text Text)
asciidoctor extensions vkPath manTxt = do
  let asciidoctorPath = "asciidoctor"
      -- This is mimicing the Makefile in Vulkan-Docs
      extAttribs      = preceedAll "-a" (T.unpack <$> extensions)
      -- TODO: Version information here
      attribOpts      = extAttribs
      noteOpts        = []
      adocExts =
        [ "-r"
        , vkPath </> "config/vulkan-macros.rb"
        , "-r"
        , vkPath </> "config/tilde_open_block.rb"
        ]
      adocOpts         = attribOpts ++ noteOpts ++ adocExts
      mathemeticalOpts =
        [ "-r"
        , "asciidoctor-mathematical"
        , "-r"
        , vkPath </> "config/asciidoctor-mathematical-ext.rb"
        , "-a"
        , "mathematical-format=png"
        , "-a"
        , "mathematical-ppi=100"
        ]
      args =
        adocOpts
          ++ mathemeticalOpts
          ++ ["--backend", "docbook5", manTxt, "--out-file", "-"]
      p = setStdin closed $ proc asciidoctorPath args
  (exitCode, out, err) <- readProcess p
  case exitCode of
    ExitFailure e ->
      pure
{'fraction_non_alphanumeric': 0.09676750932449234, 'fraction_numerical': 0.0010360547036883548, 'mean_word_length': 2.037759597230963, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1877325', 'n_tokens_mistral': 1492, 'n_tokens_neox': 1493, 'n_words': 435}