aboutsummaryrefslogtreecommitdiff
path: root/bbcode
diff options
context:
space:
mode:
authorGregor Kleen <gkleen@yggdrasil.li>2016-01-12 10:26:45 +0100
committerGregor Kleen <gkleen@yggdrasil.li>2016-01-12 10:26:45 +0100
commitca7cd4ee2470115939523bb985aa576f9d87f18f (patch)
tree986d5d941f718fd813bf3436ae823cf1d0df1774 /bbcode
parent9f58557a76b5ae5478a7b7fc9e83266cd215c448 (diff)
downloadthermoprint-ca7cd4ee2470115939523bb985aa576f9d87f18f.tar
thermoprint-ca7cd4ee2470115939523bb985aa576f9d87f18f.tar.gz
thermoprint-ca7cd4ee2470115939523bb985aa576f9d87f18f.tar.bz2
thermoprint-ca7cd4ee2470115939523bb985aa576f9d87f18f.tar.xz
thermoprint-ca7cd4ee2470115939523bb985aa576f9d87f18f.zip
cleanup & docs
Diffstat (limited to 'bbcode')
-rw-r--r--bbcode/src/Text/BBCode.hs17
-rw-r--r--bbcode/src/Text/BBCode/Lexer.hs6
2 files changed, 15 insertions, 8 deletions
diff --git a/bbcode/src/Text/BBCode.hs b/bbcode/src/Text/BBCode.hs
index 32f74df..75fa219 100644
--- a/bbcode/src/Text/BBCode.hs
+++ b/bbcode/src/Text/BBCode.hs
@@ -3,12 +3,14 @@
3 3
4-- | An implementation of BBcode parsing 'Text' to a syntax tree 4-- | An implementation of BBcode parsing 'Text' to a syntax tree
5module Text.BBCode 5module Text.BBCode
6 ( TreeError(..) 6 ( bbcode
7 , BBCodeError(..)
8 , TreeError(..)
7 , DomTree(..) 9 , DomTree(..)
8 , dom 10 -- , dom
9 , BBLabel 11 -- , BBLabel
10 , rose 12 -- , rose
11 , matches 13 -- , matches
12 ) where 14 ) where
13 15
14import Data.Text (Text) 16import Data.Text (Text)
@@ -36,11 +38,15 @@ import qualified Data.CaseInsensitive as CI
36 38
37import Data.Bifunctor (Bifunctor(first)) 39import Data.Bifunctor (Bifunctor(first))
38 40
41-- | Our target structure -- a rose tree with an explicit terminal constructor
39data DomTree = Element Text (Map Text Text) [DomTree] 42data DomTree = Element Text (Map Text Text) [DomTree]
40 | Content Text 43 | Content Text
41 deriving (Show, Eq) 44 deriving (Show, Eq)
42 45
43dom :: Forest BBLabel -> [DomTree] 46dom :: Forest BBLabel -> [DomTree]
47-- ^ Parse semantically constrained rose tree to syntactically constrained version
48--
49-- Silently drops children of semantically terminal nodes ('BBPlain')
44dom = map dom' 50dom = map dom'
45 where 51 where
46 dom' (Node (BBPlain t) _) = Content t 52 dom' (Node (BBPlain t) _) = Content t
@@ -54,6 +60,7 @@ data BBCodeError = LexerError String -- ^ Error while parsing input to stream of
54instance Exception BBCodeError 60instance Exception BBCodeError
55 61
56bbcode :: Text -> Either BBCodeError [DomTree] 62bbcode :: Text -> Either BBCodeError [DomTree]
63-- ^ Parse BBCode
57bbcode t = fmap dom $ first LexerError (parseOnly (many token <* endOfInput) t) >>= first TreeError . rose 64bbcode t = fmap dom $ first LexerError (parseOnly (many token <* endOfInput) t) >>= first TreeError . rose
58 65
59-- | Errors in input encountered during parsing of lexed token-stream 66-- | Errors in input encountered during parsing of lexed token-stream
diff --git a/bbcode/src/Text/BBCode/Lexer.hs b/bbcode/src/Text/BBCode/Lexer.hs
index 4ad0792..218427d 100644
--- a/bbcode/src/Text/BBCode/Lexer.hs
+++ b/bbcode/src/Text/BBCode/Lexer.hs
@@ -5,8 +5,8 @@
5module Text.BBCode.Lexer 5module Text.BBCode.Lexer
6 ( BBToken(..) 6 ( BBToken(..)
7 , token 7 , token
8 , escapedText 8 -- , escapedText
9 , escapedText' 9 -- , escapedText'
10 ) where 10 ) where
11 11
12import Data.Attoparsec.Text 12import Data.Attoparsec.Text
@@ -22,7 +22,7 @@ import Prelude hiding (takeWhile)
22 22
23-- | Our lexicographical unit 23-- | Our lexicographical unit
24data BBToken = BBOpen Text [(Text, Text)] -- ^ Tag open with attributes 24data BBToken = BBOpen Text [(Text, Text)] -- ^ Tag open with attributes
25 | BBContained Text [(Text, Text)] 25 | BBContained Text [(Text, Text)] -- ^ Tag open & immediate close with attributes
26 | BBClose Text -- ^ Tag close 26 | BBClose Text -- ^ Tag close
27 | BBStr Text -- ^ Content of a tag 27 | BBStr Text -- ^ Content of a tag
28 deriving (Eq, Show) 28 deriving (Eq, Show)