git.iko.soy/typeable/pandoc
on master help sign in
Added --id-prefix option.

This adds a prefix to all automatically generated HTML identifiers, which helps prevent duplicate identifiers when you're generating a fragment (say a blog post). Added writerIdentifierPrefix to WriterOptions. Resolves Issue #41. git-svn-id: https://pandoc.googlecode.com/svn/trunk@1650 788f1e2b-df1e-0410-8736-df70ead52e1b

commit78475498b4d9ffc1677eff55667eb87108333544 authorfiddlosopher <fiddlosopher@788f1e2b-df1e-0410-8736-df70ead52e1b> dateSat 5 Dec 2009, 17:56 (17 years ago) parent61f7a4f
5 files changed+36−10
README+5 man/man1/pandoc.1.md+5 src/Text/Pandoc/Shared.hs+2 src/Text/Pandoc/Writers/HTML.hs+13−9 src/pandoc.hs+11−1
README+5
@@ -384,6 +384,11 @@ For further documentation, see the `pandoc(1)` man page.
384384 is specified, *references* is used regardless of the presence
385385 of this option.
386386
387+`--id-prefix`*=string*
388+: specifies a prefix to be added to all automatically generated identifiers
389+ in HTML output. This is useful for preventing duplicate identifiers
390+ when generating fragments to be included in other pages.
391+
387392`--indented-code-classes`*=classes*
388393: specifies classes to use for indented code blocks--for example,
389394 `perl,numberLines` or `haskell`. Multiple classes may be separated
man/man1/pandoc.1.md+5
@@ -154,6 +154,11 @@ to Pandoc. Or use `html2markdown`(1), a wrapper around `pandoc`.
154154 If `--strict` is specified, *references* is used regardless of the
155155 presence of this option.
156156
157+\--id-prefix*=string*
158+: Specify a prefix to be added to all automatically generated identifiers
159+ in HTML output. This is useful for preventing duplicate identifiers
160+ when generating fragments to be included in other pages.
161+
157162\--indented-code-classes*=classes*
158163: Specify classes to use for indented code blocks--for example,
159164 `perl,numberLines` or `haskell`. Multiple classes may be separated
src/Text/Pandoc/Shared.hs+2
@@ -990,6 +990,7 @@ data WriterOptions = WriterOptions
990990 , writerWrapText :: Bool -- ^ Wrap text to line length
991991 , writerLiterateHaskell :: Bool -- ^ Write as literate haskell
992992 , writerEmailObfuscation :: ObfuscationMethod -- ^ How to obfuscate emails
993+ , writerIdentifierPrefix :: String -- ^ Prefix for section & note ids in HTML
993994 } deriving Show
994995
995996-- | Default writer options.
@@ -1012,6 +1013,7 @@ defaultWriterOptions =
10121013 , writerWrapText = True
10131014 , writerLiterateHaskell = False
10141015 , writerEmailObfuscation = JavascriptObfuscation
1016+ , writerIdentifierPrefix = ""
10151017 }
10161018
10171019--
src/Text/Pandoc/Writers/HTML.hs+13−9
@@ -144,13 +144,17 @@ writeHtml opts (Pandoc (Meta tit authors date) blocks) =
144144 then head' +++ body thebody
145145 else thebody
146146
147+-- | Like Text.XHtml's identifier, but adds the writerIdentifierPrefix
148+prefixedId :: WriterOptions -> String -> HtmlAttr
149+prefixedId opts s = identifier $ writerIdentifierPrefix opts ++ s
150+
147151-- | Construct table of contents from list of elements.
148152tableOfContents :: WriterOptions -> [Element] -> State WriterState Html
149153tableOfContents _ [] = return noHtml
150154tableOfContents opts sects = do
151155 let opts' = opts { writerIgnoreNotes = True }
152156 contents <- mapM (elementToListItem opts') sects
153− return $ thediv ! [identifier "TOC"] $ unordList $ catMaybes contents
157+ return $ thediv ! [prefixedId opts' "TOC"] $ unordList $ catMaybes contents
154158
155159-- | Converts an Element to a list item for a table of contents,
156160-- retrieving the appropriate identifier from state.
@@ -162,7 +166,7 @@ elementToListItem opts (Sec _ id' headerText subsecs) = do
162166 let subList = if null subHeads
163167 then noHtml
164168 else unordList subHeads
165− return $ Just $ (anchor ! [href ("#" ++ id')] $ txt) +++ subList
169+ return $ Just $ (anchor ! [href ("#" ++ writerIdentifierPrefix opts ++ id')] $ txt) +++ subList
166170
167171-- | Convert an Element to Html.
168172elementToHtml :: WriterOptions -> Element -> State WriterState Html
@@ -173,7 +177,7 @@ elementToHtml opts (Sec level id' title' elements) = do
173177 return $ if writerS5 opts || (writerStrictMarkdown opts && not (writerTableOfContents opts))
174178 -- S5 gets confused by the extra divs around sections
175179 then toHtmlFromList (header' : innerContents)
176− else thediv ! [identifier id'] << (header' : innerContents)
180+ else thediv ! [prefixedId opts id'] << (header' : innerContents)
177181
178182-- | Convert list of Note blocks to a footnote <div>.
179183-- Assumes notes are sorted.
@@ -258,7 +262,7 @@ blockToHtml opts (CodeBlock (id',classes,keyvals) rawCode) = do
258262 -- browsers ignore leading newlines in pre blocks
259263 let (leadingBreaks, rawCode') = span (=='\n') rawCode
260264 attrs = [theclass (unwords classes') | not (null classes')] ++
261− [identifier id' | not (null id')] ++
265+ [prefixedId opts id' | not (null id')] ++
262266 map (\(x,y) -> strAttr x y) keyvals
263267 in return $ pre ! attrs $ thecode <<
264268 (replicate (length leadingBreaks) br +++
@@ -282,7 +286,7 @@ blockToHtml opts (BlockQuote blocks) =
282286blockToHtml opts (Header level lst) = do
283287 contents <- inlineListToHtml opts lst
284288 let contents' = if writerTableOfContents opts
285− then anchor ! [href "#TOC"] $ contents
289+ then anchor ! [href $ "#" ++ writerIdentifierPrefix opts ++ "TOC"] $ contents
286290 else contents
287291 return $ case level of
288292 1 -> h1 contents'
@@ -465,9 +469,9 @@ inlineToHtml opts inline =
465469 htmlContents <- blockListToNote opts ref contents
466470 -- push contents onto front of notes
467471 put $ st {stNotes = (htmlContents:notes)}
468− return $ anchor ! [href ("#fn" ++ ref),
472+ return $ anchor ! [href ("#" ++ writerIdentifierPrefix opts ++ "fn" ++ ref),
469473 theclass "footnoteRef",
470− identifier ("fnref" ++ ref)] <<
474+ prefixedId opts ("fnref" ++ ref)] <<
471475 sup << ref
472476 (Cite _ il) -> inlineListToHtml opts il
473477
@@ -475,7 +479,7 @@ blockListToNote :: WriterOptions -> String -> [Block] -> State WriterState Html
475479blockListToNote opts ref blocks =
476480 -- If last block is Para or Plain, include the backlink at the end of
477481 -- that block. Otherwise, insert a new Plain block with the backlink.
478− let backlink = [HtmlInline $ " <a href=\"#fnref" ++ ref ++
482+ let backlink = [HtmlInline $ " <a href=\"#" ++ writerIdentifierPrefix opts ++ "fnref" ++ ref ++
479483 "\" class=\"footnoteBackLink\"" ++
480484 " title=\"Jump back to footnote " ++ ref ++ "\">&#8617;</a>"]
481485 blocks' = if null blocks
@@ -490,5 +494,5 @@ blockListToNote opts ref blocks =
490494 _ -> otherBlocks ++ [lastBlock,
491495 Plain backlink]
492496 in do contents <- blockListToHtml opts blocks'
493− return $ li ! [identifier ("fn" ++ ref)] $ contents
497+ return $ li ! [prefixedId opts ("fn" ++ ref)] $ contents
494498
src/pandoc.hs+11−1
@@ -153,6 +153,7 @@ data Opt = Opt
153153 , optSanitizeHTML :: Bool -- ^ Sanitize HTML
154154 , optPlugins :: [Pandoc -> IO Pandoc] -- ^ Plugins to apply
155155 , optEmailObfuscation :: ObfuscationMethod
156+ , optIdentifierPrefix :: String
156157 , optIndentedCodeClasses :: [String] -- ^ Default classes for indented code blocks
157158#ifdef _CITEPROC
158159 , optBiblioFile :: String
@@ -190,6 +191,7 @@ defaultOpts = Opt
190191 , optSanitizeHTML = False
191192 , optPlugins = []
192193 , optEmailObfuscation = JavascriptObfuscation
194+ , optIdentifierPrefix = ""
193195 , optIndentedCodeClasses = []
194196#ifdef _CITEPROC
195197 , optBiblioFile = []
@@ -314,6 +316,12 @@ options =
314316 "none|javascript|references")
315317 "" -- "Method for obfuscating email in HTML"
316318
319+ , Option "" ["id-prefix"]
320+ (ReqArg
321+ (\arg opt -> return opt { optIdentifierPrefix = arg })
322+ "STRING")
323+ "" -- "Prefix to add to automatically generated HTML identifiers"
324+
317325 , Option "" ["indented-code-classes"]
318326 (ReqArg
319327 (\arg opt -> return opt { optIndentedCodeClasses = words $
@@ -540,6 +548,7 @@ main = do
540548 , optWrapText = wrap
541549 , optSanitizeHTML = sanitize
542550 , optEmailObfuscation = obfuscationMethod
551+ , optIdentifierPrefix = idPrefix
543552 , optIndentedCodeClasses = codeBlockClasses
544553#ifdef _CITEPROC
545554 , optBiblioFile = biblioFile
@@ -627,7 +636,8 @@ main = do
627636 lhsExtension [outputFile],
628637 writerEmailObfuscation = if strict
629638 then ReferenceObfuscation
630− else obfuscationMethod }
639+ else obfuscationMethod,
640+ writerIdentifierPrefix = idPrefix }
631641
632642 when (isNonTextOutput writerName' && outputFile == "-") $
633643 do hPutStrLn stderr ("Error: Cannot write " ++ writerName ++ " output to stdout.\n" ++