Skip to content

Commit

Permalink
Extend CMakeModule to parse markdown
Browse files Browse the repository at this point in the history
  • Loading branch information
LecrisUT committed Sep 1, 2023
1 parent 0d6b277 commit 3c89e88
Showing 1 changed file with 56 additions and 36 deletions.
92 changes: 56 additions & 36 deletions sphinxcontrib/moderncmakedomain/cmake.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,9 @@ class CMakeModule(Directive):
option_spec = {'encoding': directives.encoding}

def __init__(self, *args, **keys):
self.re_start = re.compile(r'^#\[(?P<eq>=*)\[\.rst:$')
Directive.__init__(self, *args, **keys)
self.re_block_start = re.compile(r'^(?P<prefix>\s*)#\[(?P<eq>=*)\[\.(?:rst|md):$')
self.re_single = re.compile(r'^(?P<prefix>\s*)#\.(?:rst|md):$')
super().__init__(*args, **keys)

def run(self):
settings = self.state.document.settings
Expand All @@ -161,41 +162,60 @@ def run(self):
raise self.severe(msg)
raw_lines = f.read().splitlines()
f.close()
rst = None
lines = []
for line in raw_lines:
if rst is not None and rst != '#':
# Bracket mode: check for end bracket
pos = line.find(rst)
if pos >= 0:
if line[0] == '#':
line = ''
else:
line = line[0:pos]
rst = None
else:
# Line mode: check for .rst start (bracket or line)
m = self.re_start.match(line)
if m:
rst = f']{m.group("eq")}]'
line = ''
elif line == '#.rst:':
rst = '#'
line = ''
elif rst == '#':
if line == '#' or line[:2] == '# ':
line = line[2:]
else:
rst = None
line = ''
elif rst is None:
line = ''
lines.append(line)
if rst is not None and rst != '#':
raise self.warning(f'{self.name!r} found unclosed bracket '
f'"#[{rst[1:-1]}[.rst:" in {path!r}')
self.state_machine.insert_input(lines, path)
return []
block_end = ""
prefix = ""
block_start = -1
# TODO: Parse nested comments as belonging to specific directive
# Similar to python's documentaiton
for indx, line in enumerate(raw_lines):
# Check if in block comment mode
if block_end:
# Check for block_end
if line.startswith(block_end):
# Exit block mode
block_end = ""
prefix = ""
continue
# Check if line is only whitespace, then add an empty line
if not line or line.isspace():
lines.append("")
continue
# Otherwise remove prefix and add the lines
if not line.startswith(prefix):
raise self.warning(f"{self.name} found ill-formatted block line in {path}:\nline#{indx}: {line}")
lines.append(line.lstrip(prefix))
continue
# Check if in line comment mode
if prefix:
if line.startswith(prefix):
# Current line is still part of the comment
lines.append(line.lstrip(prefix))
continue
# Otherwise we are exiting line-mode
prefix = ""
continue
# Otherwise we are not in comment mode
# Check if we can enter a comment mode
# First check for block-mode
block_mode = self.re_block_start.match(line)
if block_mode:
block_start = indx
prefix = block_mode.group('prefix')
block_end = f"{prefix}]{block_mode.group('eq')}]"
continue
# Next check for line-mode
line_mode = self.re_single.match(line)
if line_mode:
prefix = line_mode.group('prefix')
continue
# Sanitize exit
if block_end:
raise self.warning(f"{self.name} found unclosed bracket in {path}:\n"
f"bracket start at:{block_start};bracket end:\"{block_end}\"")
node = nodes.Element()
self.state.nested_parse(lines, 0, node)
return node.children


class _cmake_index_entry:
Expand Down

0 comments on commit 3c89e88

Please sign in to comment.