knit-haskell-0.1.0.0: a minimal Rmarkdown sort-of-thing for haskell, by way of Pandoc

Copyright(c) Adam Conner-Sax 2019
LicenseBSD-3-Clause
Maintaineradam_conner_sax@yahoo.com
Stabilityexperimental
Safe HaskellNone
LanguageHaskell2010

Knit.Report

Contents

Description

This module re-exports the basic pieces to build reports using Pandoc as well as providing functions to do the "knitting"--produce the documents. That is, it is intended as one-stop-shopping for using this library to produce Html from various fragments which Pandoc can read.

Examples are available, and might be useful for seeing how all this works.

Notes:

  1. The Knit.Effect.RandomFu effect is not imported since the names might clash with Polysemy.Random. Import either effect directly if you need it.
  2. You can add logging from within document creation using logLE.
  3. The Knit.Report.Input.MarkDown.PandocMarkDown module is exported so if you want to use a different markdown flavor you may need to hide "addMarkDown" when you import this module.
  4. If you use any other effects in your polysemy stack (e.g., Random or RandomFu), you will need to interpretrun them before calling knitHtmlknitHtmls.
Synopsis

Knit

knitHtml Source #

Arguments

:: (MonadIO m, LastMember (Lift m) (KnitEffectStack m)) 
=> Maybe Text

outer logging prefix

-> [LogSeverity]

what to output in log

-> PandocWriterConfig

configuration for the Pandoc Html Writer

-> Semantic (KnitEffectDocStack m) ()

Knit effects "over" m

-> m (Either PandocError Text)

document, converted to Html as Text, or error

Create HTML Text from pandoc fragments In use, you may need a type-application to specify m. This allows use of any underlying monad to handle the Pandoc effects.

knitHtmls Source #

Arguments

:: (MonadIO m, LastMember (Lift m) (KnitEffectStack m)) 
=> Maybe Text

outer logging prefix

-> [LogSeverity]

what to output in log

-> PandocWriterConfig

configuration for the Pandoc Html Writer

-> Semantic (KnitEffectDocsStack m) ()

Knit effects "over" m

-> m (Either PandocError [NamedDoc Text])

named documents, converted to Html as Text or error

Create multiple HTML docs (as Text) from the named sets of pandoc fragments. In use, you may need a type-application to specify m. This allows use of any underlying monad to handle the Pandoc effects.

liftKnit :: Member (Lift m) r => m a -> Semantic r a Source #

lift an action in a base monad into a Polysemy monad

type KnitBase m effs = (MonadIO m, Member (Lift m) effs) Source #

Constraints required to build a document while also using effects from a base monad m.

Inputs

addColonnadeTextTable Source #

Arguments

:: (PandocEffects effs, Member ToPandoc effs, Foldable f) 
=> Colonnade Headed a Text

How to encode data as columns

-> f a

collection of data

-> Semantic effs () 

Add a table given a Colonnade representation producing text

addColonnadeHtmlTable Source #

Arguments

:: (PandocEffects effs, Member ToPandoc effs, Foldable f) 
=> Attribute

Attributes of table Html element, currently unused by knit-haskell

-> Colonnade Headed a Html

How to encode data as columns

-> f a

collection of data

-> Semantic effs () 

Add a Blaze-Colonnade Html Table

addColonnadeCellTable Source #

Arguments

:: (PandocEffects effs, Member ToPandoc effs, Foldable f) 
=> Attribute

Attributes of table Html element, currently unused by knit-haskell

-> Colonnade Headed a Cell

How to encode data as columns

-> f a

collection of data

-> Semantic effs () 

Add a Blaze-Colonnade Cell Table

addMarkDown :: (PandocEffects effs, Member ToPandoc effs) => Text -> Semantic effs () Source #

Add a Pandoc MarkDown fragment with default options

addStrictTextHtml :: (PandocEffects effs, Member ToPandoc effs) => Text -> Semantic effs () Source #

Add Strict Text Html to current Pandoc

addLazyTextHtml :: (PandocEffects effs, Member ToPandoc effs) => Text -> Semantic effs () Source #

Add Lazy Text Html to current Pandoc

addBlaze :: (PandocEffects effs, Member ToPandoc effs) => Html -> Semantic effs () Source #

