Skip to content

Commit

Permalink
Templatize some of the HTML in logger
Browse files Browse the repository at this point in the history
  • Loading branch information
dc-snl committed Mar 1, 2021
1 parent 512cab0 commit a1629be
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 75 deletions.
8 changes: 6 additions & 2 deletions logger/resources/code_block_style.css
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
code { color: inherit; }
pre { white-space: pre-wrap; }
code {
color: inherit;
}
pre {
white-space: pre-wrap;
}
10 changes: 10 additions & 0 deletions logger/resources/output_style.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
table.output {
width: 100%;
}
table.output tr {
line-height: 1;
table-layout: fixed;
Expand All @@ -7,6 +10,13 @@ table.output tr[line-number] {
text-align: left;
clear: left;
}
table.output tr[line-number] td {
padding: 0;
border: 0;
}
table.output tr[line-number] td code {
white-space: pre;
}
table.output code {
vertical-align: top;
}
Expand Down
18 changes: 18 additions & 0 deletions logger/resources/templates/output_block.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<div>
<input type="text"
id="{cmd_id}-{title}-search"
onkeyup="outputSearch(this)"
target="{cmd_id}-{title}-table"
placeholder="Regex Search...">
<input type="checkbox"
id="{cmd_id}-{title}-checkbox"
onclick="outputSearch(this)"
name="{cmd_id}-{title}-show-duplicates">
<label for="{cmd_id}-{title}-show-duplicates"> Show duplicates</label><br>
<table class="output display table" id="{cmd_id}-{title}-table">
<tbody>
{table_contents}
</tbody>
</table>
</div>

5 changes: 5 additions & 0 deletions logger/resources/templates/output_line.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<tr line-number="{lineno}">
<td>
<code>{line}</code>
</td>
</tr>
100 changes: 27 additions & 73 deletions logger/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,85 +225,39 @@ def html_bold(text, indent=0, add_br=True):
return ' '*indent + f"<b>{text}</b> "

def html_fixed_width_from_file(input_file, title, cmd_id, indent=0):
yield (
"<div>\n" +
'<input type="text" ' +
f'id="{cmd_id}-{title}-search" ' +
'onkeyup="outputSearch(this)" ' +
f'target="{cmd_id}-{title}-table" ' +
'placeholder="Regex Search...">\n' +
'<input type="checkbox" ' +
f'id="{cmd_id}-{title}-checkbox" ' +
'onclick="outputSearch(this)" ' +
f'name="{cmd_id}-{title}-show-duplicates">\n' +
'<label ' +
f'for="{cmd_id}-{title}-show-duplicates"> ' +
'Show duplicates</label><br>\n' +
'<table class="output display table" ' +
f'id="{cmd_id}-{title}-table" ' +
'style="width: 100%;">\n' +
'<tbody>\n'
)

lineno = 0
with open(input_file, 'r') as out:
for line in out.readlines():
lineno += 1
yield (
f'<tr line-number="{lineno}">' +
'<td style="padding: 0; border: 0;">' +
'<code style="white-space: pre;">' +
html_encode(line).rstrip() +
"</code>" +
"</td>" +
"</tr>\n"
)

yield (
"</tbody>\n" +
"</table>\n" +
"</div>\n"
)
with open(input_file) as f:
for string in output_block_html(f, title, cmd_id):
yield string

def html_fixed_width_from_str(input_str, title, cmd_id, indent=0):
yield (
"<div>\n" +
'<input type="text" ' +
f'id="{cmd_id}-{title}-search" ' +
'onkeyup="outputSearch(this)" ' +
f'target="{cmd_id}-{title}-table" ' +
'placeholder="Regex Search...">\n' +
'<input type="checkbox" ' +
f'id="{cmd_id}-{title}-checkbox" ' +
'onclick="outputSearch(this)" ' +
f'name="{cmd_id}-{title}-show-duplicates">\n' +
'<label ' +
f'for="{cmd_id}-{title}-show-duplicates"> ' +
'Show duplicates</label><br>\n' +
'<table class="output display table" ' +
f'id="{cmd_id}-{title}-table" ' +
'style="width: 100%;">\n' +
'<tbody>\n'
)
for string in output_block_html(input_str, title, cmd_id):
yield string

output_block_template_file = "resources/templates/output_block.html"
output_block_template = pkgutil.get_data(__name__,
output_block_template_file).decode()
def output_block_html(lines, title, cmd_id):
split_marker = bytes([4]).decode()
indent = ' '*12
if isinstance(lines, str):
lines = lines.split('\n')
placeholder = output_block_template.format(title=title,
cmd_id=cmd_id,
table_contents=split_marker)
header, footer = placeholder.split(split_marker + '\n')
yield header
lineno = 0
for line in input_str.split("\n"):
for line in lines:
lineno += 1
yield (
f'<tr line-number="{lineno}">' +
'<td style="padding: 0; border: 0;">' +
'<code style="white-space: pre;">' +
html_encode(line).rstrip() +
"</code>" +
"</td>" +
"</tr>\n"
)
yield textwrap.indent(output_line_html(line, lineno), indent)
yield footer

yield (
"</tbody>\n" +
"</table>\n" +
"</div>\n"
)
output_line_template_file = "resources/templates/output_line.html"
output_line_template = pkgutil.get_data(__name__,
output_line_template_file).decode()
def output_line_html(line, lineno):
encoded_line = html_encode(line).rstrip()
return output_line_template.format(line=encoded_line, lineno=lineno)

def html_encode(text):
text = text.replace('&', "&amp;")
Expand Down

0 comments on commit a1629be

Please sign in to comment.