git.iko.soy/typeable/pandoc
on master help sign in
Implemented @ for sequentially numbered examples.

Also implemented (@label) for example labels and references.

commit6972c0b5b040f3de998d936e6ddf8580ecfa7b30 authorJohn MacFarlane <jgm@berkeley.edu> dateWed 24 Mar 2010, 10:51 (17 years ago) parentdb31e06
9 files changed+75−11
README+27−1 src/Text/Pandoc/Definition.hs+1 src/Text/Pandoc/Readers/Markdown.hs+12−1 src/Text/Pandoc/Readers/RST.hs+1−1 src/Text/Pandoc/Shared.hs+28−5 src/Text/Pandoc/Writers/ConTeXt.hs+1 src/Text/Pandoc/Writers/Docbook.hs+1 src/Text/Pandoc/Writers/Markdown.hs+3−3 src/Text/Pandoc/Writers/Texinfo.hs+1
README+27−1
@@ -667,12 +667,38 @@ So, the following yields a list numbered sequentially starting from 2:
667667 1. Four
668668 * Five
669669
670−If default list markers are desired, use '`#.`':
670+If default list markers are desired, use `#.`:
671671
672672 #. one
673673 #. two
674674 #. three
675675
676+Numbered examples
677+-----------------
678+
679+The special list marker `@` can be used for sequentially numbered
680+examples. The first list item with a `@` marker will be numbered '1',
681+the next '2', and so on, throughout the document. The numbered examples
682+need not occur in a single list; each new list using `@` will take up
683+where the last stopped. So, for example:
684+
685+ (@) My first example will be numbered (1).
686+ (@) My second example will be numbered (2).
687+
688+ Explanation of examples.
689+
690+ (@) My third example will be numbered (3).
691+
692+Numbered examples can be labeled and referred to later in the
693+document:
694+
695+ (@good) This is a good example.
696+
697+ As (@good) illustrates, ...
698+
699+The label can be any string of alphanumeric characters, underscores,
700+or hyphens. The example must occur before the reference.
701+
676702Definition lists
677703----------------
678704
src/Text/Pandoc/Definition.hs+1
@@ -52,6 +52,7 @@ type ListAttributes = (Int, ListNumberStyle, ListNumberDelim)
5252
5353-- | Style of list numbers.
5454data ListNumberStyle = DefaultStyle
55+ | Example
5556 | Decimal
5657 | LowerRoman
5758 | UpperRoman
src/Text/Pandoc/Readers/Markdown.hs+12−1
@@ -35,6 +35,7 @@ import Data.List ( transpose, isSuffixOf, sortBy, findIndex, intercalate )
3535import Data.Ord ( comparing )
3636import Data.Char ( isAlphaNum )
3737import Data.Maybe
38+import qualified Data.Map as M
3839import Text.Pandoc.Definition
3940import Text.Pandoc.Shared
4041import Text.Pandoc.Readers.LaTeX ( rawLaTeXInline, rawLaTeXEnvironment' )
@@ -67,7 +68,7 @@ setextHChars = "=-"
6768
6869-- treat these as potentially non-text when parsing inline:
6970specialChars :: [Char]
70−specialChars = "\\[]*_~`<>$!^-.&'\"\8216\8217\8220\8221;"
71+specialChars = "\\[]*_~`<>$!^-.&@'\"\8216\8217\8220\8221;"
7172
7273--
7374-- auxiliary functions
@@ -915,6 +916,7 @@ inlineParsers = [ str
915916 , rawHtmlInline'
916917 , rawLaTeXInline'
917918 , escapedChar
919+ , exampleRef
918920 , symbol
919921 , ltSign ]
920922
@@ -950,6 +952,15 @@ ltSign = do
950952specialCharsMinusLt :: [Char]
951953specialCharsMinusLt = filter (/= '<') specialChars
952954
955+exampleRef :: GenParser Char ParserState Inline
956+exampleRef = try $ do
957+ char '@'
958+ lab <- many1 (alphaNum <|> oneOf "-_")
959+ examples <- liftM stateExamples getState
960+ case M.lookup lab examples of
961+ Just num -> return (Str $ show num)
962+ Nothing -> pzero
963+
953964symbol :: GenParser Char ParserState Inline
954965symbol = do
955966 result <- oneOf specialCharsMinusLt
src/Text/Pandoc/Readers/RST.hs+1−1
@@ -425,7 +425,7 @@ bulletListStart = try $ do
425425-- parses ordered list start and returns its length (inc following whitespace)
426426orderedListStart :: ListNumberStyle
427427 -> ListNumberDelim
428− -> GenParser Char st Int
428+ -> GenParser Char ParserState Int
429429orderedListStart style delim = try $ do
430430 (_, markerLen) <- withHorizDisplacement (orderedListMarker style delim)
431431 white <- many1 spaceChar
src/Text/Pandoc/Shared.hs+28−5
@@ -576,6 +576,23 @@ decimal = do
576576 num <- many1 digit
577577 return (Decimal, read num)
578578
579+-- | Parses a '@' and optional label and
580+-- returns (DefaultStyle, [next example number]). The next
581+-- example number is incremented in parser state, and the label
582+-- (if present) is added to the label table.
583+exampleNum :: GenParser Char ParserState (ListNumberStyle, Int)
584+exampleNum = do
585+ char '@'
586+ lab <- many (alphaNum <|> oneOf "_-")
587+ st <- getState
588+ let num = stateNextExample st
589+ let newlabels = if null lab
590+ then stateExamples st
591+ else M.insert lab num $ stateExamples st
592+ updateState $ \s -> s{ stateNextExample = num + 1
593+ , stateExamples = newlabels }
594+ return (Example, num)
595+
579596-- | Parses a '#' returns (DefaultStyle, 1).
580597defaultNum :: GenParser Char st (ListNumberStyle, Int)
581598defaultNum = do
@@ -600,10 +617,10 @@ romanOne = (char 'i' >> return (LowerRoman, 1)) <|>
600617 (char 'I' >> return (UpperRoman, 1))
601618
602619-- | Parses an ordered list marker and returns list attributes.
603−anyOrderedListMarker :: GenParser Char st ListAttributes
620+anyOrderedListMarker :: GenParser Char ParserState ListAttributes
604621anyOrderedListMarker = choice $
605622 [delimParser numParser | delimParser <- [inPeriod, inOneParen, inTwoParens],
606− numParser <- [decimal, defaultNum, romanOne,
623+ numParser <- [decimal, exampleNum, defaultNum, romanOne,
607624 lowerAlpha, lowerRoman, upperAlpha, upperRoman]]
608625
609626-- | Parses a list number (num) followed by a period, returns list attributes.
@@ -638,11 +655,12 @@ inTwoParens num = try $ do
638655-- returns number.
639656orderedListMarker :: ListNumberStyle
640657 -> ListNumberDelim
641− -> GenParser Char st Int
658+ -> GenParser Char ParserState Int
642659orderedListMarker style delim = do
643660 let num = defaultNum <|> -- # can continue any kind of list
644661 case style of
645662 DefaultStyle -> decimal
663+ Example -> exampleNum
646664 Decimal -> decimal
647665 UpperRoman -> upperRoman
648666 LowerRoman -> lowerRoman
@@ -700,7 +718,9 @@ data ParserState = ParserState
700718 stateLiterateHaskell :: Bool, -- ^ Treat input as literate haskell
701719 stateColumns :: Int, -- ^ Number of columns in terminal
702720 stateHeaderTable :: [HeaderType], -- ^ Ordered list of header types used
703− stateIndentedCodeClasses :: [String] -- ^ Classes to use for indented code blocks
721+ stateIndentedCodeClasses :: [String], -- ^ Classes to use for indented code blocks
722+ stateNextExample :: Int, -- ^ Number of next example
723+ stateExamples :: M.Map String Int -- ^ Map from example labels to numbers
704724 }
705725 deriving Show
706726
@@ -725,7 +745,9 @@ defaultParserState =
725745 stateLiterateHaskell = False,
726746 stateColumns = 80,
727747 stateHeaderTable = [],
728− stateIndentedCodeClasses = [] }
748+ stateIndentedCodeClasses = [],
749+ stateNextExample = 1,
750+ stateExamples = M.empty }
729751
730752data HeaderType
731753 = SingleHeader Char -- ^ Single line of characters underneath
@@ -855,6 +877,7 @@ orderedListMarkers (start, numstyle, numdelim) =
855877 let singleton c = [c]
856878 nums = case numstyle of
857879 DefaultStyle -> map show [start..]
880+ Example -> map show [start..]
858881 Decimal -> map show [start..]
859882 UpperAlpha -> drop (start - 1) $ cycle $
860883 map singleton ['A'..'Z']
src/Text/Pandoc/Writers/ConTeXt.hs+1
@@ -153,6 +153,7 @@ blockToConTeXt (OrderedList (start, style', delim) lst) = do
153153 let style'' = case style' of
154154 DefaultStyle -> orderedListStyles !! level
155155 Decimal -> "[n]"
156+ Example -> "[n]"
156157 LowerRoman -> "[r]"
157158 UpperRoman -> "[R]"
158159 LowerAlpha -> "[a]"
src/Text/Pandoc/Writers/Docbook.hs+1
@@ -154,6 +154,7 @@ blockToDocbook opts (OrderedList (start, numstyle, _) (first:rest)) =
154154 let attribs = case numstyle of
155155 DefaultStyle -> []
156156 Decimal -> [("numeration", "arabic")]
157+ Example -> [("numeration", "arabic")]
157158 UpperAlpha -> [("numeration", "upperalpha")]
158159 LowerAlpha -> [("numeration", "loweralpha")]
159160 UpperRoman -> [("numeration", "upperroman")]
src/Text/Pandoc/Writers/Markdown.hs+3−3
@@ -34,7 +34,7 @@ import Text.Pandoc.Definition
3434import Text.Pandoc.Templates (renderTemplate)
3535import Text.Pandoc.Shared
3636import Text.Pandoc.Blocks
37−import Text.ParserCombinators.Parsec ( parse, GenParser )
37+import Text.ParserCombinators.Parsec ( runParser, GenParser )
3838import Data.List ( group, isPrefixOf, find, intersperse, transpose )
3939import Text.PrettyPrint.HughesPJ hiding ( Str )
4040import Control.Monad.State
@@ -158,7 +158,7 @@ elementToListItem (Sec _ _ _ headerText subsecs) = [Plain headerText] ++
158158 else [BulletList $ map elementToListItem subsecs]
159159
160160-- | Ordered list start parser for use in Para below.
161−olMarker :: GenParser Char st Char
161+olMarker :: GenParser Char ParserState Char
162162olMarker = do (start, style', delim) <- anyOrderedListMarker
163163 if delim == Period &&
164164 (style' == UpperAlpha || (style' == UpperRoman &&
@@ -169,7 +169,7 @@ olMarker = do (start, style', delim) <- anyOrderedListMarker
169169-- | True if string begins with an ordered list marker
170170beginsWithOrderedListMarker :: String -> Bool
171171beginsWithOrderedListMarker str =
172− case parse olMarker "para start" str of
172+ case runParser olMarker defaultParserState "para start" str of
173173 Left _ -> False
174174 Right _ -> True
175175
src/Text/Pandoc/Writers/Texinfo.hs+1
@@ -144,6 +144,7 @@ blockToTexinfo (OrderedList (start, numstyle, _) lst) = do
144144 exemplar = case numstyle of
145145 DefaultStyle -> decimal
146146 Decimal -> decimal
147+ Example -> decimal
147148 UpperRoman -> decimal -- Roman numerals not supported
148149 LowerRoman -> decimal
149150 UpperAlpha -> upperAlpha