aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn MacFarlane <jgm@berkeley.edu>2016-06-26 17:51:51 -0700
committerGitHub <noreply@github.com>2016-06-26 17:51:51 -0700
commita349814665bbd2298acc367cd5485b983313a05d (patch)
tree4e8e96c1fdb4761b54c433144057ce339d18c084
parent7ded9d4af0e33f0cd5df4817ff0b2ad0dec12b4e (diff)
parent0f3f5ce1a1569d02dcb858b45dca55cb5d488358 (diff)
downloadpandoc-a349814665bbd2298acc367cd5485b983313a05d.tar.gz
Merge pull request #3001 from tarleb/org-figure-label
Org reader: support figure labels
-rw-r--r--src/Text/Pandoc/Readers/Org/Blocks.hs7
-rw-r--r--tests/Tests/Readers/Org.hs72
2 files changed, 47 insertions, 32 deletions
diff --git a/src/Text/Pandoc/Readers/Org/Blocks.hs b/src/Text/Pandoc/Readers/Org/Blocks.hs
index f8ff3f928..32deb1fc8 100644
--- a/src/Text/Pandoc/Readers/Org/Blocks.hs
+++ b/src/Text/Pandoc/Readers/Org/Blocks.hs
@@ -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)]
}
@@ -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'
}
@@ -131,6 +134,7 @@ blockAttributes = try $ do
attrCheck attr =
case attr of
"NAME" -> True
+ "LABEL" -> True
"CAPTION" -> True
"ATTR_HTML" -> True
_ -> False
@@ -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
diff --git a/tests/Tests/Readers/Org.hs b/tests/Tests/Readers/Org.hs
index 56dedee55..348b5a6aa 100644
--- a/tests/Tests/Readers/Org.hs
+++ b/tests/Tests/Readers/Org.hs
@@ -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]"