git.iko.soy/typeable/pandoc
on master help sign in
Markdown writer: Handle labels with integer names
ed73bd2Jesse Rosenthal
src/Text/Pandoc/Writers/Markdown.hs+20−3
@@ -75,6 +75,7 @@ instance Default WriterEnv
75 }
75 }
76
76
77data WriterState = WriterState { stNotes :: Notes
77data WriterState = WriterState { stNotes :: Notes
78+ , stPrevRefs :: Refs
78 , stRefs :: Refs
79 , stRefs :: Refs
79 , stKeys :: M.Map Key
80 , stKeys :: M.Map Key
80 (M.Map (Target, Attr) Int)
81 (M.Map (Target, Attr) Int)
@@ -85,6 +86,7 @@ data WriterState = WriterState { stNotes :: Notes
85
86
86instance Default WriterState
87instance Default WriterState
87 where def = WriterState{ stNotes = []
88 where def = WriterState{ stNotes = []
89+ , stPrevRefs = []
88 , stRefs = []
90 , stRefs = []
89 , stKeys = M.empty
91 , stKeys = M.empty
90 , stLastIdx = 0
92 , stLastIdx = 0
@@ -355,7 +357,8 @@ notesAndRefs opts = do
355 notes' <- reverse <$> gets stNotes >>= notesToMarkdown opts
357 notes' <- reverse <$> gets stNotes >>= notesToMarkdown opts
356 modify $ \s -> s { stNotes = [] }
358 modify $ \s -> s { stNotes = [] }
357 refs' <- reverse <$> gets stRefs >>= refsToMarkdown opts
359 refs' <- reverse <$> gets stRefs >>= refsToMarkdown opts
360+ modify $ \s -> s { stPrevRefs = stPrevRefs s ++ stRefs s
358− modify $ \s -> s { stRefs = [] }
361+ , stRefs = []}
359
362
360 let endSpacing =
363 let endSpacing =
361 if | writerReferenceLocation opts == EndOfDocument -> empty
364 if | writerReferenceLocation opts == EndOfDocument -> empty
@@ -867,6 +870,20 @@ blockListToMarkdown opts blocks = do
867getKey :: Doc -> Key
870getKey :: Doc -> Key
868getKey = toKey . render Nothing
871getKey = toKey . render Nothing
869
872
873+findUsableIndex :: [Doc] -> Int -> Int
874+findUsableIndex lbls i = do
875+ if (text (show i)) `elem` lbls
876+ then findUsableIndex lbls (i + 1)
877+ else i
878+
879+getNextIndex :: PandocMonad m => MD m Int
880+getNextIndex = do
881+ prevRefs <- gets stPrevRefs
882+ refs <- gets stRefs
883+ i <- (+ 1) <$> gets stLastIdx
884+ let refLbls = map (\(r,_,_) -> r) $ prevRefs ++ refs
885+ return $ findUsableIndex refLbls i
886+
870-- | Get reference for target; if none exists, create unique one and return.
887-- | Get reference for target; if none exists, create unique one and return.
871-- Prefer label if possible; otherwise, generate a unique key.
888-- Prefer label if possible; otherwise, generate a unique key.
872getReference :: PandocMonad m => Attr -> Doc -> Target -> MD m Doc
889getReference :: PandocMonad m => Attr -> Doc -> Target -> MD m Doc
@@ -880,7 +897,7 @@ getReference attr label target = do
880 Nothing -> do -- no other refs with this label
897 Nothing -> do -- no other refs with this label
881 (lab', idx) <- if isEmpty label
898 (lab', idx) <- if isEmpty label
882 then do
899 then do
883− i <- (+ 1) <$> gets stLastIdx
900+ i <- getNextIndex
884 modify $ \s -> s{ stLastIdx = i }
901 modify $ \s -> s{ stLastIdx = i }
885 return (text (show i), i)
902 return (text (show i), i)
886 else return (label, 0)
903 else return (label, 0)
@@ -905,7 +922,7 @@ getReference attr label target = do
905 stRefs = (lab', target, attr) : refs })
922 stRefs = (lab', target, attr) : refs })
906 return lab'
923 return lab'
907 Nothing -> do -- but this one is to a new target
924 Nothing -> do -- but this one is to a new target
908− i <- (+ 1) <$> gets stLastIdx
925+ i <- getNextIndex
909 modify $ \s -> s{ stLastIdx = i }
926 modify $ \s -> s{ stLastIdx = i }
910 let lab' = text (show i)
927 let lab' = text (show i)
911 modify (\s -> s{
928 modify (\s -> s{
test/command/5495.mdnew file+18
@@ -0,0 +1,18 @@
1+```
2+% pandoc -t markdown --reference-links
3+All because of [1](#one) link...
4+
5+[This](#foo) will break Pandoc.
6+
7+[This](#bar) will make you laugh.
8+^D
9+All because of [1] link...
10+
11+[This] will break Pandoc.
12+
13+[This][2] will make you laugh.
14+
15+ [1]: #one
16+ [This]: #foo
17+ [2]: #bar
18+```