Skip to content

Commit

Permalink
Refactoring: daded Xml module.
Browse files Browse the repository at this point in the history
  • Loading branch information
jgm committed Feb 13, 2011
1 parent 47ff28e commit dc4e8f7
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 30 deletions.
1 change: 1 addition & 0 deletions HeX.cabal
Expand Up @@ -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,
Expand Down
31 changes: 2 additions & 29 deletions 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 ()
Expand All @@ -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 "&amp;"
ch '<' = raws "&lt;"
ch '>' = raws "&gt;"
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 = "</" +++ raws 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"
Expand Down
33 changes: 33 additions & 0 deletions 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 "&amp;"
ch '<' = raws "&lt;"
ch '>' = raws "&gt;"
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 = "</" +++ raws s +++ ">"

inTags :: String -> [(String, String)] -> Doc -> Doc
inTags s attrs x = tagOpen s attrs +++ x +++ tagClose s

3 changes: 2 additions & 1 deletion 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
Expand Down

0 comments on commit dc4e8f7

Please sign in to comment.