2006-12-20 21:54:23 +01:00
|
|
|
{-
|
|
|
|
Copyright (C) 2006 John MacFarlane <jgm at berkeley dot edu>
|
|
|
|
|
|
|
|
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 07:50:14 +01:00
|
|
|
{- |
|
|
|
|
Module : Text.Pandoc.Shared
|
|
|
|
Copyright : Copyright (C) 2006 John MacFarlane
|
|
|
|
License : GNU GPL, version 2 or above
|
|
|
|
|
|
|
|
Maintainer : John MacFarlane <jgm at berkeley dot edu>
|
2006-12-20 21:20:10 +01:00
|
|
|
Stability : alpha
|
2006-12-20 07:50:14 +01:00
|
|
|
Portability : portable
|
|
|
|
|
|
|
|
Utility functions and definitions used by the various Pandoc modules.
|
|
|
|
-}
|
2006-10-17 16:22:29 +02:00
|
|
|
module Text.Pandoc.Shared (
|
2007-01-04 23:52:16 +01:00
|
|
|
-- * List processing
|
|
|
|
splitBy,
|
2007-01-15 20:52:42 +01:00
|
|
|
splitByIndices,
|
2007-01-22 22:28:46 +01:00
|
|
|
substitute,
|
2006-10-17 16:22:29 +02:00
|
|
|
-- * Text processing
|
|
|
|
joinWithSep,
|
|
|
|
tabsToSpaces,
|
|
|
|
backslashEscape,
|
|
|
|
endsWith,
|
|
|
|
stripTrailingNewlines,
|
|
|
|
removeLeadingTrailingSpace,
|
|
|
|
removeLeadingSpace,
|
|
|
|
removeTrailingSpace,
|
2006-11-26 08:01:37 +01:00
|
|
|
stripFirstAndLast,
|
2006-10-17 16:22:29 +02:00
|
|
|
-- * Parsing
|
|
|
|
readWith,
|
|
|
|
testStringWith,
|
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 03:56:50 +02:00
|
|
|
Reference (..),
|
|
|
|
isNoteBlock,
|
|
|
|
isKeyBlock,
|
|
|
|
isLineClump,
|
2006-10-17 16:22:29 +02:00
|
|
|
HeaderType (..),
|
|
|
|
ParserContext (..),
|
2007-01-06 19:41:01 +01:00
|
|
|
QuoteContext (..),
|
2006-10-17 16:22:29 +02:00
|
|
|
ParserState (..),
|
|
|
|
defaultParserState,
|
|
|
|
-- * Native format prettyprinting
|
|
|
|
prettyPandoc,
|
|
|
|
-- * Pandoc block list processing
|
|
|
|
normalizeSpaces,
|
|
|
|
compactify,
|
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 03:56:50 +02:00
|
|
|
-- * Writer options
|
2006-10-17 16:22:29 +02:00
|
|
|
WriterOptions (..),
|
2007-02-21 02:22:08 +01:00
|
|
|
defaultWriterOptions,
|
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 03:56:50 +02:00
|
|
|
-- * Reference key lookup functions
|
2006-10-17 16:22:29 +02:00
|
|
|
KeyTable,
|
|
|
|
lookupKeySrc,
|
|
|
|
refsMatch,
|
|
|
|
) where
|
|
|
|
import Text.Pandoc.Definition
|
2007-01-25 00:25:27 +01:00
|
|
|
import Text.ParserCombinators.Parsec as Parsec
|
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 03:56:50 +02:00
|
|
|
import Text.Pandoc.Entities ( decodeEntities, escapeStringForXML )
|
2007-01-25 00:25:27 +01:00
|
|
|
import Text.PrettyPrint.HughesPJ as PP ( text, char, (<>),
|
|
|
|
($$), nest, Doc, isEmpty )
|
|
|
|
import Data.Char ( toLower, ord )
|
2007-01-22 22:28:46 +01:00
|
|
|
import Data.List ( find, groupBy, isPrefixOf )
|
2006-10-17 16:22:29 +02:00
|
|
|
|
|
|
|
-- | Parse a string with a given parser and state.
|
|
|
|
readWith :: GenParser Char ParserState a -- ^ parser
|
|
|
|
-> ParserState -- ^ initial state
|
|
|
|
-> String -- ^ input string
|
|
|
|
-> a
|
|
|
|
readWith parser state input =
|
|
|
|
case runParser parser state "source" input of
|
2006-12-20 07:50:14 +01:00
|
|
|
Left err -> error $ "\nError:\n" ++ show err
|
2006-10-17 16:22:29 +02:00
|
|
|
Right result -> result
|
|
|
|
|
|
|
|
-- | Parse a string with @parser@ (for testing).
|
|
|
|
testStringWith :: (Show a) =>
|
|
|
|
GenParser Char ParserState a
|
2006-12-20 07:50:14 +01:00
|
|
|
-> String
|
|
|
|
-> IO ()
|
|
|
|
testStringWith parser str = putStrLn $ show $
|
|
|
|
readWith parser defaultParserState str
|
2006-10-17 16:22:29 +02:00
|
|
|
|
|
|
|
data HeaderType
|
|
|
|
= SingleHeader Char -- ^ Single line of characters underneath
|
|
|
|
| DoubleHeader Char -- ^ Lines of characters above and below
|
|
|
|
deriving (Eq, Show)
|
|
|
|
|
|
|
|
data ParserContext
|
2006-12-21 10:02:06 +01:00
|
|
|
= ListItemState -- ^ Used when running parser on list item contents
|
2006-12-20 07:50:14 +01:00
|
|
|
| NullState -- ^ Default state
|
2006-10-17 16:22:29 +02:00
|
|
|
deriving (Eq, Show)
|
|
|
|
|
2007-01-06 19:41:01 +01:00
|
|
|
data QuoteContext
|
|
|
|
= InSingleQuote -- ^ Used when we're parsing inside single quotes
|
|
|
|
| InDoubleQuote -- ^ Used when we're parsing inside double quotes
|
|
|
|
| NoQuote -- ^ Used when we're not parsing inside quotes
|
|
|
|
deriving (Eq, Show)
|
|
|
|
|
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 03:56:50 +02:00
|
|
|
type KeyTable = [([Inline], Target)]
|
|
|
|
|
|
|
|
type NoteTable = [(String, [Block])]
|
|
|
|
|
|
|
|
-- | References from preliminary parsing
|
|
|
|
data Reference
|
|
|
|
= KeyBlock [Inline] Target -- ^ Key for reference-style link (label URL title)
|
|
|
|
| NoteBlock String [Block] -- ^ Footnote reference and contents
|
|
|
|
| LineClump String -- ^ Raw clump of lines with blanks at end
|
|
|
|
deriving (Eq, Read, Show)
|
|
|
|
|
|
|
|
-- | Auxiliary functions used in preliminary parsing
|
|
|
|
isNoteBlock :: Reference -> Bool
|
|
|
|
isNoteBlock (NoteBlock _ _) = True
|
|
|
|
isNoteBlock _ = False
|
|
|
|
|
|
|
|
isKeyBlock :: Reference -> Bool
|
|
|
|
isKeyBlock (KeyBlock _ _) = True
|
|
|
|
isKeyBlock _ = False
|
|
|
|
|
|
|
|
isLineClump :: Reference -> Bool
|
|
|
|
isLineClump (LineClump _) = True
|
|
|
|
isLineClump _ = False
|
|
|
|
|
2006-10-17 16:22:29 +02:00
|
|
|
data ParserState = ParserState
|
2006-12-20 07:50:14 +01:00
|
|
|
{ stateParseRaw :: Bool, -- ^ Parse untranslatable HTML
|
|
|
|
-- and LaTeX?
|
|
|
|
stateParserContext :: ParserContext, -- ^ What are we parsing?
|
2007-01-06 19:41:01 +01:00
|
|
|
stateQuoteContext :: QuoteContext, -- ^ Inside quoted environment?
|
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 03:56:50 +02:00
|
|
|
stateKeys :: KeyTable, -- ^ List of reference keys
|
|
|
|
stateNotes :: NoteTable, -- ^ List of notes
|
2006-12-20 07:50:14 +01:00
|
|
|
stateTabStop :: Int, -- ^ Tab stop
|
|
|
|
stateStandalone :: Bool, -- ^ If @True@, parse
|
|
|
|
-- bibliographic info
|
|
|
|
stateTitle :: [Inline], -- ^ Title of document
|
|
|
|
stateAuthors :: [String], -- ^ Authors of document
|
|
|
|
stateDate :: String, -- ^ Date of document
|
2006-12-30 23:51:49 +01:00
|
|
|
stateStrict :: Bool, -- ^ Use strict markdown syntax
|
2007-01-06 10:54:58 +01:00
|
|
|
stateSmart :: Bool, -- ^ Use smart typography
|
2007-01-15 20:52:42 +01:00
|
|
|
stateColumns :: Int, -- ^ Number of columns in
|
|
|
|
-- terminal (used for tables)
|
2006-12-20 07:50:14 +01:00
|
|
|
stateHeaderTable :: [HeaderType] -- ^ List of header types used,
|
|
|
|
-- in what order (rst only)
|
2006-11-08 18:50:09 +01:00
|
|
|
}
|
2006-10-17 16:22:29 +02:00
|
|
|
deriving Show
|
|
|
|
|
|
|
|
defaultParserState :: ParserState
|
|
|
|
defaultParserState =
|
2006-12-20 00:13:03 +01:00
|
|
|
ParserState { stateParseRaw = False,
|
|
|
|
stateParserContext = NullState,
|
2007-01-06 19:41:01 +01:00
|
|
|
stateQuoteContext = NoQuote,
|
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 03:56:50 +02:00
|
|
|
stateKeys = [],
|
|
|
|
stateNotes = [],
|
2006-12-20 00:13:03 +01:00
|
|
|
stateTabStop = 4,
|
|
|
|
stateStandalone = False,
|
|
|
|
stateTitle = [],
|
|
|
|
stateAuthors = [],
|
|
|
|
stateDate = [],
|
2006-12-30 23:51:49 +01:00
|
|
|
stateStrict = False,
|
2007-01-06 10:54:58 +01:00
|
|
|
stateSmart = False,
|
2007-01-15 20:52:42 +01:00
|
|
|
stateColumns = 80,
|
2006-12-20 00:13:03 +01:00
|
|
|
stateHeaderTable = [] }
|
2006-10-17 16:22:29 +02:00
|
|
|
|
|
|
|
-- | Indent string as a block.
|
2006-12-20 07:50:14 +01:00
|
|
|
indentBy :: Int -- ^ Number of spaces to indent the block
|
|
|
|
-> Int -- ^ Number of spaces (rel to block) to indent first line
|
|
|
|
-> String -- ^ Contents of block to indent
|
2006-10-17 16:22:29 +02:00
|
|
|
-> String
|
|
|
|
indentBy num first [] = ""
|
|
|
|
indentBy num first str =
|
|
|
|
let (firstLine:restLines) = lines str
|
|
|
|
firstLineIndent = num + first in
|
|
|
|
(replicate firstLineIndent ' ') ++ firstLine ++ "\n" ++ (joinWithSep "\n" $ map (\line -> (replicate num ' ') ++ line) restLines)
|
|
|
|
|
|
|
|
-- | Prettyprint list of Pandoc blocks elements.
|
|
|
|
prettyBlockList :: Int -- ^ Number of spaces to indent list of blocks
|
|
|
|
-> [Block] -- ^ List of blocks
|
|
|
|
-> String
|
|
|
|
prettyBlockList indent [] = indentBy indent 0 "[]"
|
2006-12-20 07:50:14 +01:00
|
|
|
prettyBlockList indent blocks = indentBy indent (-2) $ "[ " ++
|
|
|
|
(joinWithSep "\n, " (map prettyBlock blocks)) ++ " ]"
|
2006-10-17 16:22:29 +02:00
|
|
|
|
|
|
|
-- | Prettyprint Pandoc block element.
|
|
|
|
prettyBlock :: Block -> String
|
2006-12-20 07:50:14 +01:00
|
|
|
prettyBlock (BlockQuote blocks) = "BlockQuote\n " ++
|
|
|
|
(prettyBlockList 2 blocks)
|
|
|
|
prettyBlock (OrderedList blockLists) =
|
|
|
|
"OrderedList\n" ++ indentBy 2 0 ("[ " ++ (joinWithSep ", "
|
|
|
|
(map (\blocks -> prettyBlockList 2 blocks) blockLists))) ++ " ]"
|
|
|
|
prettyBlock (BulletList blockLists) = "BulletList\n" ++
|
|
|
|
indentBy 2 0 ("[ " ++ (joinWithSep ", "
|
|
|
|
(map (\blocks -> prettyBlockList 2 blocks) blockLists))) ++ " ]"
|
2007-03-10 18:45:00 +01:00
|
|
|
prettyBlock (DefinitionList blockLists) = "DefinitionList\n" ++
|
|
|
|
indentBy 2 0 ("[ " ++ (joinWithSep ", "
|
|
|
|
(map (\(term, blocks) -> "(" ++ show term ++ ",\n" ++
|
|
|
|
indentBy 1 2 (prettyBlockList 2 blocks) ++ ")") blockLists))) ++ " ]"
|
2006-10-17 16:22:29 +02:00
|
|
|
prettyBlock block = show block
|
|
|
|
|
|
|
|
-- | Prettyprint Pandoc document.
|
|
|
|
prettyPandoc :: Pandoc -> String
|
2006-12-20 07:50:14 +01:00
|
|
|
prettyPandoc (Pandoc meta blocks) = "Pandoc " ++ "(" ++ (show meta) ++
|
|
|
|
")\n" ++ (prettyBlockList 0 blocks) ++ "\n"
|
2006-10-17 16:22:29 +02:00
|
|
|
|
|
|
|
-- | Convert tabs to spaces (with adjustable tab stop).
|
|
|
|
tabsToSpaces :: Int -- ^ Tabstop
|
|
|
|
-> String -- ^ String to convert
|
|
|
|
-> String
|
|
|
|
tabsToSpaces tabstop str =
|
|
|
|
unlines (map (tabsInLine tabstop tabstop) (lines str))
|
|
|
|
|
|
|
|
-- | Convert tabs to spaces in one line.
|
|
|
|
tabsInLine :: Int -- ^ Number of spaces to next tab stop
|
|
|
|
-> Int -- ^ Tabstop
|
|
|
|
-> String -- ^ Line to convert
|
|
|
|
-> String
|
|
|
|
tabsInLine num tabstop "" = ""
|
|
|
|
tabsInLine num tabstop (c:cs) =
|
|
|
|
let replacement = (if (c == '\t') then (replicate num ' ') else [c]) in
|
|
|
|
let nextnumraw = (num - (length replacement)) in
|
2006-12-20 07:50:14 +01:00
|
|
|
let nextnum = if (nextnumraw < 1)
|
|
|
|
then (nextnumraw + tabstop)
|
|
|
|
else nextnumraw in
|
2006-10-17 16:22:29 +02:00
|
|
|
replacement ++ (tabsInLine nextnum tabstop cs)
|
|
|
|
|
|
|
|
-- | Escape designated characters with backslash.
|
|
|
|
backslashEscape :: [Char] -- ^ list of special characters to escape
|
|
|
|
-> String -- ^ string input
|
|
|
|
-> String
|
|
|
|
backslashEscape special [] = []
|
2006-12-20 07:50:14 +01:00
|
|
|
backslashEscape special (x:xs) = if x `elem` special
|
|
|
|
then '\\':x:(backslashEscape special xs)
|
|
|
|
else x:(backslashEscape special xs)
|
2006-10-17 16:22:29 +02:00
|
|
|
|
|
|
|
-- | Returns @True@ if string ends with given character.
|
|
|
|
endsWith :: Char -> [Char] -> Bool
|
|
|
|
endsWith char [] = False
|
|
|
|
endsWith char str = (char == last str)
|
|
|
|
|
|
|
|
-- | Joins a list of lists, separated by another list.
|
|
|
|
joinWithSep :: [a] -- ^ List to use as separator
|
|
|
|
-> [[a]] -- ^ Lists to join
|
|
|
|
-> [a]
|
|
|
|
joinWithSep sep [] = []
|
|
|
|
joinWithSep sep lst = foldr1 (\a b -> a ++ sep ++ b) lst
|
|
|
|
|
|
|
|
-- | Strip trailing newlines from string.
|
|
|
|
stripTrailingNewlines :: String -> String
|
|
|
|
stripTrailingNewlines "" = ""
|
|
|
|
stripTrailingNewlines str =
|
2006-12-20 07:50:14 +01:00
|
|
|
if (last str) == '\n'
|
|
|
|
then stripTrailingNewlines (init str)
|
|
|
|
else str
|
2006-10-17 16:22:29 +02:00
|
|
|
|
|
|
|
-- | Remove leading and trailing space (including newlines) from string.
|
|
|
|
removeLeadingTrailingSpace :: String -> String
|
|
|
|
removeLeadingTrailingSpace = removeLeadingSpace . removeTrailingSpace
|
|
|
|
|
|
|
|
-- | Remove leading space (including newlines) from string.
|
|
|
|
removeLeadingSpace :: String -> String
|
2006-12-20 07:50:14 +01:00
|
|
|
removeLeadingSpace = dropWhile (\x -> (x == ' ') || (x == '\n') ||
|
|
|
|
(x == '\t'))
|
2006-10-17 16:22:29 +02:00
|
|
|
|
|
|
|
-- | Remove trailing space (including newlines) from string.
|
|
|
|
removeTrailingSpace :: String -> String
|
|
|
|
removeTrailingSpace = reverse . removeLeadingSpace . reverse
|
|
|
|
|
2006-11-26 08:01:37 +01:00
|
|
|
-- | Strip leading and trailing characters from string
|
|
|
|
stripFirstAndLast str =
|
|
|
|
drop 1 $ take ((length str) - 1) str
|
|
|
|
|
2007-01-22 22:28:46 +01:00
|
|
|
-- | Replace each occurrence of one sublist in a list with another.
|
|
|
|
substitute :: (Eq a) => [a] -> [a] -> [a] -> [a]
|
|
|
|
substitute _ _ [] = []
|
|
|
|
substitute [] _ lst = lst
|
|
|
|
substitute target replacement lst =
|
|
|
|
if isPrefixOf target lst
|
|
|
|
then replacement ++ (substitute target replacement $ drop (length target) lst)
|
|
|
|
else (head lst):(substitute target replacement $ tail lst)
|
|
|
|
|
2007-01-04 02:04:56 +01:00
|
|
|
-- | Split list into groups separated by sep.
|
|
|
|
splitBy :: (Eq a) => a -> [a] -> [[a]]
|
|
|
|
splitBy _ [] = []
|
|
|
|
splitBy sep lst =
|
|
|
|
let (first, rest) = break (== sep) lst
|
|
|
|
rest' = dropWhile (== sep) rest in
|
|
|
|
first:(splitBy sep rest')
|
2006-10-17 16:22:29 +02:00
|
|
|
|
2007-01-15 20:52:42 +01:00
|
|
|
-- | Split list into chunks divided at specified indices.
|
|
|
|
splitByIndices :: [Int] -> [a] -> [[a]]
|
|
|
|
splitByIndices [] lst = [lst]
|
|
|
|
splitByIndices (x:xs) lst =
|
|
|
|
let (first, rest) = splitAt x lst in
|
|
|
|
first:(splitByIndices (map (\y -> y - x) xs) rest)
|
|
|
|
|
2006-10-17 16:22:29 +02:00
|
|
|
-- | Normalize a list of inline elements: remove leading and trailing
|
2007-01-24 09:14:43 +01:00
|
|
|
-- @Space@ elements, collapse double @Space@s into singles, and
|
|
|
|
-- remove empty Str elements.
|
2006-10-17 16:22:29 +02:00
|
|
|
normalizeSpaces :: [Inline] -> [Inline]
|
|
|
|
normalizeSpaces [] = []
|
|
|
|
normalizeSpaces list =
|
|
|
|
let removeDoubles [] = []
|
|
|
|
removeDoubles (Space:Space:rest) = removeDoubles (Space:rest)
|
2007-01-24 09:14:43 +01:00
|
|
|
removeDoubles ((Str ""):rest) = removeDoubles rest
|
2006-10-17 16:22:29 +02:00
|
|
|
removeDoubles (x:rest) = x:(removeDoubles rest) in
|
|
|
|
let removeLeading [] = []
|
2006-12-20 07:50:14 +01:00
|
|
|
removeLeading lst = if ((head lst) == Space)
|
|
|
|
then tail lst
|
|
|
|
else lst in
|
2006-10-17 16:22:29 +02:00
|
|
|
let removeTrailing [] = []
|
2006-12-20 07:50:14 +01:00
|
|
|
removeTrailing lst = if ((last lst) == Space)
|
|
|
|
then init lst
|
|
|
|
else lst in
|
2006-10-17 16:22:29 +02:00
|
|
|
removeLeading $ removeTrailing $ removeDoubles list
|
|
|
|
|
2006-12-20 07:50:14 +01:00
|
|
|
-- | Change final list item from @Para@ to @Plain@ if the list should
|
|
|
|
-- be compact.
|
2006-10-17 16:22:29 +02:00
|
|
|
compactify :: [[Block]] -- ^ List of list items (each a list of blocks)
|
|
|
|
-> [[Block]]
|
|
|
|
compactify [] = []
|
|
|
|
compactify items =
|
|
|
|
let final = last items
|
|
|
|
others = init items in
|
|
|
|
case final of
|
2006-12-20 07:50:14 +01:00
|
|
|
[Para a] -> if any containsPara others
|
|
|
|
then items
|
|
|
|
else others ++ [[Plain a]]
|
2006-10-17 16:22:29 +02:00
|
|
|
otherwise -> items
|
|
|
|
|
|
|
|
containsPara :: [Block] -> Bool
|
|
|
|
containsPara [] = False
|
|
|
|
containsPara ((Para a):rest) = True
|
2006-12-20 07:50:14 +01:00
|
|
|
containsPara ((BulletList items):rest) = (any containsPara items) ||
|
|
|
|
(containsPara rest)
|
|
|
|
containsPara ((OrderedList items):rest) = (any containsPara items) ||
|
|
|
|
(containsPara rest)
|
2006-10-17 16:22:29 +02:00
|
|
|
containsPara (x:rest) = containsPara rest
|
|
|
|
|
|
|
|
-- | Options for writers
|
|
|
|
data WriterOptions = WriterOptions
|
2007-01-01 22:08:12 +01:00
|
|
|
{ writerStandalone :: Bool -- ^ Include header and footer
|
2006-12-20 07:50:14 +01:00
|
|
|
, writerTitlePrefix :: String -- ^ Prefix for HTML titles
|
|
|
|
, writerHeader :: String -- ^ Header for the document
|
|
|
|
, writerIncludeBefore :: String -- ^ String to include before the body
|
|
|
|
, writerIncludeAfter :: String -- ^ String to include after the body
|
2006-12-30 23:51:49 +01:00
|
|
|
, writerS5 :: Bool -- ^ We're writing S5
|
|
|
|
, writerIncremental :: Bool -- ^ Incremental S5 lists
|
|
|
|
, writerNumberSections :: Bool -- ^ Number sections in LaTeX
|
|
|
|
, writerStrictMarkdown :: Bool -- ^ Use strict markdown syntax
|
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 03:56:50 +02:00
|
|
|
, writerReferenceLinks :: Bool -- ^ Use reference links in writing markdown, rst
|
2006-12-20 07:50:14 +01:00
|
|
|
, writerTabStop :: Int -- ^ Tabstop for conversion between
|
|
|
|
-- spaces and tabs
|
|
|
|
} deriving Show
|
2006-10-17 16:22:29 +02:00
|
|
|
|
2007-02-21 02:22:08 +01:00
|
|
|
-- | Default writer options.
|
|
|
|
defaultWriterOptions =
|
|
|
|
WriterOptions { writerStandalone = True,
|
|
|
|
writerHeader = "",
|
|
|
|
writerTitlePrefix = "",
|
|
|
|
writerTabStop = 4,
|
|
|
|
writerS5 = False,
|
|
|
|
writerIncremental = False,
|
|
|
|
writerNumberSections = False,
|
|
|
|
writerIncludeBefore = "",
|
|
|
|
writerIncludeAfter = "",
|
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 03:56:50 +02:00
|
|
|
writerStrictMarkdown = False,
|
|
|
|
writerReferenceLinks = False }
|
2006-10-17 16:22:29 +02:00
|
|
|
|
|
|
|
--
|
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 03:56:50 +02:00
|
|
|
-- code to lookup reference keys in key table
|
2006-10-17 16:22:29 +02:00
|
|
|
--
|
|
|
|
|
|
|
|
-- | Look up key in key table and return target object.
|
|
|
|
lookupKeySrc :: KeyTable -- ^ Key table
|
|
|
|
-> [Inline] -- ^ Key
|
|
|
|
-> Maybe Target
|
|
|
|
lookupKeySrc table key = case table of
|
2006-12-20 07:50:14 +01:00
|
|
|
[] -> Nothing
|
|
|
|
(k, src):rest -> if (refsMatch k key)
|
|
|
|
then Just src
|
|
|
|
else lookupKeySrc rest key
|
2006-10-17 16:22:29 +02:00
|
|
|
|
|
|
|
-- | Returns @True@ if keys match (case insensitive).
|
|
|
|
refsMatch :: [Inline] -> [Inline] -> Bool
|
2006-12-20 07:50:14 +01:00
|
|
|
refsMatch ((Str x):restx) ((Str y):resty) =
|
|
|
|
((map toLower x) == (map toLower y)) && refsMatch restx resty
|
|
|
|
refsMatch ((Code x):restx) ((Code y):resty) =
|
|
|
|
((map toLower x) == (map toLower y)) && refsMatch restx resty
|
|
|
|
refsMatch ((TeX x):restx) ((TeX y):resty) =
|
|
|
|
((map toLower x) == (map toLower y)) && refsMatch restx resty
|
|
|
|
refsMatch ((HtmlInline x):restx) ((HtmlInline y):resty) =
|
|
|
|
((map toLower x) == (map toLower y)) && refsMatch restx resty
|
|
|
|
refsMatch ((Emph x):restx) ((Emph y):resty) =
|
|
|
|
refsMatch x y && refsMatch restx resty
|
|
|
|
refsMatch ((Strong x):restx) ((Strong y):resty) =
|
|
|
|
refsMatch x y && refsMatch restx resty
|
2007-01-06 10:54:58 +01:00
|
|
|
refsMatch ((Quoted t x):restx) ((Quoted u y):resty) =
|
|
|
|
t == u && refsMatch x y && refsMatch restx resty
|
2006-10-17 16:22:29 +02:00
|
|
|
refsMatch (x:restx) (y:resty) = (x == y) && refsMatch restx resty
|
|
|
|
refsMatch [] x = null x
|
|
|
|
refsMatch x [] = null x
|
|
|
|
|