This implements the feature requested in issue 11 by allowing hyperlinks to the original source code. It allows for both direct links and the option to specify a base url. I've listed the changes and modified the README.md with instructions and examples.
Changes:
Added containers to build-dependencies in
pandoc-include-code.cabal
Added a field for the base url attribute in
InclusionSpec for when the base url is specified in the
CodeBlock
data InclusionSpec = InclusionSpec
{ include :: FilePath
, mode :: InclusionMode
, dedent :: Maybe Int
, base :: Maybe [Char]
}
Changed InclusionState from newtype to
data in order to add the filepath specified in the
include attribute of the CodeBlock
data InclusionState = InclusionState
{ startLineNumber :: Maybe LineNumber,
link :: Maybe FilePath
}
Modified ParseInclusion to retrieve the base url
specified as an attribute in the CodeBlock (which may be
Nothing) and add it to the base field in
InclusionSpec
base <- getBase
.
.
.
getBase = case (HM.lookup "base" attrs) of
Nothing -> return Nothing
Just b -> return $ Just (Text.unpack b)
Added a function to set the filepath in InclusionState.
If a base url is specified through the meta block or command-line,
append it to the link specified in the include attribute.
Otherwise, set the link to the same path defined in include
setLink :: Maybe [Char] -> [Char] -> Inclusion ()
setLink (Just b) n = modify (\s -> s {link = Just (b ++ n)})
setLink Nothing n = modify (\s -> s {link = Just n})
Added the base attribute to the list of items to remove from
the CodeBlock
Added function to extract the filename from the FilePath
getName :: Text -> Text
getName filepath = last $ splitOn (Text.pack "/") filepath
Added function to create the a Block list containing either a
CodeBlock or a CodeBlock and Plain text with a
Link
modifiedCodeBlock :: Block -> (Text, InclusionState) -> Text -> Either InclusionError [Block]
modifiedCodeBlock cb@(CodeBlock (id', classes, attrs) _) (contents, state) base =
case link state of
Just path | "includeLink" `elem` classes -> Right [(CodeBlock (id', modifyClasses classes, modifyAttributes state classes attrs) contents), Plain [Link nullAttr [Str (getName path)] ( addBase path, "")]]
_ -> Right [CodeBlock (id', classes, modifyAttributes state classes attrs) contents]
where
addBase link
| or $ map ($ link) (map Text.isPrefixOf ["C:", "/", "\\", "file:", "http:", "https:"]) = link
| otherwise = Text.append base link
Modified includeCode' to add thebase` url if the
user specified it in the metadata block or command line
includeCode' :: Text -> Block -> IO (Either InclusionError [Block])
includeCode' base cb@(CodeBlock (id', classes, attrs) _) =
case parseInclusion (HM.fromList attrs) of
Right (Just spec) ->
runInclusion' spec allSteps >>= \case
Left err -> return (Left err)
Right out-> return (modifiedCodeBlock cb out base)
Right Nothing -> return (Right [cb])
Left err -> return (Left err)
includeCode' _ x = return (Right [x])
Changed includeCode to modify Pandoc instead of
Block types: this allows the metadata to be used for
extending links with a base url the user may specify by adding the
value to the metadata block of the input file or by adding the
metavalue flag in command line
includeCode :: Maybe Format -> Pandoc -> IO Pandoc
includeCode _ (Pandoc m@(Meta list) bs) = do
b <- sequence $ map (includeCode' $ getBaseURL (lookupMeta "base" m) ) bs
return (Pandoc modMeta (modBlocks b))
where modMeta = Meta (Data.Map.delete "base" list)
modBlocks b = concat $ snd $ partitionEithers b
getBaseURL base = case base of
Just (MetaInlines [Str baseURL]) -> baseURL
Just (MetaString baseURL) -> baseURL
Nothing-> ""
This implements the feature requested in issue 11 by allowing hyperlinks to the original source code. It allows for both direct links and the option to specify a base url. I've listed the changes and modified the README.md with instructions and examples.
Changes:
Added
containers
to build-dependencies inpandoc-include-code.cabal
Added the following imports to
IncludeCode.hs
Added a field for the base url attribute in
InclusionSpec
for when the base url is specified in theCodeBlock
Changed
InclusionState
fromnewtype
todata
in order to add the filepath specified in the include attribute of theCodeBlock
Modified
ParseInclusion
to retrieve the base url specified as an attribute in theCodeBlock
(which may beNothing
) and add it to the base field inInclusionSpec
Added a function to set the filepath in
InclusionState
. If a base url is specified through the meta block or command-line, append it to the link specified in theinclude
attribute. Otherwise, set the link to the same path defined ininclude
Added the
base
attribute to the list of items to remove from theCodeBlock
Added function to remove the
.includeLink
class from theCodeBlock
Added function to retrieve the
include
andbase
attribute from the respective fields inInclusionSpec
to then be used to create the complete linkAdded includeLink to steps to be performed
Added function to extract the filename from the FilePath
Added function to create the a
Block
list containing either aCodeBlock
or aCodeBlock
andPlain
text with aLink
Modified
includeCode' to add the
base` url if the user specified it in the metadata block or command lineChanged
includeCode
to modifyPandoc
instead ofBlock
types: this allows the metadata to be used for extending links with a base url the user may specify by adding the value to the metadata block of the input file or by adding the metavalue flag in command line