Explicitly extract code block language.
Some checks failed
Testing / pytest (push) Successful in 26s
Testing / mypy (push) Successful in 35s
Testing / style-formatting (push) Failing after 30s
Testing / style-sorted-imports (push) Successful in 35s
Testing / stability-extra-test (push) Successful in 28s

This commit is contained in:
Sergio Martínez Portela 2024-09-30 23:39:43 +02:00
parent 8fe3c27595
commit 5432c23202
2 changed files with 17 additions and 2 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) END_BLOCK_RE = re.compile(r"^\s*#\+END_(?P<subtype>[^ ]+)\s*$", re.I)
RESULTS_DRAWER_RE = re.compile(r"^\s*:results:\s*$", re.I) RESULTS_DRAWER_RE = re.compile(r"^\s*:results:\s*$", re.I)
CodeSnippet = collections.namedtuple( CodeSnippet = collections.namedtuple(
"CodeSnippet", ("name", "content", "result", "arguments") "CodeSnippet", ("name", "content", "result", "language", "arguments")
) )
# Groupings # Groupings
@ -916,12 +916,22 @@ class Headline:
# the content parsing must be re-thinked # the content parsing must be re-thinked
contents = contents[:-1] 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 :]
else:
language = arguments
arguments = None
sections.append( sections.append(
{ {
"line_first": start + 1, "line_first": start + 1,
"line_last": end - 1, "line_last": end - 1,
"content": contents, "content": contents,
"arguments": arguments, "arguments": arguments,
"language": language,
"name": name, "name": name,
} }
) )
@ -977,12 +987,14 @@ class Headline:
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)
language = section.get("language", None)
name = section.get("name", None) name = section.get("name", None)
results.append( results.append(
CodeSnippet( CodeSnippet(
content=content, content=content,
result=code_result, result=code_result,
arguments=arguments, arguments=arguments,
language=language,
name=name, name=name,
) )
) )

View File

@ -481,6 +481,7 @@ 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(snippets[0].name, "first-code-name")
self.assertEqual(snippets[0].language, "shell")
self.assertEqual( self.assertEqual(
snippets[0].content, snippets[0].content,
'echo "This is a test"\n' 'echo "This is a test"\n'
@ -488,7 +489,7 @@ class TestSerde(unittest.TestCase):
+ "exit 0 # Exit successfully", + "exit 0 # Exit successfully",
) )
self.assertEqual( self.assertEqual(
snippets[0].arguments.split(), ["shell", ":results", "verbatim"] snippets[0].arguments.split(), [":results", "verbatim"]
) )
self.assertEqual( self.assertEqual(
snippets[0].result, snippets[0].result,
@ -496,6 +497,7 @@ class TestSerde(unittest.TestCase):
) )
self.assertEqual(snippets[1].name, None) self.assertEqual(snippets[1].name, None)
self.assertEqual(snippets[1].language, "shell")
self.assertEqual( self.assertEqual(
snippets[1].content, snippets[1].content,
'echo "This is another test"\n' 'echo "This is another test"\n'
@ -507,6 +509,7 @@ class TestSerde(unittest.TestCase):
) )
self.assertEqual(snippets[2].name, None) self.assertEqual(snippets[2].name, None)
self.assertEqual(snippets[2].language, "c")
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"