git.iko.soy/typeable/pandoc
on master help sign in
Textile reader: Implemented footnotes.
commitf02080b62db7671aac090f89bb3df48637134e75 authorJohn MacFarlane <jgm@berkeley.edu> dateWed 8 Dec 2010, 00:44 (16 years ago) parent200ea33
3 files changed+54−6
src/Text/Pandoc/Readers/Textile.hs+43−4 tests/textile-reader.native+4−2 tests/textile-reader.textile+7
src/Text/Pandoc/Readers/Textile.hs+43−4
@@ -35,12 +35,12 @@ Implemented and parsed:
3535 - blockquote
3636 - Inlines : strong, emph, cite, code, deleted, superscript,
3737 subscript, links
38+ - footnotes
3839
3940Implemented but discarded:
4041 - HTML-specific and CSS-specific attributes
4142
4243Left to be implemented:
43− - footnotes
4444 - dimension sign
4545 - all caps
4646 - definition lists
@@ -64,7 +64,7 @@ import Text.Pandoc.Readers.HTML ( htmlTag, htmlEndTag, -- find code blocks
6464-- import Text.Pandoc.Readers.Markdown (smartPunctuation)
6565import Text.ParserCombinators.Parsec
6666import Data.Char ( digitToInt, isLetter )
67−import Control.Monad ( guard )
67+import Control.Monad ( guard, liftM )
6868
6969-- | Parse a Textile text and return a Pandoc document.
7070readTextile :: ParserState -- ^ Parser state, including options for parser
@@ -87,8 +87,35 @@ parseTextile = do
8787 -- textile allows raw HTML and does smart punctuation by default
8888 updateState (\state -> state { stateParseRaw = True, stateSmart = True })
8989 many blankline
90− blocks <- parseBlocks
91− return $ Pandoc (Meta [Str ""] [[Str ""]] [Str ""]) blocks -- FIXME
90+ startPos <- getPosition
91+ -- go through once just to get list of reference keys and notes
92+ -- docMinusKeys is the raw document with blanks where the keys/notes were...
93+ let firstPassParser = noteBlock <|> lineClump
94+ manyTill firstPassParser eof >>= setInput . concat
95+ setPosition startPos
96+ st' <- getState
97+ let reversedNotes = stateNotes st'
98+ updateState $ \s -> s { stateNotes = reverse reversedNotes }
99+ -- now parse it for real...
100+ blocks <- parseBlocks
101+ return $ Pandoc (Meta [] [] []) blocks -- FIXME
102+
103+noteMarker :: GenParser Char ParserState [Char]
104+noteMarker = skipMany spaceChar >> string "fn" >> manyTill digit (char '.')
105+
106+noteBlock :: GenParser Char ParserState [Char]
107+noteBlock = try $ do
108+ startPos <- getPosition
109+ ref <- noteMarker
110+ optional blankline
111+ contents <- liftM unlines $ many1Till anyLine (blanklines <|> noteBlock)
112+ endPos <- getPosition
113+ let newnote = (ref, contents ++ "\n")
114+ st <- getState
115+ let oldnotes = stateNotes st
116+ updateState $ \s -> s { stateNotes = newnote : oldnotes }
117+ -- return blanks so line count isn't affected
118+ return $ replicate (sourceLine endPos - sourceLine startPos) '\n'
92119
93120-- | Parse document blocks
94121parseBlocks :: GenParser Char ParserState [Block]
@@ -306,6 +333,7 @@ inlines = manyTill inline newline
306333inlineParsers :: [GenParser Char ParserState Inline]
307334inlineParsers = [ autoLink
308335 , mark
336+ , note
309337 , str
310338 , htmlSpan
311339 , whitespace
@@ -349,6 +377,17 @@ copy = do
349377 char ')'
350378 return $ Str "\169"
351379
380+note :: GenParser Char ParserState Inline
381+note = try $ do
382+ char '['
383+ ref <- many1 digit
384+ char ']'
385+ state <- getState
386+ let notes = stateNotes state
387+ case lookup ref notes of
388+ Nothing -> fail "note not found"
389+ Just raw -> liftM Note $ parseFromString parseBlocks raw
390+
352391-- | Any string
353392str :: GenParser Char ParserState Inline
354393str = do
tests/textile-reader.native+4−2
@@ -1,4 +1,4 @@
1−Pandoc (Meta {docTitle = [Str ""], docAuthors = [[Str ""]], docDate = [Str ""]})
1+Pandoc (Meta {docTitle = [], docAuthors = [], docDate = []})
22[ Para [Str "This",Space,Str "is",Space,Str "a",Space,Str "set",Space,Str "of",Space,Str "tests",Space,Str "for",Space,Str "pandoc",Space,Str "Textile",Space,Str "Reader",Str ".",Space,Str "Part",Space,Str "of",Space,Str "it",Space,Str "comes",LineBreak,Str "from",Space,Str "John",Space,Str "Gruber",Apostrophe,Str "s",Space,Str "markdown",Space,Str "test",Space,Str "suite",Str "."]
33, HorizontalRule
44, Header 1 [Str "Headers"]
@@ -131,4 +131,6 @@ Pandoc (Meta {docTitle = [Str ""], docAuthors = [[Str ""]], docDate = [Str ""]})
131131, Para [Str "Hi",Str "\8482"]
132132, Para [Str "Hi",Space,Str "\8482"]
133133, Para [Str "\174",Space,Str "Hi",Str "\174"]
134−, Para [Str "Hi",Str "\169",Str "2008",Space,Str "\169",Space,Str "2008"] ]
134+, Para [Str "Hi",Str "\169",Str "2008",Space,Str "\169",Space,Str "2008"]
135+, Header 1 [Str "Footnotes"]
136+, Para [Str "A",Space,Str "note",Str ".",Note [Para [Str "The",Space,Str "note",LineBreak,Str "is",Space,Str "here",Str "!"]]] ]
tests/textile-reader.textile+7
@@ -190,3 +190,10 @@ Hi (TM)
190190(r) Hi(r)
191191
192192Hi(c)2008 (C) 2008
193+
194+h1. Footnotes
195+
196+A note.[1]
197+
198+fn1. The note
199+is here!