git.iko.soy/typeable/pandoc
on master help sign in
JATS writer: use either styled-content or named-content for spans.

If the element has a content-type attribute, or at least one class, then that value is used as `content-type` and the span is put inside a `<named-content>` element. Otherwise a `<styled-content>` element is used instead. Closes: #7211

commit85f379e474be72ae0f7a53ebc5efe2ad4f8165b4 authorAlbert Krewinkel <albert@zeitkraut.de> dateWed 28 Apr 2021, 12:46 (6 years ago) parent0921b82
2 files changed+35−15
src/Text/Pandoc/Writers/JATS.hs+26−10 test/Tests/Writers/JATS.hs+9−5
src/Text/Pandoc/Writers/JATS.hs+26−10
@@ -21,12 +21,13 @@ module Text.Pandoc.Writers.JATS
2121 , writeJatsPublishing
2222 , writeJatsArticleAuthoring
2323 ) where
24+import Control.Applicative ((<|>))
2425import Control.Monad.Reader
2526import Control.Monad.State
2627import Data.Generics (everywhere, mkT)
2728import Data.List (partition)
2829import qualified Data.Map as M
29−import Data.Maybe (fromMaybe)
30+import Data.Maybe (fromMaybe, listToMaybe)
3031import Data.Time (toGregorian, Day, parseTimeM, defaultTimeLocale, formatTime)
3132import qualified Data.Text as T
3233import Data.Text (Text)
@@ -449,18 +450,33 @@ inlineToJATS opts (Note contents) = do
449450 $ text (show notenum)
450451inlineToJATS opts (Cite _ lst) =
451452 inlinesToJATS opts lst
452−inlineToJATS opts (Span (ident,_,kvs) ils) = do
453+inlineToJATS opts (Span (ident,classes,kvs) ils) = do
453454 contents <- inlinesToJATS opts ils
454− let attr = [("id", escapeNCName ident) | not (T.null ident)] ++
455− [("xml:lang",l) | ("lang",l) <- kvs] ++
456− [(k,v) | (k,v) <- kvs
457− , k `elem` ["alt", "content-type", "rid", "specific-use",
458− "vocab", "vocab-identifier", "vocab-term",
459− "vocab-term-identifier"]]
455+ let commonAttr = [("id", escapeNCName ident) | not (T.null ident)] ++
456+ [("xml:lang",l) | ("lang",l) <- kvs] ++
457+ [(k,v) | (k,v) <- kvs, k `elem` ["alt", "specific-use"]]
458+ -- A named-content element is a good fit for spans, but requires a
459+ -- content-type attribute to be present. We use either the explicit
460+ -- attribute or the first class as content type. If neither is
461+ -- available, then we fall back to using a @styled-content@ element.
462+ let (tag, specificAttr) =
463+ case lookup "content-type" kvs <|> listToMaybe classes of
464+ Just ct -> ( "named-content"
465+ , ("content-type", ct) :
466+ [(k, v) | (k, v) <- kvs
467+ , k `elem` ["rid", "vocab", "vocab-identifier",
468+ "vocab-term", "vocab-term-identifier"]])
469+ -- Fall back to styled-content
470+ Nothing -> ("styled-content"
471+ , [(k, v) | (k,v) <- kvs
472+ , k `elem` ["style", "style-type", "style-detail",
473+ "toggle"]])
474+ let attr = commonAttr ++ specificAttr
475+ -- unwrap if wrapping element would have no attributes
460476 return $
461477 if null attr
462− then contents -- unwrap if no relevant attributes are given
463− else inTags False "named-content" attr contents
478+ then contents
479+ else inTags False tag attr contents
464480inlineToJATS _ (Math t str) = do
465481 let addPref (Xml.Attr q v)
466482 | Xml.qName q == "xmlns" = Xml.Attr q{ Xml.qName = "xmlns:mml" } v
test/Tests/Writers/JATS.hs+9−5
@@ -148,13 +148,17 @@ tests =
148148 spanWith nullAttr "text in span" =?>
149149 "<p>text in span</p>"
150150
151− , "converted to named-content element" =:
152− spanWith ("a", ["ignored"], [("alt", "aa")]) "text" =?>
153− "<p><named-content id=\"a\" alt=\"aa\">text</named-content></p>"
151+ , "converted to named-content element if class given" =:
152+ spanWith ("a", ["genus-species"], [("alt", "aa")]) "C. elegans" =?>
153+ ("<p><named-content id=\"a\" alt=\"aa\" content-type=\"genus-species\">"
154+ <> "C. elegans</named-content></p>")
154155
155− , "unwrapped if named-content element would have no attributes" =:
156− spanWith ("", ["ignored"], [("hidden", "true")]) "text in span" =?>
156+ , "unwrapped if styled-content element would have no attributes" =:
157+ spanWith ("", [], [("hidden", "true")]) "text in span" =?>
157158 "<p>text in span</p>"
158159
160+ , "use content-type attribute if present" =:
161+ spanWith ("", [], [("content-type", "species")]) "E. coli" =?>
162+ "<p><named-content content-type=\"species\">E. coli</named-content></p>"
159163 ]
160164 ]