From 10fbde625b41801dc95b52699e09b1265999490b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9ophile=20Choutri=20de=20Tarl=C3=A9?= Date: Sat, 13 Jun 2026 11:56:44 +0200 Subject: [PATCH] Add code and outut style options This PR adds styling options for the code fences and results in the final document. The idea is to support "cleaner" markdown generation for non-interactive workflows so that neither the original code nor blockquotes are visible in the generated markdown. --- app/Main.hs | 56 ++++-------------------- scripths.cabal | 5 ++- src/ScriptHs/CLI/Types.hs | 72 +++++++++++++++++++++++++++++++ src/ScriptHs/Markdown.hs | 66 +++++++++++++++++++++++----- src/ScriptHs/Notebook.hs | 43 ++++++++++++------ test/Test/Integration.hs | 9 +++- test/Test/Markdown.hs | 91 ++++++++++++++++++++++++++------------- test/Test/Notebook.hs | 49 ++++++++++++++++++--- 8 files changed, 282 insertions(+), 109 deletions(-) create mode 100644 src/ScriptHs/CLI/Types.hs diff --git a/app/Main.hs b/app/Main.hs index 1e999c1..2c66e55 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -1,7 +1,6 @@ module Main where import Control.Monad (unless, when) -import Data.List (isPrefixOf) import Data.Maybe (isJust) import qualified Data.Text as T import qualified Data.Text.IO as TIO @@ -25,20 +24,8 @@ import ScriptHs.Version ( tagStyleFor, tagVersion, ) - --- | Parsed command-line options. -data Args = Args - { argScript :: Maybe FilePath - , argOutput :: Maybe FilePath - , argPackages :: [FilePath] - , argNoLocalProject :: Bool - , argInPlace :: Bool - , argHelp :: Bool - , argVersion :: Bool - } - -emptyArgs :: Args -emptyArgs = Args Nothing Nothing [] False False False False +import ScriptHs.CLI.Types +import ScriptHs.Markdown main :: IO () main = do @@ -61,7 +48,7 @@ main = do { roPackages = pkgs , roEnclosingProject = not (argNoLocalProject a) } - dispatch opts path outPath + dispatch (argOutputStyle a) (argCodeStyle a) opts path outPath {- | Resolve the output destination, honouring @--in-place@: write back over the notebook itself. In-place is only meaningful for notebooks (the @.ghci@\/@.hs@ @@ -80,43 +67,16 @@ resolveOutput a path isNotebook :: FilePath -> Bool isNotebook path = takeExtension path `elem` [".md", ".markdown"] -dispatch :: RunOptions -> FilePath -> Maybe FilePath -> IO () -dispatch opts path outputPath = +dispatch :: OutputStyle -> CodeStyle -> RunOptions -> FilePath -> Maybe FilePath -> IO () +dispatch outputStyle codeStyle opts path outputPath = case takeExtension path of - ".md" -> runNotebook opts path outputPath - ".markdown" -> runNotebook opts path outputPath + ".md" -> runNotebook outputStyle codeStyle opts path outputPath + ".markdown" -> runNotebook outputStyle codeStyle opts path outputPath _ -> do contents <- TIO.readFile path let sf = parseScript contents runScript opts path sf -{- | Parse args: @[-o FILE | --output=FILE] [-i | --in-place] [--package DIR | --p DIR | --package=DIR]... [--no-local-project] [-h | --help]