Expose #+BEGIN_
block arguments.
This commit is contained in:
parent
b819904457
commit
dec91e0e8d
@ -92,10 +92,10 @@ LIST_ITEM_RE = re.compile(
|
|||||||
)
|
)
|
||||||
|
|
||||||
# Org-Babel
|
# Org-Babel
|
||||||
BEGIN_BLOCK_RE = re.compile(r"^\s*#\+BEGIN_(?P<subtype>[^ ]+)(?P<content>.*)$", re.I)
|
BEGIN_BLOCK_RE = re.compile(r"^\s*#\+BEGIN_(?P<subtype>[^ ]+)(?P<arguments>.*)$", re.I)
|
||||||
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", ("name", "content", "result"))
|
CodeSnippet = collections.namedtuple("CodeSnippet", ("name", "content", "result", "arguments"))
|
||||||
|
|
||||||
# Groupings
|
# Groupings
|
||||||
NON_FINISHED_GROUPS = (type(None), dom.ListGroupNode, dom.ResultsDrawerNode, dom.PropertyDrawerNode)
|
NON_FINISHED_GROUPS = (type(None), dom.ListGroupNode, dom.ResultsDrawerNode, dom.PropertyDrawerNode)
|
||||||
@ -764,11 +764,13 @@ class Headline:
|
|||||||
inside_code = False
|
inside_code = False
|
||||||
|
|
||||||
sections = []
|
sections = []
|
||||||
|
arguments = None
|
||||||
|
|
||||||
for delimiter in self.delimiters:
|
for delimiter in self.delimiters:
|
||||||
if delimiter.delimiter_type == DelimiterLineType.BEGIN_BLOCK and delimiter.type_data.subtype.lower() == "src":
|
if delimiter.delimiter_type == DelimiterLineType.BEGIN_BLOCK and delimiter.type_data.subtype.lower() == "src":
|
||||||
line_start = delimiter.linenum
|
line_start = delimiter.linenum
|
||||||
inside_code = True
|
inside_code = True
|
||||||
|
arguments = delimiter.arguments
|
||||||
elif delimiter.delimiter_type == DelimiterLineType.END_BLOCK and delimiter.type_data.subtype.lower() == "src":
|
elif delimiter.delimiter_type == DelimiterLineType.END_BLOCK and delimiter.type_data.subtype.lower() == "src":
|
||||||
inside_code = False
|
inside_code = False
|
||||||
start, end = line_start, delimiter.linenum
|
start, end = line_start, delimiter.linenum
|
||||||
@ -785,8 +787,10 @@ class Headline:
|
|||||||
"line_first": start + 1,
|
"line_first": start + 1,
|
||||||
"line_last": end - 1,
|
"line_last": end - 1,
|
||||||
"content": contents,
|
"content": contents,
|
||||||
|
"arguments": arguments,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
arguments = None
|
||||||
line_start = None
|
line_start = None
|
||||||
|
|
||||||
for kword in self.keywords:
|
for kword in self.keywords:
|
||||||
@ -837,7 +841,8 @@ class Headline:
|
|||||||
name = None
|
name = None
|
||||||
content = section["content"]
|
content = section["content"]
|
||||||
code_result = section.get("result", None)
|
code_result = section.get("result", None)
|
||||||
results.append(CodeSnippet(name=name, content=content, result=code_result))
|
arguments = section.get("arguments", None)
|
||||||
|
results.append(CodeSnippet(name=name, content=content, result=code_result, arguments=arguments))
|
||||||
|
|
||||||
return results
|
return results
|
||||||
|
|
||||||
@ -991,7 +996,7 @@ BlockDelimiterTypeData = collections.namedtuple(
|
|||||||
)
|
)
|
||||||
|
|
||||||
DelimiterLine = collections.namedtuple(
|
DelimiterLine = collections.namedtuple(
|
||||||
"DelimiterLine", ("linenum", "line", "delimiter_type", "type_data")
|
"DelimiterLine", ("linenum", "line", "delimiter_type", "type_data", "arguments")
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@ -2081,7 +2086,7 @@ class OrgDocReader:
|
|||||||
|
|
||||||
def add_begin_block_line(self, linenum: int, match: re.Match):
|
def add_begin_block_line(self, linenum: int, match: re.Match):
|
||||||
line = DelimiterLine(linenum, match.group(0), DelimiterLineType.BEGIN_BLOCK,
|
line = DelimiterLine(linenum, match.group(0), DelimiterLineType.BEGIN_BLOCK,
|
||||||
BlockDelimiterTypeData(match.group("subtype")))
|
BlockDelimiterTypeData(match.group("subtype")), match.group('arguments'))
|
||||||
if len(self.headline_hierarchy) == 0:
|
if len(self.headline_hierarchy) == 0:
|
||||||
self.delimiters.append(line)
|
self.delimiters.append(line)
|
||||||
else:
|
else:
|
||||||
@ -2089,7 +2094,7 @@ class OrgDocReader:
|
|||||||
|
|
||||||
def add_end_block_line(self, linenum: int, match: re.Match):
|
def add_end_block_line(self, linenum: int, match: re.Match):
|
||||||
line = DelimiterLine(linenum, match.group(0), DelimiterLineType.END_BLOCK,
|
line = DelimiterLine(linenum, match.group(0), DelimiterLineType.END_BLOCK,
|
||||||
BlockDelimiterTypeData(match.group("subtype")))
|
BlockDelimiterTypeData(match.group("subtype")), None)
|
||||||
if len(self.headline_hierarchy) == 0:
|
if len(self.headline_hierarchy) == 0:
|
||||||
self.delimiters.append(line)
|
self.delimiters.append(line)
|
||||||
else:
|
else:
|
||||||
|
@ -440,6 +440,7 @@ class TestSerde(unittest.TestCase):
|
|||||||
+ 'echo "with two lines"\n'
|
+ 'echo "with two lines"\n'
|
||||||
+ "exit 0 # Exit successfully",
|
+ "exit 0 # Exit successfully",
|
||||||
)
|
)
|
||||||
|
self.assertEqual(snippets[0].arguments.split(), ['shell', ':results', 'verbatim'])
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
snippets[0].result,
|
snippets[0].result,
|
||||||
"This is a test\n" + "with two lines",
|
"This is a test\n" + "with two lines",
|
||||||
|
Loading…
Reference in New Issue
Block a user