Org writer: add code block identifier as #+name to src blocks.

This commit is contained in:
Albert Krewinkel
2022-08-22 12:53:50 +02:00
parent eb96defcc6
commit 1de40c62fc
2 changed files with 16 additions and 2 deletions
+5 -2
View File
@@ -148,7 +148,10 @@ blockToOrg (Header level attr inlines) = do
then empty
else cr <> propertiesDrawer attr
return $ headerStr <> " " <> contents <> drawerStr <> cr
blockToOrg (CodeBlock (_,classes,kvs) str) = do
blockToOrg (CodeBlock (ident,classes,kvs) str) = do
let name = if T.null ident
then empty
else literal $ "#+name: " <> ident
let startnum = maybe "" (\x -> " " <> trimr x) $ lookup "startFrom" kvs
let numberlines = if "numberLines" `elem` classes
then if "continuedSourceBlock" `elem` classes
@@ -159,7 +162,7 @@ blockToOrg (CodeBlock (_,classes,kvs) str) = do
let (beg, end) = case at of
[] -> ("#+begin_example" <> numberlines, "#+end_example")
(x:_) -> ("#+begin_src " <> x <> numberlines, "#+end_src")
return $ literal beg $$ literal str $$ text end $$ blankline
return $ name $$ literal beg $$ literal str $$ text end $$ blankline
blockToOrg (BlockQuote blocks) = do
contents <- blockListToOrg blocks
return $ blankline $$ "#+begin_quote" $$
+11
View File
@@ -66,4 +66,15 @@ tests =
, "- ☒ b"
]
]
, testGroup "code blocks"
[ "identifier"
=: codeBlockWith ("abc", ["python"], []) "return True"
=?> T.unlines
[ "#+name: abc"
, "#+begin_src python"
, "return True"
, "#+end_src"
]
]
]