Copyright | Copyright (C) 2010-2019 John MacFarlane |
---|---|
License | BSD 3 |
Maintainer | John MacFarlane <jgm@berkeley.edu> |
Stability | alpha |
Portability | portable |
Safe Haskell | Safe-Inferred |
Language | Haskell2010 |
A prettyprinting library for the production of text documents, including wrapped text, indentation and other prefixes, and blocks for tables.
Synopsis
- render :: HasChars a => Maybe Int -> Doc a -> a
- renderPlain :: HasChars a => Maybe Int -> Doc a -> a
- renderANSI :: HasChars a => Maybe Int -> Doc a -> Text
- cr :: Doc a
- blankline :: Doc a
- blanklines :: Int -> Doc a
- space :: Doc a
- literal :: HasChars a => a -> Doc a
- text :: HasChars a => String -> Doc a
- char :: HasChars a => Char -> Doc a
- prefixed :: IsString a => String -> Doc a -> Doc a
- flush :: Doc a -> Doc a
- nest :: IsString a => Int -> Doc a -> Doc a
- hang :: IsString a => Int -> Doc a -> Doc a -> Doc a
- beforeNonBlank :: Doc a -> Doc a
- nowrap :: IsString a => Doc a -> Doc a
- afterBreak :: Text -> Doc a
- lblock :: HasChars a => Int -> Doc a -> Doc a
- cblock :: HasChars a => Int -> Doc a -> Doc a
- rblock :: HasChars a => Int -> Doc a -> Doc a
- vfill :: HasChars a => a -> Doc a
- nestle :: Doc a -> Doc a
- chomp :: Doc a -> Doc a
- inside :: Doc a -> Doc a -> Doc a -> Doc a
- braces :: HasChars a => Doc a -> Doc a
- brackets :: HasChars a => Doc a -> Doc a
- parens :: HasChars a => Doc a -> Doc a
- quotes :: HasChars a => Doc a -> Doc a
- doubleQuotes :: HasChars a => Doc a -> Doc a
- bold :: HasChars a => Doc a -> Doc a
- italic :: HasChars a => Doc a -> Doc a
- underlined :: HasChars a => Doc a -> Doc a
- strikeout :: HasChars a => Doc a -> Doc a
- fg :: HasChars a => Color -> Doc a -> Doc a
- bg :: HasChars a => Color -> Doc a -> Doc a
- type Color = Color8
- black :: Color
- red :: Color
- green :: Color
- yellow :: Color
- blue :: Color
- magenta :: Color
- cyan :: Color
- white :: Color
- link :: HasChars a => Text -> Doc a -> Doc a
- empty :: Doc a
- (<+>) :: Doc a -> Doc a -> Doc a
- ($$) :: Doc a -> Doc a -> Doc a
- ($+$) :: Doc a -> Doc a -> Doc a
- hcat :: [Doc a] -> Doc a
- hsep :: [Doc a] -> Doc a
- vcat :: [Doc a] -> Doc a
- vsep :: [Doc a] -> Doc a
- isEmpty :: Doc a -> Bool
- offset :: (IsString a, HasChars a) => Doc a -> Int
- minOffset :: HasChars a => Doc a -> Int
- updateColumn :: HasChars a => Doc a -> Int -> Int
- height :: HasChars a => Doc a -> Int
- charWidth :: Char -> Int
- realLength :: HasChars a => a -> Int
- realLengthNarrowContext :: HasChars a => a -> Int
- realLengthWideContext :: HasChars a => a -> Int
- realLengthNarrowContextNoShortcut :: HasChars a => a -> Int
- realLengthWideContextNoShortcut :: HasChars a => a -> Int
- isSkinToneModifier :: Char -> Bool
- isEmojiVariation :: Char -> Bool
- isZWJ :: Char -> Bool
- unfoldD :: Doc a -> [Doc a]
- data Doc a
- = Text Int a
- | Block Int [Attributed a]
- | VFill Int a
- | CookedText Int (Attributed a)
- | Prefixed Text (Doc a)
- | BeforeNonBlank (Doc a)
- | Flush (Doc a)
- | BreakingSpace
- | AfterBreak Text
- | CarriageReturn
- | NewLine
- | BlankLines Int
- | Concat (Doc a) (Doc a)
- | Styled StyleReq (Doc a)
- | Linked Text (Doc a)
- | Empty
- class (IsString a, Semigroup a, Monoid a, Show a) => HasChars a where
- data Attributed a
Rendering
renderPlain :: HasChars a => Maybe Int -> Doc a -> a Source #
Render a Doc
without using ANSI escapes. renderPlain (Just n)
will use
a line length of n
to reflow text on breakable spaces.
renderPlain Nothing
will not reflow text.
renderANSI :: HasChars a => Maybe Int -> Doc a -> Text Source #
Render a Doc
with ANSI escapes. renderANSI (Just n)
will use
a line length of n
to reflow text on breakable spaces.
renderANSI Nothing
will not reflow text.
Doc constructors
A carriage return. Does nothing if we're at the beginning of a line; otherwise inserts a newline.
Inserts a blank line unless one exists already.
(blankline <> blankline
has the same effect as blankline
.
blanklines :: Int -> Doc a Source #
Inserts blank lines unless they exist already.
(blanklines m <> blanklines n
has the same effect as blanklines (max m n)
.
text :: HasChars a => String -> Doc a Source #
A literal string. (Like literal
, but restricted to String.)
prefixed :: IsString a => String -> Doc a -> Doc a Source #
Uses the specified string as a prefix for every line of the inside document (except the first, if not at the beginning of the line).
nest :: IsString a => Int -> Doc a -> Doc a Source #
Indents a Doc
by the specified number of spaces.
hang :: IsString a => Int -> Doc a -> Doc a -> Doc a Source #
A hanging indent. hang ind start doc
prints start
,
then doc
, leaving an indent of ind
spaces on every
line but the first.
beforeNonBlank :: Doc a -> Doc a Source #
beforeNonBlank d
conditionally includes d
unless it is
followed by blank space.
afterBreak :: Text -> Doc a Source #
Content to print only if it comes at the beginning of a line,
to be used e.g. for escaping line-initial .
in roff man.
lblock :: HasChars a => Int -> Doc a -> Doc a Source #
lblock n d
is a block of width n
characters, with
text derived from d
and aligned to the left.
vfill :: HasChars a => a -> Doc a Source #
An expandable border that, when placed next to a box, expands to the height of the box. Strings cycle through the list provided.
Functions for concatenating documents
(<+>) :: Doc a -> Doc a -> Doc a infixr 6 Source #
Concatenate a list of Doc
s, putting breakable spaces
between them.
($+$) :: Doc a -> Doc a -> Doc a infixr 5 Source #
a $+$ b
puts a
above b
, with a blank line between.
Functions for querying documents
minOffset :: HasChars a => Doc a -> Int Source #
Returns the minimal width of a Doc
when reflowed at breakable spaces.
updateColumn :: HasChars a => Doc a -> Int -> Int Source #
Returns the column that would be occupied by the last laid out character (assuming no wrapping).
charWidth :: Char -> Int Source #
Returns width of a character in a monospace font: 0 for a combining character, 1 for a regular character, 2 for an East Asian wide character. Ambiguous characters are treated as width 1.
realLength :: HasChars a => a -> Int Source #
Get real length of string, taking into account combining and double-wide characters. Ambiguous characters are treated as width 1.
realLengthNarrowContext :: HasChars a => a -> Int Source #
Get the real length of a string, taking into account combining and double-wide characters. Ambiguous characters are treated as width 1.
realLengthWideContext :: HasChars a => a -> Int Source #
Get the real length of a string, taking into account combining and double-wide characters. Ambiguous characters are treated as width 2.
realLengthNarrowContextNoShortcut :: HasChars a => a -> Int Source #
Like realLengthNarrowContext
, but avoids optimizations (shortcuts).
This is exposed for testing, to ensure that the optimizations are safe.
realLengthWideContextNoShortcut :: HasChars a => a -> Int Source #
Like realLengthWideContext
, but avoids optimizations (shortcuts).
This is exposed for testing, to ensure that the optimizations are safe.
Char properties
isSkinToneModifier :: Char -> Bool Source #
Checks whether a character is a skin tone modifier.
isEmojiVariation :: Char -> Bool Source #
Checks whether a character is an emoji variation modifier.
Utility functions
unfoldD :: Doc a -> [Doc a] Source #
Deprecated: unfoldD will be removed from the API.
Unfold a Doc
into a flat list.
Types
Document, including structure relevant for layout.
Text Int a | Text with specified width. |
Block Int [Attributed a] | A block with a width and lines. |
VFill Int a | A vertically expandable block; when concatenated with a block, expands to height of block, with each line containing the specified text. |
CookedText Int (Attributed a) | Text which doesn't need further cooking |
Prefixed Text (Doc a) | Doc with each line prefixed with text. Note that trailing blanks are omitted from the prefix when the line after it is empty. |
BeforeNonBlank (Doc a) | Doc that renders only before nonblank. |
Flush (Doc a) | Doc laid out flush to left margin. |
BreakingSpace | A space or line break, in context. |
AfterBreak Text | Text printed only at start of line. |
CarriageReturn | Newline unless we're at start of line. |
NewLine | newline. |
BlankLines Int | Ensure a number of blank lines. |
Concat (Doc a) (Doc a) | Two documents concatenated. |
Styled StyleReq (Doc a) | |
Linked Text (Doc a) | A hyperlink |
Empty |
Instances
Foldable Doc Source # | |
Defined in Text.DocLayout fold :: Monoid m => Doc m -> m # foldMap :: Monoid m => (a -> m) -> Doc a -> m # foldMap' :: Monoid m => (a -> m) -> Doc a -> m # foldr :: (a -> b -> b) -> b -> Doc a -> b # foldr' :: (a -> b -> b) -> b -> Doc a -> b # foldl :: (b -> a -> b) -> b -> Doc a -> b # foldl' :: (b -> a -> b) -> b -> Doc a -> b # foldr1 :: (a -> a -> a) -> Doc a -> a # foldl1 :: (a -> a -> a) -> Doc a -> a # elem :: Eq a => a -> Doc a -> Bool # maximum :: Ord a => Doc a -> a # | |
Traversable Doc Source # | |
Functor Doc Source # | |
Data a => Data (Doc a) Source # | |
Defined in Text.DocLayout gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> Doc a -> c (Doc a) # gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c (Doc a) # dataTypeOf :: Doc a -> DataType # dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c (Doc a)) # dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (Doc a)) # gmapT :: (forall b. Data b => b -> b) -> Doc a -> Doc a # gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Doc a -> r # gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Doc a -> r # gmapQ :: (forall d. Data d => d -> u) -> Doc a -> [u] # gmapQi :: Int -> (forall d. Data d => d -> u) -> Doc a -> u # gmapM :: Monad m => (forall d. Data d => d -> m d) -> Doc a -> m (Doc a) # gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> Doc a -> m (Doc a) # gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> Doc a -> m (Doc a) # | |
HasChars a => IsString (Doc a) Source # | |
Defined in Text.DocLayout fromString :: String -> Doc a # | |
Monoid (Doc a) Source # | |
Semigroup (Doc a) Source # | |
Generic (Doc a) Source # | |
Read a => Read (Doc a) Source # | |
Show a => Show (Doc a) Source # | |
Eq a => Eq (Doc a) Source # | |
Ord a => Ord (Doc a) Source # | |
type Rep (Doc a) Source # | |
Defined in Text.DocLayout |
class (IsString a, Semigroup a, Monoid a, Show a) => HasChars a where Source #
Class abstracting over various string types that
can fold over characters. Minimal definition is foldrChar
and foldlChar
, but defining the other methods can give better
performance.
foldrChar :: (Char -> b -> b) -> b -> a -> b Source #
foldlChar :: (b -> Char -> b) -> b -> a -> b Source #
replicateChar :: Int -> Char -> a Source #
splitLines :: a -> [a] Source #
Instances
HasChars Text Source # | |
HasChars Text Source # | |
HasChars String Source # | |
Defined in Text.DocLayout.HasChars | |
HasChars a => HasChars (Attributed a) Source # | |
Defined in Text.DocLayout.HasChars foldrChar :: (Char -> b -> b) -> b -> Attributed a -> b Source # foldlChar :: (b -> Char -> b) -> b -> Attributed a -> b Source # replicateChar :: Int -> Char -> Attributed a Source # isNull :: Attributed a -> Bool Source # splitLines :: Attributed a -> [Attributed a] Source # build :: Attributed a -> Builder Source # |
data Attributed a Source #
A sequence of strings with font attributes.