You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ilinx/scripts/html_linear_series.py

82 lines
2.1 KiB
Python

# This script takes a txt file and use a template to create
# html pages linked in a linear sequence and containing each a line of the txt
# It generate also the css out of a template.
# I would like to make also a circular version. A loop that arrived at the last page reconnects with the first page.
# and eventually try other possible sequences and links (such as metarefresh etc..)
# This could become a commad, like > htgen [chose if linear (-l) or circular (-c) or ...] [input file] [input html template]
from jinja2 import Template
file = "./filoXX_fN.txt"
#ask name input file and use it also for name output file
# read formatted text
with open(file, "r") as sentences_file:
for n, line in enumerate(sentences_file.readlines()):
# CHECK IF THE FORMAT IS CORRECT FIRST - comment all the above print()
print(line)
# format text again if needed - comment if not needed
line = line.replace("\n", "")
# link to next page
nxt_n = n + 1
nxt_n = str(nxt_n)
file_name_next = "file_{}.html".format(nxt_n.zfill(2))
# file name
n = str(n)
file_name = "file_{}.html".format(n.zfill(2))
# jinja html template
template_html = Template('''
<!DOCTYPE html>
<html>
<head>
<title>Tao one</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<p> {{ sentence }} </p>
<a href="{{ next }}" class="button">next</a>
</body>
</html>
''')
# render template
html_list = template_html.render(sentence = line, next = file_name_next)
# write html
with open(file_name, "w") as output:
output.write(html_list)
# css template
template_css = '''
body {
background-color: black;
color: red;
}
p {font-size: 100px;}
.button {
font: bold 11px Arial;
text-decoration: none;
background-color: #EEEEEE;
color: #333333;
padding: 2px 6px 2px 6px;
border-top: 1px solid #CCCCCC;
border-right: 1px solid #333333;
border-bottom: 1px solid #333333;
border-left: 1px solid #CCCCCC;
}
'''
# write css
with open("style.css", "w") as output:
output.write(template_css)