|
|
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): |
|
|
|
|
|
cleaned = text.strip() |
|
|
if cleaned.startswith("```markdown"): |
|
|
cleaned = cleaned[len("```markdown"):].strip() |
|
|
if cleaned.endswith("```"): |
|
|
cleaned = cleaned[:-3].strip() |
|
|
|
|
|
|
|
|
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) |
|
|
|
|
|
|
|
|
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"]) |
|
|
|
|
|
|
|
|
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") |
|
|
|
|
|
|
|
|
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() |
|
|
|
|
|
|
|
|
sections = cleaned_markdown.split("## ") |
|
|
for raw_section in sections: |
|
|
if not raw_section.strip(): |
|
|
continue |
|
|
|
|
|
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 |
|
|
|
|
|
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") |
|
|
|
|
|
pattern = r"- \*\*Q: (.*?)\*\*.*?\*\*A:\*\* (.*?)\n" |
|
|
qa_pairs = re.findall(pattern, markdown_text, re.DOTALL) |
|
|
|
|
|
|
|
|
for i, (question, answer) in enumerate(qa_pairs, 1): |
|
|
with st.expander(f"Q{i}: {question.strip()}"): |
|
|
st.markdown(f"**Answer:** {answer.strip()}") |
|
|
|