module Rdf{-(RDF(..),parseRdf)-} where import qualified RichTextLex as RT import ParsOps import AnchorParser(parseTag) import Data.Maybe(mapMaybe) import Utils2(strToUpper) -------------------------------------------------------------------------------- type RdfTag = (String,[(String,String)]) data RDF = RdfCtx RdfTag [RDF] | RdfCmd RdfTag | RdfChars String deriving (Show) parseRdf s = case parseRdf' s of Right rdf -> Just rdf Left _ -> Nothing parseRdf' = parse (rdfP `chk` eof) . rdfLex rdfP = many rdfItemP rdfItemP = charsP `orelse` cmdP `orelse` ctxP charsP = lit chars where chars (Chars,s) = Just (RdfChars s) chars _ = Nothing cmdP = lit cmd where cmd (CmdTag,s) = RdfCmd `fmap` parseTag s cmd _ = Nothing ctxP = startTagP `bind` \ tag@(name,_) -> RdfCtx tag `mapP` rdfP `chk` endTagP name where startTagP = lit startTag where startTag (StartTag,s) = parseTag s startTag _ = Nothing endTagP name = tok (EndTag,name) -------------------------------------------------------------------------------- type RdfLex = (RdfToken, String) data RdfToken = StartTag | EndTag | CmdTag | Chars deriving (Eq,Show) rdfLex = mapMaybe post . RT.rtlex where notComment (RT.Comment _) = False notFComment _ = True post (RT.Chars s) = Just (Chars, s) post (RT.FmtCmd s) = case s of '/':s' -> Just (EndTag, strToUpper s') _ -> if last s=='/' then Just (CmdTag, init s) else Just (StartTag, s) post _ = Nothing --------------------------------------------------------------------------------