{-# LANGUAGE GeneralizedNewtypeDeriving #-} {-# LANGUAGE NoMonomorphismRestriction #-} {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE StandaloneDeriving #-} module Language.DTC.Sym where import Control.Applicative (Applicative(..), (<$>), (<$)) import Control.Monad (void) import Data.Default.Class (Default(..)) import Data.Foldable (Foldable(..), concat) import Data.Function (($), (.), flip) import Data.Maybe (Maybe(..), maybe) import Data.Text (Text) import Data.TreeSeq.Strict (Tree(..)) import qualified Data.Sequence as Seq import qualified Data.Text as Text import Language.XML import Language.RNC.Sym as RNC import Language.DTC.Anchor (wordify) import qualified Language.DTC.Document as DTC import qualified Language.RNC.Write as RNC foldlApp :: (Default a, Foldable t) => t (a -> a) -> a foldlApp = foldl' (flip ($)) def foldrApp :: (Default a, Foldable t) => t (a -> a) -> a foldrApp = foldr ($) def class RNC.Sym_RNC repr => Sym_DTC repr where position :: repr DTC.Pos document :: repr DTC.Document head :: repr DTC.Head about :: repr DTC.About keyword :: repr Text version :: repr MayText author :: repr DTC.Entity editor :: repr DTC.Entity date :: repr DTC.Date entity :: repr DTC.Entity link :: repr DTC.Link serie :: repr DTC.Serie alias :: repr DTC.Alias body :: repr DTC.Body bodyValue :: repr DTC.BodyValue toc :: repr DTC.BodyValue tof :: repr DTC.BodyValue index :: repr DTC.BodyValue figure :: repr DTC.BodyValue references :: repr DTC.BodyValue reference :: repr DTC.Reference include :: repr DTC.Include block :: repr DTC.Block para :: repr DTC.Para lines :: repr (Tree DTC.LineKey DTC.LineValue) commonAttrs :: repr DTC.CommonAttrs ident :: repr Ident title :: repr DTC.Title name :: repr Text url :: repr URL path :: repr Path to :: repr Ident id :: repr Ident commonAttrs = rule "commonAttrs" $ interleaved $ DTC.CommonAttrs <$?> (def, Just <$> id) <|?> (def, rule "class" $ attribute "class" $ Text.words <$> text) document = rule "document" $ DTC.Document <$> head <*> body head = rule "head" $ maybe def DTC.Head <$> optional (rule "about" $ element "about" about) body = rule "body" $ (Seq.fromList <$>) $ many $ choice [ element "section" $ TreeN <$> section <*> body , Tree0 <$> bodyValue ] where section = DTC.Section <$> position <*> commonAttrs <*> title <*> many alias bodyValue = choice [ toc , tof , index , figure , references , DTC.Block <$> block ] title = rule "title" $ DTC.Title <$> element "title" para name = rule "name" $ attribute "name" text url = rule "url" $ URL <$> text path = rule "path" $ Path <$> text ident = rule "ident" $ Ident <$> text to = rule "to" $ attribute "to" ident id = rule "id" $ attribute "id" ident date = rule "date" $ element "date" $ interleaved $ DTC.Date <$?> (0, attribute "year" int) <|?> (Nothing, Just <$> attribute "month" nat1) <|?> (Nothing, Just <$> attribute "day" nat1) include = rule "include" $ element "include" $ interleaved $ DTC.Include <$?> (def, attribute "href" path) block = rule "block" $ choice [ DTC.Comment <$> comment , element "para" $ DTC.Para <$> position <*> commonAttrs <*> para , element "ol" $ DTC.OL <$> position <*> commonAttrs <*> many (element "li" $ many block) , element "ul" $ DTC.UL <$> position <*> commonAttrs <*> many (element "li" $ many block) {- , anyElem $ \n@XmlName{..} -> case xmlNameSpace of "" -> figure n -} ] toc = rule "toc" $ element "toc" $ DTC.ToC <$> position <*> commonAttrs <*> optional (attribute "depth" nat) tof = rule "tof" $ element "tof" $ DTC.ToF <$> position <*> commonAttrs <*> option [] ( element "ul" $ many $ element "li" $ element "para" text) index = rule "index" $ element "index" $ DTC.Index <$> position <*> commonAttrs <*> option [] ( element "ul" $ many $ element "li" $ element "para" $ (concat <$>) $ many $ (wordify <$>) . Text.lines <$> text) figure = rule "figure" $ element "figure" $ DTC.Figure <$> position <*> commonAttrs <*> attribute "type" text <*> title <*> many block references = element "references" $ DTC.References <$> position <*> commonAttrs <*> many reference para = rule "para" $ (Seq.fromList <$>) $ many lines lines = rule "lines" $ choice [ element "b" $ TreeN DTC.B <$> para , element "code" $ TreeN DTC.Code <$> para , element "del" $ TreeN DTC.Del <$> para , element "i" $ TreeN DTC.I <$> para , element "note" $ TreeN DTC.Note <$> para , element "q" $ TreeN DTC.Q <$> para , element "sc" $ TreeN DTC.SC <$> para , element "sub" $ TreeN DTC.Sub <$> para , element "sup" $ TreeN DTC.Sup <$> para , element "u" $ TreeN DTC.U <$> para , element "eref" $ TreeN . DTC.Eref <$> attribute "to" url <*> para , element "iref" $ TreeN . DTC.Iref Nothing . wordify <$> attribute "to" text <*> para , element "ref" $ TreeN . DTC.Ref <$> to <*> para , element "rref" $ TreeN . DTC.Rref Nothing <$> to <*> para , element "br" $ Tree0 DTC.BR <$ none , Tree0 . DTC.Plain <$> text ] keyword = rule "keyword" $ element "keyword" text version = rule "version" $ MayText <$> element "version" text about = interleaved $ DTC.About <$*> title <|?> (def, Just <$> attribute "url" url) <|*> author <|?> (Nothing, Just <$> editor) <|?> (Nothing, Just <$> date) <|?> (def, version) <|*> keyword <|*> link <|*> serie <|*> include author = rule "author" $ element "author" entity editor = rule "editor" $ element "editor" entity entity = rule "entity" $ interleaved $ DTC.Entity <$?> (def, name) <|?> (def, attribute "street" text) <|?> (def, attribute "zipcode" text) <|?> (def, attribute "city" text) <|?> (def, attribute "region" text) <|?> (def, attribute "country" text) <|?> (def, attribute "email" text) <|?> (def, attribute "tel" text) <|?> (def, attribute "fax" text) <|?> (def, Just <$> attribute "url" url) <|?> (def, Just <$> attribute "org" entity) serie = rule "serie" $ element "serie" $ interleaved $ DTC.Serie <$?> (def, name) <|?> (def, attribute "key" text) link = rule "link" $ element "link" $ interleaved $ (\n h r ls -> DTC.Link n h r (Seq.fromList ls)) <$?> (def, name) <|?> (def, attribute "href" url) <|?> (def, attribute "rel" text) <|*> lines alias = rule "alias" $ element "alias" $ interleaved $ DTC.Alias <$?> (def, id) reference = rule "reference" $ element "reference" $ DTC.Reference <$> id <*> about instance Sym_DTC RNC.Writer where position = RNC.writeText "" instance Sym_DTC RNC.RuleWriter where position = RNC.RuleWriter position dtcRNC :: [RNC.RuleWriter ()] dtcRNC = [ void $ document , void $ head , void $ rule "about" $ element "about" about , void $ keyword , void $ version , void $ author , void $ editor , void $ date , void $ entity , void $ link , void $ serie , void $ alias , void $ body , void $ bodyValue , void $ toc , void $ tof , void $ index , void $ figure , void $ references , void $ reference , void $ include , void $ block , void $ para , void $ lines , void $ commonAttrs , void $ ident , void $ title , void $ name , void $ url , void $ path , void $ to , void $ id ]