File size: 10,285 Bytes
f258b94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1f34dc9
 
f258b94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1f34dc9
f258b94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1f34dc9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f258b94
 
1f34dc9
 
f258b94
1f34dc9
 
 
f258b94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1f34dc9
 
f258b94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1f34dc9
f258b94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import streamlit as st
from pathlib import Path
import base64
import datetime
import re
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib.pagesizes import letter, A4, legal, landscape
from reportlab.lib.units import inch
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.lib import colors

# Define layouts using reportlab's pagesizes
LAYOUTS = {
    "A4 Portrait": {"size": A4, "icon": "πŸ“„"},
    "A4 Landscape": {"size": landscape(A4), "icon": "πŸ“„"},
    "Letter Portrait": {"size": letter, "icon": "πŸ“„"},
    "Letter Landscape": {"size": landscape(letter), "icon": "πŸ“„"},
    "Legal Portrait": {"size": legal, "icon": "πŸ“„"},
    "Legal Landscape": {"size": landscape(legal), "icon": "πŸ“„"},
}

# Directory to save the generated PDFs
OUTPUT_DIR = Path("generated_pdfs")
OUTPUT_DIR.mkdir(exist_ok=True)

# ⚠️ UPDATED: Path for the required NON-COLOR emoji font file.
EMOJI_FONT_PATH = Path("NotoEmoji-Regular.ttf")

# Regex to find and wrap emojis for ReportLab
EMOJI_PATTERN = re.compile(
    "["
    "\U0001F600-\U0001F64F"  # emoticons
    "\U0001F300-\U0001F5FF"  # symbols & pictographs
    "\U0001F680-\U0001F6FF"  # transport & map symbols
    "\U0001F700-\U0001F77F"  # alchemical symbols
    "\U0001F780-\U0001F7FF"  # Geometric Shapes Extended
    "\U0001F800-\U0001F8FF"  # Supplemental Arrows-C
    "\U0001F900-\U0001F9FF"  # Supplemental Symbols and Pictographs
    "\U0001FA00-\U0001FA6F"  # Chess Symbols
    "\U0001FA70-\U0001FAFF"  # Symbols and Pictographs Extended-A
    "\U00002702-\U000027B0"  # Dingbats
    "\U000024C2-\U0001F251"
    "]+",
    flags=re.UNICODE,
)

# --- Core PDF Generation Class ---

class PDFGenerator:
    """
    Handles font registration, markdown parsing, and PDF creation.
    """
    def __init__(self, font_path: Path):
        """
        ✨ To start the PDF show, a font we must know.
        Initializes the generator and registers the necessary emoji font.
        """
        self.emoji_font_name = "NotoEmoji"
        self._register_emoji_font(font_path)

    def _register_emoji_font(self, font_path: Path):
        """
        ✍️ Before new fonts can grace the page, first they must be set on stage.
        Registers the TTF font file with ReportLab if the file exists.
        """
        if font_path.exists():
            pdfmetrics.registerFont(TTFont(self.emoji_font_name, str(font_path)))
        else:
            st.error(f"Emoji font not found at '{font_path}'. Emojis will not be rendered. Please download it.")
            self.emoji_font_name = "Helvetica" # Fallback to a standard font

    def _wrap_emojis_for_reportlab(self, text: str) -> str:
        """
        πŸ˜€ To make emojis appear so grand, wrap them with a font command.
        Finds all emojis and wraps them in ReportLab <font> tags.
        """
        if self.emoji_font_name != "NotoEmoji":
            return text
        return EMOJI_PATTERN.sub(lambda m: f'<font name="{self.emoji_font_name}">{m.group(0)}</font>', text)

    def _markdown_to_story(self, markdown_text: str) -> list:
        """
        πŸ“œ From markdown text, a simple scrawl, this story builder answers the call.
        Converts a markdown string πŸ“ into a list of ReportLab Flowables (a 'story').
        """
        styles = getSampleStyleSheet()
        style_normal = styles['BodyText']
        style_h1 = styles['h1']
        style_h2 = styles['h2']
        style_h3 = styles['h3']
        style_code = ParagraphStyle('Code', parent=styles['Normal'], fontName='Courier', textColor=colors.darkred)

        story = []
        lines = markdown_text.split('\n')
        in_code_block = False
        code_block_text = ""

        for line in lines:
            if line.strip().startswith("```"):
                if in_code_block:
                    story.append(Paragraph(code_block_text, style_code))
                    in_code_block = False
                    code_block_text = ""
                else:
                    in_code_block = True
                continue

            if in_code_block:
                escaped_line = line.replace('&', '&amp;').replace('<', '&lt;').replace('>', '&gt;')
                code_block_text += escaped_line + '<br/>'
                continue

            # Process the line for markdown syntax first
            if line.startswith("# "):
                final_text = self._wrap_emojis_for_reportlab(line[2:])
                story.append(Paragraph(final_text, style_h1))
            elif line.startswith("## "):
                final_text = self._wrap_emojis_for_reportlab(line[3:])
                story.append(Paragraph(final_text, style_h2))
            elif line.startswith("### "):
                final_text = self._wrap_emojis_for_reportlab(line[4:])
                story.append(Paragraph(final_text, style_h3))
            elif line.strip().startswith(("* ", "- ")):
                final_text = self._wrap_emojis_for_reportlab(line.strip()[2:])
                story.append(Paragraph(f"β€’ {final_text}", style_normal))
            elif re.match(r'^\d+\.\s', line.strip()):
                final_text = self._wrap_emojis_for_reportlab(line.strip())
                story.append(Paragraph(final_text, style_normal))
            elif line.strip() == "":
                story.append(Spacer(1, 0.2 * inch))
            else:
                # Handle bold/italics, then wrap emojis in the final string
                formatted_line = re.sub(r'\*\*(.*?)\*\*', r'<b>\1</b>', line)
                formatted_line = re.sub(r'_(.*?)_', r'<i>\1</i>', formatted_line)
                final_text = self._wrap_emojis_for_reportlab(formatted_line)
                story.append(Paragraph(final_text, style_normal))

        return story

    def create_pdf(self, md_asset: Path, layout_name: str, layout_properties: dict):
        """
        πŸ“„ With content and a layout's grace, this function builds the PDF space.
        Creates a single PDF file πŸ“„ from a given markdown file πŸ“.
        """
        try:
            md_content = md_asset.read_text(encoding="utf-8")
            date_str = datetime.datetime.now().strftime("%Y-%m-%d")
            output_filename = f"{md_asset.stem}_{layout_name.replace(' ', '-')}_{date_str}.pdf"
            output_path = OUTPUT_DIR / output_filename

            doc = SimpleDocTemplate(
                str(output_path),
                pagesize=layout_properties.get("size", A4),
                rightMargin=inch, leftMargin=inch,
                topMargin=inch, bottomMargin=inch
            )
            story = self._markdown_to_story(md_content)
            doc.build(story)
        except Exception as e:
            st.error(f"Failed to process {md_asset.name} with ReportLab: {e}")


