File size: 6,892 Bytes
2132d78
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
244
245
246
247
248
249
250
251
252
253
254
255
256
"""
HTML generation and file management for dev flow.
"""
import os
from datetime import datetime
from pathlib import Path
from logger import logger
from config import HTML_OUTPUT_DIR


class HTMLHandler:
    """Handles HTML generation and file management."""

    def __init__(self, output_dir=HTML_OUTPUT_DIR):
        """Initialize HTML handler."""
        self.output_dir = output_dir
        Path(self.output_dir).mkdir(exist_ok=True)
        logger.debug(f"HTMLHandler initialized with output dir: {output_dir}")

    def generate_html(self, title, body, tags, image_url=None, original_url=None):
        """
        Generate HTML content from article data.

        Args:
            title (str): Article title
            body (str): Article body (HTML)
            tags (list): List of tags
            image_url (str, optional): Featured image URL
            original_url (str, optional): Original article URL

        Returns:
            str: Generated HTML content
        """
        try:
            # Format tags as HTML
            tags_html = " ".join([f'<span class="tag">{tag}</span>' for tag in tags])

            # Build featured image section
            image_section = ""
            if image_url:
                image_section = (
                    f'<div class="featured-image">'
                    f'<img src="{image_url}" alt="{title}">'
                    f"</div>"
                )

            # Generate complete HTML
            html_content = f"""<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{title}</title>
    <style>
        * {{
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }}

        body {{
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
            line-height: 1.6;
            color: #333;
            background-color: #f5f5f5;
        }}

        .container {{
            max-width: 800px;
            margin: 0 auto;
            padding: 40px 20px;
            background-color: white;
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
        }}

        header {{
            margin-bottom: 30px;
            border-bottom: 3px solid #007bff;
            padding-bottom: 20px;
        }}

        h1 {{
            font-size: 2.2em;
            margin-bottom: 15px;
            line-height: 1.3;
            color: #1a1a1a;
        }}

        .meta {{
            font-size: 0.9em;
            color: #666;
            margin-bottom: 10px;
        }}

        .tags {{
            display: flex;
            flex-wrap: wrap;
            gap: 8px;
            margin-top: 10px;
        }}

        .tag {{
            background-color: #e9ecef;
            color: #007bff;
            padding: 4px 12px;
            border-radius: 20px;
            font-size: 0.85em;
            font-weight: 500;
        }}

        .featured-image {{
            margin: 30px 0;
            text-align: center;
        }}

            .featured-image img {{
                max-width: 100%;
                height: auto;
                border-radius: 8px;
                box-shadow: 0 2px 6px rgba(0, 0, 0, 0.1);
            }}

        .content {{
            font-size: 1.1em;
            margin-bottom: 30px;
            line-height: 1.8;
        }}

        .content p {{
            margin-bottom: 15px;
            text-align: justify;
        }}

        .content figure {{
            margin: 20px 0;
        }}

        .content img {{
            max-width: 100%;
            height: auto;
            border-radius: 6px;
        }}

        footer {{
            margin-top: 30px;
            padding-top: 20px;
            border-top: 1px solid #ddd;
            font-size: 0.9em;
            color: #666;
            text-align: center;
        }}

        /* Desktop: max-width 1200px and above */
        @media (min-width: 1200px) {{
            .featured-image img {{
                max-width: 300px;
            }}
        }}

        /* Tablet: 768px to 1199px */
        @media (min-width: 768px) and (max-width: 1199px) {{
            .featured-image img {{
                max-width: 250px;
            }}
        }}

            /* Responsive container and content font size remain */
            @media (max-width: 600px) {{
                .container {{
                    padding: 20px;
                }}

                h1 {{
                    font-size: 1.6em;
                }}

                .content {{
                    font-size: 1em;
                }}
            }}
    </style>
</head>
<body>
    <div class="container">
        <header>
            <h1>{title}</h1>
            <div class="meta">
                Generated on {datetime.now().strftime('%B %d, %Y at %I:%M %p')}
            </div>
            <div class="tags">
                {tags_html}
            </div>
        </header>

        {image_section}

        <div class="content">
            {body}
        </div>

    </div>
</body>
</html>"""
            logger.debug("HTML content generated successfully")
            return html_content

        except Exception as e:
            logger.error(f"Failed to generate HTML: {e}")
            raise

    def save_html(self, html_content, filename=None):
        """
        Save HTML content to file.

        Args:
            html_content (str): HTML content to save
            filename (str, optional): Filename to use. If None, generates timestamp-based name.

        Returns:
            str: Full path to saved file
        """
        try:
            if not filename:
                timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
                filename = f"article_{timestamp}.html"

            file_path = os.path.join(self.output_dir, filename)

            with open(file_path, "w", encoding="utf-8") as f:
                f.write(html_content)

            logger.info(f"✓ HTML file saved: {file_path}")
            return file_path

        except Exception as e:
            logger.error(f"Failed to save HTML file: {e}")
            raise

    def generate_and_save(self, title, body, tags, image_url=None, original_url=None, filename=None):
        """
        Generate and save HTML in one call.

        Args:
            title (str): Article title
            body (str): Article body (HTML)
            tags (list): List of tags
            image_url (str, optional): Featured image URL
            original_url (str, optional): Original article URL
            filename (str, optional): Custom filename

        Returns:
            str: Full path to saved file
        """
        html_content = self.generate_html(title, body, tags, image_url, original_url)
        return self.save_html(html_content, filename)