Compare commits

...

2 Commits

Author SHA1 Message Date
Sergio Martínez Portela
b70baa0eb8 Read names for code blocks.
Some checks failed
Testing / pytest (push) Successful in 31s
Testing / mypy (push) Has been cancelled
Testing / style-formatting (push) Has been cancelled
Testing / style-sorted-imports (push) Has been cancelled
Testing / stability-extra-test (push) Has been cancelled
2024-09-30 23:11:21 +02:00
Sergio Martínez Portela
1dc6eb0b43 fix: On OrgDoc.get_code_snippets, consider headlines of all levels. 2024-09-30 22:59:04 +02:00
3 changed files with 26 additions and 2 deletions

View File

@ -882,6 +882,12 @@ class Headline:
sections = [] sections = []
arguments = None 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: for delimiter in self.delimiters:
if ( if (
delimiter.delimiter_type == DelimiterLineType.BEGIN_BLOCK delimiter.delimiter_type == DelimiterLineType.BEGIN_BLOCK
@ -890,6 +896,12 @@ class Headline:
line_start = delimiter.linenum line_start = delimiter.linenum
inside_code = True inside_code = True
arguments = delimiter.arguments 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 ( elif (
delimiter.delimiter_type == DelimiterLineType.END_BLOCK delimiter.delimiter_type == DelimiterLineType.END_BLOCK
and delimiter.type_data.subtype.lower() == "src" and delimiter.type_data.subtype.lower() == "src"
@ -960,7 +972,6 @@ class Headline:
results = [] results = []
for section in sections: for section in sections:
name = None
content = section["content"] content = section["content"]
code_result = section.get("result", None) code_result = section.get("result", None)
arguments = section.get("arguments", None) arguments = section.get("arguments", None)
@ -2338,7 +2349,7 @@ class OrgDoc:
yield hl yield hl
def get_code_snippets(self): def get_code_snippets(self):
for headline in self.headlines: for headline in self.getAllHeadlines():
yield from headline.get_code_snippets() yield from headline.get_code_snippets()
# Writing # Writing

View File

@ -9,6 +9,7 @@
:CREATED: [2020-01-01 Wed 01:01] :CREATED: [2020-01-01 Wed 01:01]
:END: :END:
#+NAME: first-code-name
#+BEGIN_SRC shell :results verbatim #+BEGIN_SRC shell :results verbatim
echo "This is a test" echo "This is a test"
echo "with two lines" echo "with two lines"

View File

@ -480,6 +480,10 @@ class TestSerde(unittest.TestCase):
snippets = list(doc.get_code_snippets()) snippets = list(doc.get_code_snippets())
self.assertEqual(len(snippets), 3) self.assertEqual(len(snippets), 3)
self.assertEqual(
snippets[0].name,
'first-code-name'
)
self.assertEqual( self.assertEqual(
snippets[0].content, snippets[0].content,
'echo "This is a test"\n' 'echo "This is a test"\n'
@ -494,6 +498,10 @@ class TestSerde(unittest.TestCase):
"This is a test\n" + "with two lines", "This is a test\n" + "with two lines",
) )
self.assertEqual(
snippets[1].name,
None
)
self.assertEqual( self.assertEqual(
snippets[1].content, snippets[1].content,
'echo "This is another test"\n' 'echo "This is another test"\n'
@ -504,6 +512,10 @@ class TestSerde(unittest.TestCase):
snippets[1].result, "This is another test\n" + "with two lines too" snippets[1].result, "This is another test\n" + "with two lines too"
) )
self.assertEqual(
snippets[2].name,
None
)
self.assertEqual( self.assertEqual(
snippets[2].content, snippets[2].content,
"/* This code has to be escaped to\n" "/* This code has to be escaped to\n"