2006-12-20 20:54:23 +00:00
|
|
|
{-
|
2007-07-07 22:51:55 +00:00
|
|
|
Copyright (C) 2006-7 John MacFarlane <jgm@berkeley.edu>
|
2006-12-20 20:54:23 +00:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
-}
|
|
|
|
|
2006-12-20 06:50:14 +00:00
|
|
|
{- |
|
|
|
|
Module : Text.Pandoc.Readers.HTML
|
2007-07-07 22:51:55 +00:00
|
|
|
Copyright : Copyright (C) 2006-7 John MacFarlane
|
2006-12-20 06:50:14 +00:00
|
|
|
License : GNU GPL, version 2 or above
|
|
|
|
|
2007-07-07 22:51:55 +00:00
|
|
|
Maintainer : John MacFarlane <jgm@berkeley.edu>
|
2006-12-20 20:20:10 +00:00
|
|
|
Stability : alpha
|
2006-12-20 06:50:14 +00:00
|
|
|
Portability : portable
|
|
|
|
|
|
|
|
Conversion of HTML to 'Pandoc' document.
|
|
|
|
-}
|
2006-10-17 14:22:29 +00:00
|
|
|
module Text.Pandoc.Readers.HTML (
|
|
|
|
readHtml,
|
|
|
|
rawHtmlInline,
|
|
|
|
rawHtmlBlock,
|
|
|
|
anyHtmlBlockTag,
|
2006-12-30 22:51:49 +00:00
|
|
|
anyHtmlInlineTag,
|
|
|
|
anyHtmlTag,
|
|
|
|
anyHtmlEndTag,
|
|
|
|
htmlEndTag,
|
|
|
|
extractTagType,
|
|
|
|
htmlBlockElement
|
2006-10-17 14:22:29 +00:00
|
|
|
) where
|
|
|
|
|
|
|
|
import Text.ParserCombinators.Parsec
|
2007-07-09 03:39:25 +00:00
|
|
|
import Text.Pandoc.ParserCombinators
|
2006-10-17 14:22:29 +00:00
|
|
|
import Text.Pandoc.Definition
|
|
|
|
import Text.Pandoc.Shared
|
Changes in entity handling:
+ Entities are parsed (and unicode characters returned) in both
Markdown and HTML readers.
+ Parsers characterEntity, namedEntity, decimalEntity, hexEntity added
to Entities.hs; these parse a string and return a unicode character.
+ Changed 'entity' parser in HTML reader to use the 'characterEntity'
parser from Entities.hs.
+ Added new 'entity' parser to Markdown reader, and added '&' as a
special character. Adjusted test suite accordingly since now we
get 'Str "AT",Str "&",Str "T"' instead of 'Str "AT&T"..
+ stringToSGML moved to Entities.hs. escapeSGML removed as redundant,
given encodeEntities.
+ stringToSGML, encodeEntities, and specialCharToEntity are given a
boolean parameter that causes only numerical entities to be used.
This is used in the docbook writer. The HTML writer uses named
entities where possible, but not all docbook-consumers know about
the named entities without special instructions, so it seems safer
to use numerical entities there.
+ decodeEntities is rewritten in a way that avoids Text.Regex, using
the new parsers.
+ charToEntity and charToNumericalEntity added to Entities.hs.
+ Moved specialCharToEntity from Shared.hs to Entities.hs.
+ Removed unneeded 'decodeEntities' from 'str' parser in HTML and
Markdown readers.
+ Removed sgmlHexEntity, sgmlDecimalEntity, sgmlNamedEntity, and
sgmlCharacterEntity from Shared.hs.
+ Modified Docbook writer so that it doesn't rely on Text.Regex for
detecting "mailto" links.
git-svn-id: https://pandoc.googlecode.com/svn/trunk@515 788f1e2b-df1e-0410-8736-df70ead52e1b
2007-01-27 03:04:40 +00:00
|
|
|
import Text.Pandoc.Entities ( characterEntity, decodeEntities )
|
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
2007-04-10 01:56:50 +00:00
|
|
|
import Data.Maybe ( fromMaybe )
|
2007-01-24 20:26:06 +00:00
|
|
|
import Data.List ( intersect, takeWhile, dropWhile )
|
2007-01-24 19:40:32 +00:00
|
|
|
import Data.Char ( toUpper, toLower, isAlphaNum )
|
2006-10-17 14:22:29 +00:00
|
|
|
|
|
|
|
-- | Convert HTML-formatted string to 'Pandoc' document.
|
|
|
|
readHtml :: ParserState -- ^ Parser state
|
|
|
|
-> String -- ^ String to parse
|
|
|
|
-> Pandoc
|
|
|
|
readHtml = readWith parseHtml
|
|
|
|
|
|
|
|
-- for testing
|
|
|
|
testString :: String -> IO ()
|
|
|
|
testString = testStringWith parseHtml
|
|
|
|
|
|
|
|
--
|
|
|
|
-- Constants
|
|
|
|
--
|
|
|
|
|
2006-12-20 06:50:14 +00:00
|
|
|
inlineHtmlTags = ["a", "abbr", "acronym", "b", "basefont", "bdo", "big",
|
|
|
|
"br", "cite", "code", "dfn", "em", "font", "i", "img",
|
|
|
|
"input", "kbd", "label", "q", "s", "samp", "select",
|
|
|
|
"small", "span", "strike", "strong", "sub", "sup",
|
|
|
|
"textarea", "tt", "u", "var"]
|
2006-10-17 14:22:29 +00:00
|
|
|
|
|
|
|
--
|
|
|
|
-- HTML utility functions
|
|
|
|
--
|
|
|
|
|
|
|
|
-- | Read blocks until end tag.
|
|
|
|
blocksTilEnd tag = try (do
|
|
|
|
blocks <- manyTill (do {b <- block; spaces; return b}) (htmlEndTag tag)
|
|
|
|
return blocks)
|
|
|
|
|
|
|
|
-- | Read inlines until end tag.
|
|
|
|
inlinesTilEnd tag = try (do
|
|
|
|
inlines <- manyTill inline (htmlEndTag tag)
|
|
|
|
return inlines)
|
|
|
|
|
2007-07-12 08:31:39 +00:00
|
|
|
-- | Extract type from a tag: e.g. @br@ from @\<br\>@
|
2007-01-24 17:43:39 +00:00
|
|
|
extractTagType :: String -> String
|
2007-01-24 20:26:06 +00:00
|
|
|
extractTagType ('<':rest) =
|
|
|
|
let isSpaceOrSlash c = c `elem` "/ \n\t" in
|
|
|
|
map toLower $ takeWhile isAlphaNum $ dropWhile isSpaceOrSlash rest
|
2007-01-24 17:43:39 +00:00
|
|
|
extractTagType _ = ""
|
2006-10-17 14:22:29 +00:00
|
|
|
|
2006-12-30 22:51:49 +00:00
|
|
|
-- | Parse any HTML tag (closing or opening) and return text of tag
|
2006-10-17 14:22:29 +00:00
|
|
|
anyHtmlTag = try (do
|
|
|
|
char '<'
|
|
|
|
spaces
|
|
|
|
tag <- many1 alphaNum
|
2006-12-30 22:51:49 +00:00
|
|
|
attribs <- htmlAttributes
|
2006-10-17 14:22:29 +00:00
|
|
|
spaces
|
|
|
|
ender <- option "" (string "/")
|
|
|
|
let ender' = if (null ender) then "" else " /"
|
|
|
|
spaces
|
|
|
|
char '>'
|
|
|
|
return ("<" ++ tag ++ attribs ++ ender' ++ ">"))
|
|
|
|
|
|
|
|
anyHtmlEndTag = try (do
|
|
|
|
char '<'
|
|
|
|
spaces
|
|
|
|
char '/'
|
|
|
|
spaces
|
|
|
|
tagType <- many1 alphaNum
|
|
|
|
spaces
|
|
|
|
char '>'
|
|
|
|
return ("</" ++ tagType ++ ">"))
|
|
|
|
|
|
|
|
htmlTag :: String -> GenParser Char st (String, [(String, String)])
|
|
|
|
htmlTag tag = try (do
|
|
|
|
char '<'
|
|
|
|
spaces
|
|
|
|
stringAnyCase tag
|
|
|
|
attribs <- many htmlAttribute
|
|
|
|
spaces
|
|
|
|
option "" (string "/")
|
|
|
|
spaces
|
|
|
|
char '>'
|
|
|
|
return (tag, (map (\(name, content, raw) -> (name, content)) attribs)))
|
|
|
|
|
|
|
|
-- parses a quoted html attribute value
|
|
|
|
quoted quoteChar = do
|
2006-12-20 06:50:14 +00:00
|
|
|
result <- between (char quoteChar) (char quoteChar)
|
|
|
|
(many (noneOf [quoteChar]))
|
2006-10-17 14:22:29 +00:00
|
|
|
return (result, [quoteChar])
|
|
|
|
|
|
|
|
htmlAttributes = do
|
|
|
|
attrList <- many htmlAttribute
|
|
|
|
return (concatMap (\(name, content, raw) -> raw) attrList)
|
|
|
|
|
|
|
|
htmlAttribute = htmlRegularAttribute <|> htmlMinimizedAttribute
|
|
|
|
|
|
|
|
-- minimized boolean attribute (no = and value)
|
|
|
|
htmlMinimizedAttribute = try (do
|
2007-01-24 19:07:35 +00:00
|
|
|
many1 space
|
2006-10-17 14:22:29 +00:00
|
|
|
name <- many1 (choice [letter, oneOf ".-_:"])
|
|
|
|
spaces
|
|
|
|
notFollowedBy (char '=')
|
|
|
|
let content = name
|
|
|
|
return (name, content, (" " ++ name)))
|
|
|
|
|
|
|
|
htmlRegularAttribute = try (do
|
2007-01-24 19:07:35 +00:00
|
|
|
many1 space
|
2006-10-17 14:22:29 +00:00
|
|
|
name <- many1 (choice [letter, oneOf ".-_:"])
|
|
|
|
spaces
|
|
|
|
char '='
|
|
|
|
spaces
|
|
|
|
(content, quoteStr) <- choice [ (quoted '\''),
|
|
|
|
(quoted '"'),
|
2006-12-20 06:50:14 +00:00
|
|
|
(do
|
|
|
|
a <- many (alphaNum <|> (oneOf "-._:"))
|
|
|
|
return (a,"")) ]
|
2006-12-30 22:51:49 +00:00
|
|
|
return (name, content,
|
2006-12-20 06:50:14 +00:00
|
|
|
(" " ++ name ++ "=" ++ quoteStr ++ content ++ quoteStr)))
|
2006-10-17 14:22:29 +00:00
|
|
|
|
2006-12-30 22:51:49 +00:00
|
|
|
-- | Parse an end tag of type 'tag'
|
2006-10-17 14:22:29 +00:00
|
|
|
htmlEndTag tag = try (do
|
|
|
|
char '<'
|
|
|
|
spaces
|
|
|
|
char '/'
|
|
|
|
spaces
|
|
|
|
stringAnyCase tag
|
|
|
|
spaces
|
|
|
|
char '>'
|
|
|
|
return ("</" ++ tag ++ ">"))
|
|
|
|
|
|
|
|
-- | Returns @True@ if the tag is an inline tag.
|
|
|
|
isInline tag = (extractTagType tag) `elem` inlineHtmlTags
|
|
|
|
|
|
|
|
anyHtmlBlockTag = try (do
|
|
|
|
tag <- choice [anyHtmlTag, anyHtmlEndTag]
|
2006-12-20 06:50:14 +00:00
|
|
|
if isInline tag then fail "inline tag" else return tag)
|
2006-10-17 14:22:29 +00:00
|
|
|
|
|
|
|
anyHtmlInlineTag = try (do
|
|
|
|
tag <- choice [ anyHtmlTag, anyHtmlEndTag ]
|
2006-12-20 06:50:14 +00:00
|
|
|
if isInline tag then return tag else fail "not an inline tag")
|
2006-10-17 14:22:29 +00:00
|
|
|
|
2006-12-30 22:51:49 +00:00
|
|
|
-- | Parses material between script tags.
|
|
|
|
-- Scripts must be treated differently, because they can contain '<>' etc.
|
2006-10-17 14:22:29 +00:00
|
|
|
htmlScript = try (do
|
|
|
|
open <- string "<script"
|
|
|
|
rest <- manyTill anyChar (htmlEndTag "script")
|
|
|
|
return (open ++ rest ++ "</script>"))
|
|
|
|
|
2006-12-30 22:51:49 +00:00
|
|
|
htmlBlockElement = choice [ htmlScript, htmlComment, xmlDec, definition ]
|
|
|
|
|
2006-11-26 07:01:37 +00:00
|
|
|
rawHtmlBlock = try (do
|
|
|
|
notFollowedBy' (choice [htmlTag "/body", htmlTag "/html"])
|
2006-12-30 22:51:49 +00:00
|
|
|
body <- htmlBlockElement <|> anyHtmlBlockTag
|
2006-10-17 14:22:29 +00:00
|
|
|
sp <- (many space)
|
|
|
|
state <- getState
|
2006-12-20 06:50:14 +00:00
|
|
|
if stateParseRaw state then return (RawHtml (body ++ sp)) else return Null)
|
2006-10-17 14:22:29 +00:00
|
|
|
|
2006-12-30 22:51:49 +00:00
|
|
|
-- | Parses an HTML comment.
|
2006-10-17 14:22:29 +00:00
|
|
|
htmlComment = try (do
|
|
|
|
string "<!--"
|
|
|
|
comment <- manyTill anyChar (try (string "-->"))
|
|
|
|
return ("<!--" ++ comment ++ "-->"))
|
|
|
|
|
|
|
|
--
|
|
|
|
-- parsing documents
|
|
|
|
--
|
|
|
|
|
|
|
|
xmlDec = try (do
|
|
|
|
string "<?"
|
|
|
|
rest <- manyTill anyChar (char '>')
|
|
|
|
return ("<?" ++ rest ++ ">"))
|
|
|
|
|
|
|
|
definition = try (do
|
|
|
|
string "<!"
|
|
|
|
rest <- manyTill anyChar (char '>')
|
|
|
|
return ("<!" ++ rest ++ ">"))
|
|
|
|
|
|
|
|
nonTitleNonHead = try (do
|
|
|
|
notFollowedBy' (htmlTag "title")
|
|
|
|
notFollowedBy' (htmlTag "/head")
|
|
|
|
result <- choice [do {rawHtmlBlock; return ' '}, anyChar]
|
|
|
|
return result)
|
|
|
|
|
|
|
|
parseTitle = try (do
|
|
|
|
(tag, attribs) <- htmlTag "title"
|
|
|
|
contents <- inlinesTilEnd tag
|
|
|
|
spaces
|
|
|
|
return contents)
|
|
|
|
|
|
|
|
-- parse header and return meta-information (for now, just title)
|
|
|
|
parseHead = try (do
|
|
|
|
htmlTag "head"
|
|
|
|
spaces
|
|
|
|
skipMany nonTitleNonHead
|
|
|
|
contents <- option [] parseTitle
|
|
|
|
skipMany nonTitleNonHead
|
|
|
|
htmlTag "/head"
|
|
|
|
return (contents, [], ""))
|
|
|
|
|
|
|
|
skipHtmlTag tag = option ("",[]) (htmlTag tag)
|
|
|
|
|
|
|
|
-- h1 class="title" representation of title in body
|
|
|
|
bodyTitle = try (do
|
|
|
|
(tag, attribs) <- htmlTag "h1"
|
|
|
|
cl <- case (extractAttribute "class" attribs) of
|
|
|
|
Just "title" -> do {return ""}
|
|
|
|
otherwise -> fail "not title"
|
|
|
|
inlinesTilEnd "h1"
|
|
|
|
return "")
|
|
|
|
|
|
|
|
parseHtml = do
|
|
|
|
sepEndBy (choice [xmlDec, definition, htmlComment]) spaces
|
|
|
|
skipHtmlTag "html"
|
|
|
|
spaces
|
|
|
|
(title, authors, date) <- option ([], [], "") parseHead
|
|
|
|
spaces
|
|
|
|
skipHtmlTag "body"
|
|
|
|
spaces
|
|
|
|
option "" bodyTitle -- skip title in body, because it's represented in meta
|
|
|
|
blocks <- parseBlocks
|
|
|
|
spaces
|
|
|
|
option "" (htmlEndTag "body")
|
|
|
|
spaces
|
|
|
|
option "" (htmlEndTag "html")
|
|
|
|
many anyChar -- ignore anything after </html>
|
|
|
|
eof
|
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
2007-04-10 01:56:50 +00:00
|
|
|
return (Pandoc (Meta title authors date) blocks)
|
2006-10-17 14:22:29 +00:00
|
|
|
|
|
|
|
--
|
|
|
|
-- parsing blocks
|
|
|
|
--
|
|
|
|
|
|
|
|
parseBlocks = do
|
|
|
|
spaces
|
|
|
|
result <- sepEndBy block spaces
|
|
|
|
return result
|
|
|
|
|
|
|
|
block = choice [ codeBlock, header, hrule, list, blockQuote, para, plain,
|
|
|
|
rawHtmlBlock ] <?> "block"
|
|
|
|
|
|
|
|
--
|
|
|
|
-- header blocks
|
|
|
|
--
|
|
|
|
|
|
|
|
header = choice (map headerLevel (enumFromTo 1 5)) <?> "header"
|
|
|
|
|
|
|
|
headerLevel n = try (do
|
|
|
|
let level = "h" ++ show n
|
|
|
|
(tag, attribs) <- htmlTag level
|
|
|
|
contents <- inlinesTilEnd level
|
|
|
|
return (Header n (normalizeSpaces contents)))
|
|
|
|
|
|
|
|
--
|
|
|
|
-- hrule block
|
|
|
|
--
|
|
|
|
|
|
|
|
hrule = try (do
|
|
|
|
(tag, attribs) <- htmlTag "hr"
|
|
|
|
state <- getState
|
2006-12-20 06:50:14 +00:00
|
|
|
if (not (null attribs)) && (stateParseRaw state)
|
|
|
|
then -- in this case we want to parse it as raw html
|
|
|
|
unexpected "attributes in hr"
|
|
|
|
else return HorizontalRule)
|
2006-10-17 14:22:29 +00:00
|
|
|
|
|
|
|
--
|
|
|
|
-- code blocks
|
|
|
|
--
|
|
|
|
|
|
|
|
codeBlock = choice [ preCodeBlock, bareCodeBlock ] <?> "code block"
|
|
|
|
|
|
|
|
preCodeBlock = try (do
|
|
|
|
htmlTag "pre"
|
|
|
|
spaces
|
|
|
|
htmlTag "code"
|
|
|
|
result <- manyTill anyChar (htmlEndTag "code")
|
|
|
|
spaces
|
|
|
|
htmlEndTag "pre"
|
2006-11-26 07:01:37 +00:00
|
|
|
return (CodeBlock (stripTrailingNewlines (decodeEntities result))))
|
2006-10-17 14:22:29 +00:00
|
|
|
|
|
|
|
bareCodeBlock = try (do
|
|
|
|
htmlTag "code"
|
|
|
|
result <- manyTill anyChar (htmlEndTag "code")
|
2006-11-26 07:01:37 +00:00
|
|
|
return (CodeBlock (stripTrailingNewlines (decodeEntities result))))
|
2006-10-17 14:22:29 +00:00
|
|
|
|
|
|
|
--
|
|
|
|
-- block quotes
|
|
|
|
--
|
|
|
|
|
|
|
|
blockQuote = try (do
|
|
|
|
tag <- htmlTag "blockquote"
|
|
|
|
spaces
|
|
|
|
blocks <- blocksTilEnd "blockquote"
|
|
|
|
return (BlockQuote blocks))
|
|
|
|
|
|
|
|
--
|
|
|
|
-- list blocks
|
|
|
|
--
|
|
|
|
|
|
|
|
list = choice [ bulletList, orderedList ] <?> "list"
|
|
|
|
|
|
|
|
orderedList = try (do
|
|
|
|
tag <- htmlTag "ol"
|
|
|
|
spaces
|
|
|
|
items <- sepEndBy1 listItem spaces
|
|
|
|
htmlEndTag "ol"
|
|
|
|
return (OrderedList items))
|
|
|
|
|
|
|
|
bulletList = try (do
|
|
|
|
tag <- htmlTag "ul"
|
|
|
|
spaces
|
|
|
|
items <- sepEndBy1 listItem spaces
|
|
|
|
htmlEndTag "ul"
|
|
|
|
return (BulletList items))
|
|
|
|
|
|
|
|
listItem = try (do
|
|
|
|
tag <- htmlTag "li"
|
|
|
|
spaces
|
|
|
|
blocks <- blocksTilEnd "li"
|
|
|
|
return blocks)
|
|
|
|
|
|
|
|
--
|
|
|
|
-- paragraph block
|
|
|
|
--
|
|
|
|
|
|
|
|
para = try (do
|
|
|
|
tag <- htmlTag "p"
|
|
|
|
result <- inlinesTilEnd "p"
|
|
|
|
return (Para (normalizeSpaces result)))
|
|
|
|
|
|
|
|
--
|
|
|
|
-- plain block
|
|
|
|
--
|
|
|
|
|
|
|
|
plain = do
|
|
|
|
result <- many1 inline
|
|
|
|
return (Plain (normalizeSpaces result))
|
|
|
|
|
|
|
|
--
|
|
|
|
-- inline
|
|
|
|
--
|
|
|
|
|
|
|
|
inline = choice [ text, special ] <?> "inline"
|
|
|
|
|
2007-07-21 22:54:40 +00:00
|
|
|
text = choice [ entity, strong, emph, superscript, subscript,
|
|
|
|
strikeout, spanStrikeout, code, str,
|
|
|
|
linebreak, whitespace ] <?> "text"
|
2006-10-17 14:22:29 +00:00
|
|
|
|
2006-12-20 06:50:14 +00:00
|
|
|
special = choice [ link, image, rawHtmlInline ] <?>
|
|
|
|
"link, inline html, or image"
|
2006-10-17 14:22:29 +00:00
|
|
|
|
Changes in entity handling:
+ Entities are parsed (and unicode characters returned) in both
Markdown and HTML readers.
+ Parsers characterEntity, namedEntity, decimalEntity, hexEntity added
to Entities.hs; these parse a string and return a unicode character.
+ Changed 'entity' parser in HTML reader to use the 'characterEntity'
parser from Entities.hs.
+ Added new 'entity' parser to Markdown reader, and added '&' as a
special character. Adjusted test suite accordingly since now we
get 'Str "AT",Str "&",Str "T"' instead of 'Str "AT&T"..
+ stringToSGML moved to Entities.hs. escapeSGML removed as redundant,
given encodeEntities.
+ stringToSGML, encodeEntities, and specialCharToEntity are given a
boolean parameter that causes only numerical entities to be used.
This is used in the docbook writer. The HTML writer uses named
entities where possible, but not all docbook-consumers know about
the named entities without special instructions, so it seems safer
to use numerical entities there.
+ decodeEntities is rewritten in a way that avoids Text.Regex, using
the new parsers.
+ charToEntity and charToNumericalEntity added to Entities.hs.
+ Moved specialCharToEntity from Shared.hs to Entities.hs.
+ Removed unneeded 'decodeEntities' from 'str' parser in HTML and
Markdown readers.
+ Removed sgmlHexEntity, sgmlDecimalEntity, sgmlNamedEntity, and
sgmlCharacterEntity from Shared.hs.
+ Modified Docbook writer so that it doesn't rely on Text.Regex for
detecting "mailto" links.
git-svn-id: https://pandoc.googlecode.com/svn/trunk@515 788f1e2b-df1e-0410-8736-df70ead52e1b
2007-01-27 03:04:40 +00:00
|
|
|
entity = do
|
|
|
|
ent <- characterEntity
|
|
|
|
return $ Str [ent]
|
2006-10-17 14:22:29 +00:00
|
|
|
|
|
|
|
code = try (do
|
|
|
|
htmlTag "code"
|
|
|
|
result <- manyTill anyChar (htmlEndTag "code")
|
2006-12-20 06:50:14 +00:00
|
|
|
-- remove internal line breaks, leading and trailing space,
|
|
|
|
-- and decode entities
|
|
|
|
let result' = decodeEntities $ removeLeadingTrailingSpace $
|
|
|
|
joinWithSep " " $ lines result
|
2006-10-17 14:22:29 +00:00
|
|
|
return (Code result'))
|
|
|
|
|
|
|
|
rawHtmlInline = do
|
|
|
|
result <- choice [htmlScript, anyHtmlInlineTag]
|
|
|
|
state <- getState
|
2006-12-20 06:50:14 +00:00
|
|
|
if stateParseRaw state then return (HtmlInline result) else return (Str "")
|
2006-10-17 14:22:29 +00:00
|
|
|
|
|
|
|
betweenTags tag = try (do
|
|
|
|
htmlTag tag
|
|
|
|
result <- inlinesTilEnd tag
|
|
|
|
return (normalizeSpaces result))
|
|
|
|
|
|
|
|
emph = try (do
|
|
|
|
result <- choice [betweenTags "em", betweenTags "it"]
|
|
|
|
return (Emph result))
|
|
|
|
|
2007-07-21 22:54:40 +00:00
|
|
|
superscript = try $ do
|
|
|
|
failIfStrict -- strict markdown has no superscript, so treat as raw HTML
|
|
|
|
result <- betweenTags "sup"
|
|
|
|
return (Superscript result)
|
|
|
|
|
|
|
|
subscript = try $ do
|
|
|
|
failIfStrict -- strict markdown has no subscript, so treat as raw HTML
|
|
|
|
result <- betweenTags "sub"
|
|
|
|
return (Subscript result)
|
|
|
|
|
|
|
|
strikeout = try $ do
|
|
|
|
failIfStrict -- strict markdown has no strikeout, so treat as raw HTML
|
|
|
|
result <- choice [betweenTags "s", betweenTags "strike"]
|
|
|
|
return (Strikeout result)
|
|
|
|
|
|
|
|
spanStrikeout = try $ do
|
|
|
|
failIfStrict -- strict markdown has no strikeout, so treat as raw HTML
|
|
|
|
(tag, attributes) <- htmlTag "span"
|
|
|
|
result <- case (extractAttribute "class" attributes) of
|
|
|
|
Just "strikeout" -> inlinesTilEnd "span"
|
|
|
|
Nothing -> fail "not a strikeout"
|
|
|
|
return (Strikeout result)
|
|
|
|
|
2006-10-17 14:22:29 +00:00
|
|
|
strong = try (do
|
|
|
|
result <- choice [betweenTags "b", betweenTags "strong"]
|
|
|
|
return (Strong result))
|
|
|
|
|
|
|
|
whitespace = do
|
|
|
|
many1 space
|
|
|
|
return Space
|
|
|
|
|
|
|
|
-- hard line break
|
|
|
|
linebreak = do
|
|
|
|
htmlTag "br"
|
2007-01-03 20:52:12 +00:00
|
|
|
option ' ' newline
|
2006-10-17 14:22:29 +00:00
|
|
|
return LineBreak
|
|
|
|
|
|
|
|
str = do
|
|
|
|
result <- many1 (noneOf "<& \t\n")
|
Changes in entity handling:
+ Entities are parsed (and unicode characters returned) in both
Markdown and HTML readers.
+ Parsers characterEntity, namedEntity, decimalEntity, hexEntity added
to Entities.hs; these parse a string and return a unicode character.
+ Changed 'entity' parser in HTML reader to use the 'characterEntity'
parser from Entities.hs.
+ Added new 'entity' parser to Markdown reader, and added '&' as a
special character. Adjusted test suite accordingly since now we
get 'Str "AT",Str "&",Str "T"' instead of 'Str "AT&T"..
+ stringToSGML moved to Entities.hs. escapeSGML removed as redundant,
given encodeEntities.
+ stringToSGML, encodeEntities, and specialCharToEntity are given a
boolean parameter that causes only numerical entities to be used.
This is used in the docbook writer. The HTML writer uses named
entities where possible, but not all docbook-consumers know about
the named entities without special instructions, so it seems safer
to use numerical entities there.
+ decodeEntities is rewritten in a way that avoids Text.Regex, using
the new parsers.
+ charToEntity and charToNumericalEntity added to Entities.hs.
+ Moved specialCharToEntity from Shared.hs to Entities.hs.
+ Removed unneeded 'decodeEntities' from 'str' parser in HTML and
Markdown readers.
+ Removed sgmlHexEntity, sgmlDecimalEntity, sgmlNamedEntity, and
sgmlCharacterEntity from Shared.hs.
+ Modified Docbook writer so that it doesn't rely on Text.Regex for
detecting "mailto" links.
git-svn-id: https://pandoc.googlecode.com/svn/trunk@515 788f1e2b-df1e-0410-8736-df70ead52e1b
2007-01-27 03:04:40 +00:00
|
|
|
return (Str result)
|
2006-10-17 14:22:29 +00:00
|
|
|
|
|
|
|
--
|
|
|
|
-- links and images
|
|
|
|
--
|
|
|
|
|
|
|
|
-- extract contents of attribute (attribute names are case-insensitive)
|
|
|
|
extractAttribute name [] = Nothing
|
|
|
|
extractAttribute name ((attrName, contents):rest) =
|
|
|
|
let name' = map toLower name
|
|
|
|
attrName' = map toLower attrName in
|
2007-01-28 00:04:43 +00:00
|
|
|
if (attrName' == name')
|
|
|
|
then Just (decodeEntities contents)
|
|
|
|
else extractAttribute name rest
|
2006-10-17 14:22:29 +00:00
|
|
|
|
2007-03-03 18:19:31 +00:00
|
|
|
link = try $ do
|
2006-10-17 14:22:29 +00:00
|
|
|
(tag, attributes) <- htmlTag "a"
|
|
|
|
url <- case (extractAttribute "href" attributes) of
|
|
|
|
Just url -> do {return url}
|
|
|
|
Nothing -> fail "no href"
|
|
|
|
let title = fromMaybe "" (extractAttribute "title" attributes)
|
|
|
|
label <- inlinesTilEnd "a"
|
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
2007-04-10 01:56:50 +00:00
|
|
|
return $ Link (normalizeSpaces label) (url, title)
|
2006-10-17 14:22:29 +00:00
|
|
|
|
2007-03-03 18:25:49 +00:00
|
|
|
image = try $ do
|
2006-10-17 14:22:29 +00:00
|
|
|
(tag, attributes) <- htmlTag "img"
|
|
|
|
url <- case (extractAttribute "src" attributes) of
|
|
|
|
Just url -> do {return url}
|
|
|
|
Nothing -> fail "no src"
|
|
|
|
let title = fromMaybe "" (extractAttribute "title" attributes)
|
|
|
|
let alt = fromMaybe "" (extractAttribute "alt" attributes)
|
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
2007-04-10 01:56:50 +00:00
|
|
|
return $ Image [Str alt] (url, title)
|
|
|
|
|