File size: 6,548 Bytes
77c8775 d3f8648 77c8775 da9117a 77c8775 da9117a 24a0b74 0c7bb1d 24a0b74 da9117a 0c7bb1d da9117a 11fc5ef e210c0a 11fc5ef e210c0a da792ab e210c0a da792ab e210c0a da792ab e210c0a da792ab 11fc5ef 918f390 d3f8648 7438398 d3f8648 918f390 5bcf38b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 | import streamlit as st
import re
st.set_page_config(page_title="π¦ Markdown Formatter", layout="centered")
st.title("π Markdown Formatter App")
method = st.selectbox("Choose a formatting method:", ["Select a method", "Boxing Method","Outline Method", "Raw Markdown","Flashcard", "Cornell Method"])
uploaded_file = st.file_uploader("Upload a Markdown (.md) file", type=["md"])
if uploaded_file and method != "Select a method":
markdown_text = uploaded_file.read().decode("utf-8")
if method == "Raw Markdown":
st.markdown(markdown_text)
# elif method == "Boxing Method":
# def parse_markdown_into_boxes(text):
# pattern = r'(^#+ .*$)'
# parts = re.split(pattern, text, flags=re.MULTILINE)
# sections = []
# for i in range(1, len(parts), 2):
# header = parts[i].strip()
# content = parts[i + 1].strip() if i + 1 < len(parts) else ""
# sections.append({"header": header, "content": content})
# return sections
# st.subheader("π¦ Formatted Output")
# for section in parse_markdown_into_boxes(markdown_text):
# with st.container(border=True):
# if section["header"]:
# st.markdown(f"#### {section['header']}")
# st.markdown(section["content"])
elif method == "Boxing Method":
def parse_markdown_into_boxes(text):
# Remove ```markdown block if it exists
cleaned = text.strip()
if cleaned.startswith("```markdown"):
cleaned = cleaned[len("```markdown"):].strip()
if cleaned.endswith("```"):
cleaned = cleaned[:-3].strip()
# Split based on Markdown headers (## Box or ## Summary)
pattern = r'(^## .*$)'
parts = re.split(pattern, cleaned, flags=re.MULTILINE)
sections = []
for i in range(1, len(parts), 2):
header = parts[i].strip()
content = parts[i + 1].strip() if i + 1 < len(parts) else ""
sections.append({"header": header, "content": content})
return sections
st.subheader("π¦ Boxing Method Output")
all_sections = parse_markdown_into_boxes(markdown_text)
boxes = [s for s in all_sections if not s["header"].lower().startswith("## summary")]
summary = next((s for s in all_sections if s["header"].lower().startswith("## summary")), None)
# Display boxes in two columns
col1, col2 = st.columns(2)
for i, section in enumerate(boxes):
target_col = col1 if i % 2 == 0 else col2
with target_col.container(border=True):
st.markdown(f"#### {section['header']}")
st.markdown(section["content"])
# Display summary below full width
if summary:
st.markdown("---")
with st.container(border=True):
st.markdown(f"### {summary['header']}")
st.markdown(summary['content'])
# elif method == "Outline Method":
# st.subheader("π Outline Method Output")
# sections = markdown_text.strip().split("\n\n")
# for section in sections:
# lines = section.strip().split("\n")
# if lines:
# title = lines[0]
# content = "\n".join(lines[1:]) if len(lines) > 1 else ""
# with st.container(border=True):
# st.markdown(f"**{title}**")
# st.markdown(content)
elif method == "Outline Method":
st.subheader("π Outline Method Output")
# Remove ```markdown code fences if present
cleaned_markdown = markdown_text.strip()
if cleaned_markdown.startswith("```markdown"):
cleaned_markdown = cleaned_markdown[len("```markdown"):].strip()
if cleaned_markdown.endswith("```"):
cleaned_markdown = cleaned_markdown[:-3].strip()
# Split into sections using '## ' headers
sections = cleaned_markdown.split("## ")
for raw_section in sections:
if not raw_section.strip():
continue # skip empty sections
lines = raw_section.strip().splitlines()
title_line = lines[0] if lines else "Untitled Section"
content_lines = lines[1:]
with st.container(border=True):
st.markdown(f"### {title_line.strip()}")
for line in content_lines:
stripped = line.lstrip()
indent_level = (len(line) - len(stripped)) // 2 # assume 2 spaces per indent
if stripped.startswith("- "):
st.markdown(f"{' ' * 4 * indent_level}{stripped}")
else:
st.markdown(stripped)
elif method == "Cornell Method":
st.subheader("π Cornell Notes Formatted")
def parse_markdown_sections(text):
sections = {}
current_section = None
for line in text.splitlines():
if line.strip().startswith("## "):
current_section = line.replace("##", "").strip()
sections[current_section] = ""
elif current_section:
sections[current_section] += line + "\n"
return sections
sections = parse_markdown_sections(markdown_text)
cue_column = sections.get("Cues", "Cues Column not found.")
notes_section = sections.get("Notes", "Notes Section not found.")
summary = sections.get("Summary", "Summary not found.")
col1, col2 = st.columns([1, 2])
with col1:
st.markdown("### Cue Column")
st.markdown(cue_column)
with col2:
st.markdown("### Notes Section")
st.markdown(notes_section)
st.markdown("---")
st.markdown("### Summary")
st.markdown(summary)
elif method == "Flashcard":
st.subheader("π Flashcards")
# Extract Q&A pairs using regex
pattern = r"- \*\*Q: (.*?)\*\*.*?\*\*A:\*\* (.*?)\n"
qa_pairs = re.findall(pattern, markdown_text, re.DOTALL)
# Display in expandable flashcards
for i, (question, answer) in enumerate(qa_pairs, 1):
with st.expander(f"Q{i}: {question.strip()}"):
st.markdown(f"**Answer:** {answer.strip()}")
|