git.iko.soy/typeable/pandoc
on master help sign in
Added optional section numbering in HTML output.

This involves a change to the Element data structure, including a section number as well as an id and title for each section. Section numbers are lists of integers; this should allow different numbering schemes to be used in the future. Currently [1,2,3] -> 1.2.3. Resolves Issue #150. git-svn-id: https://pandoc.googlecode.com/svn/trunk@1658 788f1e2b-df1e-0410-8736-df70ead52e1b

commit2ced785e95e0799f3c8a705e828e6522e11a8a98 authorfiddlosopher <fiddlosopher@788f1e2b-df1e-0410-8736-df70ead52e1b> dateTue 8 Dec 2009, 02:36 (17 years ago) parent0543698
7 files changed+33−19
README+2−2 man/man1/pandoc.1.md+2−2 src/Text/Pandoc/Shared.hs+11−7 src/Text/Pandoc/Writers/Docbook.hs+1−1 src/Text/Pandoc/Writers/HTML.hs+15−5 src/Text/Pandoc/Writers/Markdown.hs+1−1 src/Text/Pandoc/Writers/RTF.hs+1−1
README+2−2
@@ -363,8 +363,8 @@ For further documentation, see the `pandoc(1)` man page.
363363 displayed all at once.
364364
365365`-N` or `--number-sections`
366−: causes sections to be numbered in LaTeX or ConTeXt output. By default,
367− sections are not numbered.
366+: causes sections to be numbered in LaTeX, ConTeXt, or HTML output.
367+ By default, sections are not numbered.
368368
369369`--no-wrap`
370370: disables text-wrapping in output. By default, text is wrapped
man/man1/pandoc.1.md+2−2
@@ -134,8 +134,8 @@ to Pandoc. Or use `html2markdown`(1), a wrapper around `pandoc`.
134134: Make list items in S5 display incrementally (one by one).
135135
136136-N, \--number-sections
137−: Number section headings in LaTeX output. (Default is not to number
138− them.)
137+: Number section headings in LaTeX, ConTeXt, or HTML output.
138+ (Default is not to number them.)
139139
140140\--no-wrap
141141: Disable text wrapping in output. (Default is to wrap text.)
src/Text/Pandoc/Shared.hs+11−7
@@ -882,8 +882,8 @@ isPara _ = False
882882
883883-- | Data structure for defining hierarchical Pandoc documents
884884data Element = Blk Block
885− | Sec Int String [Inline] [Element]
886− -- lvl ident label contents
885+ | Sec Int [Int] String [Inline] [Element]
886+ -- lvl num ident label contents
887887 deriving (Eq, Read, Show, Typeable, Data)
888888
889889-- | Convert Pandoc inline list to plain text identifier.
@@ -921,18 +921,22 @@ inlineListToIdentifier' (x:xs) =
921921
922922-- | Convert list of Pandoc blocks into (hierarchical) list of Elements
923923hierarchicalize :: [Block] -> [Element]
924−hierarchicalize blocks = S.evalState (hierarchicalizeWithIds blocks) []
924+hierarchicalize blocks = S.evalState (hierarchicalizeWithIds blocks) ([],[])
925925
926−hierarchicalizeWithIds :: [Block] -> S.State [String] [Element]
926+hierarchicalizeWithIds :: [Block] -> S.State ([Int],[String]) [Element]
927927hierarchicalizeWithIds [] = return []
928928hierarchicalizeWithIds ((Header level title'):xs) = do
929− usedIdents <- S.get
929+ (lastnum, usedIdents) <- S.get
930930 let ident = uniqueIdent title' usedIdents
931− S.modify (ident :)
931+ let lastnum' = take level lastnum
932+ let newnum = if length lastnum' >= level
933+ then init lastnum' ++ [last lastnum' + 1]
934+ else lastnum ++ replicate (level - length lastnum - 1) 0 ++ [1]
935+ S.put (newnum, (ident : usedIdents))
932936 let (sectionContents, rest) = break (headerLtEq level) xs
933937 sectionContents' <- hierarchicalizeWithIds sectionContents
934938 rest' <- hierarchicalizeWithIds rest
935− return $ Sec level ident title' sectionContents' : rest'
939+ return $ Sec level newnum ident title' sectionContents' : rest'
936940hierarchicalizeWithIds (x:rest) = do
937941 rest' <- hierarchicalizeWithIds rest
938942 return $ (Blk x) : rest'
src/Text/Pandoc/Writers/Docbook.hs+1−1
@@ -82,7 +82,7 @@ writeDocbook opts (Pandoc (Meta title authors date) blocks) =
8282-- | Convert an Element to Docbook.
8383elementToDocbook :: WriterOptions -> Element -> Doc
8484elementToDocbook opts (Blk block) = blockToDocbook opts block
85−elementToDocbook opts (Sec _ id' title elements) =
85+elementToDocbook opts (Sec _ _num id' title elements) =
8686 -- Docbook doesn't allow sections with no content, so insert some if needed
8787 let elements' = if null elements
8888 then [Blk (Para [])]
src/Text/Pandoc/Writers/HTML.hs+15−5
@@ -38,7 +38,7 @@ import Text.Pandoc.Highlighting ( highlightHtml, defaultHighlightingCss )
3838import Text.Pandoc.XML (stripTags)
3939import Numeric ( showHex )
4040import Data.Char ( ord, toLower )
41−import Data.List ( isPrefixOf )
41+import Data.List ( isPrefixOf, intersperse )
4242import Data.Maybe ( catMaybes )
4343import qualified Data.Set as S
4444import Control.Monad.State
@@ -156,12 +156,19 @@ tableOfContents opts sects = do
156156 contents <- mapM (elementToListItem opts') sects
157157 return $ thediv ! [prefixedId opts' "TOC"] $ unordList $ catMaybes contents
158158
159+-- | Convert section number to inline
160+showSecNum :: [Int] -> Inline
161+showSecNum = Str . concat . intersperse "." . map show
162+
159163-- | Converts an Element to a list item for a table of contents,
160164-- retrieving the appropriate identifier from state.
161165elementToListItem :: WriterOptions -> Element -> State WriterState (Maybe Html)
162166elementToListItem _ (Blk _) = return Nothing
163−elementToListItem opts (Sec _ id' headerText subsecs) = do
164− txt <- inlineListToHtml opts headerText
167+elementToListItem opts (Sec _ num id' headerText subsecs) = do
168+ let headerText' = if writerNumberSections opts
169+ then showSecNum num : Space : headerText
170+ else headerText
171+ txt <- inlineListToHtml opts headerText'
165172 subHeads <- mapM (elementToListItem opts) subsecs >>= return . catMaybes
166173 let subList = if null subHeads
167174 then noHtml
@@ -171,9 +178,12 @@ elementToListItem opts (Sec _ id' headerText subsecs) = do
171178-- | Convert an Element to Html.
172179elementToHtml :: WriterOptions -> Element -> State WriterState Html
173180elementToHtml opts (Blk block) = blockToHtml opts block
174−elementToHtml opts (Sec level id' title' elements) = do
181+elementToHtml opts (Sec level num id' title' elements) = do
175182 innerContents <- mapM (elementToHtml opts) elements
176− header' <- blockToHtml opts (Header level title')
183+ let title'' = if writerNumberSections opts
184+ then showSecNum num : Space : title'
185+ else title'
186+ header' <- blockToHtml opts (Header level title'')
177187 return $ if writerS5 opts || (writerStrictMarkdown opts && not (writerTableOfContents opts))
178188 -- S5 gets confused by the extra divs around sections
179189 then toHtmlFromList (header' : innerContents)
src/Text/Pandoc/Writers/Markdown.hs+1−1
@@ -138,7 +138,7 @@ tableOfContents opts headers =
138138-- | Converts an Element to a list item for a table of contents,
139139elementToListItem :: Element -> [Block]
140140elementToListItem (Blk _) = []
141−elementToListItem (Sec _ _ headerText subsecs) = [Plain headerText] ++
141+elementToListItem (Sec _ _ _ headerText subsecs) = [Plain headerText] ++
142142 if null subsecs
143143 then []
144144 else [BulletList $ map elementToListItem subsecs]
src/Text/Pandoc/Writers/RTF.hs+1−1
@@ -59,7 +59,7 @@ tableOfContents headers =
5959
6060elementToListItem :: Element -> [Block]
6161elementToListItem (Blk _) = []
62−elementToListItem (Sec _ _ sectext subsecs) = [Plain sectext] ++
62+elementToListItem (Sec _ _ _ sectext subsecs) = [Plain sectext] ++
6363 if null subsecs
6464 then []
6565 else [BulletList (map elementToListItem subsecs)]