From dc4e8f7a8550b217a1d04c0ab5139cee29b12dd7 Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Sat, 12 Feb 2011 21:51:11 -0800 Subject: [PATCH] Refactoring: daded Xml module. --- HeX.cabal | 1 + Text/HeX/Standard/Html.hs | 31 ++----------------------------- Text/HeX/Standard/Xml.hs | 33 +++++++++++++++++++++++++++++++++ test/Docbook.hs | 3 ++- 4 files changed, 38 insertions(+), 30 deletions(-) create mode 100644 Text/HeX/Standard/Xml.hs diff --git a/HeX.cabal b/HeX.cabal index 3bcae63..781769d 100644 --- a/HeX.cabal +++ b/HeX.cabal @@ -16,6 +16,7 @@ Library Text.HeX.Types Text.HeX.Standard Text.HeX.Standard.Generic + Text.HeX.Standard.Xml Text.HeX.Standard.Html Text.HeX.Standard.LaTeX Build-depends: parsec >= 3.1, base >= 4 && < 5, diff --git a/Text/HeX/Standard/Html.hs b/Text/HeX/Standard/Html.hs index 61541d8..68d1791 100644 --- a/Text/HeX/Standard/Html.hs +++ b/Text/HeX/Standard/Html.hs @@ -1,7 +1,8 @@ {-# LANGUAGE OverloadedStrings, ScopedTypeVariables #-} -module Text.HeX.Standard.Html (commands, str, ch, tagOpen, tagClose, tagSelfClosing, inTags, emitMath) where +module Text.HeX.Standard.Html (commands, emitMath) where import Text.HeX +import Text.HeX.Standard.Xml (ch, inTags) import Text.HeX.Standard.Generic (getSectionNum) commands :: HeX () @@ -16,34 +17,6 @@ commands = do registerFor "html" "paragraph" (section 4) registerFor "html" "subparagraph" (section 5) -str :: String -> Doc -str = mconcat . map ch - -ch :: Char -> Doc -ch '&' = raws "&" -ch '<' = raws "<" -ch '>' = raws ">" -ch c = rawc c - -tag :: Bool -> String -> [(String, String)] -> Doc -tag selfclosing s attrs = "<" +++ raws s +++ toattrs attrs +++ ending - where toattrs = mconcat . map toattr - toattr (k,v) = " " +++ raws k +++ ch '=' +++ ch '"' +++ - str v +++ ch '"' - ending = raws $ if selfclosing then " />" else ">" - -tagOpen :: String -> [(String, String)] -> Doc -tagOpen = tag False - -tagSelfClosing :: String -> [(String, String)] -> Doc -tagSelfClosing = tag True - -tagClose :: String -> Doc -tagClose s = "" - -inTags :: String -> [(String, String)] -> Doc -> Doc -inTags s attrs x = tagOpen s attrs +++ x +++ tagClose s - emitMath :: Bool -> Doc -> HeX Doc emitMath display b = do let tagtype = if display then "div" else "span" diff --git a/Text/HeX/Standard/Xml.hs b/Text/HeX/Standard/Xml.hs new file mode 100644 index 0000000..f99d36f --- /dev/null +++ b/Text/HeX/Standard/Xml.hs @@ -0,0 +1,33 @@ +{-# LANGUAGE OverloadedStrings, ScopedTypeVariables #-} +module Text.HeX.Standard.Xml (str, ch, tagSelfClosing, inTags) where + +import Text.HeX + +str :: String -> Doc +str = mconcat . map ch + +ch :: Char -> Doc +ch '&' = raws "&" +ch '<' = raws "<" +ch '>' = raws ">" +ch c = rawc c + +tag :: Bool -> String -> [(String, String)] -> Doc +tag selfclosing s attrs = "<" +++ raws s +++ toattrs attrs +++ ending + where toattrs = mconcat . map toattr + toattr (k,v) = " " +++ raws k +++ ch '=' +++ ch '"' +++ + str v +++ ch '"' + ending = raws $ if selfclosing then " />" else ">" + +tagOpen :: String -> [(String, String)] -> Doc +tagOpen = tag False + +tagSelfClosing :: String -> [(String, String)] -> Doc +tagSelfClosing = tag True + +tagClose :: String -> Doc +tagClose s = "" + +inTags :: String -> [(String, String)] -> Doc -> Doc +inTags s attrs x = tagOpen s attrs +++ x +++ tagClose s + diff --git a/test/Docbook.hs b/test/Docbook.hs index 413bdd9..9831438 100644 --- a/test/Docbook.hs +++ b/test/Docbook.hs @@ -1,8 +1,9 @@ {-# LANGUAGE OverloadedStrings, ScopedTypeVariables #-} module Docbook (commands) where +import Text.HeX.Standard.Xml (str, ch, inTags, tagSelfClosing) import Text.HeX.Standard.Generic (getSectionNum) -import Text.HeX.Standard.Html (str, ch, inTags, tagOpen, tagClose, tagSelfClosing, emitMath) +import Text.HeX.Standard.Html (emitMath) import Text.HeX import Text.Parsec import Control.Monad