@@ -45,7 +45,7 @@ import Text.XML.Light hiding (ppTopElement)
4545import Text.Pandoc.UUID
4646import Text.Pandoc.Writers.HTML
4747import Text.Pandoc.Writers.Markdown ( writePlain )
48−import Data.Char ( toLower )
48+import Data.Char ( toLower, isDigit )
4949import Network.URI ( unEscapeString )
5050
5151-- | Produce an EPUB file from a Pandoc document.
@@ -276,7 +276,17 @@ transformBlock x = x
276276
277277-- | Version of 'ppTopElement' that specifies UTF-8 encoding.
278278ppTopElement :: Element -> String
279−ppTopElement = ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" ++) . ppElement
279+ppTopElement = ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" ++) . unEntity . ppElement
280+ -- unEntity removes decimal entities introduced by ppElement
281+ -- (kindlegen seems to choke on these).
282+ where unEntity [] = ""
283+ unEntity ('&':'#':d:xs) | isDigit d =
284+ let ds = takeWhile isDigit xs
285+ c = read $ '\'' : '\\' : d : ds ++ "'"
286+ in if c > '\127'
287+ then c : unEntity (drop (length ds + 2) xs)
288+ else '&':'#':d:ds ++ unEntity (drop (length ds + 2) xs)
289+ unEntity (x:xs) = x : unEntity xs
280290
281291imageTypeOf :: FilePath -> Maybe String
282292imageTypeOf x = case drop 1 (map toLower (takeExtension x)) of