I often work on embedded system projects in which I have a number of documentation files, such as microprocessor datasheets, schematics, bills of materials, etc. I find it convenient to create a simple little HTML file that contains the file descriptions and a hyperlink to each file. I used to create these all by hand, but I now have an automated script that does that for me. I still have to edit the file to include the descriptions of the files, but this creates both an index.html
and style.css
file as a start. I'd be interested in a review of both the Python code and the HTML and Javascript and CSS that it creates. The code is intended solely for use on a Linux box.
Invocation and use
This is designed to placed in the path and invoked from the command line from within the directory to be documented. Then the resulting HTML file is invoked in the same way as with firefox index.html
. Although it doesn't make much difference in this case, the intent is that the HTML file is not served from a web server but simply interpreted from the file system so that the entire project, with documentation, can be put on a CD or DVD (or other read-only media) and used without having to run a web server.
docdir.py
#! /usr/bin/python3
from os import walk
style = '''\
body
{
font-style:normal;
font-family:sans-serif,Georgia, Helvetica, Arial, sans-serif;
background-color:white;
color:black;
margin-left:5%;
}
div.navbar
{
text-align:center;
}
h1
{
text-indent:1em;
color:white;
background-color:navy;
}
h2
{
text-indent:2em;
color:white;
background-color:navy;
}
h3
{
margin-left:1em;
margin-right:1em;
text-indent:2em;
color:white;
background-color:navy;
}
div.trailer table
{
width:100%;
border-style:none;
padding-left:0;
}
table
{
border-style:solid;
}
td
{
padding-left:20px;
}
caption
{
font-style:oblique;
}'''
index_tail='''\
];
function loadLinks() {
var table = document.getElementById("docTable");
links.reverse();
links.forEach(function(element, index, array) {
var row = table.insertRow(0);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
cell1.innerHTML = "<a href=\\""+element.name + "\\">" + element.name + "</a>";
cell2.innerHTML = element.desc;
});
}
</script>
</head>
<body onload="loadLinks()">
<h1>Documents</h1>
<p>
These directories may contain <b>proprietary and confidential</b> information.
</p>
<p>
<table id="docTable">
<caption>Document list</caption>
</table>
<p> </p>
<table>
<tr>
<td>
<a href="index.html">index.html</a>
</td>
<td>
this file
</td>
</tr>
</table>
</body>
</html>'''
if __name__=="__main__":
title="Documents"
index_head=f'''\
<!DOCTYPE html>
<html>
<head>
<title>{title}</title>
<link rel="stylesheet" href="style.css">
<script>
var links = ['''
# create style file
css=open('./style.css', 'w+')
print(style, file=css)
css.close()
# get files into array
_, _, filenames = next(walk('.'), (None, None, []))
# create index file
html=open('./index.html', 'w+')
print(index_head, file=html)
for fn in filenames:
print('{{ "name":"{0:s}", "desc":"{0:s}" }},'.format(fn), file=html)
print(index_tail, file=html)
html.close()
# sample of how each document line looks after editing:
# { "name":"msp430f5438a.pdf", "desc":"MSP430F5438 datasheet" },
~/cwd> python /path/to/file.py
? \$\endgroup\$