|
Packit |
dda32d |
{-# LANGUAGE OverloadedStrings #-}
|
|
Packit |
dda32d |
{-
|
|
Packit |
dda32d |
Copyright (C) 2006-2017 John MacFarlane <jgm@berkeley.edu>
|
|
Packit |
dda32d |
|
|
Packit |
dda32d |
This program is free software; you can redistribute it and/or modify
|
|
Packit |
dda32d |
it under the terms of the GNU General Public License as published by
|
|
Packit |
dda32d |
the Free Software Foundation; either version 2 of the License, or
|
|
Packit |
dda32d |
(at your option) any later version.
|
|
Packit |
dda32d |
|
|
Packit |
dda32d |
This program is distributed in the hope that it will be useful,
|
|
Packit |
dda32d |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
dda32d |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
Packit |
dda32d |
GNU General Public License for more details.
|
|
Packit |
dda32d |
|
|
Packit |
dda32d |
You should have received a copy of the GNU General Public License
|
|
Packit |
dda32d |
along with this program; if not, write to the Free Software
|
|
Packit |
dda32d |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
Packit |
dda32d |
-}
|
|
Packit |
dda32d |
|
|
Packit |
dda32d |
{- |
|
|
Packit |
dda32d |
Module : Text.Pandoc.Writers.Native
|
|
Packit |
dda32d |
Copyright : Copyright (C) 2006-2017 John MacFarlane
|
|
Packit |
dda32d |
License : GNU GPL, version 2 or above
|
|
Packit |
dda32d |
|
|
Packit |
dda32d |
Maintainer : John MacFarlane <jgm@berkeley.edu>
|
|
Packit |
dda32d |
Stability : alpha
|
|
Packit |
dda32d |
Portability : portable
|
|
Packit |
dda32d |
|
|
Packit |
dda32d |
Conversion of a 'Pandoc' document to a string representation.
|
|
Packit |
dda32d |
-}
|
|
Packit |
dda32d |
module Text.Pandoc.Writers.Native ( writeNative )
|
|
Packit |
dda32d |
where
|
|
Packit |
dda32d |
import Data.List (intersperse)
|
|
Packit |
dda32d |
import Data.Text (Text)
|
|
Packit |
dda32d |
import Text.Pandoc.Class (PandocMonad)
|
|
Packit |
dda32d |
import Text.Pandoc.Definition
|
|
Packit |
dda32d |
import Text.Pandoc.Options (WrapOption (..), WriterOptions (..))
|
|
Packit |
dda32d |
import Text.Pandoc.Pretty
|
|
Packit |
dda32d |
|
|
Packit |
dda32d |
prettyList :: [Doc] -> Doc
|
|
Packit |
dda32d |
prettyList ds =
|
|
Packit |
dda32d |
"[" <>
|
|
Packit |
dda32d |
cat (intersperse (cr <> ",") $ map (nest 1) ds) <> "]"
|
|
Packit |
dda32d |
|
|
Packit |
dda32d |
-- | Prettyprint Pandoc block element.
|
|
Packit |
dda32d |
prettyBlock :: Block -> Doc
|
|
Packit |
dda32d |
prettyBlock (LineBlock lines') =
|
|
Packit |
dda32d |
"LineBlock" $$ prettyList (map (text . show) lines')
|
|
Packit |
dda32d |
prettyBlock (BlockQuote blocks) =
|
|
Packit |
dda32d |
"BlockQuote" $$ prettyList (map prettyBlock blocks)
|
|
Packit |
dda32d |
prettyBlock (OrderedList attribs blockLists) =
|
|
Packit |
dda32d |
"OrderedList" <> space <> text (show attribs) $$
|
|
Packit |
dda32d |
prettyList (map (prettyList . map prettyBlock) blockLists)
|
|
Packit |
dda32d |
prettyBlock (BulletList blockLists) =
|
|
Packit |
dda32d |
"BulletList" $$
|
|
Packit |
dda32d |
prettyList (map (prettyList . map prettyBlock) blockLists)
|
|
Packit |
dda32d |
prettyBlock (DefinitionList items) = "DefinitionList" $$
|
|
Packit |
dda32d |
prettyList (map deflistitem items)
|
|
Packit |
dda32d |
where deflistitem (term, defs) = "(" <> text (show term) <> "," <> cr <>
|
|
Packit |
dda32d |
nest 1 (prettyList $ map (prettyList . map prettyBlock) defs) <> ")"
|
|
Packit |
dda32d |
prettyBlock (Table caption aligns widths header rows) =
|
|
Packit |
dda32d |
"Table " <> text (show caption) <> " " <> text (show aligns) <> " " <>
|
|
Packit |
dda32d |
text (show widths) $$
|
|
Packit |
dda32d |
prettyRow header $$
|
|
Packit |
dda32d |
prettyList (map prettyRow rows)
|
|
Packit |
dda32d |
where prettyRow cols = prettyList (map (prettyList . map prettyBlock) cols)
|
|
Packit |
dda32d |
prettyBlock (Div attr blocks) =
|
|
Packit |
dda32d |
text ("Div " <> show attr) $$ prettyList (map prettyBlock blocks)
|
|
Packit |
dda32d |
prettyBlock block = text $ show block
|
|
Packit |
dda32d |
|
|
Packit |
dda32d |
-- | Prettyprint Pandoc document.
|
|
Packit |
dda32d |
writeNative :: PandocMonad m => WriterOptions -> Pandoc -> m Text
|
|
Packit |
dda32d |
writeNative opts (Pandoc meta blocks) = return $
|
|
Packit |
dda32d |
let colwidth = if writerWrapText opts == WrapAuto
|
|
Packit |
dda32d |
then Just $ writerColumns opts
|
|
Packit |
dda32d |
else Nothing
|
|
Packit |
dda32d |
withHead = case writerTemplate opts of
|
|
Packit |
dda32d |
Just _ -> \bs -> text ("Pandoc (" ++ show meta ++ ")") $$
|
|
Packit |
dda32d |
bs $$ cr
|
|
Packit |
dda32d |
Nothing -> id
|
|
Packit |
dda32d |
in render colwidth $ withHead $ prettyList $ map prettyBlock blocks
|