git.iko.soy/typeable/pandoc
on master help sign in
Use entities for all characters above 127 in docbook output.

Though XML tools should support unicode, some people will be using SGML tools, and these do not. Using entities makes the docbook files more portable. Also refactored encodeEntities and charToHtmlEntity in HtmlEntities.hs git-svn-id: https://pandoc.googlecode.com/svn/trunk@394 788f1e2b-df1e-0410-8736-df70ead52e1b

commit4e5745134a8867d68a4678285272869d1c0ebce3 authorfiddlosopher <fiddlosopher@788f1e2b-df1e-0410-8736-df70ead52e1b> dateTue 2 Jan 2007, 00:29 (20 years ago) parent2716943
3 files changed+22−17
src/Text/Pandoc/HtmlEntities.hs+10−7 src/Text/Pandoc/Writers/Docbook.hs+7−5 tests/writer.docbook+5−5
src/Text/Pandoc/HtmlEntities.hs+10−7
@@ -57,7 +57,10 @@ decodeEntities str =
5757-- | Returns a string with characters replaced with entity references where
5858-- possible.
5959encodeEntities :: String -> String
60−encodeEntities = concatMap (\c -> fromMaybe [c] (charToHtmlEntity c))
60+encodeEntities [] = []
61+encodeEntities (c:cs) = if ord c < 127
62+ then c:(encodeEntities cs)
63+ else (charToHtmlEntity c) ++ (encodeEntities cs)
6164
6265-- | If the string is a valid entity reference, returns @Just@ the character,
6366-- otherwise @Nothing@.
@@ -69,14 +72,14 @@ htmlEntityToChar entity =
6972 Just (_, _, _, [sub]) -> Just (chr (read sub))
7073 Nothing -> Nothing
7174
72−-- | If there is an entity reference corresponding to the character, returns
73−-- @Just@ the entity reference, otherwise @Nothing@.
74−charToHtmlEntity :: Char -> Maybe String
75+-- | Returns a string containing an entity reference for the character.
76+charToHtmlEntity :: Char -> String
7577charToHtmlEntity char =
76− let matches = filter (\(entity, character) -> (character == char)) htmlEntityTable in
78+ let matches = filter (\(entity, character) -> (character == char))
79+ htmlEntityTable in
7780 if (length matches) == 0
78− then Nothing
79− else Just (fst (head matches))
81+ then "&#" ++ show (ord char) ++ ";"
82+ else fst (head matches)
8083
8184htmlEntityTable :: [(String, Char)]
8285htmlEntityTable = [
src/Text/Pandoc/Writers/Docbook.hs+7−5
@@ -33,9 +33,10 @@ module Text.Pandoc.Writers.Docbook (
3333import Text.Pandoc.Definition
3434import Text.Pandoc.Shared
3535import Text.Pandoc.Writers.HTML ( stringToSmartHtml, stringToHtml )
36+import Text.Pandoc.HtmlEntities ( encodeEntities )
3637import Text.Html ( stringToHtmlString )
3738import Text.Regex ( mkRegex, matchRegex )
38−import Data.Char ( toLower )
39+import Data.Char ( toLower, ord )
3940import Data.List ( isPrefixOf, partition )
4041import Text.PrettyPrint.HughesPJ hiding ( Str )
4142
@@ -179,9 +180,10 @@ wrap options lst = fsep $ map (hcat . (map (inlineToDocbook options))) (splitByS
179180
180181-- | Escape a string for XML (with "smart" option if specified).
181182stringToXML :: WriterOptions -> String -> String
182−stringToXML options = if writerSmart options
183− then stringToSmartHtml
184− else stringToHtml
183+stringToXML options = encodeEntities .
184+ (if writerSmart options
185+ then stringToSmartHtml
186+ else stringToHtml)
185187
186188-- | Escape string to XML appropriate for attributes
187189attributeStringToXML :: String -> String
@@ -189,7 +191,7 @@ attributeStringToXML = gsub "\"" "&quot;" . codeStringToXML
189191
190192-- | Escape a literal string for XML.
191193codeStringToXML :: String -> String
192−codeStringToXML = gsub "<" "&lt;" . gsub "&" "&amp;"
194+codeStringToXML = encodeEntities . gsub "<" "&lt;" . gsub "&" "&amp;"
193195
194196-- | Convert a list of inline elements to Docbook.
195197inlinesToDocbook :: WriterOptions -> [Inline] -> Doc
tests/writer.docbook+5−5
@@ -701,27 +701,27 @@ Cat &amp; 1 \\ \hline
701701 <itemizedlist>
702702 <listitem>
703703 <para>
704− I hat: Î
704+ I hat: &Icirc;
705705 </para>
706706 </listitem>
707707 <listitem>
708708 <para>
709− o umlaut: ö
709+ o umlaut: &ouml;
710710 </para>
711711 </listitem>
712712 <listitem>
713713 <para>
714− section: §
714+ section: &sect;
715715 </para>
716716 </listitem>
717717 <listitem>
718718 <para>
719− set membership: ∈
719+ set membership: &isin;
720720 </para>
721721 </listitem>
722722 <listitem>
723723 <para>
724− copyright: ©
724+ copyright: &copy;
725725 </para>
726726 </listitem>
727727 </itemizedlist>