1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
|
{-# LANGUAGE TypeSynonymInstances, FlexibleInstances, MultiParamTypeClasses,
DeriveDataTypeable, GeneralizedNewtypeDeriving, CPP #-}
{-
Copyright (C) 2010 John MacFarlane <jgm@berkeley.edu>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
-}
{- |
Module : Text.Pandoc.Builder
Copyright : Copyright (C) 2010 John MacFarlane
License : GNU GPL, version 2 or above
Maintainer : John MacFarlane <jgm@berkeley.edu>
Stability : alpha
Portability : portable
Convenience functions for building pandoc documents programmatically.
Example of use:
> {-# LANGUAGE OverloadedStrings #-}
> import Text.Pandoc.Builder
>
> myDoc :: Pandoc
> myDoc = setTitle "My title" $ doc $
> para "This is the first paragraph" <>
> para ("And " <> emph "another" <> ".") <>
> bulletList [ para "item one" <> para "continuation"
> , plain ("item two and a " <>
> link "/url" "go to url" "link")
> ]
Isn't that nicer than writing the following?
> import Text.Pandoc.Definition
>
> myDoc :: Pandoc
> myDoc = Pandoc (Meta {docTitle = [Str "My",Space,Str "title"]
> , docAuthors = []
> , docDate = []})
> [Para [Str "This",Space,Str "is",Space,Str "the",Space,Str "first",
> Space,Str "paragraph"]
> ,Para [Str "And",Space,Emph [Str "another"],Str "."]
> ,BulletList [[Para [Str "item",Space,Str "one"]
> ,Para [Str "continuation"]]
> ,[Plain [Str "item",Space,Str "two",Space,Str "and", Space,
> Str "a",Space,Link [Str "link"] ("/url","go to url")]]]]
And of course, you can use Haskell to define your own builders:
> import Text.Pandoc.Builder
> import Text.JSON
> import Control.Arrow ((***))
> import Data.Monoid (mempty)
>
> -- | Converts a JSON document into 'Blocks'.
> json :: String -> Blocks
> json x =
> case decode x of
> Ok y -> jsValueToBlocks y
> Error y -> error y
> where jsValueToBlocks x =
> case x of
> JSNull -> mempty
> JSBool x -> plain $ text $ show x
> JSRational _ x -> plain $ text $ show x
> JSString x -> plain $ text $ fromJSString x
> JSArray xs -> bulletList $ map jsValueToBlocks xs
> JSObject x -> definitionList $
> map (text *** (:[]) . jsValueToBlocks) $
> fromJSObject x
-}
module Text.Pandoc.Builder ( module Text.Pandoc.Definition
, Inlines(..)
, Blocks(..)
, (<>)
, Listable(..)
-- * Document builders
, doc
, setTitle
, setAuthors
, setDate
-- * Inline list builders
, text
, str
, emph
, strong
, strikeout
, superscript
, subscript
, smallcaps
, singleQuoted
, doubleQuoted
, cite
, codeWith
, code
, space
, linebreak
, math
, displayMath
, rawInline
, link
, image
, note
, trimInlines
-- * Block list builders
, para
, plain
, codeBlockWith
, codeBlock
, rawBlock
, blockQuote
, bulletList
, orderedListWith
, orderedList
, definitionList
, header
, horizontalRule
, table
, simpleTable
)
where
import Text.Pandoc.Definition
import Data.String
import Data.Monoid
import Data.Maybe (fromMaybe)
import Data.Sequence (Seq, (|>), viewr, viewl, ViewR(..), ViewL(..))
import qualified Data.Sequence as Seq
import Data.Foldable (Foldable)
import qualified Data.Foldable as F
import Data.List (groupBy, intersperse)
import Data.Data
import Data.Typeable
import Control.Arrow ((***))
#if MIN_VERSION_base(4,5,0)
-- (<>) is defined in Data.Monoid
#else
infixr 6 <>
-- | An infix synonym for 'mappend'.
(<>) :: Monoid m => m -> m -> m
(<>) = mappend
{-# INLINE (<>) #-}
#endif
newtype Inlines = Inlines { unInlines :: Seq Inline }
deriving (Data, Ord, Eq, Typeable)
-- We show an Inlines just like [Inline].
instance Show Inlines where
show = show . F.toList . unInlines
instance Read Inlines where
readsPrec n = map (\(x,y) -> (Inlines . Seq.fromList $ x, y)) . readsPrec n
instance Monoid Inlines where
mempty = Inlines mempty
(Inlines xs) `mappend` (Inlines ys) =
case (viewr xs, viewl ys) of
(EmptyR, _) -> Inlines ys
(_, EmptyL) -> Inlines xs
(xs' :> x, y :< ys') -> Inlines (meld `mappend` ys')
where meld = case (x, y) of
(Space, Space) -> xs' |> Space
(Str t1, Str t2) -> xs' |> Str (t1 <> t2)
(Emph i1, Emph i2) -> xs' |> Emph (i1 <> i2)
(Strong i1, Strong i2) -> xs' |> Strong (i1 <> i2)
(Subscript i1, Subscript i2) -> xs' |> Subscript (i1 <> i2)
(Superscript i1, Superscript i2) -> xs' |> Superscript (i1 <> i2)
(Strikeout i1, Strikeout i2) -> xs' |> Strikeout (i1 <> i2)
(Space, LineBreak) -> xs' |> LineBreak
_ -> xs' |> x |> y
instance IsString Inlines where
fromString = text
newtype Blocks = Blocks { unBlocks :: Seq Block }
deriving (Data, Ord, Eq, Typeable, Monoid)
-- We show a Blocks just like [Block].
instance Show Blocks where
show = show . F.toList . unBlocks
instance Read Blocks where
readsPrec n = map (\(x,y) -> (Blocks . Seq.fromList $ x, y)) . readsPrec n
class Listable a b where
toList :: a -> [b]
fromList :: [b] -> a
foldMap :: (b -> a) -> a -> a
singleton :: b -> a
foldlM :: Monad m => (a -> b -> m a) -> a -> a -> m a
isNull :: a -> Bool
instance Listable Inlines Inline where
toList = F.toList . unInlines
fromList = Inlines . Seq.fromList
foldMap f = F.foldMap f . unInlines
singleton = Inlines . Seq.singleton
foldlM f x = F.foldlM f x . unInlines
isNull = Seq.null . unInlines
instance Listable Blocks Block where
toList = F.toList . unBlocks
fromList = Blocks . Seq.fromList
foldMap f = F.foldMap f . unBlocks
singleton = Blocks . Seq.singleton
foldlM f x = F.foldlM f x . unBlocks
isNull = Seq.null . unBlocks
-- | Trim leading and trailing Sp (spaces) from an Inlines.
trimInlines :: Inlines -> Inlines
#if MIN_VERSION_containers(0,4,0)
trimInlines (Inlines ils) = Inlines $ Seq.dropWhileL (== Space) $
Seq.dropWhileR (== Space) $ ils
#else
-- for GHC 6.12, we need to workaround a bug in dropWhileR
-- see http://hackage.haskell.org/trac/ghc/ticket/4157
trimInlines (Inlines ils) = Inlines $ Seq.dropWhileL (== Space) $
Seq.reverse $ Seq.dropWhileL (== Space) $
Seq.reverse ils
#endif
-- Document builders
doc :: Blocks -> Pandoc
doc = Pandoc (Meta [] [] []) . toList
setTitle :: Inlines -> Pandoc -> Pandoc
setTitle t (Pandoc m bs) = Pandoc m{ docTitle = toList t } bs
setAuthors :: [Inlines] -> Pandoc -> Pandoc
setAuthors as (Pandoc m bs) = Pandoc m{ docAuthors = map toList as } bs
setDate :: Inlines -> Pandoc -> Pandoc
setDate d (Pandoc m bs) = Pandoc m{ docDate = toList d } bs
-- Inline list builders
-- | Convert a 'String' to 'Inlines', treating interword spaces as 'Space's.
-- If you want a 'Str' with literal spaces, use 'str'.
text :: String -> Inlines
text = fromList . map conv . breakBySpaces
where breakBySpaces = groupBy sameCategory
sameCategory x y = (is_space x && is_space y) ||
(not $ is_space x || is_space y)
conv xs | all is_space xs = Space
conv xs = Str xs
is_space ' ' = True
is_space '\n' = True
is_space '\t' = True
is_space _ = False
str :: String -> Inlines
str = singleton . Str
emph :: Inlines -> Inlines
emph = singleton . Emph . toList
strong :: Inlines -> Inlines
strong = singleton . Strong . toList
strikeout :: Inlines -> Inlines
strikeout = singleton . Strikeout . toList
superscript :: Inlines -> Inlines
superscript = singleton . Superscript . toList
subscript :: Inlines -> Inlines
subscript = singleton . Subscript . toList
smallcaps :: Inlines -> Inlines
smallcaps = singleton . SmallCaps . toList
singleQuoted :: Inlines -> Inlines
singleQuoted = quoted SingleQuote
doubleQuoted :: Inlines -> Inlines
doubleQuoted = quoted DoubleQuote
quoted :: QuoteType -> Inlines -> Inlines
quoted qt = singleton . Quoted qt . toList
cite :: [Citation] -> Inlines -> Inlines
cite cts = singleton . Cite cts . toList
-- | Inline code with attributes.
codeWith :: Attr -> String -> Inlines
codeWith attrs = singleton . Code attrs
-- | Plain inline code.
code :: String -> Inlines
code = codeWith nullAttr
space :: Inlines
space = singleton Space
linebreak :: Inlines
linebreak = singleton LineBreak
-- | Inline math
math :: String -> Inlines
math = singleton . Math InlineMath
-- | Display math
displayMath :: String -> Inlines
displayMath = singleton . Math DisplayMath
rawInline :: Format -> String -> Inlines
rawInline format = singleton . RawInline format
link :: String -- ^ URL
-> String -- ^ Title
-> Inlines -- ^ Label
-> Inlines
link url title x = singleton $ Link (toList x) (url, title)
image :: String -- ^ URL
-> String -- ^ Title
-> Inlines -- ^ Alt text
-> Inlines
image url title x = singleton $ Image (toList x) (url, title)
note :: Blocks -> Inlines
note = singleton . Note . toList
-- Block list builders
para :: Inlines -> Blocks
para = singleton . Para . toList
plain :: Inlines -> Blocks
plain = singleton . Plain . toList
-- | A code block with attributes.
codeBlockWith :: Attr -> String -> Blocks
codeBlockWith attrs = singleton . CodeBlock attrs
-- | A plain code block.
codeBlock :: String -> Blocks
codeBlock = codeBlockWith nullAttr
rawBlock :: Format -> String -> Blocks
rawBlock format = singleton . RawBlock format
blockQuote :: Blocks -> Blocks
blockQuote = singleton . BlockQuote . toList
-- | Ordered list with attributes.
orderedListWith :: ListAttributes -> [Blocks] -> Blocks
orderedListWith attrs = singleton . OrderedList attrs . map toList
-- | Ordered list with default attributes.
orderedList :: [Blocks] -> Blocks
orderedList = orderedListWith (1, DefaultStyle, DefaultDelim)
bulletList :: [Blocks] -> Blocks
bulletList = singleton . BulletList . map toList
definitionList :: [(Inlines, [Blocks])] -> Blocks
definitionList = singleton . DefinitionList . map (toList *** map toList)
header :: Int -- ^ Level
-> Inlines
-> Blocks
header level = singleton . Header level . toList
horizontalRule :: Blocks
horizontalRule = singleton HorizontalRule
table :: Inlines -- ^ Caption
-> [(Alignment, Double)] -- ^ Column alignments and fractional widths
-> [Blocks] -- ^ Headers
-> [[Blocks]] -- ^ Rows
-> Blocks
table caption cellspecs headers rows = singleton $
Table (toList caption) aligns widths
(map toList headers) (map (map toList) rows)
where (aligns, widths) = unzip cellspecs
-- | A simple table without a caption.
simpleTable :: [Blocks] -- ^ Headers
-> [[Blocks]] -- ^ Rows
-> Blocks
simpleTable headers = table mempty (mapConst defaults headers) headers
where defaults = (AlignDefault, 0)
mapConst :: Functor f => b -> f a -> f b
mapConst = fmap . const
|