git.iko.soy/typeable/pandoc
on master help sign in
Use entities for all characters above 127 in docbook output.
4e57451fiddlosopher
src/Text/Pandoc/HtmlEntities.hs+10−7
@@ -57,7 +57,10 @@ decodeEntities str =
57-- | Returns a string with characters replaced with entity references where
57-- | Returns a string with characters replaced with entity references where
58-- possible.
58-- possible.
59encodeEntities :: String -> String
59encodeEntities :: 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)
61
64
62-- | If the string is a valid entity reference, returns @Just@ the character,
65-- | If the string is a valid entity reference, returns @Just@ the character,
63-- otherwise @Nothing@.
66-- otherwise @Nothing@.
@@ -69,14 +72,14 @@ htmlEntityToChar entity =
69 Just (_, _, _, [sub]) -> Just (chr (read sub))
72 Just (_, _, _, [sub]) -> Just (chr (read sub))
70 Nothing -> Nothing
73 Nothing -> Nothing
71
74
72−-- | If there is an entity reference corresponding to the character, returns
75+-- | Returns a string containing an entity reference for the character.
73−-- @Just@ the entity reference, otherwise @Nothing@.
74−charToHtmlEntity :: Char -> Maybe String
76+charToHtmlEntity :: Char -> String
75charToHtmlEntity char =
77charToHtmlEntity char =
76− let matches = filter (\(entity, character) -> (character == char)) htmlEntityTable in
78+ let matches = filter (\(entity, character) -> (character == char))
79+ htmlEntityTable in
77 if (length matches) == 0
80 if (length matches) == 0
78− then Nothing
81+ then "&#" ++ show (ord char) ++ ";"
79− else Just (fst (head matches))
82+ else fst (head matches)
80
83
81htmlEntityTable :: [(String, Char)]
84htmlEntityTable :: [(String, Char)]
82htmlEntityTable = [
85htmlEntityTable = [
src/Text/Pandoc/Writers/Docbook.hs+7−5
@@ -33,9 +33,10 @@ module Text.Pandoc.Writers.Docbook (
33import Text.Pandoc.Definition
33import Text.Pandoc.Definition
34import Text.Pandoc.Shared
34import Text.Pandoc.Shared
35import Text.Pandoc.Writers.HTML ( stringToSmartHtml, stringToHtml )
35import Text.Pandoc.Writers.HTML ( stringToSmartHtml, stringToHtml )
36+import Text.Pandoc.HtmlEntities ( encodeEntities )
36import Text.Html ( stringToHtmlString )
37import Text.Html ( stringToHtmlString )
37import Text.Regex ( mkRegex, matchRegex )
38import Text.Regex ( mkRegex, matchRegex )
38−import Data.Char ( toLower )
39+import Data.Char ( toLower, ord )
39import Data.List ( isPrefixOf, partition )
40import Data.List ( isPrefixOf, partition )
40import Text.PrettyPrint.HughesPJ hiding ( Str )
41import Text.PrettyPrint.HughesPJ hiding ( Str )
41
42
@@ -179,9 +180,10 @@ wrap options lst = fsep $ map (hcat . (map (inlineToDocbook options))) (splitByS
179
180
180-- | Escape a string for XML (with "smart" option if specified).
181-- | Escape a string for XML (with "smart" option if specified).
181stringToXML :: WriterOptions -> String -> String
182stringToXML :: WriterOptions -> String -> String
183+stringToXML options = encodeEntities .
182−stringToXML options = if writerSmart options
184+ (if writerSmart options
183− then stringToSmartHtml
185+ then stringToSmartHtml
184− else stringToHtml
186+ else stringToHtml)
185
187
186-- | Escape string to XML appropriate for attributes
188-- | Escape string to XML appropriate for attributes
187attributeStringToXML :: String -> String
189attributeStringToXML :: String -> String
@@ -189,7 +191,7 @@ attributeStringToXML = gsub "\"" "&quot;" . codeStringToXML
189
191
190-- | Escape a literal string for XML.
192-- | Escape a literal string for XML.
191codeStringToXML :: String -> String
193codeStringToXML :: String -> String
192−codeStringToXML = gsub "<" "&lt;" . gsub "&" "&amp;"
194+codeStringToXML = encodeEntities . gsub "<" "&lt;" . gsub "&" "&amp;"
193
195
194-- | Convert a list of inline elements to Docbook.
196-- | Convert a list of inline elements to Docbook.
195inlinesToDocbook :: WriterOptions -> [Inline] -> Doc
197inlinesToDocbook :: WriterOptions -> [Inline] -> Doc
tests/writer.docbook+5−5
@@ -701,27 +701,27 @@ Cat &amp; 1 \\ \hline
701 <itemizedlist>
701 <itemizedlist>
702 <listitem>
702 <listitem>
703 <para>
703 <para>
704− I hat: Î
704+ I hat: &Icirc;
705 </para>
705 </para>
706 </listitem>
706 </listitem>
707 <listitem>
707 <listitem>
708 <para>
708 <para>
709− o umlaut: ö
709+ o umlaut: &ouml;
710 </para>
710 </para>
711 </listitem>
711 </listitem>
712 <listitem>
712 <listitem>
713 <para>
713 <para>
714− section: §
714+ section: &sect;
715 </para>
715 </para>
716 </listitem>
716 </listitem>
717 <listitem>
717 <listitem>
718 <para>
718 <para>
719− set membership: ∈
719+ set membership: &isin;
720 </para>
720 </para>
721 </listitem>
721 </listitem>
722 <listitem>
722 <listitem>
723 <para>
723 <para>
724− copyright: ©
724+ copyright: &copy;
725 </para>
725 </para>
726 </listitem>
726 </listitem>
727 </itemizedlist>
727 </itemizedlist>