{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE OverloadedStrings #-}
-{-# LANGUAGE ViewPatterns #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
module Language.TCT.Write.Plain where
import Data.Function (($))
import Data.Functor ((<$>))
import Data.Int (Int)
+import Data.List.NonEmpty (NonEmpty(..))
import Data.Maybe (Maybe(..))
import Data.Monoid (Monoid(..))
import Data.Ord (Ord(..), Ordering(..))
import Data.Semigroup (Semigroup(..))
import Data.Sequence (ViewL(..))
import Data.String (String, IsString(..))
-import Data.Tuple (fst)
import Prelude (Num(..), error)
import Text.Show (Show(..))
import qualified Control.Monad.Trans.State as S
import Language.TCT.Utils
-- import Language.TCT.Debug
+writePlain :: Roots -> TL.Text
+writePlain doc = text (setStart doc def) doc
+
-- * Type 'Plain'
type Plain = S.State State TLB.Builder
-- NOTE: To get maximum performance when building lazy Text values using a builder,
mappend = (<>)
runPlain :: Plain -> State -> TL.Text
-runPlain p s = TLB.toLazyText $ fst $ S.runState p s
+runPlain p s = TLB.toLazyText $ S.evalState p s
text :: Plainify a => State -> a -> TL.Text
text s a = runPlain (plainify a) s
-plainDocument :: Roots -> TL.Text
-plainDocument doc = text (setStart doc def) doc
-
-- ** Type 'State'
data State
= State
where pos =
case Seq.viewl ts of
EmptyL -> pos1
- Tree Cell{cell_begin} _ :< _ -> cell_begin
+ Tree (Cell (Span{span_begin}:|_) _) _ :< _ -> span_begin
-- * Class 'Plainify'
class Plainify a where
instance Plainify Roots where
plainify = foldMap plainify
instance Plainify Root where
- plainify (Tree (Cell bp _ep nod) ts) =
+ plainify (Tree (Cell (Span{span_begin=bp}:|_) nod) ts) =
plainify bp <>
case nod of
- ----------------------
- NodeGroup -> plainify ts
----------------------
NodeLower n as -> "<" <> plainify n <> plainify as <> plainify ts
----------------------
NodeHeader hdr ->
case hdr of
- HeaderGreat{} -> plainHeaderRepeated
- HeaderBar{} -> plainHeaderRepeated
+ HeaderGreat{} -> plainHeaderRepeated
+ HeaderBar{} -> plainHeaderRepeated
+ HeaderDotSlash{} -> plainify hdr
_ -> plainify hdr <> plainify ts
where
plainHeaderRepeated = do
HeaderDash -> "- "
HeaderDashDash -> "-- "
HeaderSection lvl -> plainify (List.replicate lvl '#')
- HeaderDotSlash n -> "./" <> plainify n
+ HeaderDotSlash n -> plainify n
instance Plainify Token where
plainify = \case
TokenText t -> plainify t