From 2ced785e95e0799f3c8a705e828e6522e11a8a98 Mon Sep 17 00:00:00 2001 From: fiddlosopher Date: Tue, 8 Dec 2009 02:36:16 +0000 Subject: 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 --- src/Text/Pandoc/Writers/Docbook.hs | 2 +- src/Text/Pandoc/Writers/HTML.hs | 20 +++++++++++++++----- src/Text/Pandoc/Writers/Markdown.hs | 2 +- src/Text/Pandoc/Writers/RTF.hs | 2 +- 4 files changed, 18 insertions(+), 8 deletions(-) (limited to 'src/Text/Pandoc/Writers') diff --git a/src/Text/Pandoc/Writers/Docbook.hs b/src/Text/Pandoc/Writers/Docbook.hs index b4f760e6c..b46bb0eb4 100644 --- a/src/Text/Pandoc/Writers/Docbook.hs +++ b/src/Text/Pandoc/Writers/Docbook.hs @@ -82,7 +82,7 @@ writeDocbook opts (Pandoc (Meta title authors date) blocks) = -- | Convert an Element to Docbook. elementToDocbook :: WriterOptions -> Element -> Doc elementToDocbook opts (Blk block) = blockToDocbook opts block -elementToDocbook opts (Sec _ id' title elements) = +elementToDocbook opts (Sec _ _num id' title elements) = -- Docbook doesn't allow sections with no content, so insert some if needed let elements' = if null elements then [Blk (Para [])] diff --git a/src/Text/Pandoc/Writers/HTML.hs b/src/Text/Pandoc/Writers/HTML.hs index 9e397477e..56398b263 100644 --- a/src/Text/Pandoc/Writers/HTML.hs +++ b/src/Text/Pandoc/Writers/HTML.hs @@ -38,7 +38,7 @@ import Text.Pandoc.Highlighting ( highlightHtml, defaultHighlightingCss ) import Text.Pandoc.XML (stripTags) import Numeric ( showHex ) import Data.Char ( ord, toLower ) -import Data.List ( isPrefixOf ) +import Data.List ( isPrefixOf, intersperse ) import Data.Maybe ( catMaybes ) import qualified Data.Set as S import Control.Monad.State @@ -156,12 +156,19 @@ tableOfContents opts sects = do contents <- mapM (elementToListItem opts') sects return $ thediv ! [prefixedId opts' "TOC"] $ unordList $ catMaybes contents +-- | Convert section number to inline +showSecNum :: [Int] -> Inline +showSecNum = Str . concat . intersperse "." . map show + -- | Converts an Element to a list item for a table of contents, -- retrieving the appropriate identifier from state. elementToListItem :: WriterOptions -> Element -> State WriterState (Maybe Html) elementToListItem _ (Blk _) = return Nothing -elementToListItem opts (Sec _ id' headerText subsecs) = do - txt <- inlineListToHtml opts headerText +elementToListItem opts (Sec _ num id' headerText subsecs) = do + let headerText' = if writerNumberSections opts + then showSecNum num : Space : headerText + else headerText + txt <- inlineListToHtml opts headerText' subHeads <- mapM (elementToListItem opts) subsecs >>= return . catMaybes let subList = if null subHeads then noHtml @@ -171,9 +178,12 @@ elementToListItem opts (Sec _ id' headerText subsecs) = do -- | Convert an Element to Html. elementToHtml :: WriterOptions -> Element -> State WriterState Html elementToHtml opts (Blk block) = blockToHtml opts block -elementToHtml opts (Sec level id' title' elements) = do +elementToHtml opts (Sec level num id' title' elements) = do innerContents <- mapM (elementToHtml opts) elements - header' <- blockToHtml opts (Header level title') + let title'' = if writerNumberSections opts + then showSecNum num : Space : title' + else title' + header' <- blockToHtml opts (Header level title'') return $ if writerS5 opts || (writerStrictMarkdown opts && not (writerTableOfContents opts)) -- S5 gets confused by the extra divs around sections then toHtmlFromList (header' : innerContents) diff --git a/src/Text/Pandoc/Writers/Markdown.hs b/src/Text/Pandoc/Writers/Markdown.hs index 31b12976d..0e1231b62 100644 --- a/src/Text/Pandoc/Writers/Markdown.hs +++ b/src/Text/Pandoc/Writers/Markdown.hs @@ -138,7 +138,7 @@ tableOfContents opts headers = -- | Converts an Element to a list item for a table of contents, elementToListItem :: Element -> [Block] elementToListItem (Blk _) = [] -elementToListItem (Sec _ _ headerText subsecs) = [Plain headerText] ++ +elementToListItem (Sec _ _ _ headerText subsecs) = [Plain headerText] ++ if null subsecs then [] else [BulletList $ map elementToListItem subsecs] diff --git a/src/Text/Pandoc/Writers/RTF.hs b/src/Text/Pandoc/Writers/RTF.hs index 2376557a8..15bac115d 100644 --- a/src/Text/Pandoc/Writers/RTF.hs +++ b/src/Text/Pandoc/Writers/RTF.hs @@ -59,7 +59,7 @@ tableOfContents headers = elementToListItem :: Element -> [Block] elementToListItem (Blk _) = [] -elementToListItem (Sec _ _ sectext subsecs) = [Plain sectext] ++ +elementToListItem (Sec _ _ _ sectext subsecs) = [Plain sectext] ++ if null subsecs then [] else [BulletList (map elementToListItem subsecs)] -- cgit v1.2.3