Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cleanup: make buildDiagram (and several other functions) take a Build…
…Opts record
- Loading branch information
Brent Yorgey
committed
Nov 27, 2013
1 parent
c63160e
commit 05c17f8
Showing
4 changed files
with
146 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
{-# LANGUAGE DeriveDataTypeable #-} | ||
{-# LANGUAGE FlexibleContexts #-} | ||
{-# LANGUAGE MultiParamTypeClasses #-} | ||
{-# LANGUAGE ScopedTypeVariables #-} | ||
{-# LANGUAGE StandaloneDeriving #-} | ||
{-# LANGUAGE TemplateHaskell #-} | ||
{-# OPTIONS_GHC -fno-warn-orphans #-} | ||
|
||
----------------------------------------------------------------------------- | ||
-- | | ||
-- Module : Diagrams.Builder.Opts | ||
-- Copyright : (c) 2013 diagrams-lib team (see LICENSE) | ||
-- License : BSD-style (see LICENSE) | ||
-- Maintainer : diagrams-discuss@googlegroups.com | ||
-- | ||
-- Options for dynamic creation of diagrams. | ||
-- | ||
----------------------------------------------------------------------------- | ||
module Diagrams.Builder.Opts | ||
( | ||
BuildOpts(..), backendOpts, snippets, pragmas, imports, decideRegen, diaExpr, postProcess | ||
) | ||
where | ||
|
||
import Control.Lens (Lens', generateSignatures, lensRules, | ||
makeLensesWith, (&), (.~)) | ||
|
||
import Diagrams.Prelude (Diagram, Options) | ||
|
||
data BuildOpts b v x | ||
= BuildOpts | ||
{ backendToken :: b | ||
-- ^ Backend token | ||
, vectorToken :: v | ||
-- ^ Dummy vector argument to fix the vector space type | ||
, _backendOpts :: Options b v | ||
, _snippets :: [String] | ||
, _pragmas :: [String] | ||
, _imports :: [String] | ||
, _decideRegen :: String -> IO (x, Maybe (Options b v -> Options b v)) | ||
, _diaExpr :: String | ||
, _postProcess :: Diagram b v -> Diagram b v | ||
} | ||
|
||
makeLensesWith (lensRules & generateSignatures .~ False) ''BuildOpts | ||
|
||
-- | Backend-specific options to use. | ||
backendOpts :: Lens' (BuildOpts b v x) (Options b v) | ||
|
||
-- | Source code snippets. Each should be a syntactically valid | ||
-- Haskell module. They will be combined intelligently, /i.e./ | ||
-- not just pasted together textually but combining pragmas, | ||
-- imports, /etc./ separately. | ||
snippets :: Lens' (BuildOpts b v x) [String] | ||
|
||
-- | Extra @LANGUAGE@ pragmas to use (@NoMonomorphismRestriction@ | ||
-- is automatically enabled.) | ||
pragmas :: Lens' (BuildOpts b v x) [String] | ||
|
||
-- | Additional module imports (note that "Diagrams.Prelude" is | ||
-- automatically imported). | ||
imports :: Lens' (BuildOpts b v x) [String] | ||
|
||
-- | A function to decide whether a particular diagram needs to | ||
-- be regenerated. It will be passed the final assembled | ||
-- source for the diagram (but with the module name set to | ||
-- @Main@ instead of something auto-generated, so that hashing | ||
-- the source will produce consistent results across runs). It | ||
-- can return some information (such as a hash of the source) | ||
-- via the @x@ result, which will be passed through to the | ||
-- result of 'buildDiagram'. More importantly, it decides | ||
-- whether the diagram should be built: a result of 'Just' | ||
-- means the diagram /should/ be built; 'Nothing' means it | ||
-- should not. In the case that it should be built, it returns | ||
-- a function for updating the rendering options. This could | ||
-- be used, /e.g./, to request a filename based on a hash of | ||
-- the source. | ||
-- | ||
-- Two standard decision functions are provided for | ||
-- convenience: 'alwaysRegenerate' returns no extra information | ||
-- and always decides to regenerate the diagram; | ||
-- 'hashedRegenerate' creates a hash of the diagram source and | ||
-- looks for a file with that name in a given directory. | ||
decideRegen :: Lens' (BuildOpts b v x) (String -> IO (x, Maybe (Options b v -> Options b v))) | ||
|
||
-- | The diagram expression to interpret. All the given import sand | ||
-- snippets will be in scope, with the given LANGUAGE pragmas | ||
-- enabled. The expression may have either of the types @Diagram b | ||
-- v@ or @IO (Diagram b v)@. | ||
diaExpr :: Lens' (BuildOpts b v x) String | ||
|
||
-- | A function to apply to the interpreted diagram prior to | ||
-- rendering. For example, you might wish to apply @pad 1.1 | ||
-- . centerXY@. This is preferred over directly modifying the | ||
-- string expression to be interpreted, since it gives better | ||
-- typechecking, and works no matter whether the expression | ||
-- represents a diagram or an IO action. | ||
postProcess :: Lens' (BuildOpts b v x) (Diagram b v -> Diagram b v) |