Compare commits

...

2 Commits

Author SHA1 Message Date
Sergio Martínez Portela
66ccb66211 Explicitly extract code block language.
Some checks failed
Testing / pytest (push) Successful in 29s
Testing / mypy (push) Successful in 32s
Testing / style-formatting (push) Failing after 32s
Testing / style-sorted-imports (push) Successful in 23s
Testing / stability-extra-test (push) Successful in 31s
2024-09-30 23:39:43 +02:00
Sergio Martínez Portela
8fe3c27595 Read names for code blocks. 2024-09-30 23:39:37 +02:00
3 changed files with 36 additions and 4 deletions

View File

@ -113,7 +113,7 @@ BEGIN_BLOCK_RE = re.compile(r"^\s*#\+BEGIN_(?P<subtype>[^ ]+)(?P<arguments>.*)$"
END_BLOCK_RE = re.compile(r"^\s*#\+END_(?P<subtype>[^ ]+)\s*$", re.I)
RESULTS_DRAWER_RE = re.compile(r"^\s*:results:\s*$", re.I)
CodeSnippet = collections.namedtuple(
"CodeSnippet", ("name", "content", "result", "arguments")
"CodeSnippet", ("name", "content", "result", "language", "arguments")
)
# Groupings
@ -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"
@ -904,14 +916,23 @@ class Headline:
# the content parsing must be re-thinked
contents = contents[:-1]
language = None
if arguments is not None:
arguments = arguments.strip()
if " " in arguments:
language = arguments[: arguments.index(" ")]
arguments = arguments[arguments.index(" ") + 1 :]
sections.append(
{
"line_first": start + 1,
"line_last": end - 1,
"content": contents,
"arguments": arguments,
"language": language,
"name": name,
}
)
name = None
arguments = None
line_start = None
@ -960,13 +981,17 @@ class Headline:
results = []
for section in sections:
name = None
content = section["content"]
code_result = section.get("result", None)
arguments = section.get("arguments", None)
name = section.get("name", None)
results.append(
CodeSnippet(
name=name, content=content, result=code_result, arguments=arguments
content=content,
result=code_result,
arguments=arguments,
language=language,
name=name,
)
)

View File

@ -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"

View File

@ -480,6 +480,8 @@ 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].language, "shell")
self.assertEqual(
snippets[0].content,
'echo "This is a test"\n'
@ -487,13 +489,15 @@ class TestSerde(unittest.TestCase):
+ "exit 0 # Exit successfully",
)
self.assertEqual(
snippets[0].arguments.split(), ["shell", ":results", "verbatim"]
snippets[0].arguments.split(), [":results", "verbatim"]
)
self.assertEqual(
snippets[0].result,
"This is a test\n" + "with two lines",
)
self.assertEqual(snippets[1].name, None)
self.assertEqual(snippets[1].language, "shell")
self.assertEqual(
snippets[1].content,
'echo "This is another test"\n'
@ -504,6 +508,8 @@ 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].language, "c")
self.assertEqual(
snippets[2].content,
"/* This code has to be escaped to\n"