mirror of
https://github.com/jgm/pandoc.git
synced 2026-08-30 17:59:17 +08:00
JATS writer: include alt-text in <graphic>, <inline-graphic> elements
Closes: #8631
This commit is contained in:
@@ -351,11 +351,16 @@ blockToJATS _ b@(RawBlock f str)
|
||||
blockToJATS _ HorizontalRule = return empty -- not semantic
|
||||
blockToJATS opts (Table attr caption colspecs thead tbody tfoot) =
|
||||
tableToJATS opts (Ann.toTable attr caption colspecs thead tbody tfoot)
|
||||
blockToJATS opts (Figure (ident, _, kvs) caption body) = do
|
||||
capt <- case caption of
|
||||
Caption _ [] -> pure empty
|
||||
Caption _ cpt -> inTagsSimple "caption" <$> blocksToJATS opts cpt
|
||||
figbod <- blocksToJATS opts body
|
||||
blockToJATS opts (Figure (ident, _, kvs) (Caption _short longcapt) body) = do
|
||||
-- Remove the alt text from images if it's the same as the caption text.
|
||||
let unsetAltIfDupl = \case
|
||||
Image attr alt tgt
|
||||
| stringify alt == stringify longcapt -> Image attr [] tgt
|
||||
inline -> inline
|
||||
capt <- if null longcapt
|
||||
then pure empty
|
||||
else inTagsSimple "caption" <$> blocksToJATS opts longcapt
|
||||
figbod <- blocksToJATS opts $ walk unsetAltIfDupl body
|
||||
let figattr = [("id", escapeNCName ident) | not (T.null ident)] ++
|
||||
[(k,v) | (k,v) <- kvs
|
||||
, k `elem` [ "fig-type", "orientation"
|
||||
@@ -520,11 +525,17 @@ inlineToJATS opts (Link (ident,_,kvs) txt (src, tit)) = do
|
||||
contents <- inlinesToJATS opts txt
|
||||
return $ inTags False "ext-link" attr contents
|
||||
inlineToJATS _ (Image attr alt tgt) = do
|
||||
return $ selfClosingTag "inline-graphic" (graphicAttr attr alt tgt)
|
||||
let elattr = graphicAttr attr alt tgt
|
||||
return $ case altToJATS alt of
|
||||
Nothing -> selfClosingTag "inline-graphic" elattr
|
||||
Just altTag -> inTags True "inline-graphic" elattr altTag
|
||||
|
||||
graphic :: Attr -> [Inline] -> Target -> (Doc Text)
|
||||
graphic attr alt tgt =
|
||||
selfClosingTag "graphic" (graphicAttr attr alt tgt)
|
||||
let elattr = graphicAttr attr alt tgt
|
||||
in case altToJATS alt of
|
||||
Nothing -> selfClosingTag "graphic" elattr
|
||||
Just altTag -> inTags True "graphic" elattr altTag
|
||||
|
||||
graphicAttr :: Attr -> [Inline] -> Target -> [(Text, Text)]
|
||||
graphicAttr (ident, _, kvs) _alt (src, tit) =
|
||||
@@ -541,6 +552,13 @@ graphicAttr (ident, _, kvs) _alt (src, tit) =
|
||||
, "xlink:show", "xlink:type"]
|
||||
]
|
||||
|
||||
altToJATS :: [Inline] -> Maybe (Doc Text)
|
||||
altToJATS alt =
|
||||
if null alt
|
||||
then Nothing
|
||||
else Just . inTagsSimple "alt-text" .
|
||||
hsep . map literal . T.words $ stringify alt
|
||||
|
||||
imageMimeType :: Text -> [(Text, Text)] -> (Text, Text)
|
||||
imageMimeType src kvs =
|
||||
let mbMT = getMimeType (T.unpack src)
|
||||
|
||||
@@ -134,7 +134,9 @@ tests =
|
||||
, "containing image" =:
|
||||
header 1 (image "imgs/foo.jpg" "" (text "Alt text")) =?>
|
||||
"<sec>\n\
|
||||
\ <title><inline-graphic mimetype=\"image\" mime-subtype=\"jpeg\" xlink:href=\"imgs/foo.jpg\" /></title>\n\
|
||||
\ <title><inline-graphic mimetype=\"image\" mime-subtype=\"jpeg\" xlink:href=\"imgs/foo.jpg\">\n\
|
||||
\ <alt-text>Alt text</alt-text>\n\
|
||||
\ </inline-graphic></title>\n\
|
||||
\</sec>"
|
||||
]
|
||||
|
||||
|
||||
@@ -12,3 +12,20 @@ Para [Image ("fig-id-2",[],[]) [] ("foo.png", "fig:")]]]
|
||||
<graphic id="fig-id-2" mimetype="image" mime-subtype="png" xlink:href="foo.png" xlink:title="fig:" />
|
||||
</fig>
|
||||
```
|
||||
|
||||
Figure float with caption and alt text.
|
||||
|
||||
```
|
||||
% pandoc -f native -t jats
|
||||
[Figure ("fig-id",[],[]) (Caption Nothing [Para [Str "Caption"]]) [Para [Str "Text"],
|
||||
Para [Image ("fig-id-2",[],[]) [Str "alt"] ("foo.png", "fig:")]]]
|
||||
|
||||
^D
|
||||
<fig id="fig-id">
|
||||
<caption><p>Caption</p></caption>
|
||||
<p>Text</p>
|
||||
<graphic id="fig-id-2" mimetype="image" mime-subtype="png" xlink:href="foo.png" xlink:title="fig:">
|
||||
<alt-text>alt</alt-text>
|
||||
</graphic>
|
||||
</fig>
|
||||
```
|
||||
|
||||
@@ -845,8 +845,9 @@ These should not be escaped: \$ \\ \> \[ \{</preformat>
|
||||
<graphic mimetype="image" mime-subtype="jpeg" xlink:href="lalune.jpg" xlink:title="Voyage dans la Lune" />
|
||||
</fig>
|
||||
<p>Here is a movie
|
||||
<inline-graphic mimetype="image" mime-subtype="jpeg" xlink:href="movie.jpg" />
|
||||
icon.</p>
|
||||
<inline-graphic mimetype="image" mime-subtype="jpeg" xlink:href="movie.jpg">
|
||||
<alt-text>movie</alt-text>
|
||||
</inline-graphic> icon.</p>
|
||||
</sec>
|
||||
<sec id="footnotes">
|
||||
<title>Footnotes</title>
|
||||
|
||||
@@ -830,8 +830,9 @@ These should not be escaped: \$ \\ \> \[ \{</preformat>
|
||||
<graphic mimetype="image" mime-subtype="jpeg" xlink:href="lalune.jpg" xlink:title="Voyage dans la Lune" />
|
||||
</fig>
|
||||
<p>Here is a movie
|
||||
<inline-graphic mimetype="image" mime-subtype="jpeg" xlink:href="movie.jpg" />
|
||||
icon.</p>
|
||||
<inline-graphic mimetype="image" mime-subtype="jpeg" xlink:href="movie.jpg">
|
||||
<alt-text>movie</alt-text>
|
||||
</inline-graphic> icon.</p>
|
||||
</sec>
|
||||
<sec id="footnotes">
|
||||
<title>Footnotes</title>
|
||||
|
||||
@@ -845,8 +845,9 @@ These should not be escaped: \$ \\ \> \[ \{</preformat>
|
||||
<graphic mimetype="image" mime-subtype="jpeg" xlink:href="lalune.jpg" xlink:title="Voyage dans la Lune" />
|
||||
</fig>
|
||||
<p>Here is a movie
|
||||
<inline-graphic mimetype="image" mime-subtype="jpeg" xlink:href="movie.jpg" />
|
||||
icon.</p>
|
||||
<inline-graphic mimetype="image" mime-subtype="jpeg" xlink:href="movie.jpg">
|
||||
<alt-text>movie</alt-text>
|
||||
</inline-graphic> icon.</p>
|
||||
</sec>
|
||||
<sec id="footnotes">
|
||||
<title>Footnotes</title>
|
||||
|
||||
Reference in New Issue
Block a user