Oprydning og opdeling i programfiler og datafiler - og datafiler i input og output.
This commit is contained in:
1 parent
00697527f6
commit
b859d67ac2
87 files changed
+22278
-61575
No files matched your search
@@ -0,0 +1,364 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Render Docling- eller Next OCR-JSON som lokal HTML-preview."""
|
||||
|
||||
import argparse
|
||||
import html
|
||||
import json
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def render_table(table, idx):
|
||||
"""Render en tabel fra Doclings JSON-format."""
|
||||
cells = table.get("data", {}).get("table_cells", [])
|
||||
|
||||
if not cells:
|
||||
return (
|
||||
f'<section><h2>Tabel {idx + 1}</h2>'
|
||||
'<p>Ingen tabelceller fundet.</p></section>'
|
||||
)
|
||||
|
||||
max_row = max(
|
||||
int(
|
||||
c.get(
|
||||
"end_row_offset_idx",
|
||||
int(c.get("start_row_offset_idx", 0))
|
||||
+ int(c.get("row_span", 1)),
|
||||
)
|
||||
- 1
|
||||
)
|
||||
for c in cells
|
||||
)
|
||||
|
||||
max_col = max(
|
||||
int(
|
||||
c.get(
|
||||
"end_col_offset_idx",
|
||||
int(c.get("start_col_offset_idx", 0))
|
||||
+ int(c.get("col_span", 1)),
|
||||
)
|
||||
- 1
|
||||
)
|
||||
for c in cells
|
||||
)
|
||||
|
||||
grid = {}
|
||||
|
||||
for c in cells:
|
||||
row = int(c.get("start_row_offset_idx", 0))
|
||||
col = int(c.get("start_col_offset_idx", 0))
|
||||
|
||||
grid[(row, col)] = (
|
||||
c,
|
||||
max(1, int(c.get("row_span", 1))),
|
||||
max(1, int(c.get("col_span", 1))),
|
||||
)
|
||||
|
||||
out = [
|
||||
f'<section><h2>Tabel {idx + 1}</h2>',
|
||||
'<div class="table-wrap"><table><tbody>',
|
||||
]
|
||||
|
||||
row = 0
|
||||
|
||||
while row <= max_row:
|
||||
out.append("<tr>")
|
||||
col = 0
|
||||
|
||||
while col <= max_col:
|
||||
item = grid.get((row, col))
|
||||
|
||||
if item:
|
||||
cell, row_span, col_span = item
|
||||
|
||||
text = html.escape(
|
||||
str(cell.get("text", ""))
|
||||
).replace("\n", "<br>")
|
||||
|
||||
classes = []
|
||||
|
||||
if cell.get("column_header"):
|
||||
classes.append("column-header")
|
||||
|
||||
if cell.get("row_header"):
|
||||
classes.append("row-header")
|
||||
|
||||
if not text.strip():
|
||||
classes.append("empty")
|
||||
|
||||
attrs = (
|
||||
f' rowspan="{row_span}"'
|
||||
if row_span > 1
|
||||
else ""
|
||||
)
|
||||
|
||||
attrs += (
|
||||
f' colspan="{col_span}"'
|
||||
if col_span > 1
|
||||
else ""
|
||||
)
|
||||
|
||||
cls = (
|
||||
f' class="{" ".join(classes)}"'
|
||||
if classes
|
||||
else ""
|
||||
)
|
||||
|
||||
out.append(
|
||||
f"<td{attrs}{cls}>{text or ' '}</td>"
|
||||
)
|
||||
|
||||
col += col_span
|
||||
|
||||
else:
|
||||
out.append(
|
||||
'<td class="missing" '
|
||||
'title="Ingen celle i Docling-data">∅</td>'
|
||||
)
|
||||
col += 1
|
||||
|
||||
out.append("</tr>")
|
||||
row += 1
|
||||
|
||||
out.append("</tbody></table></div></section>")
|
||||
|
||||
return "".join(out)
|
||||
|
||||
|
||||
def render_ocr_table(element, idx):
|
||||
"""Render en tabel fra Next OCRs JSON-format."""
|
||||
headers = element.get("headers", [])
|
||||
rows = element.get("rows", [])
|
||||
|
||||
out = [
|
||||
f'<section><h2>Tabel {idx + 1}</h2>',
|
||||
'<div class="table-wrap"><table>',
|
||||
]
|
||||
|
||||
if headers:
|
||||
out.append("<thead><tr>")
|
||||
|
||||
for header in headers:
|
||||
text = html.escape(str(header))
|
||||
out.append(f"<th>{text or ' '}</th>")
|
||||
|
||||
out.append("</tr></thead>")
|
||||
|
||||
out.append("<tbody>")
|
||||
|
||||
for row in rows:
|
||||
out.append("<tr>")
|
||||
|
||||
for cell in row:
|
||||
text = html.escape(str(cell)).replace("\n", "<br>")
|
||||
out.append(f"<td>{text or ' '}</td>")
|
||||
|
||||
out.append("</tr>")
|
||||
|
||||
out.append("</tbody></table></div></section>")
|
||||
|
||||
return "".join(out)
|
||||
|
||||
|
||||
def render_ocr_elements(elements):
|
||||
"""Render elementerne fra Next OCRs JSON-format."""
|
||||
sections = []
|
||||
table_count = 0
|
||||
has_content = False
|
||||
|
||||
for element in elements:
|
||||
element_type = element.get("type")
|
||||
|
||||
if element_type == "table":
|
||||
sections.append(
|
||||
render_ocr_table(element, table_count)
|
||||
)
|
||||
table_count += 1
|
||||
has_content = True
|
||||
|
||||
elif element_type == "text":
|
||||
content = element.get("content", "")
|
||||
|
||||
if content:
|
||||
text = html.escape(str(content)).replace(
|
||||
"\n", "<br>"
|
||||
)
|
||||
sections.append(
|
||||
f"<section><p>{text}</p></section>"
|
||||
)
|
||||
has_content = True
|
||||
|
||||
if not has_content:
|
||||
sections.append("<p>Ingen data fundet.</p>")
|
||||
|
||||
return sections
|
||||
|
||||
|
||||
def main():
|
||||
ap = argparse.ArgumentParser(description=__doc__)
|
||||
|
||||
ap.add_argument(
|
||||
"json_files",
|
||||
nargs="+",
|
||||
help="JSON-fil(er) fra Docling eller Next OCR",
|
||||
)
|
||||
|
||||
ap.add_argument(
|
||||
"-o",
|
||||
"--output",
|
||||
help="Navn på den genererede HTML-fil",
|
||||
)
|
||||
|
||||
args = ap.parse_args()
|
||||
|
||||
files = [Path(p) for p in args.json_files]
|
||||
docs = []
|
||||
|
||||
for path in files:
|
||||
obj = json.loads(path.read_text(encoding="utf-8"))
|
||||
|
||||
title = obj.get("name", path.stem)
|
||||
|
||||
# Docling kan indeholde overskrifter i "texts".
|
||||
headings = [
|
||||
item.get("text", "")
|
||||
for item in obj.get("texts", [])
|
||||
if (
|
||||
item.get("label") == "section_header"
|
||||
and item.get("text")
|
||||
)
|
||||
]
|
||||
|
||||
heading = headings[0] if headings else title
|
||||
|
||||
sections = [
|
||||
f'<article><h1>{html.escape(heading)}</h1>'
|
||||
f'<p class="meta">{html.escape(path.name)}</p>'
|
||||
]
|
||||
|
||||
# Next OCR-format:
|
||||
# {"elements": [{"type": "table", "headers": [...],
|
||||
# "rows": [...]}]}
|
||||
if "elements" in obj:
|
||||
sections.extend(
|
||||
render_ocr_elements(obj.get("elements", []))
|
||||
)
|
||||
|
||||
# Docling-format:
|
||||
# {"tables": [{"data": {"table_cells": [...]}}]}
|
||||
else:
|
||||
tables = obj.get("tables", [])
|
||||
|
||||
if not tables:
|
||||
sections.append("<p>Ingen tabeller fundet.</p>")
|
||||
|
||||
sections.extend(
|
||||
render_table(table, i)
|
||||
for i, table in enumerate(tables)
|
||||
)
|
||||
|
||||
sections.append("</article>")
|
||||
docs.append("".join(sections))
|
||||
|
||||
output = (
|
||||
Path(args.output)
|
||||
if args.output
|
||||
else files[0].with_suffix(".preview.html")
|
||||
)
|
||||
|
||||
page = """<!doctype html>
|
||||
<html lang="da">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>Skema-preview</title>
|
||||
<style>
|
||||
body {
|
||||
font: 14px/1.35 system-ui, sans-serif;
|
||||
margin: 24px;
|
||||
color: #222;
|
||||
background: #f4f5f7;
|
||||
}
|
||||
article {
|
||||
background: white;
|
||||
padding: 18px;
|
||||
margin: 0 auto 28px;
|
||||
max-width: 1250px;
|
||||
box-shadow: 0 1px 5px #0002;
|
||||
overflow-x: auto;
|
||||
}
|
||||
h1 {
|
||||
margin: 0 0 4px;
|
||||
font-size: 1.45rem;
|
||||
}
|
||||
h2 {
|
||||
margin: 16px 0 8px;
|
||||
font-size: 1rem;
|
||||
}
|
||||
.meta {
|
||||
color: #666;
|
||||
font-size: 12px;
|
||||
margin: 0 0 12px;
|
||||
}
|
||||
.table-wrap {
|
||||
overflow-x: auto;
|
||||
}
|
||||
table {
|
||||
border-collapse: collapse;
|
||||
width: 100%;
|
||||
table-layout: auto;
|
||||
}
|
||||
td, th {
|
||||
border: 1px solid #777;
|
||||
padding: 6px 7px;
|
||||
vertical-align: top;
|
||||
white-space: pre-wrap;
|
||||
overflow-wrap: anywhere;
|
||||
min-width: 70px;
|
||||
}
|
||||
th {
|
||||
background: #e8edf5;
|
||||
font-weight: 700;
|
||||
text-align: center;
|
||||
}
|
||||
td.column-header {
|
||||
background: #e8edf5;
|
||||
font-weight: 700;
|
||||
text-align: center;
|
||||
}
|
||||
td.row-header {
|
||||
background: #f1f1f1;
|
||||
font-weight: 600;
|
||||
}
|
||||
td.empty {
|
||||
background: #fafafa;
|
||||
}
|
||||
td.missing {
|
||||
background: #ffe4e4;
|
||||
color: #a00;
|
||||
text-align: center;
|
||||
font-size: 11px;
|
||||
}
|
||||
@media print {
|
||||
body {
|
||||
background: white;
|
||||
margin: 0;
|
||||
}
|
||||
article {
|
||||
box-shadow: none;
|
||||
page-break-after: always;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
""" + "\n".join(docs) + """
|
||||
</body>
|
||||
</html>
|
||||
"""
|
||||
|
||||
output.write_text(page, encoding="utf-8")
|
||||
print(f"Skrev: {output}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in new issue
Block a user