Pour un fichier template html donné :
Code : Tout sélectionner
<!DOCTYPE html>
<html lang="{LANG}">
<head>
<title>{TITLE}</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="bootstrap.min.css">
<script src="bootstrap.min.js"></script>
<style>
body { background-color: #ccc; }
/* Set height of the grid so .sidenav can be 100% (adjust if needed) */
.row.content {height: auto;}
/* Set gray background color and 100% height */
.sidenav {
background-color: #ccc;
height: 100%;
}
.brd-l {
border-left: 1px dotted #333;
background-color: #eee;
}
article {
background-color: #eee;
border-bottom: 1px solid #333;
}
header {
border-bottom: 1px dotted #333;
padding: 10px;
}
/* Set black background color, white text and some padding */
footer {
background-color: #555;
color: #ccc;
padding: 15px;
}
footer a {
color: #fff;
}
footer a:hover {
color: #ccc;
}
/* On small screens, set height to 'auto' for sidenav and grid */
@media screen and (max-width: 767px) {
.sidenav {
height: auto;
padding: 15px;
}
.row.content {height: auto;}
}
</style>
</head>
<body>
<div class="container-fluid">
<div class="row content">
<div class="col-sm-3 sidenav">
<h1>{NAME} :: {DESCR}</h1>
<h2>{ABOUT}</h2>
<ul id="nav">
<!-- {LINK} -->
</ul>
</div>
<div class="col-sm-9 brd-l">
<section>
<!-- {ARTICLE} -->
</section>
</div>
</div>
</div>
<footer class="container-fluid">
<p> - {NAME} :: <a href="{URL_WEB}">{DESCR}</a> - {COPYRIGHT} - {LICENCE}</p>
</footer>
</body>
</html>
Code : Tout sélectionner
patterns = [
'{ABOUT}',
'{COPYRIGHT}',
'{DESCR}',
'{LANG}',
'{LICENCE}',
'{NAME}',
'{TITLE}',
'{URL_WEB}',
]
replaces = [
self.text['about']['historic'],
self.text['about']['copyright'],
self.DESCR,
self.config['lang'],
self.text['about']['licence'],
self.NAME,
self.title['html'],
self.url['web'],
]
with open(filename, 'r') as rfile:
lines = rfile.readlines()
self.log.debug('=> Sed Lines :: Lines: %s' % lines)
with open(filename, 'w') as wfile:
for line in lines:
search = False
for key, pattern in enumerate(patterns):
if pattern in line:
wfile.write(re.sub(
r'' + pattern, replaces[key], line))
search = True
if not search:
wfile.write(line)
Ainsi, il me restitue :
Code : Tout sélectionner
(...)
<h1>{NAME} :: Yet Uploader Pixxie</h1>
<h1>YUP :: {DESCR}</h1>
(...)
<footer class="container-fluid">
<p> - {NAME} :: <a href="{URL_WEB}">{DESCR}</a> - © 2015-2018 - {LICENCE}</p>
<p> - {NAME} :: <a href="{URL_WEB}">Yet Uploader Pixxie</a> - {COPYRIGHT} - {LICENCE}</p>
<p> - {NAME} :: <a href="{URL_WEB}">{DESCR}</a> - {COPYRIGHT} - License : <a href="LICENSE">OpenBSD</a></p>
<p> - YUP :: <a href="{URL_WEB}">{DESCR}</a> - {COPYRIGHT} - {LICENCE}</p>
<p> - {NAME} :: <a href="https://framagit.org/hucste/YUP.py/">{DESCR}</a> - {COPYRIGHT} - {LICENCE}</p>
</footer>
(...)