Add Blaze Html

addLucid :: (PandocEffects effs, Member ToPandoc effs) => Html () -> Semantic effs () Source #

Add Lucid Html

addLatex :: (PandocEffects effs, Member ToPandoc effs) => Text -> Semantic effs () Source #

Add LaTeX

addHvega :: (PandocEffects effs, Member ToPandoc effs) => Text -> VegaLite -> Semantic effs () Source #

Add hvega (via html). Requires html since vega-lite renders using javascript.

Output

data PandocWriterConfig Source #

Constructors

PandocWriterConfig 

Fields

pandocWriterToBlazeDocument Source #

Arguments

:: PandocEffects effs 
=> PandocWriterConfig

Configuration info for the Pandoc writer

-> Semantic (ToPandoc ': effs) ()

Effects stack to run to get Pandoc

-> Semantic effs Html

Blaze Html (in remaining effects)

Convert current Pandoc document (from the ToPandoc effect) into a Blaze Html document. Incudes support for template and template variables and changes to the default writer options.

mindocOptionsF :: WriterOptions -> WriterOptions Source #

options for the mindoc template

Effects

data Semantic (r :: [(Type -> Type) -> Type -> Type]) a #

The Semantic monad handles computations of arbitrary extensible effects. A value of type Semantic r describes a program with the capabilities of r. For best results, r should always be kept polymorphic, but you can add capabilities via the Member constraint.

The value of the Semantic monad is that it allows you to write programs against a set of effects without a predefined meaning, and provide that meaning later. For example, unlike with mtl, you can decide to interpret an Error effect tradtionally as an Either, or instead significantly faster as an IO Exception. These interpretations (and others that you might add) may be used interchangably without needing to write any newtypes or Monad instances. The only change needed to swap interpretations is to change a call from runError to runErrorInIO.

The effect stack r can contain arbitrary other monads inside of it. These monads are lifted into effects via the Lift effect. Monadic values can be lifted into a Semantic via sendM.

A Semantic can be interpreted as a pure value (via run) or as any traditional Monad (via runM). Each effect E comes equipped with some interpreters of the form:

runE :: Semantic (E ': r) a -> Semantic r a

which is responsible for removing the effect E from the effect stack. It is the order in which you call the interpreters that determines the monomorphic representation of the r parameter.

After all of your effects are handled, you'll be left with either a Semantic '[] a or a Semantic '[ Lift m ] a value, which can be consumed respectively by run and runM.

Examples

As an example of keeping r polymorphic, we can consider the type

Member (State String) r => Semantic r ()

to be a program with access to

get :: Semantic r String
put :: String -> Semantic r ()

methods.

By also adding a

Member (Error Bool) r

constraint on r, we gain access to the

throw :: Bool -> Semantic r a
catch :: Semantic r a -> (Bool -> Semantic r a) -> Semantic r a

functions as well.

In this sense, a Member (State s) r constraint is analogous to mtl's MonadState s m and should be thought of as such. However, unlike mtl, a Semantic monad may have an arbitrary number of the same effect.

For example, we can write a Semantic program which can output either Ints or Bools:

foo :: ( Member (Output Int) r
       , Member (Output Bool) r
       )
    => Semantic r ()
foo = do
  output @Int  5
  output True

Notice that we must use -XTypeApplications to specify that we'd like to use the (Output Int) effect.

Instances
Member (Error PandocError :: (Type -> Type) -> Type -> Type) effs => MonadError PandocError (Semantic effs) Source #

Split off the error piece. We will handle directly with the polysemy Error effect

Instance details

Defined in Knit.Effect.PandocMonad

Methods

throwError :: PandocError -> Semantic effs a #

catchError :: Semantic effs a -> (PandocError -> Semantic effs a) -> Semantic effs a #

Monad (Semantic f) 
Instance details

Defined in Polysemy.Internal

Methods

(>>=) :: Semantic f a -> (a -> Semantic f b) -> Semantic f b #

(>>) :: Semantic f a -> Semantic f b -> Semantic f b #

return :: a -> Semantic f a #

fail :: String -> Semantic f a #

Functor (Semantic f) 
Instance details

Defined in Polysemy.Internal

Methods

fmap :: (a -> b) -> Semantic f a -> Semantic f b #

(<$) :: a -> Semantic f b -> Semantic f a #

Member Fixpoint r => MonadFix (Semantic r) 
Instance details

Defined in Polysemy.Internal

Methods

mfix :: (a -> Semantic r a) -> Semantic r a #

Applicative (Semantic f) 
Instance details

Defined in Polysemy.Internal

Methods

pure :: a -> Semantic f a #

(<*>) :: Semantic f (a -> b) -> Semantic f a -> Semantic f b #

liftA2 :: (a -> b -> c) -> Semantic f a -> Semantic f b -> Semantic f c #

(*>) :: Semantic f a -> Semantic f b -> Semantic f b #

(<*) :: Semantic f a -> Semantic f b -> Semantic f a #

Member NonDet r => Alternative (Semantic r) 
Instance details

Defined in Polysemy.Internal

Methods

empty :: Semantic r a #

(<|>) :: Semantic r a -> Semantic r a -> Semantic r a #

some :: Semantic r a -> Semantic r [a] #

many :: Semantic r a -> Semantic r [a] #

Member (Lift IO) r => MonadIO (Semantic r)

This instance will only lift IO actions. If you want to lift into some other MonadIO type, use this instance, and handle it via the runIO interpretation.

Instance details

Defined in Polysemy.Internal

Methods

liftIO :: IO a -> Semantic r a #

PandocEffects effs => PandocMonad (Semantic effs) Source #

PandocMonad instance so that pandoc functions can be run in the polysemy union effect

Instance details

Defined in Knit.Effect.PandocMonad

Member (Random :: (Type -> Type) -> Type -> Type) effs => MonadRandom (Semantic effs) Source #

supply instance of MonadRandom for functions which require it

Instance details

Defined in Knit.Effect.RandomFu

type Member (e :: (Type -> Type) -> Type -> Type) (r :: [(Type -> Type) -> Type -> Type]) = Member' e r #

A proof that the effect e is available somewhere inside of the effect stack r.

data Lift (m :: Type -> Type) (z :: Type -> Type) a #

An effect which allows a regular Monad m into the Semantic ecosystem. Monadic actions in m can be lifted into Semantic via sendM.

For example, you can use this effect to lift IO actions directly into Semantic:

sendM (putStrLn "hello") :: Member (Lift IO) r => Semantic r ()

That being said, you lose out on a significant amount of the benefits of Semantic by using sendM directly in application code; doing so will tie your application code directly to the underlying monad, and prevent you from interpreting it differently. For best results, only use Lift in your effect interpreters.

Consider using trace and runTraceIO as a substitute for using putStrLn directly.

type Pandocs = Docs PandocWithRequirements Source #

Type-alias for use with the Docs effect.

data ToPandoc m r Source #

Pandoc writer, add any read format to current doc

data Requirement Source #

ADT to allow inputs to request support, if necessary or possible, in the output format. E.g., Latex output in Html needs MathJax. But Latex needs to nothing to output in Latex. Vega-lite needs some script headers to output in Html and can't be output in other formats. For now, we support all the things we can in any output format so this just results in a runtime test.

Constructors

VegaSupport

Supported only for Html output.

LatexSupport

Supported in Html output (via MathJax) and Latex output.

type LogWithPrefixesLE effs = LogWithPrefixes LogEntry effs Source #

Constraint helper for LogEntry type with prefixes

logAll :: [LogSeverity] Source #

LogSeverity list used in order to output everything.

nonDiagnostic :: [LogSeverity] Source #

LogSeverity list used to output all but Diagnostic. Diagnostic messages are sometimes useful for debugging but can get noisy depending on how you use it.

logLE :: Member (Logger LogEntry) effs => LogSeverity -> Text -> Semantic effs () Source #

Add one log-entry of the LogEntry type.

wrapPrefix :: Member PrefixLog effs => Text -> Semantic effs a -> Semantic effs a Source #

Add a prefix for the block of code.

filteredLogEntriesToIO :: MonadIO (Semantic effs) => [LogSeverity] -> Semantic (Logger LogEntry ': (PrefixLog ': effs)) x -> Semantic effs x Source #

Run the Logger and PrefixLog effects using the preferred handler and filter output in any Polysemy monad with IO in the union.