text
stringlengths
0
828
options={}
) -> str:
'''Cut part of Markdown string between two headings, set internal heading level,
and remove top heading.
If only the starting heading is defined, cut to the next heading
of the same level.
Heading shift and top heading elimination are optional.
:param content: Markdown content
:param from_heading: Starting heading
:param to_heading: Ending heading (will not be incuded in the output)
:param options: ``sethead``, ``nohead``
:returns: Part of the Markdown content between headings with internal headings adjusted
'''
self.logger.debug(f'Cutting from heading: {from_heading}, to heading: {to_heading}, options: {options}')
from_heading_pattern = re.compile(r'^\#{1,6}\s+' + rf'{from_heading}\s*$', flags=re.MULTILINE)
if not from_heading_pattern.findall(content):
return ''
from_heading_line = from_heading_pattern.findall(content)[0]
from_heading_level = len(self._heading_pattern.match(from_heading_line).group('hashes'))
self.logger.debug(f'From heading level: {from_heading_level}')
result = from_heading_pattern.split(content)[1]
if to_heading:
to_heading_pattern = re.compile(r'^\#{1,6}\s+' + rf'{to_heading}\s*$', flags=re.MULTILINE)
else:
to_heading_pattern = re.compile(
rf'^\#{{1,{from_heading_level}}}[^\#]+?$',
flags=re.MULTILINE
)
result = to_heading_pattern.split(result)[0]
if not options.get('nohead'):
result = from_heading_line + result
if options.get('sethead'):
if options['sethead'] > 0:
result = self._shift_headings(
result,
options['sethead'] - from_heading_level
)
return result"
1480,"def _cut_to_heading(
self,
content: str,
to_heading: str or None = None,
options={}
) -> str:
'''Cut part of Markdown string from the start to a certain heading,
set internal heading level, and remove top heading.
If not heading is defined, the whole string is returned.
Heading shift and top heading elimination are optional.
:param content: Markdown content
:param to_heading: Ending heading (will not be incuded in the output)
:param options: ``sethead``, ``nohead``
:returns: Part of the Markdown content from the start to ``to_heading``,
with internal headings adjusted
'''
self.logger.debug(f'Cutting to heading: {to_heading}, options: {options}')
content_buffer = StringIO(content)
first_line = content_buffer.readline()
if self._heading_pattern.fullmatch(first_line):
from_heading_line = first_line
from_heading_level = len(self._heading_pattern.match(from_heading_line).group('hashes'))
result = content_buffer.read()
else:
from_heading_line = ''
from_heading_level = self._find_top_heading_level(content)
result = content
self.logger.debug(f'From heading level: {from_heading_level}')
if to_heading:
to_heading_pattern = re.compile(r'^\#{1,6}\s+' + rf'{to_heading}\s*$', flags=re.MULTILINE)
result = to_heading_pattern.split(result)[0]
if not options.get('nohead'):
result = from_heading_line + result