From 9b319a90dc5f46b55ff55ed021a61190b8553ef1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Mart=C3=ADnez=20Portela?= Date: Mon, 30 Sep 2024 23:11:21 +0200 Subject: [PATCH] Read names for code blocks. --- org_rw/org_rw.py | 13 ++++++++++++- tests/04-code.org | 1 + tests/test_org.py | 3 +++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/org_rw/org_rw.py b/org_rw/org_rw.py index d7d6ad8..7129393 100644 --- a/org_rw/org_rw.py +++ b/org_rw/org_rw.py @@ -882,6 +882,12 @@ class Headline: sections = [] arguments = None + names_by_line = {} + for kw in self.keywords: + if kw.key == "NAME": + names_by_line[kw.linenum] = kw.value + + name = None for delimiter in self.delimiters: if ( delimiter.delimiter_type == DelimiterLineType.BEGIN_BLOCK @@ -890,6 +896,12 @@ class Headline: line_start = delimiter.linenum inside_code = True arguments = delimiter.arguments + + name_line = line_start - 1 + if name_line in names_by_line: + name = names_by_line[name_line] + else: + name = None elif ( delimiter.delimiter_type == DelimiterLineType.END_BLOCK and delimiter.type_data.subtype.lower() == "src" @@ -960,7 +972,6 @@ class Headline: results = [] for section in sections: - name = None content = section["content"] code_result = section.get("result", None) arguments = section.get("arguments", None) diff --git a/tests/04-code.org b/tests/04-code.org index 956d961..7af3aed 100644 --- a/tests/04-code.org +++ b/tests/04-code.org @@ -9,6 +9,7 @@ :CREATED: [2020-01-01 Wed 01:01] :END: +#+NAME: first-code-name #+BEGIN_SRC shell :results verbatim echo "This is a test" echo "with two lines" diff --git a/tests/test_org.py b/tests/test_org.py index f27185b..5a0bc53 100644 --- a/tests/test_org.py +++ b/tests/test_org.py @@ -480,6 +480,7 @@ class TestSerde(unittest.TestCase): snippets = list(doc.get_code_snippets()) self.assertEqual(len(snippets), 3) + self.assertEqual(snippets[0].name, "first-code-name") self.assertEqual( snippets[0].content, 'echo "This is a test"\n' @@ -494,6 +495,7 @@ class TestSerde(unittest.TestCase): "This is a test\n" + "with two lines", ) + self.assertEqual(snippets[1].name, None) self.assertEqual( snippets[1].content, 'echo "This is another test"\n' @@ -504,6 +506,7 @@ class TestSerde(unittest.TestCase): snippets[1].result, "This is another test\n" + "with two lines too" ) + self.assertEqual(snippets[2].name, None) self.assertEqual( snippets[2].content, "/* This code has to be escaped to\n"