doclayout-0.3: A prettyprinting library for laying out text documents.

Text.DocLayout

Description

A prettyprinting library for the production of text documents, including wrapped text, indentation and other prefixes, and blocks for tables.

Synopsis

# Rendering

render :: HasChars a => Maybe Int -> Doc a -> a Source #

Render a Doc. render (Just n) will use a line length of n to reflow text on breakable spaces. render Nothing will not reflow text.

# Doc constructors

cr :: Doc a Source #

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.

Inserts blank lines unless they exist already. (blanklines m <> blanklines n has the same effect as blanklines (max m n).

A breaking (reflowable) space.

literal :: HasChars a => a -> Doc a Source #

Create a Doc from a stringlike value.

text :: HasChars a => String -> Doc a Source #

A literal string. (Like literal, but restricted to String.)

char :: HasChars a => Char -> Doc a Source #

A character.

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).

flush :: Doc a -> Doc a Source #

Makes a Doc flush against the left margin.

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.

nowrap :: IsString a => Doc a -> Doc a Source #

Makes a Doc non-reflowable.

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.

cblock :: HasChars a => Int -> Doc a -> Doc a Source #

Like lblock but aligned centered.

rblock :: HasChars a => Int -> Doc a -> Doc a Source #

Like lblock but aligned to the right.

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.

nestle :: Doc a -> Doc a Source #

Removes leading blank lines from a Doc.

chomp :: Doc a -> Doc a Source #

Chomps trailing blank space off of a Doc.

inside :: Doc a -> Doc a -> Doc a -> Doc a Source #

Encloses a Doc inside a start and end Doc.

braces :: HasChars a => Doc a -> Doc a Source #

Puts a Doc in curly braces.

brackets :: HasChars a => Doc a -> Doc a Source #

Puts a Doc in square brackets.

parens :: HasChars a => Doc a -> Doc a Source #

Puts a Doc in parentheses.

quotes :: HasChars a => Doc a -> Doc a Source #

Wraps a Doc in single quotes.

doubleQuotes :: HasChars a => Doc a -> Doc a Source #

Wraps a Doc in double quotes.

The empty document.

# Functions for concatenating documents

(<+>) :: Doc a -> Doc a -> Doc a infixr 6 Source #

Concatenate a list of Docs, putting breakable spaces between them.

($$) :: Doc a -> Doc a -> Doc a infixr 5 Source # a$$ b puts a above b.

($+$) :: Doc a -> Doc a -> Doc a infixr 5 Source #

a $+$ b puts a above b, with a blank line between.

hcat :: [Doc a] -> Doc a Source #

Concatenate documents horizontally.

hsep :: [Doc a] -> Doc a Source #

Same as hcat, but putting breakable spaces between the Docs.

vcat :: [Doc a] -> Doc a Source #

List version of .

vsep :: [Doc a] -> Doc a Source #

List version of $+$.

# Functions for querying documents

isEmpty :: Doc a -> Bool Source #

True if the document is empty.

offset :: (IsString a, HasChars a) => Doc a -> Int Source #

Returns the width of a Doc.

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).

height :: HasChars a => Doc a -> Int Source #

Returns the height of a block or other Doc.

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.

realLength :: HasChars a => a -> Int Source #

Get real length of string, taking into account combining and double-wide characters.

# Types

data Doc a Source #

Document, including structure relevant for layout.

Constructors

 Text Int a Text with specified width. Block Int [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. 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. Empty
Instances

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.

Minimal complete definition

Methods

foldrChar :: (Char -> b -> b) -> b -> a -> b Source #

foldlChar :: (b -> Char -> b) -> b -> a -> b Source #

replicateChar :: Int -> Char -> a Source #

isNull :: a -> Bool Source #

splitLines :: a -> [a] Source #

Instances
 Source # Instance detailsDefined in Text.DocLayout MethodsfoldrChar :: (Char -> b -> b) -> b -> String -> b Source #foldlChar :: (b -> Char -> b) -> b -> String -> b Source # Source # Instance detailsDefined in Text.DocLayout MethodsfoldrChar :: (Char -> b -> b) -> b -> Text -> b Source #foldlChar :: (b -> Char -> b) -> b -> Text -> b Source #splitLines :: Text -> [Text] Source # Source # Instance detailsDefined in Text.DocLayout MethodsfoldrChar :: (Char -> b -> b) -> b -> Text -> b Source #foldlChar :: (b -> Char -> b) -> b -> Text -> b Source #splitLines :: Text -> [Text] Source #