# --- Streamlit UI and File Handling ---

def get_file_download_link(file_path: Path) -> str:
    """
    πŸ”— To grab your file and not delay, a special link is paved today.
    """
    with open(file_path, "rb") as f:
        data = base64.b64encode(f.read()).decode()
    return f'<a href="data:application/octet-stream;base64,{data}" download="{file_path.name}">Download</a>'

def display_file_explorer():
    """
    πŸ“‚ To see your files, both old and new, this handy explorer gives a view.
    """
    st.header("πŸ“‚ File Explorer")
    st.subheader("Source Markdown Files (.md)")
    md_files = list(Path(".").glob("*.md"))
    if not md_files:
        st.info("No Markdown files found. A `sample.md` has been created for you.")
    else:
        for md_file in md_files:
            col1, col2 = st.columns([0.8, 0.2])
            with col1:
                st.write(f"πŸ“ `{md_file.name}`")
            with col2:
                st.markdown(get_file_download_link(md_file), unsafe_allow_html=True)

    st.subheader("Generated PDF Files")
    pdf_files = sorted(list(OUTPUT_DIR.glob("*.pdf")), key=lambda p: p.stat().st_mtime, reverse=True)
    if not pdf_files:
        st.info("No PDFs generated yet. Click the button above to start.")
    else:
        for pdf_file in pdf_files:
            col1, col2 = st.columns([0.8, 0.2])
            with col1:
                st.write(f"πŸ“„ `{pdf_file.name}`")
            with col2:
                st.markdown(get_file_download_link(pdf_file), unsafe_allow_html=True)

# --- Main App Execution ---

def main():
    """
    πŸš€ To run the app and make it go, call this main function, you know!
    """
    st.set_page_config(layout="wide", page_title="PDF Generator")
    st.title("πŸ“„ Markdown to PDF Generator")
    st.markdown("This tool converts all `.md` files in this directory to PDF. It now supports emojis! πŸ‘")

    if not any(Path(".").glob("*.md")):
        with open("sample.md", "w", encoding="utf-8") as f:
            f.write("# Sample Document πŸ‘\n\nThis is a sample markdown file. **ReportLab** is creating the PDF. Emojis like πŸš€ and πŸ’‘ should now appear correctly.\n\n### Features\n- Item 1\n- Item 2\n\n```\ndef hello_world():\n    print(\"Hello, PDF! πŸ‘‹\")\n```\n")
        st.rerun()

    pdf_generator = PDFGenerator(EMOJI_FONT_PATH)

    if st.button("πŸš€ Generate PDFs from all Markdown Files", type="primary"):
        markdown_files = list(Path(".").glob("*.md"))
        if not markdown_files:
            st.warning("No `.md` files found. Please add a markdown file to the directory.")
        else:
            total_pdfs = len(markdown_files) * len(LAYOUTS)
            progress_bar = st.progress(0, text="Starting PDF generation...")
            pdf_count = 0
            with st.spinner("Generating PDFs... Please wait."):
                for md_file in markdown_files:
                    st.info(f"Processing: **{md_file.name}**")
                    for name, properties in LAYOUTS.items():
                        pdf_generator.create_pdf(md_file, name, properties)
                        pdf_count += 1
                        progress_bar.progress(pdf_count / total_pdfs, f"Generated {pdf_count}/{total_pdfs} PDFs...")
            st.success("βœ… PDF generation complete!")
            st.balloons()
            st.rerun()

    display_file_explorer()

if __name__ == "__main__":
    main()