Update app.py
Browse files
app.py
CHANGED
@@ -158,18 +158,35 @@ def apply_emoji_font(text, emoji_font):
|
|
158 |
combined_text = before + f'<a href="{url}">{label}</a>' + after
|
159 |
return combined_text
|
160 |
|
161 |
-
def markdown_to_pdf_content(markdown_text, render_with_bold, auto_bold_numbers):
|
162 |
lines = markdown_text.strip().split('\n')
|
163 |
pdf_content = []
|
164 |
number_pattern = re.compile(r'^\d+\.\s')
|
|
|
|
|
|
|
|
|
165 |
for line in lines:
|
166 |
line = line.strip()
|
167 |
if not line or line.startswith('# '):
|
168 |
continue
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
169 |
line = detect_and_convert_links(line)
|
170 |
if render_with_bold:
|
171 |
line = re.sub(r'\*\*(.*?)\*\*', r'<b>\1</b>', line)
|
172 |
-
if auto_bold_numbers and
|
173 |
if not (line.startswith("<b>") and line.endswith("</b>")):
|
174 |
if "<b>" in line and "</b>" in line:
|
175 |
line = re.sub(r'</?b>', '', line)
|
@@ -180,7 +197,7 @@ def markdown_to_pdf_content(markdown_text, render_with_bold, auto_bold_numbers):
|
|
180 |
total_lines = len(pdf_content)
|
181 |
return pdf_content, total_lines
|
182 |
|
183 |
-
def create_pdf(markdown_text, base_font_size, render_with_bold, auto_bold_numbers, enlarge_numbered, num_columns):
|
184 |
buffer = io.BytesIO()
|
185 |
page_width = A4[0] * 2
|
186 |
page_height = A4[1]
|
@@ -312,6 +329,7 @@ with st.sidebar:
|
|
312 |
render_with_bold = st.checkbox("Render with Bold Formatting (remove ** markers)", value=True, key="render_with_bold")
|
313 |
auto_bold_numbers = st.checkbox("Auto Bold Numbered Lines", value=True, key="auto_bold_numbers")
|
314 |
enlarge_numbered = st.checkbox("Enlarge Font Size for Numbered Lines", value=True, key="enlarge_numbered")
|
|
|
315 |
# Add AutoColumns option to automatically determine column count based on line length
|
316 |
auto_columns = st.checkbox("AutoColumns", value=False, key="auto_columns")
|
317 |
|
@@ -393,7 +411,7 @@ with st.sidebar:
|
|
393 |
)
|
394 |
|
395 |
with st.spinner("Generating PDF..."):
|
396 |
-
pdf_bytes = create_pdf(st.session_state.markdown_content, base_font_size, render_with_bold, auto_bold_numbers, enlarge_numbered, num_columns)
|
397 |
|
398 |
with st.container():
|
399 |
pdf_images = pdf_to_image(pdf_bytes)
|
|
|
158 |
combined_text = before + f'<a href="{url}">{label}</a>' + after
|
159 |
return combined_text
|
160 |
|
161 |
+
def markdown_to_pdf_content(markdown_text, render_with_bold, auto_bold_numbers, add_space_before_numbered):
|
162 |
lines = markdown_text.strip().split('\n')
|
163 |
pdf_content = []
|
164 |
number_pattern = re.compile(r'^\d+\.\s')
|
165 |
+
|
166 |
+
# Track if we've seen the first numbered line already
|
167 |
+
first_numbered_seen = False
|
168 |
+
|
169 |
for line in lines:
|
170 |
line = line.strip()
|
171 |
if not line or line.startswith('# '):
|
172 |
continue
|
173 |
+
|
174 |
+
# Check if this is a numbered line
|
175 |
+
is_numbered_line = number_pattern.match(line) is not None
|
176 |
+
|
177 |
+
# Add a blank line before numbered lines (except the first one with "1.")
|
178 |
+
if add_space_before_numbered and is_numbered_line:
|
179 |
+
# Only add space if this isn't the first numbered line
|
180 |
+
if first_numbered_seen and not line.startswith("1."):
|
181 |
+
pdf_content.append("") # Add an empty line
|
182 |
+
# Mark that we've seen a numbered line
|
183 |
+
if not first_numbered_seen:
|
184 |
+
first_numbered_seen = True
|
185 |
+
|
186 |
line = detect_and_convert_links(line)
|
187 |
if render_with_bold:
|
188 |
line = re.sub(r'\*\*(.*?)\*\*', r'<b>\1</b>', line)
|
189 |
+
if auto_bold_numbers and is_numbered_line:
|
190 |
if not (line.startswith("<b>") and line.endswith("</b>")):
|
191 |
if "<b>" in line and "</b>" in line:
|
192 |
line = re.sub(r'</?b>', '', line)
|
|
|
197 |
total_lines = len(pdf_content)
|
198 |
return pdf_content, total_lines
|
199 |
|
200 |
+
def create_pdf(markdown_text, base_font_size, render_with_bold, auto_bold_numbers, enlarge_numbered, num_columns, add_space_before_numbered):
|
201 |
buffer = io.BytesIO()
|
202 |
page_width = A4[0] * 2
|
203 |
page_height = A4[1]
|
|
|
329 |
render_with_bold = st.checkbox("Render with Bold Formatting (remove ** markers)", value=True, key="render_with_bold")
|
330 |
auto_bold_numbers = st.checkbox("Auto Bold Numbered Lines", value=True, key="auto_bold_numbers")
|
331 |
enlarge_numbered = st.checkbox("Enlarge Font Size for Numbered Lines", value=True, key="enlarge_numbered")
|
332 |
+
add_space_before_numbered = st.checkbox("Add Space Ahead of Numbered Lines", value=False, key="add_space_before_numbered")
|
333 |
# Add AutoColumns option to automatically determine column count based on line length
|
334 |
auto_columns = st.checkbox("AutoColumns", value=False, key="auto_columns")
|
335 |
|
|
|
411 |
)
|
412 |
|
413 |
with st.spinner("Generating PDF..."):
|
414 |
+
pdf_bytes = create_pdf(st.session_state.markdown_content, base_font_size, render_with_bold, auto_bold_numbers, enlarge_numbered, num_columns, add_space_before_numbered)
|
415 |
|
416 |
with st.container():
|
417 |
pdf_images = pdf_to_image(pdf_bytes)
|