Skip to content

Commit

Permalink
Merge pull request #3001 from tarleb/org-figure-label
Browse files Browse the repository at this point in the history
Org reader: support figure labels
  • Loading branch information
jgm committed Jun 27, 2016
2 parents 7ded9d4 + 0f3f5ce commit a349814
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 32 deletions.
7 changes: 6 additions & 1 deletion src/Text/Pandoc/Readers/Org/Blocks.hs
Expand Up @@ -98,6 +98,7 @@ block = choice [ mempty <$ blanklines
-- | Attributes that may be added to figures (like a name or caption).
data BlockAttributes = BlockAttributes
{ blockAttrName :: Maybe String
, blockAttrLabel :: Maybe String
, blockAttrCaption :: Maybe (F Inlines)
, blockAttrKeyValues :: [(String, String)]
}
Expand All @@ -117,12 +118,14 @@ blockAttributes = try $ do
let caption = foldl' (appendValues "CAPTION") Nothing kv
let kvAttrs = foldl' (appendValues "ATTR_HTML") Nothing kv
let name = lookup "NAME" kv
let label = lookup "LABEL" kv
caption' <- maybe (return Nothing)
(fmap Just . parseFromString inlines)
caption
kvAttrs' <- parseFromString keyValues . (++ "\n") $ fromMaybe mempty kvAttrs
return $ BlockAttributes
{ blockAttrName = name
, blockAttrLabel = label
, blockAttrCaption = caption'
, blockAttrKeyValues = kvAttrs'
}
Expand All @@ -131,6 +134,7 @@ blockAttributes = try $ do
attrCheck attr =
case attr of
"NAME" -> True
"LABEL" -> True
"CAPTION" -> True
"ATTR_HTML" -> True
_ -> False
Expand Down Expand Up @@ -415,9 +419,10 @@ figure = try $ do
guard . not . isNothing . blockAttrCaption $ figAttrs
guard (isImageFilename src)
let figName = fromMaybe mempty $ blockAttrName figAttrs
let figLabel = fromMaybe mempty $ blockAttrLabel figAttrs
let figCaption = fromMaybe mempty $ blockAttrCaption figAttrs
let figKeyVals = blockAttrKeyValues figAttrs
let attr = (mempty, mempty, figKeyVals)
let attr = (figLabel, mempty, figKeyVals)
return $ (B.para . B.imageWith attr src (withFigPrefix figName) <$> figCaption)
where
withFigPrefix :: String -> String
Expand Down
72 changes: 41 additions & 31 deletions tests/Tests/Readers/Org.hs
Expand Up @@ -767,37 +767,47 @@ tests =
, "#+END_COMMENT"] =?>
(mempty::Blocks)

, "Figure" =:
unlines [ "#+caption: A very courageous man."
, "#+name: goodguy"
, "[[edward.jpg]]"
] =?>
para (image "edward.jpg" "fig:goodguy" "A very courageous man.")

, "Figure with no name" =:
unlines [ "#+caption: I've been through the desert on this"
, "[[horse.png]]"
] =?>
para (image "horse.png" "fig:" "I've been through the desert on this")

, "Figure with `fig:` prefix in name" =:
unlines [ "#+caption: Used as a metapher in evolutionary biology."
, "#+name: fig:redqueen"
, "[[the-red-queen.jpg]]"
] =?>
para (image "the-red-queen.jpg" "fig:redqueen"
"Used as a metapher in evolutionary biology.")

, "Figure with HTML attributes" =:
unlines [ "#+CAPTION: mah brain just explodid"
, "#+NAME: lambdacat"
, "#+ATTR_HTML: :style color: blue :role button"
, "[[lambdacat.jpg]]"
] =?>
let kv = [("style", "color: blue"), ("role", "button")]
name = "fig:lambdacat"
caption = "mah brain just explodid"
in para (imageWith (mempty, mempty, kv) "lambdacat.jpg" name caption)
, testGroup "Figures" $
[ "Figure" =:
unlines [ "#+caption: A very courageous man."
, "#+name: goodguy"
, "[[edward.jpg]]"
] =?>
para (image "edward.jpg" "fig:goodguy" "A very courageous man.")

, "Figure with no name" =:
unlines [ "#+caption: I've been through the desert on this"
, "[[horse.png]]"
] =?>
para (image "horse.png" "fig:" "I've been through the desert on this")

, "Figure with `fig:` prefix in name" =:
unlines [ "#+caption: Used as a metapher in evolutionary biology."
, "#+name: fig:redqueen"
, "[[the-red-queen.jpg]]"
] =?>
para (image "the-red-queen.jpg" "fig:redqueen"
"Used as a metapher in evolutionary biology.")

, "Figure with HTML attributes" =:
unlines [ "#+CAPTION: mah brain just explodid"
, "#+NAME: lambdacat"
, "#+ATTR_HTML: :style color: blue :role button"
, "[[lambdacat.jpg]]"
] =?>
let kv = [("style", "color: blue"), ("role", "button")]
name = "fig:lambdacat"
caption = "mah brain just explodid"
in para (imageWith (mempty, mempty, kv) "lambdacat.jpg" name caption)

, "Labelled figure" =:
unlines [ "#+CAPTION: My figure"
, "#+LABEL: fig:myfig"
, "[[blub.png]]"
] =?>
let attr = ("fig:myfig", mempty, mempty)
in para (imageWith attr "blub.png" "fig:" "My figure")
]

, "Footnote" =:
unlines [ "A footnote[1]"
Expand Down

0 comments on commit a349814

Please sign in to comment.