|
| 1 | +import os |
| 2 | +import zipfile |
| 3 | + |
| 4 | +# Folder structure |
| 5 | +folder_name = 'blog-website' |
| 6 | +os.makedirs(f'{folder_name}/css', exist_ok=True) |
| 7 | +os.makedirs(f'{folder_name}/js', exist_ok=True) |
| 8 | +os.makedirs(f'{folder_name}/assets/images', exist_ok=True) |
| 9 | + |
| 10 | +# Files content |
| 11 | +files = { |
| 12 | + f'{folder_name}/index.html': """<!DOCTYPE html> |
| 13 | +<html lang='en'> |
| 14 | +<head> |
| 15 | +<meta charset='UTF-8'> |
| 16 | +<meta name='viewport' content='width=device-width, initial-scale=1.0'> |
| 17 | +<title>My Blog</title> |
| 18 | +<link rel='stylesheet' href='css/styles.css'> |
| 19 | +</head> |
| 20 | +<body> |
| 21 | +<header> |
| 22 | +<nav> |
| 23 | +<h1 class='logo'>My Blog</h1> |
| 24 | +<ul class='nav-links'> |
| 25 | +<li><a href='index.html'>Home</a></li> |
| 26 | +<li><a href='blog.html'>Blog</a></li> |
| 27 | +<li><a href='about.html'>About</a></li> |
| 28 | +<li><a href='contact.html'>Contact</a></li> |
| 29 | +<li><a href='admin.html'>Admin</a></li> |
| 30 | +</ul> |
| 31 | +<button id='theme-toggle'>🌙</button> |
| 32 | +</nav> |
| 33 | +</header> |
| 34 | +<main> |
| 35 | +<section class='hero'> |
| 36 | +<h2>Welcome to My Blog</h2> |
| 37 | +<p>Discover articles on web development, design, and more!</p> |
| 38 | +</section> |
| 39 | +<section class='featured-posts'> |
| 40 | +<h3>Featured Posts</h3> |
| 41 | +<div class='posts-grid' id='posts-grid'></div> |
| 42 | +</section> |
| 43 | +</main> |
| 44 | +<footer> |
| 45 | +<p>© 2025 My Blog. All rights reserved.</p> |
| 46 | +</footer> |
| 47 | +<script src='js/script.js'></script> |
| 48 | +</body> |
| 49 | +</html>""", |
| 50 | + |
| 51 | + f'{folder_name}/admin.html': """<!DOCTYPE html> |
| 52 | +<html lang='en'> |
| 53 | +<head> |
| 54 | +<meta charset='UTF-8'> |
| 55 | +<meta name='viewport' content='width=device-width, initial-scale=1.0'> |
| 56 | +<title>Admin Panel - My Blog</title> |
| 57 | +<link rel='stylesheet' href='css/styles.css'> |
| 58 | +<style> |
| 59 | +body { padding: 2rem; } |
| 60 | +.post-form { margin-bottom: 2rem; display: flex; flex-direction: column; gap: 1rem; } |
| 61 | +.post-form input, .post-form textarea { padding: 0.5rem; width: 100%; } |
| 62 | +.posts-list { margin-top: 2rem; } |
| 63 | +.post-item { border: 1px solid #ccc; padding: 1rem; margin-bottom: 1rem; display: flex; justify-content: space-between; align-items: center; } |
| 64 | +.post-actions button { margin-left: 0.5rem; } |
| 65 | +</style> |
| 66 | +</head> |
| 67 | +<body> |
| 68 | +<h1>Admin Panel</h1> |
| 69 | +<form class='post-form' id='post-form'> |
| 70 | +<input type='text' id='post-title' placeholder='Post Title' required> |
| 71 | +<textarea id='post-content' rows='5' placeholder='Post Content' required></textarea> |
| 72 | +<input type='file' id='post-image' accept='image/*'> |
| 73 | +<button type='submit'>Add / Update Post</button> |
| 74 | +</form> |
| 75 | +<div class='posts-list' id='posts-list'></div> |
| 76 | +<script src='js/admin.js'></script> |
| 77 | +</body> |
| 78 | +</html>""", |
| 79 | + |
| 80 | + f'{folder_name}/css/styles.css': """* { margin: 0; padding: 0; box-sizing: border-box; } |
| 81 | +body { font-family: Arial, sans-serif; transition: background 0.3s, color 0.3s; } |
| 82 | +body.light { background: #fff; color: #000; } |
| 83 | +body.dark { background: #121212; color: #fff; } |
| 84 | +header { background: #6200ee; color: #fff; padding: 1rem 2rem; display: flex; justify-content: space-between; align-items: center; } |
| 85 | +.nav-links { list-style: none; display: flex; gap: 1rem; } |
| 86 | +.nav-links a { color: #fff; text-decoration: none; } |
| 87 | +#theme-toggle { background: none; border: none; font-size: 1.2rem; cursor: pointer; } |
| 88 | +.hero { text-align: center; padding: 4rem 2rem; } |
| 89 | +.featured-posts { padding: 2rem; } |
| 90 | +.posts-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 1rem; } |
| 91 | +.posts-grid img { width: 100%; border-radius: 8px; } |
| 92 | +footer { text-align: center; padding: 1rem; background: #eee; margin-top: 2rem; } |
| 93 | +body.dark footer { background: #1f1f1f; }""", |
| 94 | + |
| 95 | + f'{folder_name}/js/script.js': """const toggle = document.getElementById('theme-toggle'); |
| 96 | +const body = document.body; |
| 97 | +if(localStorage.getItem('theme') === 'dark') body.classList.add('dark'); |
| 98 | +else body.classList.add('light'); |
| 99 | +toggle.addEventListener('click', () => { |
| 100 | + body.classList.toggle('dark'); |
| 101 | + body.classList.toggle('light'); |
| 102 | + localStorage.setItem('theme', body.classList.contains('dark') ? 'dark' : 'light'); |
| 103 | +}); |
| 104 | +
|
| 105 | +// Load posts in homepage |
| 106 | +const postsGrid = document.getElementById('posts-grid'); |
| 107 | +const posts = JSON.parse(localStorage.getItem('posts')) || []; |
| 108 | +if(postsGrid) { |
| 109 | + posts.forEach(post => { |
| 110 | + const postDiv = document.createElement('div'); |
| 111 | + postDiv.className = 'post-card'; |
| 112 | + postDiv.innerHTML = `<h4>${post.title}</h4>${post.image ? `<img src='${post.image}' alt='${post.title}'>` : ''}<p>${post.content.substring(0, 100)}...</p>`; |
| 113 | + postsGrid.appendChild(postDiv); |
| 114 | + }); |
| 115 | +} """, |
| 116 | + |
| 117 | + f'{folder_name}/js/admin.js': """const postForm = document.getElementById('post-form'); |
| 118 | +const postsList = document.getElementById('posts-list'); |
| 119 | +let posts = JSON.parse(localStorage.getItem('posts')) || []; |
| 120 | +let editingIndex = null; |
| 121 | +
|
| 122 | +function renderPosts() { |
| 123 | + postsList.innerHTML = ''; |
| 124 | + posts.forEach((post, index) => { |
| 125 | + const postDiv = document.createElement('div'); |
| 126 | + postDiv.className = 'post-item'; |
| 127 | + postDiv.innerHTML = `<div><h4>${post.title}</h4>${post.image ? `<img src='${post.image}' width='100'>` : ''}<p>${post.content.substring(0, 50)}...</p></div><div class='post-actions'><button onclick='editPost(${index})'>Edit</button><button onclick='deletePost(${index})'>Delete</button></div>`; |
| 128 | + postsList.appendChild(postDiv); |
| 129 | + }); |
| 130 | +} |
| 131 | +
|
| 132 | +postForm.addEventListener('submit', (e) => { |
| 133 | + e.preventDefault(); |
| 134 | + const title = document.getElementById('post-title').value; |
| 135 | + const content = document.getElementById('post-content').value; |
| 136 | + const file = document.getElementById('post-image').files[0]; |
| 137 | + if(file){ |
| 138 | + const reader = new FileReader(); |
| 139 | + reader.onload = () => { savePost(title, content, reader.result); }; |
| 140 | + reader.readAsDataURL(file); |
| 141 | + } else { savePost(title, content, editingIndex !== null ? posts[editingIndex].image : null); } |
| 142 | +}); |
| 143 | +
|
| 144 | +function savePost(title, content, image) { |
| 145 | + const postData = {title, content, image}; |
| 146 | + if(editingIndex !== null){ posts[editingIndex] = postData; editingIndex = null; } else { posts.push(postData); } |
| 147 | + localStorage.setItem('posts', JSON.stringify(posts)); |
| 148 | + postForm.reset(); |
| 149 | + renderPosts(); |
| 150 | +} |
| 151 | +
|
| 152 | +window.editPost = function(index){ |
| 153 | + editingIndex = index; |
| 154 | + const post = posts[index]; |
| 155 | + document.getElementById('post-title').value = post.title; |
| 156 | + document.getElementById('post-content').value = post.content; |
| 157 | +} |
| 158 | +
|
| 159 | +window.deletePost = function(index){ |
| 160 | + if(confirm('Are you sure you want to delete this post?')){ |
| 161 | + posts.splice(index, 1); |
| 162 | + localStorage.setItem('posts', JSON.stringify(posts)); |
| 163 | + renderPosts(); |
| 164 | + } |
| 165 | +} |
| 166 | +
|
| 167 | +renderPosts();""" |
| 168 | +} |
| 169 | + |
| 170 | +# Create files |
| 171 | +for path, content in files.items(): |
| 172 | + with open(path, 'w', encoding='utf-8') as f: |
| 173 | + f.write(content) |
| 174 | + |
| 175 | +# Create ZIP |
| 176 | +zipf = zipfile.ZipFile('blog_website.zip', 'w', zipfile.ZIP_DEFLATED) |
| 177 | +for root, dirs, fs in os.walk(folder_name): |
| 178 | + for file in fs: |
| 179 | + zipf.write(os.path.join(root, file), os.path.relpath(os.path.join(root, file), folder_name)) |
| 180 | +zipf.close() |
| 181 | + |
| 182 | +print('blog_website.zip file is now ready for download.') |
0 commit comments