conductor-python icon indicating copy to clipboard operation
conductor-python copied to clipboard

your regex logic in snippet() is brittle:

Open shashanknr172-beep opened this issue 4 months ago • 4 comments

Hi there your regex logic in snippet() looks like more brittle:

p= re.compile("^#+") m= p.search(section_name)

some thing like these it is used

*you're treating section_name(a string passed by the user , like "## section") as if it's markdown content. but in reality , we want to find the section header inside the file , not check the input string format , also headers and spacing , which often fails.

The change that can be done 👍

    1. Accept just the plain title text(e.g " Introduction") instead of forcing "## Introduction" .
    2. Build the regex dynamically to match headings in the file.

for example

@env.macro def snippet(file_path, section_name, num_sections=1): root = env.variables['config']['docs_dir'] full_path = os.path.join(root, file_path)

with open(full_path, 'r') as myfile:
    content = myfile.read()

# Match a heading that contains the given text
heading_pattern = re.compile(r'^(#+)\s+' + re.escape(section_name) + r'\s*$', re.MULTILINE)
start = heading_pattern.search(content)

if not start:
    return f"Section '{section_name}' not found in {file_path}"

section_level = len(start.group(1))  # number of '#' determines level
start_index = start.end()

# Find the next heading of same or higher level
next_heading_pattern = re.compile(r'^(#{1,' + str(section_level) + r'})\s+', re.MULTILINE)
following = [m for m in next_heading_pattern.finditer(content[start_index:])]

if len(following) == 0 or (num_sections - 1) >= len(following):
    result = content[start.start():]
else:
    end = following[num_sections - 1]
    result = content[start.start(): start_index + end.start()]

return result
  • These change can make the macro more robust and user-friendly.

shashanknr172-beep avatar Aug 26 '25 08:08 shashanknr172-beep

please let me know i am right on wrong about these concept

shashanknr172-beep avatar Aug 26 '25 08:08 shashanknr172-beep

Hey @shashanknr172-beep

Thank you for reporting this issue. Maybe I'm missing something, but I cannot completely understand context of this issue. Could you please provide more details and context?

IgorChvyrov-sm avatar Oct 22 '25 11:10 IgorChvyrov-sm

Hi, Sure i can provide more details on it . Currently, the snippet() macro expects the section_name argument to be passed with markdown syntax (for example, "## Introduction"). This makes the regex brittle because it tightly couples the input format to the markdown heading style.

In practice, users typically want to extract a section by its logical title (e.g., "Introduction") rather than remembering or specifying the exact markdown prefix. This becomes error-prone when files have inconsistent heading levels (#, ##, etc.) or spacing differences.

improves can be made on my basis could be 1.Separating content from presentation — accepting only the plain title text instead of markdown syntax.

2.Dynamically building a regex that automatically matches headings of any level that contain the specified title, handling spacing and formatting variations gracefully.

3.Ensuring future scalability — it will continue to work even if the markdown structure changes (like promoting ## to ###).

**the goal is to make the macro more user-friendly, flexible, and less error-prone, while keeping full backward compatibility.

I hope these has given the enough explanation regarding these concept and i also think my way of feedback with concept is correct , if any issue or my concept is wrong please let me know so that i can learn or gain more knowledge about the practical concepts.

Thankyou

shashanknr172-beep avatar Oct 23 '25 13:10 shashanknr172-beep

Hey @shashanknr172-beep

Thanks for the clarification you provided. The think is that it isn't the proper repo to raise an issue in. The logic you provided is located in https://github.com/conductor-oss/conductor repository. Please raise an issue in the repo provided

Thank you!

IgorChvyrov-sm avatar Nov 12 '25 17:11 IgorChvyrov-sm

ok

shashanknr172-beep avatar Nov 15 '25 15:11 shashanknr172-beep