MCPcopy Create free account
hub / github.com/RASAAS/docmcp-knowledge / reformat_footnotes

Function reformat_footnotes

scripts/postprocess_fulltext.py:106–171  ·  view source on GitHub ↗

Convert inline footnote lines to a footnotes section at the end. Detects lines matching: ^ that appear after a blank line and are not inside a table. Groups them into a "---\n**Footnotes**" block at the end of the document.

(content: str)

Source from the content-addressed store, hash-verified

104
105
106def reformat_footnotes(content: str) -> tuple[str, int]:
107 """
108 Convert inline footnote lines to a footnotes section at the end.
109
110 Detects lines matching: ^<digits> <text>
111 that appear after a blank line and are not inside a table.
112 Groups them into a "---\n**Footnotes**" block at the end of the document.
113 """
114 lines = content.split('\n')
115 footnotes = []
116 new_lines = []
117 i = 0
118 while i < len(lines):
119 line = lines[i]
120 # Skip table rows
121 if line.startswith('|'):
122 new_lines.append(line)
123 i += 1
124 continue
125
126 m = FOOTNOTE_RE.match(line)
127 if m:
128 num = m.group(1)
129 text = m.group(2).strip()
130
131 # Collect continuation lines (non-empty, non-table, not a new footnote)
132 # Limit to max 3 continuation lines to avoid swallowing normal paragraphs
133 j = i + 1
134 cont_count = 0
135 while j < len(lines) and cont_count < 3:
136 next_line = lines[j]
137 if next_line == '':
138 break
139 if next_line.startswith('|'):
140 break
141 if FOOTNOTE_RE.match(next_line):
142 break
143 # Heading or HR
144 if next_line.startswith('#') or next_line.startswith('---'):
145 break
146 # If next line looks like a new sentence/paragraph (starts uppercase after period)
147 if cont_count > 0 and re.match(r'^[A-Z][a-z]', next_line) and text.endswith('.'):
148 break
149 text += ' ' + next_line.strip()
150 j += 1
151 cont_count += 1
152
153 footnotes.append((num, text))
154 # Remove the blank line before this footnote if present
155 if new_lines and new_lines[-1] == '':
156 new_lines.pop()
157 i = j
158 else:
159 new_lines.append(line)
160 i += 1
161
162 if not footnotes:
163 return content, 0

Callers 1

process_fileFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected