Skip to content

Commit

Permalink
refactor: updates markdown heading and comment strip function to remo…
Browse files Browse the repository at this point in the history
…ve regex

Signed-off-by: Jennifer Power <barnabei.jennifer@gmail.com>
  • Loading branch information
jpower432 committed Aug 30, 2023
1 parent db08a0f commit 01a5a33
Showing 1 changed file with 22 additions and 14 deletions.
36 changes: 22 additions & 14 deletions trestle/core/crm/leveraged_statements.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
"""Handle writing of inherited statements to markdown."""
import logging
import pathlib
import re
from abc import ABC, abstractmethod
from dataclasses import dataclass
from typing import Any, Dict, List, Optional
Expand Down Expand Up @@ -287,16 +286,25 @@ def get_leveraged_component_header_value(self) -> Dict[str, str]:
@staticmethod
def strip_heading_and_comments(markdown_text: str) -> str:
"""Remove the heading and comments from lines to get the multi-line paragraph."""
heading_pattern = r'^#+.*$'
comment_pattern = r'<!--.*?-->'

# Remove headings and comments
markdown_text = re.sub(heading_pattern, '', markdown_text, flags=re.MULTILINE)
markdown_text = re.sub(comment_pattern, '', markdown_text, flags=re.DOTALL)

markdown_text = '\n'.join(line.strip() for line in markdown_text.splitlines())

# Remove consecutive empty lines
markdown_text = re.sub(r'\n{2,}', '\n\n', markdown_text)

return markdown_text.strip()
lines = markdown_text.split('\n')
non_heading_comment_lines = []
inside_heading = False
inside_comment = False

for line in lines:
if line.startswith('#'):
inside_heading = True
elif line.startswith('<!--'):
inside_comment = True
if line.rstrip().endswith('-->'):
inside_comment = False
continue
elif line.strip() == '':
inside_heading = False
inside_comment = False

if not inside_heading and not inside_comment:
non_heading_comment_lines.append(line)

stripped_markdown = '\n'.join(non_heading_comment_lines).strip()
return stripped_markdown

0 comments on commit 01a5a33

Please sign in to comment.