ravenous_typst/converter.sh

256 lines
6.1 KiB
Bash

#!/usr/bin/env bash
set -euo pipefail
# =============================================================================
# Split technique costs and normalize TSV columns
#
# Usage:
# ./split-cost.sh input.tsv output.tsv
#
# Example:
# ./split-cost.sh techniques.tsv techniques-normalized.tsv
# =============================================================================
if [[ $# -ne 2 ]]; then
echo "Usage: $0 <input.tsv> <output.tsv>" >&2
exit 1
fi
input="$1"
output="$2"
python3 - "$input" "$output" <<'PY'
import sys
import re
input_path = sys.argv[1]
output_path = sys.argv[2]
# =============================================================================
# Canonical schema
# =============================================================================
BASE_COLUMNS = [
"Technique",
"Niv",
"Type",
"Sang",
"Jauge",
"Autre",
"Effet",
]
STAT_COLUMNS = [
"Support",
"Dégats",
"Survivabilité",
"Mobilité",
"C.C",
"Altérations",
"QoL",
"AB",
"Compléxité",
]
COLUMNS = BASE_COLUMNS + STAT_COLUMNS
# Old format:
#
# Technique | Niv | Type | Coût | [empty] | Effet | ...
#
# The old empty column after Coût is deliberately discarded.
# =============================================================================
# Cost parsing
# =============================================================================
def parse_cost(cost):
"""
Convert:
4🩸 -> 4, "", ""
2⚡ -> "", 2, ""
4🩸2⚡ -> 4, 2, ""
3🩸1◆ -> 3, "", 1
Also accepts multiple occurrences, e.g. 3🩸2🩸 -> 5.
"""
blood = 0
gauge = 0
other = 0
for value, symbol in re.findall(r'(\d+(?:\.\d+)?)\s*(🩸|⚡|◆)', cost):
value = float(value)
if symbol == "🩸":
blood += value
elif symbol == "⚡":
gauge += value
elif symbol == "◆":
other += value
def clean(value):
if value == 0:
return ""
if value.is_integer():
return str(int(value))
return str(value)
return clean(blood), clean(gauge), clean(other)
# =============================================================================
# TSV helpers
# =============================================================================
def normalize_row(row, size):
"""
Make sure the row contains exactly `size` fields.
Missing fields are added as empty strings.
Extra fields are preserved rather than silently discarded.
"""
if len(row) < size:
row.extend([""] * (size - len(row)))
return row
def is_empty(row):
return not row or all(cell.strip() == "" for cell in row)
def is_tier(row):
return bool(row) and row[0].strip().startswith("Tier ")
# =============================================================================
# Read source
# =============================================================================
with open(input_path, "r", encoding="utf-8", newline="") as f:
lines = f.readlines()
output_lines = []
for raw_line in lines:
# Remove only line endings.
line = raw_line.rstrip("\r\n")
# Preserve completely empty lines.
if line.strip() == "":
output_lines.append("")
continue
row = line.split("\t")
# -------------------------------------------------------------------------
# Tier rows
# -------------------------------------------------------------------------
if is_tier(row):
# Keep the tier text exactly as it is.
output_lines.append("\t".join(row))
continue
# -------------------------------------------------------------------------
# Header rows
# -------------------------------------------------------------------------
if row and row[0].strip() == "Technique":
# Always produce the complete canonical header.
output_lines.append("\t".join(COLUMNS))
continue
# -------------------------------------------------------------------------
# Technique rows
# -------------------------------------------------------------------------
# Ensure the old minimum structure exists:
#
# 0 Technique
# 1 Niv
# 2 Type
# 3 Coût
# 4 old empty column
# 5 Effet
# ...
#
normalize_row(row, 6)
technique = row[0]
level = row[1]
type_ = row[2]
cost = row[3]
# -------------------------------------------------------------------------
# Split cost
# -------------------------------------------------------------------------
blood, gauge, other = parse_cost(cost)
# -------------------------------------------------------------------------
# Extract effect and statistics
# -------------------------------------------------------------------------
effect = row[5]
# Everything after the old Effect column corresponds to:
#
# Support
# Dégats
# Survivabilité
# Mobilité
# C.C
# Altérations
# QoL
# AB
# Compléxité
#
# If fewer values exist, the missing ones are simply empty.
stats = row[6:]
normalize_row(stats, len(STAT_COLUMNS))
# Deliberately keep only the canonical number of statistic fields.
stats = stats[:len(STAT_COLUMNS)]
# -------------------------------------------------------------------------
# Build normalized row
# -------------------------------------------------------------------------
new_row = [
technique,
level,
type_,
blood,
gauge,
other,
effect,
*stats,
]
# Final safety check.
normalize_row(new_row, len(COLUMNS))
# The canonical row must have exactly the expected number of fields.
new_row = new_row[:len(COLUMNS)]
output_lines.append("\t".join(new_row))
# =============================================================================
# Write result
# =============================================================================
with open(output_path, "w", encoding="utf-8", newline="") as f:
f.write("\n".join(output_lines))
f.write("\n")
print(f"Converted: {input_path} -> {output_path}")
print(f"Schema: {' | '.join(COLUMNS)}")
PY