feat: Handle greater blocks different from SRC ones.

This commit is contained in:
Sergio Martínez Portela 2022-08-28 21:23:44 +02:00
parent 0e90abbb63
commit 7b7c186b83
2 changed files with 29 additions and 22 deletions

View File

@ -69,9 +69,10 @@ class BlockNode:
class CodeBlock(BlockNode): class CodeBlock(BlockNode):
def __init__(self, header): def __init__(self, header, subtype):
self.header = header self.header = header
self.lines = None self.lines = None
self.subtype = subtype
def set_lines(self, lines): def set_lines(self, lines):
self.lines = lines self.lines = lines

View File

@ -92,8 +92,8 @@ LIST_ITEM_RE = re.compile(
) )
# Org-Babel # Org-Babel
BEGIN_SRC_RE = re.compile(r"^\s*#\+BEGIN_SRC(?P<content>.*)$", re.I) BEGIN_BLOCK_RE = re.compile(r"^\s*#\+BEGIN_(?P<subtype>[^ ]+)(?P<content>.*)$", re.I)
END_SRC_RE = re.compile(r"^\s*#\+END_SRC\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"))
@ -343,7 +343,7 @@ class Headline:
if isinstance(current_node, dom.CodeBlock): if isinstance(current_node, dom.CodeBlock):
if ( if (
isinstance(line, DelimiterLine) isinstance(line, DelimiterLine)
and line.delimiter_type == DelimiterLineType.END_SRC and line.delimiter_type == DelimiterLineType.END_BLOCK
): ):
start = current_node.header.linenum start = current_node.header.linenum
@ -431,17 +431,17 @@ class Headline:
elif ( elif (
isinstance(line, DelimiterLine) isinstance(line, DelimiterLine)
and line.delimiter_type == DelimiterLineType.BEGIN_SRC and line.delimiter_type == DelimiterLineType.BEGIN_BLOCK
): ):
assert type(current_node) in NON_FINISHED_GROUPS assert type(current_node) in NON_FINISHED_GROUPS
current_node = dom.CodeBlock(line) current_node = dom.CodeBlock(line, line.type_data.subtype)
elif isinstance(line, Keyword): elif isinstance(line, Keyword):
logging.warning("Keywords not implemented on `as_dom()`") logging.warning("Keywords not implemented on `as_dom()`")
# elif ( # elif (
# isinstance(line, DelimiterLine) # isinstance(line, DelimiterLine)
# and line.delimiter_type == DelimiterLineType.END_SRC # and line.delimiter_type == DelimiterLineType.END_BLOCK
# ): # ):
# assert isinstance(current_node, dom.BlockNode) # assert isinstance(current_node, dom.BlockNode)
# current_node = None # current_node = None
@ -679,10 +679,10 @@ class Headline:
sections = [] sections = []
for delimiter in self.delimiters: for delimiter in self.delimiters:
if delimiter.delimiter_type == DelimiterLineType.BEGIN_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
elif delimiter.delimiter_type == DelimiterLineType.END_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
@ -873,12 +873,16 @@ class Timestamp:
class DelimiterLineType(Enum): class DelimiterLineType(Enum):
BEGIN_SRC = 1 BEGIN_BLOCK = 1
END_SRC = 2 END_BLOCK = 2
BlockDelimiterTypeData = collections.namedtuple(
"BlockDelimiterTypeData", ("subtype")
)
DelimiterLine = collections.namedtuple( DelimiterLine = collections.namedtuple(
"DelimiterLine", ("linenum", "line", "delimiter_type") "DelimiterLine", ("linenum", "line", "delimiter_type", "type_data")
) )
@ -1891,15 +1895,17 @@ class OrgDocReader:
else: else:
self.headline_hierarchy[-1]["contents"].append(raw) self.headline_hierarchy[-1]["contents"].append(raw)
def add_begin_src_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_SRC) line = DelimiterLine(linenum, match.group(0), DelimiterLineType.BEGIN_BLOCK,
BlockDelimiterTypeData(match.group("subtype")))
if len(self.headline_hierarchy) == 0: if len(self.headline_hierarchy) == 0:
self.delimiters.append(line) self.delimiters.append(line)
else: else:
self.headline_hierarchy[-1]["delimiters"].append(line) self.headline_hierarchy[-1]["delimiters"].append(line)
def add_end_src_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_SRC) line = DelimiterLine(linenum, match.group(0), DelimiterLineType.END_BLOCK,
BlockDelimiterTypeData(match.group("subtype")))
if len(self.headline_hierarchy) == 0: if len(self.headline_hierarchy) == 0:
self.delimiters.append(line) self.delimiters.append(line)
else: else:
@ -1956,8 +1962,8 @@ class OrgDocReader:
linenum = lnum + 1 linenum = lnum + 1
try: try:
if in_block: if in_block:
if m := END_SRC_RE.match(line): if m := END_BLOCK_RE.match(line):
self.add_end_src_line(linenum, m) self.add_end_block_line(linenum, m)
in_block = False in_block = False
else: else:
self.add_raw_line(linenum, line) self.add_raw_line(linenum, line)
@ -1969,11 +1975,11 @@ class OrgDocReader:
elif m := RAW_LINE_RE.match(line): elif m := RAW_LINE_RE.match(line):
self.add_raw_line(linenum, line) self.add_raw_line(linenum, line)
# Org-babel # Org-babel
elif m := BEGIN_SRC_RE.match(line): elif m := BEGIN_BLOCK_RE.match(line):
self.add_begin_src_line(linenum, m) self.add_begin_block_line(linenum, m)
in_block = True in_block = True
elif m := END_SRC_RE.match(line): elif m := END_BLOCK_RE.match(line):
self.add_end_src_line(linenum, m) self.add_end_block_line(linenum, m)
in_block = False in_block = False
# Generic properties # Generic properties
elif m := KEYWORDS_RE.match(line): elif m := KEYWORDS_RE.match(line):