git.iko.soy/typeable/pandoc
on master help sign in
Shared: Fixed uniqueIdent so it behaves as described in README.

Previously some characters that are illegal in HTML identifiers, such as '<', were being allowed in header identifiers. The logic has now been fixed. Thanks to Xyne for reporting.

commitc5c4e19c99003e64665d244bb40ffa0eff93d269 authorJohn MacFarlane <jgm@berkeley.edu> dateSun 28 Mar 2010, 22:29 (17 years ago) parentbe832b3
1 file changed+34−33
src/Text/Pandoc/Shared.hs+34−33
src/Text/Pandoc/Shared.hs+34−33
@@ -117,7 +117,7 @@ import Text.PrettyPrint.HughesPJ ( Doc, fsep, ($$), (<>), empty, isEmpty, text,
117117import qualified Text.PrettyPrint.HughesPJ as PP
118118import Text.Pandoc.CharacterReferences ( characterReference )
119119import Data.Char ( toLower, toUpper, ord, isLower, isUpper, isAlpha, isAscii,
120− isPunctuation )
120+ isLetter, isDigit )
121121import Data.List ( find, isPrefixOf, intercalate )
122122import Network.URI ( parseURI, URI (..), isAllowedInURI, escapeURIString, unEscapeString )
123123import Codec.Binary.UTF8.String ( encodeString, decodeString )
@@ -914,38 +914,37 @@ data Element = Blk Block
914914 -- lvl num ident label contents
915915 deriving (Eq, Read, Show, Typeable, Data)
916916
917−-- | Convert Pandoc inline list to plain text identifier.
917+-- | Convert Pandoc inline list to plain text identifier. HTML
918+-- identifiers must start with a letter, and may contain only
919+-- letters, digits, and the characters _-:.
918920inlineListToIdentifier :: [Inline] -> String
919−inlineListToIdentifier = dropWhile (not . isAlpha) . inlineListToIdentifier'
920−
921−inlineListToIdentifier' :: [Inline] -> [Char]
922−inlineListToIdentifier' [] = ""
923−inlineListToIdentifier' (x:xs) =
924− xAsText ++ inlineListToIdentifier' xs
925− where xAsText = case x of
926− Str s -> filter (\c -> c `elem` "_-." || not (isPunctuation c)) $
927− intercalate "-" $ words $ map toLower s
928− Emph lst -> inlineListToIdentifier' lst
929− Strikeout lst -> inlineListToIdentifier' lst
930− Superscript lst -> inlineListToIdentifier' lst
931− SmallCaps lst -> inlineListToIdentifier' lst
932− Subscript lst -> inlineListToIdentifier' lst
933− Strong lst -> inlineListToIdentifier' lst
934− Quoted _ lst -> inlineListToIdentifier' lst
935− Cite _ lst -> inlineListToIdentifier' lst
936− Code s -> s
937− Space -> "-"
938− EmDash -> "-"
939− EnDash -> "-"
940− Apostrophe -> ""
941− Ellipses -> ""
942− LineBreak -> "-"
943− Math _ _ -> ""
944− TeX _ -> ""
945− HtmlInline _ -> ""
946− Link lst _ -> inlineListToIdentifier' lst
947− Image lst _ -> inlineListToIdentifier' lst
948− Note _ -> ""
921+inlineListToIdentifier =
922+ dropWhile (not . isAlpha) . intercalate "-" . words . map toLower .
923+ filter (\c -> isLetter c || isDigit c || c `elem` "_-:. ") .
924+ concatMap extractText
925+ where extractText x = case x of
926+ Str s -> s
927+ Emph lst -> concatMap extractText lst
928+ Strikeout lst -> concatMap extractText lst
929+ Superscript lst -> concatMap extractText lst
930+ SmallCaps lst -> concatMap extractText lst
931+ Subscript lst -> concatMap extractText lst
932+ Strong lst -> concatMap extractText lst
933+ Quoted _ lst -> concatMap extractText lst
934+ Cite _ lst -> concatMap extractText lst
935+ Code s -> s
936+ Space -> " "
937+ EmDash -> "---"
938+ EnDash -> "--"
939+ Apostrophe -> ""
940+ Ellipses -> "..."
941+ LineBreak -> " "
942+ Math _ s -> s
943+ TeX _ -> ""
944+ HtmlInline _ -> ""
945+ Link lst _ -> concatMap extractText lst
946+ Image lst _ -> concatMap extractText lst
947+ Note _ -> ""
949948
950949-- | Convert list of Pandoc blocks into (hierarchical) list of Elements
951950hierarchicalize :: [Block] -> [Element]
@@ -977,7 +976,9 @@ headerLtEq _ _ = False
977976-- Second argument is a list of already used identifiers.
978977uniqueIdent :: [Inline] -> [String] -> String
979978uniqueIdent title' usedIdents =
980− let baseIdent = inlineListToIdentifier title'
979+ let baseIdent = case inlineListToIdentifier title' of
980+ "" -> "section"
981+ x -> x
981982 numIdent n = baseIdent ++ "-" ++ show n
982983 in if baseIdent `elem` usedIdents
983984 then case find (\x -> numIdent x `notElem` usedIdents) ([1..60000] :: [Int]) of