import Data.Maybe (Maybe(..))
import Data.Semigroup (Semigroup(..))
import Data.Text (Text)
-import Data.Traversable (sequence)
+import Data.Traversable (Traversable(..))
import Data.Tuple (fst, snd)
import Prelude (fromIntegral)
import System.IO (FilePath)
import Debug.Trace (trace)
-readTCT ::
+readTreeCell ::
FilePath -> Text ->
Either (P.ParseError (P.Token Text) P.Dec)
(Trees (Cell Key) (Cell Tokens))
-readTCT inp txt = do
+readTreeCell inp txt = do
tct <- P.runParser (p_Trees <* P.eof) inp txt
- sequence $ (<$> tct) {-(<$> trace (show $ PrettyTree tct) tct)-} $ \tr ->
+ (`traverse` tct) {- $ (<$> trace (show $ PrettyTree tct) tct)-} $ \tr ->
sequence $ (`mapTreeWithKey`tr) $ \key (Cell pos posEnd t) ->
case key of
-- Verbatim Keys
- Just (unCell -> KeyBar{}) ->
- Right $ Cell pos posEnd $ tokens [TokenPlain t]
- Just (unCell -> KeyLower{}) ->
- Right $ Cell pos posEnd $ tokens [TokenPlain t]
- Just (unCell -> KeyEqual{}) ->
- Right $ Cell pos posEnd $ tokens [TokenPlain t]
+ Just (unCell -> KeyBar{}) -> Right $ Cell pos posEnd $ tokens [TokenPlain t]
+ Just (unCell -> KeyLower{}) -> Right $ Cell pos posEnd $ tokens [TokenPlain t]
+ Just (unCell -> KeyEqual{}) -> Right $ Cell pos posEnd $ tokens [TokenPlain t]
-- Token Keys
_ ->
Cell pos posEnd <$>
p_Tokens <* P.eof
) inp (StreamCell t)
+readTree ::
+ FilePath -> Text ->
+ Either (P.ParseError (P.Token Text) P.Dec)
+ (Trees Key Tokens)
+readTree inp txt = do
+ tct <- P.runParser (p_Trees <* P.eof) inp txt
+ (`traverse` tct) $ \tr ->
+ sequence $ (\f -> mapTreeKey unCell f tr) $ \key (Cell pos _posEnd t) ->
+ case unCell <$> key of
+ -- Verbatim Keys
+ Just KeyBar{} -> Right $ tokens [TokenPlain t]
+ Just KeyLower{} -> Right $ tokens [TokenPlain t]
+ Just KeyEqual{} -> Right $ tokens [TokenPlain t]
+ -- Token Keys
+ _ ->
+ P.runParser (do
+ P.setTabWidth $ P.unsafePos $ fromIntegral $ columnPos pos
+ P.setPosition $ P.SourcePos inp
+ (P.unsafePos $ fromIntegral $ linePos pos)
+ (P.unsafePos $ fromIntegral $ columnPos pos)
+ p_Tokens <* P.eof
+ ) inp (StreamCell t)
+
-- * Type 'StreamCell'
-- | Wrap 'Text' to have a 'P.Stream' instance
-- whose 'P.updatePos' method abuses the tab width state
dbg :: Show a => String -> a -> a
dbg m x = trac (m <> ": " <> show x) x
-dtc :: Trees (Cell Key) (Cell Tokens) -> DTC
+dtc :: Trees Key Tokens -> DTC
dtc ts = do
let lang = "fr"
D.xmlModel "./schema/dtc.rnc"
D.atomStylesheet $ "./xsl/document.atom."<>lang<>".xsl"
D.document $
case Seq.viewl ts of
- TreeN (unCell -> KeySection{})
- (Seq.viewl -> Tree0 (unCell -> Write.t_Tokens -> TL.toStrict -> title) :< head)
+ TreeN KeySection{}
+ (Seq.viewl -> Tree0 (Write.t_Tokens -> TL.toStrict -> title) :< head)
:< body -> do
d_Trees [] (mangleHead title head)
d_Trees [] body
where
mangleHead ::
Text ->
- Trees (Cell Key) (Cell Tokens) ->
- Trees (Cell Key) (Cell Tokens)
+ Trees Key Tokens ->
+ Trees Key Tokens
mangleHead title head =
let mi =
(`Seq.findIndexL` head) $ \case
- TreeN (unCell -> KeyColon "about" _) _ -> True
+ TreeN (KeyColon "about" _) _ -> True
_ -> False in
case mi of
Nothing ->
- TreeN (cell0 (KeyColon "about" ""))
+ TreeN (KeyColon "about" "")
(Seq.fromList names)
<| head
Just i -> Seq.adjust f i head
where
names = name <$> Text.splitOn "\n" title
name =
- TreeN (cell0 (KeyColon "name" "")) .
- Seq.singleton .
- Tree0 . cell0 .
- Tokens . Seq.singleton . TokenPlain
+ TreeN (KeyColon "name" "") .
+ Seq.singleton . Tree0 . Tokens .
+ Seq.singleton . TokenPlain
-d_Trees :: [Key] -> Trees (Cell Key) (Cell Tokens) -> DTC
+d_Trees :: [Key] -> Trees Key Tokens -> DTC
d_Trees path ts =
case () of
_ | (ul,ts') <- gatherUL ts, not (null ul) -> do
_ ->
return ()
-gatherUL ::
- Trees (Cell Key) (Cell Tokens) ->
- ( Trees (Cell Key) (Cell Tokens)
- , Trees (Cell Key) (Cell Tokens) )
+gatherUL :: Trees Key Tokens -> (Trees Key Tokens, Trees Key Tokens)
gatherUL ts =
let (lis, ts') = spanLIs ts in
foldl' accumLIs (mempty,ts') lis
where
spanLIs = Seq.spanl $ \case
- TreeN (unCell -> KeyDash) _ -> True
- Tree0 (unCell -> Tokens toks) ->
+ TreeN KeyDash _ -> True
+ Tree0 (Tokens toks) ->
(`any` toks) $ \case
TokenPair (PairElem "li" _) _ -> True
_ -> False
_ -> False
accumLIs acc@(oks,kos) t =
case t of
- TreeN (unCell -> KeyDash) _ -> (oks|>t,kos)
- Tree0 (Cell pos posEnd (Tokens toks)) ->
- let mk = Tree0 . Cell pos posEnd . Tokens in
+ TreeN KeyDash _ -> (oks|>t,kos)
+ Tree0 (Tokens toks) ->
+ let mk = Tree0 . Tokens in
let (ok,ko) =
(`Seq.spanl` toks) $ \case
TokenPair (PairElem "li" _) _ -> True
TokenPlain{} -> False
_ -> True
-gatherOL ::
- Trees (Cell Key) (Cell Tokens) ->
- ( Trees (Cell Key) (Cell Tokens)
- , Trees (Cell Key) (Cell Tokens) )
+gatherOL :: Trees Key Tokens -> (Trees Key Tokens, Trees Key Tokens)
gatherOL ts =
let (lis, ts') = spanLIs ts in
foldl' accumLIs (mempty,ts') lis
where
spanLIs = Seq.spanl $ \case
- TreeN (unCell -> KeyDot{}) _ -> True
- Tree0 (unCell -> Tokens toks) ->
+ TreeN KeyDot{} _ -> True
+ Tree0 (Tokens toks) ->
(`any` toks) $ \case
TokenPair (PairElem "li" _) _ -> True
_ -> False
_ -> False
accumLIs acc@(oks,kos) t =
case t of
- TreeN (unCell -> KeyDot{}) _ -> (oks|>t,kos)
- Tree0 (Cell pos posEnd (Tokens toks)) ->
- let mk = Tree0 . Cell pos posEnd . Tokens in
+ TreeN KeyDot{} _ -> (oks|>t,kos)
+ Tree0 (Tokens toks) ->
+ let mk = Tree0 . Tokens in
let (ok,ko) =
(`Seq.spanl` toks) $ \case
TokenPair (PairElem "li" _) _ -> True
TokenPlain{} -> False
_ -> True
-d_Tree :: [Key] -> Tree (Cell Key) (Cell Tokens) -> DTC
-d_Tree path (TreeN (unCell -> key@KeySection{}) ts) =
+d_Tree :: [Key] -> Tree Key Tokens -> DTC
+d_Tree path (TreeN key@KeySection{} ts) =
case Seq.viewl children of
- Tree0 (Cell _posTitle _ (toList -> [TokenPlain title])) :< body ->
+ Tree0 (toList -> [TokenPlain title]) :< body ->
d_attrs (mangleAttrs title attrs) $
case Text.splitOn "\n" title of
t0:t1 ->
[] ->
D.section ! DA.name (attrValue title) $
d_content body
- Tree0 (Cell _posTitle _ title) :< body ->
+ Tree0 title :< body ->
d_attrs (mangleAttrs (TL.toStrict $ Write.t_Tokens title) attrs) $
D.section $ do
D.name $ d_Tokens (key:path) title
d_content cs = d_Trees (key:path) cs
mangleAttrs :: Text -> Attributes -> Attributes
mangleAttrs title = Map.insertWith (\_new old -> old) "id" title
-d_Tree path (Tree0 cell) = d_CellTokens path cell
-d_Tree path (TreeN cell@(unCell -> KeyColon{}) ts) =
+d_Tree path (Tree0 ts) =
+ case path of
+ [] ->
+ case ts of
+ (toList -> [TokenPair PairElem{} _ts]) -> d_Tokens path ts
+ _ -> D.para $ d_Tokens path ts
+ KeySection{}:_ ->
+ case ts of
+ (toList -> [TokenPair PairElem{} _ts]) -> d_Tokens path ts
+ _ -> D.para $ d_Tokens path ts
+ _ -> d_Tokens path ts
+d_Tree path (TreeN cell@KeyColon{} ts) =
let (attrs,children) = partitionAttributesChildren ts in
- d_attrs attrs $ d_CellKey path cell children
-d_Tree path (TreeN cell ts) = d_CellKey path cell ts
+ d_attrs attrs $ d_Key path cell children
+d_Tree path (TreeN cell ts) = d_Key path cell ts
-d_CellKey :: [Key] -> Cell Key -> Trees (Cell Key) (Cell Tokens) -> DTC
-d_CellKey path (Cell _pos _posEnd key) cells = do
+d_Key :: [Key] -> Key -> Trees Key Tokens -> DTC
+d_Key path key cells = do
case key of
- KeyColon n _wh -> d_Key n
- KeyGreat n _wh -> d_Key n
- KeyEqual n _wh -> d_Key n
- KeyBar n _wh -> d_Key n
+ KeyColon n _wh -> d_key n
+ KeyGreat n _wh -> d_key n
+ KeyEqual n _wh -> d_key n
+ KeyBar n _wh -> d_key n
KeyDot _n -> D.li $ d_Trees (key:path) cells
KeyDash -> D.li $ d_Trees (key:path) cells
{-
forM_ cells $ d_Tree path
-}
where
- d_Key :: Text -> DTC
- d_Key name | null cells =
+ d_key :: Text -> DTC
+ d_key name | null cells =
B.CustomLeaf (B.Text name) True mempty
- d_Key name =
+ d_key name =
B.CustomParent (B.Text name) $
d_Trees (key:path) cells
-d_CellTokens :: [Key] -> Cell Tokens -> DTC
-d_CellTokens path (Cell _pos _posEnd ts) =
- -- case L.find (\case KeySection{} -> True; _ -> False) $ dbg "d_CellTokens: path:" path of
- case dbg "d_CellTokens: path" path of
- [] ->
- case ts of
- (toList -> [TokenPair PairElem{} _ts]) -> d_Tokens path ts
- _ -> D.para $ d_Tokens path ts
- KeySection{}:_ ->
- case ts of
- (toList -> [TokenPair PairElem{} _ts]) -> d_Tokens path ts
- _ -> D.para $ d_Tokens path ts
- _ -> d_Tokens path ts
-
d_Tokens :: [Key] -> Tokens -> DTC
d_Tokens _path tok = goTokens tok
where
B.AddCustomAttribute (B.Text n) (B.Text v)
partitionAttributesChildren ::
- Trees (Cell Key) (Cell Tokens) ->
- (Attributes, Trees (Cell Key) (Cell Tokens))
+ Trees Key Tokens ->
+ (Attributes, Trees Key Tokens)
partitionAttributesChildren ts = (attrs,children)
where
attrs :: Attributes
foldr (\t acc ->
case t of
Tree0{} -> acc
- TreeN (unCell -> KeyEqual n _wh) a -> Map.insert n v acc
+ TreeN (KeyEqual n _wh) a -> Map.insert n v acc
where
v = TL.toStrict $
Write.text Write.config_text{Write.config_text_escape = False} $
- Write.treeRackUpLeft <$> a
+ mapTreeKey cell1 (\_path -> cell1) <$> a
+ -- Write.treeRackUpLeft <$> a
TreeN{} -> acc
) mempty ts
children = Seq.filter (\t ->
case t of
Tree0{} -> True
- TreeN (unCell -> KeyEqual{}) _cs -> False
+ TreeN KeyEqual{} _cs -> False
TreeN{} -> True
) ts