From 23df0ed1768c4489d41180e145e98a37fd4ac9fc Mon Sep 17 00:00:00 2001 From: fiddlosopher Date: Tue, 10 Apr 2007 01:56:50 +0000 Subject: Extensive changes stemming from a rethinking of the Pandoc data structure. Key and Note blocks have been removed. Link and image URLs are now stored directly in Link and Image inlines, and note blocks are stored in Note inlines. This requires changes in both parsers and writers. Markdown and RST parsers need to extract data from key and note blocks and insert them into the relevant inline elements. Other parsers can be simplified, since there is no longer any need to construct separate key and note blocks. Markdown, RST, and HTML writers need to construct lists of notes; Markdown and RST writers need to construct lists of link references (when the --reference-links option is specified); and the RST writer needs to construct a list of image substitution references. All writers have been rewritten to use the State monad when state is required. This rewrite yields a small speed boost and considerably cleaner code. * Text/Pandoc/Definition.hs: + blocks: removed Key and Note + inlines: removed NoteRef, added Note + modified Target: there is no longer a 'Ref' target; all targets are explicit URL, title pairs * Text/Pandoc/Shared.hs: + Added 'Reference', 'isNoteBlock', 'isKeyBlock', 'isLineClump', used in some of the readers. + Removed 'generateReference', 'keyTable', 'replaceReferenceLinks', 'replaceRefLinksBlockList', along with some auxiliary functions used only by them. These are no longer needed, since reference links are resolved in the Markdown and RST readers. + Moved 'inTags', 'selfClosingTag', 'inTagsSimple', and 'inTagsIndented' to the Docbook writer, since that is now the only module that uses them. + Changed name of 'escapeSGMLString' to 'escapeStringForXML' + Added KeyTable and NoteTable types + Removed fields from ParserState; 'stateKeyBlocks', 'stateKeysUsed', 'stateNoteBlocks', 'stateNoteIdentifiers', 'stateInlineLinks'. Added 'stateKeys' and 'stateNotes'. + Added clause for Note to 'prettyBlock'. + Added 'writerNotes', 'writerReferenceLinks' fields to WriterOptions. * Text/Pandoc/Entities.hs: Renamed 'escapeSGMLChar' and 'escapeSGMLString' to 'escapeCharForXML' and 'escapeStringForXML' * Text/ParserCombinators/Pandoc.hs: Added lineClump parser: parses a raw line block up to and including following blank lines. * Main.hs: Replaced --inline-links with --reference-links. * README: + Documented --reference-links and removed description of --inline-links. + Added note that footnotes may occur anywhere in the document, but must be at the outer level, not embedded in block elements. * man/man1/pandoc.1, man/man1/html2markdown.1: Removed --inline-links option, added --reference-links option * Markdown and RST readers: + Rewrote to fit new Pandoc definition. Since there are no longer Note or Key blocks, all note and key blocks are parsed on a first pass through the document. Once tables of notes and keys have been constructed, the remaining parts of the document are reassembled and parsed. + Refactored link parsers. * LaTeX and HTML readers: Rewrote to fit new Pandoc definition. Since there are no longer Note or Key blocks, notes and references can be parsed in a single pass through the document. * RST, Markdown, and HTML writers: Rewrote using state monad new Pandoc and definition. State is used to hold lists of references footnotes to and be printed at the end of the document. * RTF and LaTeX writers: Rewrote using new Pandoc definition. (Because of the different treatment of footnotes, the "notes" parameter is no longer needed in the block and inline conversion functions.) * Docbook writer: + Moved the functions 'attributeList', 'inTags', 'selfClosingTag', 'inTagsSimple', 'inTagsIndented' from Text/Pandoc/Shared, since they are now used only by the Docbook writer. + Rewrote using new Pandoc definition. (Because of the different treatment of footnotes, the "notes" parameter is no longer needed in the block and inline conversion functions.) * Updated test suite * Throughout: old haskell98 module names replaced by hierarchical module names, e.g. List by Data.List. * debian/control: Include libghc6-xhtml-dev instead of libghc6-html-dev in "Build-Depends." * cabalize: + Remove haskell98 from BASE_DEPENDS (since now the new hierarchical module names are being used throughout) + Added mtl to BASE_DEPENDS (needed for state monad) + Removed html from GHC66_DEPENDS (not needed since xhtml is now used) git-svn-id: https://pandoc.googlecode.com/svn/trunk@580 788f1e2b-df1e-0410-8736-df70ead52e1b --- src/Text/Pandoc/Readers/Markdown.hs | 217 ++++++++++++++++-------------------- 1 file changed, 94 insertions(+), 123 deletions(-) (limited to 'src/Text/Pandoc/Readers/Markdown.hs') diff --git a/src/Text/Pandoc/Readers/Markdown.hs b/src/Text/Pandoc/Readers/Markdown.hs index a2e84e8c2..353dd45dd 100644 --- a/src/Text/Pandoc/Readers/Markdown.hs +++ b/src/Text/Pandoc/Readers/Markdown.hs @@ -31,7 +31,7 @@ module Text.Pandoc.Readers.Markdown ( readMarkdown ) where -import Data.List ( findIndex, sortBy, transpose, isSuffixOf, intersect ) +import Data.List ( findIndex, sortBy, transpose, isSuffixOf, intersect, lookup ) import Data.Char ( isAlphaNum ) import Text.ParserCombinators.Pandoc import Text.Pandoc.Definition @@ -160,28 +160,72 @@ titleBlock = try (do option "" blanklines return (title, author, date)) --- | Returns the number assigned to a Note block -numberOfNote :: Block -> Int -numberOfNote (Note ref _) = (read ref) -numberOfNote _ = 0 - parseMarkdown = do - updateState (\state -> state { stateParseRaw = True }) - -- need to parse raw HTML, since markdown allows it + updateState (\state -> state { stateParseRaw = True }) -- parse raw HTML: markdown allows it (title, author, date) <- option ([],[],"") titleBlock -- go through once just to get list of reference keys - keysUsed <- lookAhead $ (do {manyTill (referenceKey <|> (do{anyLine; return Null})) eof; - newState <- getState; - return $ stateKeysUsed newState}) - updateState (\st -> st { stateKeysUsed = keysUsed }) + refs <- manyTill (noteBlock <|> referenceKey <|> (do l <- lineClump + return (LineClump l))) eof + let keys = map (\(KeyBlock label target) -> (label, target)) $ + filter isKeyBlock refs + let notes = map (\(NoteBlock label blocks) -> (label, blocks)) $ + filter isNoteBlock refs + let rawlines = map (\(LineClump ln) -> ln) $ filter isLineClump refs + setInput $ concat rawlines -- with note blocks and keys stripped out + updateState (\state -> state { stateKeys = keys, stateNotes = notes }) blocks <- parseBlocks -- go through again, for real let blocks' = filter (/= Null) blocks - state <- getState - let keys = reverse $ stateKeyBlocks state - let notes = reverse $ stateNoteBlocks state - let sortedNotes = sortBy (\x y -> compare (numberOfNote x) - (numberOfNote y)) notes - return (Pandoc (Meta title author date) (blocks' ++ sortedNotes ++ keys)) + return (Pandoc (Meta title author date) blocks') + +-- +-- initial pass for references +-- + +referenceKey = try $ do + nonindentSpaces + label <- reference + char labelSep + skipSpaces + option ' ' (char autoLinkStart) + src <- many (noneOf (titleOpeners ++ [autoLinkEnd] ++ endLineChars)) + option ' ' (char autoLinkEnd) + tit <- option "" title + blanklines + return $ KeyBlock label (removeTrailingSpace src, tit) + +noteMarker = try (do + char labelStart + char noteStart + manyTill (noneOf " \t\n") (char labelEnd)) + +rawLine = try (do + notFollowedBy' blankline + notFollowedBy' noteMarker + contents <- many1 nonEndline + end <- option "" (do + newline + option "" (try indentSpaces) + return "\n") + return (contents ++ end)) + +rawLines = do + lines <- many1 rawLine + return (concat lines) + +noteBlock = try $ do + failIfStrict + ref <- noteMarker + char ':' + option ' ' (try blankline) + option "" (try indentSpaces) + raw <- sepBy rawLines (try (do {blankline; indentSpaces})) + option "" blanklines + -- parse the extracted text, which may contain various block elements: + rest <- getInput + setInput $ (joinWithSep "\n" raw) ++ "\n\n" + contents <- parseBlocks + setInput rest + return (NoteBlock ref contents) -- -- parsing blocks @@ -189,9 +233,17 @@ parseMarkdown = do parseBlocks = manyTill block eof -block = choice [ header, table, codeBlock, note, referenceKey, hrule, list, - blockQuote, htmlBlock, rawLaTeXEnvironment', para, - plain, nullBlock ] "block" +block = choice [ header + , table + , codeBlock + , hrule + , list + , blockQuote + , htmlBlock + , rawLaTeXEnvironment' + , para + , plain + , nullBlock ] "block" -- -- header blocks @@ -261,45 +313,6 @@ codeBlock = do option "" blanklines return (CodeBlock (stripTrailingNewlines result)) --- --- note block --- - -rawLine = try (do - notFollowedBy' blankline - notFollowedBy' noteMarker - contents <- many1 nonEndline - end <- option "" (do - newline - option "" (try indentSpaces) - return "\n") - return (contents ++ end)) - -rawLines = do - lines <- many1 rawLine - return (concat lines) - -note = try (do - failIfStrict - ref <- noteMarker - char ':' - skipSpaces - skipEndline - raw <- sepBy rawLines (try (do {blankline; indentSpaces})) - option "" blanklines - -- parse the extracted text, which may contain various block elements: - rest <- getInput - setInput $ (joinWithSep "\n" raw) ++ "\n\n" - contents <- parseBlocks - setInput rest - state <- getState - let identifiers = stateNoteIdentifiers state - case (findIndex (== ref) identifiers) of - Just n -> updateState (\s -> s {stateNoteBlocks = - (Note (show (n+1)) contents):(stateNoteBlocks s)}) - Nothing -> updateState id - return Null) - -- -- block quotes -- @@ -535,25 +548,6 @@ rawHtmlBlocks = try (do else combined return (RawHtml combined')) --- --- reference key --- - -referenceKey = try (do - nonindentSpaces - label <- reference - char labelSep - skipSpaces - option ' ' (char autoLinkStart) - src <- many (noneOf (titleOpeners ++ [autoLinkEnd] ++ endLineChars)) - option ' ' (char autoLinkEnd) - tit <- option "" title - blanklines - state <- getState - let keysUsed = stateKeysUsed state - setState state { stateKeysUsed = (label:keysUsed) } - return $ Key label (Src (removeTrailingSpace src) tit)) - -- -- LaTeX -- @@ -713,7 +707,7 @@ table = do inline = choice [ rawLaTeXInline' , escapedChar , entity - , noteRef + , note , inlineNote , link , referenceLink @@ -933,7 +927,7 @@ reference = try $ do return (normalizeSpaces label) -- source for a link, with optional title -source = try (do +source = try $ do char srcStart option ' ' (char autoLinkStart) src <- many (noneOf ([srcEnd, autoLinkEnd] ++ titleOpeners)) @@ -941,7 +935,7 @@ source = try (do tit <- option "" title skipSpaces char srcEnd - return (Src (removeTrailingSpace src) tit)) + return (removeTrailingSpace src, tit) titleWith startChar endChar = try (do skipSpaces @@ -965,30 +959,18 @@ explicitLink = try (do src <- source return (Link label src)) -referenceLink = choice [referenceLinkDouble, referenceLinkSingle] - --- a link like [this][ref] -referenceLinkDouble = try (do +-- a link like [this][ref] or [this][] or [this] +referenceLink = try $ do label <- reference - skipSpaces - option ' ' newline - skipSpaces - ref <- reference + ref <- option [] (try (do skipSpaces + option ' ' newline + skipSpaces + reference)) let ref' = if null ref then label else ref state <- getState - if ref' `elem` (stateKeysUsed state) - then return () - else fail "no corresponding key" - return (Link label (Ref ref'))) - --- a link like [this] -referenceLinkSingle = try (do - label <- reference - state <- getState - if label `elem` (stateKeysUsed state) - then return () - else fail "no corresponding key" - return (Link label (Ref label))) + case lookupKeySrc (stateKeys state) ref' of + Nothing -> fail "no corresponding key" + Just target -> return (Link label target) autoLink = autoLinkEmail <|> autoLinkRegular @@ -999,7 +981,7 @@ autoLinkEmail = try $ do domain <- sepBy1 (many1 (noneOf "/:.@<> \t\n")) (char '.') let src = name ++ "@" ++ (joinWithSep "." domain) char autoLinkEnd - return $ Link [Str src] (Src ("mailto:" ++ src) "") + return $ Link [Str src] (("mailto:" ++ src), "") -- a link autoLinkRegular = try $ do @@ -1007,39 +989,28 @@ autoLinkRegular = try $ do prot <- oneOfStrings ["http:", "ftp:", "mailto:"] rest <- many1Till (noneOf " \t\n<>") (char autoLinkEnd) let src = prot ++ rest - return $ Link [Str src] (Src src "") + return $ Link [Str src] (src, "") image = try (do char imageStart (Link label src) <- link return (Image label src)) -noteMarker = try (do - char labelStart - char noteStart - manyTill (noneOf " \t\n") (char labelEnd)) - -noteRef = try (do +note = try $ do failIfStrict ref <- noteMarker state <- getState - let identifiers = (stateNoteIdentifiers state) ++ [ref] - setState state {stateNoteIdentifiers = identifiers} - return (NoteRef (show (length identifiers)))) + let notes = stateNotes state + case lookup ref notes of + Nothing -> fail "note not found" + Just contents -> return (Note contents) -inlineNote = try (do +inlineNote = try $ do failIfStrict char noteStart char labelStart contents <- manyTill inline (char labelEnd) - state <- getState - let identifiers = stateNoteIdentifiers state - let ref = show $ (length identifiers) + 1 - let noteBlocks = stateNoteBlocks state - setState state {stateNoteIdentifiers = (identifiers ++ [ref]), - stateNoteBlocks = - (Note ref [Para contents]):noteBlocks} - return (NoteRef ref)) + return (Note [Para contents]) rawLaTeXInline' = do failIfStrict -- cgit v1.2.3