#!/usr/bin/env python3
# preprocess_gcide.py -- GCIDE 0.54 paragraph unwrapper for webfmt
# Copyright (C) 2026 Ritesh Raj Sarraf <rrs@debian.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
"""
Preprocess GCIDE 0.54 files:
Unwrap 72-column wrapped lines within <p>...</p> paragraphs
back to one line per paragraph / line-break entry for webfilter/webfmt parsing.
"""

import sys
import re

def unwrap_paragraph(match):
    content = match.group(1)
    lines = content.splitlines()
    out = []
    for line in lines:
        line = line.strip()
        if not line:
            continue
        if out and not out[-1].endswith("<br/") and not out[-1].endswith("<br>"):
            out[-1] = out[-1] + " " + line
        else:
            out.append(line)
    return "<p>" + "\n".join(out) + "</p>\n"

def process_file(filepath):
    with open(filepath, "r", encoding="latin1") as f:
        content = f.read()
    return re.sub(r"<p>(.*?)</p>", unwrap_paragraph, content, flags=re.DOTALL)

def main():
    if len(sys.argv) > 1:
        for filepath in sys.argv[1:]:
            sys.stdout.buffer.write(process_file(filepath).encode("latin1", errors="replace"))
    else:
        content = sys.stdin.read()
        unwrapped = re.sub(r"<p>(.*?)</p>", unwrap_paragraph, content, flags=re.DOTALL)
        sys.stdout.buffer.write(unwrapped.encode("latin1", errors="replace"))

if __name__ == "__main__":
    main()
