Skip to content

Commit

Permalink
Added "--force-crossrefs" option to CLI
Browse files Browse the repository at this point in the history
Passing this will force all parsing of cross-references to ignore the safety check of whether a corresponding identifier exists in the document.

This may be useful for individually compiling LaTeX for inclusion in other documents, or for cases where numeric labels would later be filled-in with a filter.

If a corresponding numeric label is not found, the default label text is replaced by the raw text. This will not affect LaTeX.
  • Loading branch information
timtylin committed Feb 15, 2015
1 parent d1a6f37 commit d9403ad
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
13 changes: 11 additions & 2 deletions scholdoc.hs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ data Opt = Opt
, optAscii :: Bool -- ^ Use ascii characters only in html
, optTeXLigatures :: Bool -- ^ Use TeX ligatures for quotes/dashes
, optDefaultImageExtension :: String -- ^ Default image extension
, optAllowUndefinedXRef :: Bool -- ^ Allows x-refs to be undefined
, optExtractMedia :: Maybe FilePath -- ^ Path to extract embedded media
, optTrace :: Bool -- ^ Print debug information
, optTrackChanges :: TrackChanges -- ^ Accept or reject MS Word track-changes.
Expand Down Expand Up @@ -272,6 +273,7 @@ defaultOpts = Opt
, optAscii = False
, optTeXLigatures = True
, optDefaultImageExtension = ""
, optAllowUndefinedXRef = False
, optExtractMedia = Nothing
, optTrace = False
, optTrackChanges = AcceptChanges
Expand Down Expand Up @@ -591,6 +593,11 @@ options =
(\opt -> return opt { optReferenceLinks = True } ))
"" -- "Use reference links in parsing HTML"

, Option "" ["force-crossrefs"]
(NoArg
(\opt -> return opt { optAllowUndefinedXRef = True } ))
"" -- "Always force cross-references, even if not defined in document"

, Option "" ["atx-headers"]
(NoArg
(\opt -> return opt { optSetextHeaders = False } ))
Expand Down Expand Up @@ -1041,14 +1048,14 @@ main = do
err 2 $ concat $ errors ++
["Try " ++ prg ++ " --help for more information."]

let preventScholarly = "--emulate-pandoc" `elem` rawArgs
let emulatePandocArgs = "--emulate-pandoc" `elem` rawArgs

let defaultOpts'
| compatMode = defaultOpts { optReader = "markdown_strict"
, optWriter = "html"
, optEmailObfuscation =
ReferenceObfuscation }
| scholarlyPandoc && not preventScholarly =
| scholarlyPandoc && not emulatePandocArgs =
defaultOpts { optReader = "markdown_scholarly"
, optSmart = True
, optParseRaw = True
Expand Down Expand Up @@ -1113,6 +1120,7 @@ main = do
, optAscii = ascii
, optTeXLigatures = texLigatures
, optDefaultImageExtension = defaultImageExtension
, optAllowUndefinedXRef = allowUndefinedXRef
, optExtractMedia = mbExtractMedia
, optTrace = trace
, optTrackChanges = trackChanges
Expand Down Expand Up @@ -1319,6 +1327,7 @@ main = do
, readerIndentedCodeClasses = codeBlockClasses
, readerApplyMacros = not laTeXOutput
, readerDefaultImageExtension = defaultImageExtension'
, readerAllowUndefinedXRef = allowUndefinedXRef
, readerTrace = trace
, readerTrackChanges = trackChanges
}
Expand Down
2 changes: 2 additions & 0 deletions src/Text/Pandoc/Options.hs
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ data ReaderOptions = ReaderOptions{
, readerIndentedCodeClasses :: [String] -- ^ Default classes for
-- indented code blocks
, readerDefaultImageExtension :: String -- ^ Default extension for images
, readerAllowUndefinedXRef :: Bool -- ^ Allows x-refs to be undefined
, readerTrace :: Bool -- ^ Print debugging info
, readerTrackChanges :: TrackChanges
} deriving (Show, Read)
Expand All @@ -277,6 +278,7 @@ instance Default ReaderOptions
, readerApplyMacros = True
, readerIndentedCodeClasses = []
, readerDefaultImageExtension = ""
, readerAllowUndefinedXRef = False
, readerTrace = False
, readerTrackChanges = AcceptChanges
}
Expand Down
12 changes: 10 additions & 2 deletions src/Text/Pandoc/Readers/Markdown.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2359,23 +2359,31 @@ scholarlyPlainXRef = try $ do
let raw = if op then ("[#" ++ label ++ "]") else ("#" ++ label)
return $ do
xRefs <- asksF stateXRefIdents
allowUndef <- readerAllowUndefinedXRef <$> asksF stateOptions
let numlabel = getNumericalLabel label xRefs
case numlabel of
Just numlabel' -> let refStyle = NumberedReference label PlainNumRef [Str numlabel']
in return $ B.numRef refStyle raw
Nothing -> return $ B.str raw
Nothing -> if allowUndef
then (let refStyle = NumberedReference label PlainNumRef [Str raw]
in return $ B.numRef refStyle raw)
else return $ B.str raw

scholarlyParensXRef :: MarkdownParser (F Inlines)
scholarlyParensXRef = try $ do
label <- string "(#" >> identifier <* char ')'
let raw = "(#" ++ label ++ ")"
return $ do
xRefs <- asksF stateXRefIdents
allowUndef <- readerAllowUndefinedXRef <$> asksF stateOptions
let numlabel = getNumericalLabel label xRefs
case numlabel of
Just numlabel' -> let refStyle = NumberedReference label ParenthesesNumRef [Str numlabel']
in return $ B.numRef refStyle raw
Nothing -> return $ B.str raw
Nothing -> if allowUndef
then (let refStyle = NumberedReference label ParenthesesNumRef [Str raw]
in return $ B.numRef refStyle raw)
else return $ B.str raw
--
-- Scholarly Markdown statements
--
Expand Down

0 comments on commit d9403ad

Please sign in to comment.