import re,os,shutil,zipfile,io
from PIL import Image,ImageDraw
SRC='/mnt/user-data/outputs/site/'
OUT='/tmp/export/'
shutil.rmtree(OUT,ignore_errors=True)
for d in ['css','js','images','videos','icons']: os.makedirs(OUT+d)
s=open(SRC+'index.src.html',encoding='utf-8').read()
# --- 1. split CSS / JS
css=re.search(r'',s,re.S).group(1)
js=re.findall(r'',s,re.S)[-1]
# --- 2. reorganise assets: images/ and videos/
def remap(t):
t=re.sub(r'assets/(v\d+\.mp4)',r'videos/\1',t)
t=re.sub(r'assets/([A-Za-z0-9_.\-]+\.(?:jpg|png))',r'images/\1',t)
return t
js=remap(js);css=remap(css)
body_html=remap(s)
used=set(re.findall(r'(?:images|videos)/[A-Za-z0-9_.\-]+',js+body_html))
for f in sorted(used):
src=SRC+'assets/'+os.path.basename(f)
shutil.copy(src,OUT+f)
# --- 3. index.html
head_end=body_html.index('')+len('')
head=body_html[:head_end]
head=re.sub(r']*>','',head)
head=head.replace('','')
meta='''
'''
rest=body_html[style_end:]
# replace inline script by external file
m=list(re.finditer(r'',rest,re.S))[-1]
rest=rest[:m.start()]+''+rest[m.end():]
index=head+meta+rest
# head closing: ensure present after meta
if '' not in index: index=index.replace('
\n
''')
open(OUT+'manifest.webmanifest','w').write('''{
"name": "MaroTech",
"short_name": "MaroTech",
"description": "Boutique high-tech au Maroc",
"start_url": "/",
"display": "standalone",
"background_color": "#ffffff",
"theme_color": "#0b3a8f",
"lang": "fr",
"icons": [
{"src": "icons/icon-192.png", "sizes": "192x192", "type": "image/png"},
{"src": "icons/icon-512.png", "sizes": "512x512", "type": "image/png"}
]
}
''')
# --- 5. config files
open(OUT+'.htaccess','w').write('''# MaroTech - configuration Apache / LiteSpeed (Hostinger)
Options -Indexes
DirectoryIndex index.html
# 1) Forcer HTTPS (activez d'abord le certificat SSL dans hPanel)
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# 2) Page d'erreur 404 personnalisee
ErrorDocument 404 /404.html
# 3) Types de fichiers
AddType image/svg+xml .svg
AddType application/manifest+json .webmanifest
AddType video/mp4 .mp4
AddDefaultCharset UTF-8
# 4) Compression
AddOutputFilterByType DEFLATE text/html text/css application/javascript text/javascript application/json image/svg+xml application/manifest+json
# 5) Cache navigateur (le site charge plus vite pour les clients qui reviennent)
ExpiresActive On
ExpiresByType image/jpeg "access plus 6 months"
ExpiresByType image/png "access plus 6 months"
ExpiresByType image/svg+xml "access plus 6 months"
ExpiresByType image/x-icon "access plus 6 months"
ExpiresByType video/mp4 "access plus 6 months"
ExpiresByType text/css "access plus 1 week"
ExpiresByType application/javascript "access plus 1 week"
ExpiresByType text/javascript "access plus 1 week"
ExpiresByType text/html "access plus 0 seconds"
# 6) En-tetes de securite
Header always set X-Content-Type-Options "nosniff"
Header always set X-Frame-Options "SAMEORIGIN"
Header always set Referrer-Policy "strict-origin-when-cross-origin"
# 7) Documentation non publique
Require all denied
''')
open(OUT+'robots.txt','w').write('User-agent: *\nAllow: /\n')
open(OUT+'404.html','w',encoding='utf-8').write('''
Page introuvable - MaroTech
''')
print('files:',sum(len(f) for _,_,f in os.walk(OUT)))