Org writer: keep code block attributes as header args.

This allows to keep more information in the resulting `src` blocks,
making it easier to roundtrip from or through Org. Org babel ignores
unknown header arguments.
This commit is contained in:
Albert Krewinkel 2022-08-22 09:48:50 +02:00
parent 1de40c62fc
commit a31cf9a005
No known key found for this signature in database
GPG key ID: 388DC0B21F631124
2 changed files with 19 additions and 4 deletions

View file

@ -159,10 +159,16 @@ blockToOrg (CodeBlock (ident,classes,kvs) str) = do
else " -n" <> startnum
else ""
let at = map pandocLangToOrg classes `intersect` orgLangIdentifiers
let (beg, end) = case at of
[] -> ("#+begin_example" <> numberlines, "#+end_example")
(x:_) -> ("#+begin_src " <> x <> numberlines, "#+end_src")
return $ name $$ literal beg $$ literal str $$ text end $$ blankline
let lang = case at of
[] -> Nothing
l:_ -> Just l
let args = mconcat $
[ " :" <> k <> " " <> v
| (k, v) <- kvs, k `notElem` ["startFrom", "org-language"]]
let (beg, end) = case lang of
Nothing -> ("#+begin_example" <> numberlines, "#+end_example")
Just x -> ("#+begin_src " <> x <> numberlines <> args, "#+end_src")
return $ name $$ literal beg $$ literal str $$ literal end $$ blankline
blockToOrg (BlockQuote blocks) = do
contents <- blockListToOrg blocks
return $ blankline $$ "#+begin_quote" $$

View file

@ -76,5 +76,14 @@ tests =
, "return True"
, "#+end_src"
]
, "attributes"
=: codeBlockWith ("", ["python"], [("cache", "yes"), ("noweb", "yes")])
"'Hello'"
=?> T.unlines
[ "#+begin_src python :cache yes :noweb yes"
, "'Hello'"
, "#+end_src"
]
]
]