From 77e9c9186ac4ba961a5a6356820f0b0159c3843848fb5bfceea7a3d15ffde9d4 Mon Sep 17 00:00:00 2001 From: Techognito Date: Thu, 14 Aug 2025 22:15:11 +0200 Subject: [PATCH] initial scripts --- foundryVTTRolltablepdf2json/coulumnsplit.sh | 45 ++++++++++ foundryVTTRolltablepdf2json/foundrycleaner.sh | 89 +++++++++++++++++++ .../foundryrolltable.sh | 77 ++++++++++++++++ foundryVTTRolltablepdf2json/greatSplitter.sh | 65 ++++++++++++++ .../rolltabletxtfix.sh | 44 +++++++++ 5 files changed, 320 insertions(+) create mode 100755 foundryVTTRolltablepdf2json/coulumnsplit.sh create mode 100755 foundryVTTRolltablepdf2json/foundrycleaner.sh create mode 100755 foundryVTTRolltablepdf2json/foundryrolltable.sh create mode 100755 foundryVTTRolltablepdf2json/greatSplitter.sh create mode 100755 foundryVTTRolltablepdf2json/rolltabletxtfix.sh diff --git a/foundryVTTRolltablepdf2json/coulumnsplit.sh b/foundryVTTRolltablepdf2json/coulumnsplit.sh new file mode 100755 index 0000000..5ad1783 --- /dev/null +++ b/foundryVTTRolltablepdf2json/coulumnsplit.sh @@ -0,0 +1,45 @@ +#!/bin/bash + +# Input and output files +input_file="columnized.txt" +output_file="decolumnized.txt" + +# Process the input file +awk ' +# Function to print the table header +function print_table_header(table) { + if (table != "") { + print "" + print "### " table + table = "" + } +} + +/^ *TABLE [0-9]+/ { # Detect table headers + table = $0 + next +} + +/^[0-9]+–[0-9]+/ || /^[0-9]+/ { # Match rows starting with "1–6", "7–9", etc. + left = substr($0, 1, 50) # Extract left column (first 50 chars) + right = substr($0, 51) # Extract right column (remaining chars) + + # Trim whitespace from both sides of each column + gsub(/ +$/, "", left) + gsub(/^ +/, "", left) + gsub(/ +$/, "", right) + gsub(/^ +/, "", right) + + # Print the table header if a new row is starting + print_table_header(table) + + # Print left column (if it exists) as part of the output + if (left != "") { + print "- **" left "**: " right + } else if (right != "") { + print right + } +}' "$input_file" > "$output_file" + +echo "Reformatted content saved to $output_file" + diff --git a/foundryVTTRolltablepdf2json/foundrycleaner.sh b/foundryVTTRolltablepdf2json/foundrycleaner.sh new file mode 100755 index 0000000..94ab7b8 --- /dev/null +++ b/foundryVTTRolltablepdf2json/foundrycleaner.sh @@ -0,0 +1,89 @@ +#!/bin/bash + +# Input and output files +if [ -z "$2" ]; then + INPUT_FILE="input.txt" +else + INPUT_FILE="$2" +fi +if [ -z "$3" ]; then + OUTPUT_FILE="output.txt" +else + OUTPUT_FILE="$3" +fi +tempfile=temp.txt + +# Process the input file +dice(){ + sed -E 's/([0-9]+[dD][0-9]+)/[[\1]]/g' "$tempfile" > "$OUTPUT_FILE" +} + +title(){ + sed -E 's/^([^ -]+.*?)-/

\1<\/h3>-/g' "$tempfile" > "$OUTPUT_FILE" +} + +RemoveNumbers() { + sed -E 's/^[0-9]+\. +//g' "$tempfile" > "$OUTPUT_FILE" +} + +ListSort(){ + sort -n "$tempfile" > "$OUTPUT_FILE" +} + +lineCombiner(){ + awk ' + /^[0-9]+–[0-9]+/ || /^[0-9]/ { + if (entry) print entry + entry = $0 + next + } + + /^TABLE/ || /^d12/ || /^-[0-9]-/ { + if (entry) print entry + entry = "" + print + next + } + + { entry = entry " " $0 } + + END { if (entry) print entry } + ' "$tempfile" > "$OUTPUT_FILE" +} + + +### RUNTIME ### + +cp "$INPUT_FILE" "$tempfile" + +case $1 in + dice) + dice + ;; + title) + title + ;; + numbers) + RemoveNumbers + ;; + sort) + ListSort + ;; + lines) + lineCombiner + ;; + auto) + ListSort + cp "$OUTPUT_FILE" "$tempfile" + RemoveNumbers + cp "$OUTPUT_FILE" "$tempfile" + dice + ;; + *) + echo "to use this script either write \"foundrycleaner.sh dice\" or \"foundrycleaner.sh title\" depending if you need [[dice]] or

title

" + echo "Alternatively use 'foundrycleaner.sh sort'" + echo "We also have foundrycleaner.sh auto" +esac + +echo "Processed file saved as $OUTPUT_FILE" + diff --git a/foundryVTTRolltablepdf2json/foundryrolltable.sh b/foundryVTTRolltablepdf2json/foundryrolltable.sh new file mode 100755 index 0000000..64cc0fb --- /dev/null +++ b/foundryVTTRolltablepdf2json/foundryrolltable.sh @@ -0,0 +1,77 @@ +#!/bin/bash + +inputfile="output.txt" +title="Title" +description="" +inputjson=Template.json +outputjson=fvtt-RollTable-$title.json +tempjson=temp.json + +#todo: check if empty +addItem () { + cp "$outputjson" $tempjson + local rangemin=$(jq '.results[.results| length-1].range[0]' "$outputjson") + local rangemax=$(jq '.results[.results| length-1].range[1]' "$outputjson") + #Check what the last range number is and add 1 to that number + local newrangemin=$(($rangemin+1)) + local newrangemax=$(($rangemax+1)) #while this will technically work, this will break of range min and max is not the same +# local text= "$1" + jq --argjson newrangemin $newrangemin --argjson newrangemax $newrangemax --arg text "${1}" '.results[.results| length] += {"Type": "text", "range": [$newrangemin,$newrangemax],"drawn": false, "text": $text }' "$outputjson" > $tempjson + cp $tempjson "$outputjson" + rm $tempjson +} + +setTitle() { + cp "$outputjson" $tempjson + jq --arg title "${title}" '.name = $title' "$outputjson" > $tempjson + cp $tempjson "$outputjson" + rm $tempjson +} + +setDescription() { + cp "$outputjson" $tempjson + jq --arg desc ${description} '.description = $desc' "$outputjson" > $tempjson + cp $tempjson "$outputjson" + rm $tempjson + +} + +readFile() { + echo $1 + if [ -z "$1" ]; then + echo test + read -r -p "File location:" fileInput + inputfile="$fileInput" + else + inputfile="$1" + fi +} + +# todo fix setTitle +readTitle() { + echo $1 + if [ -z "$1" ]; then + read -r -p "What is the Title of the RollTable? (input as string): " input + title="$input" + echo $title + else + title="$1" + fi +} + +## runtime ## +filevar="$1" +titlevar="$2" + +echo "Creating a new JSON file for import in FoundrVTT" +readFile "$filevar" +readTitle "$titlevar" +outputjson="fvtt-RollTable-$title.json" +cp $inputjson "$outputjson" +while read line; do + echo "$line" + addItem "$line" +done < "$inputfile" +#todo: make addItem able to read csv with range and text +#todo: Check outputfile if item allready exists (prevent duplicate items) +setTitle diff --git a/foundryVTTRolltablepdf2json/greatSplitter.sh b/foundryVTTRolltablepdf2json/greatSplitter.sh new file mode 100755 index 0000000..7952514 --- /dev/null +++ b/foundryVTTRolltablepdf2json/greatSplitter.sh @@ -0,0 +1,65 @@ +#!/bin/bash +outfile="" +infile="restofgreatbook.txt" +tempfile="restofgreatbook.txt.copy" + + +split(){ + outfile=$(head -n 1 $infile).txt + headline=$1 + tailline=$(($1+1)) + if [[ $1 != "" ]]; then + echo $headline + echo $tailline + clear + head -n $headline $infile + echo " " +# echo " " + echo "Lines count (102 for 100 items, 52 for 50 items): "$headline + yn=y + read -p "Continue with this list? [Y/n]" yn + case $yn in + [Yy]*) + head -n $headline $infile > "$outfile" + cp $infile $tempfile + tail -n +$tailline $tempfile > $infile + ;; + [Nn]*) + echo "split aborted" + exit + ;; + esac + else + echo "missing number" + echo "use 'greatSplitter.sh count | head' to get the next line number " + exit 1 + fi +# echo "hello test" +# echo $headline +# echo $tailline + echo $outfile + echo $1 +} + +linecount(){ + let count=0 + while read line; do + let count=$count+1 + if [ "$line" = "" ]; then + echo $count + echo $line + fi + done < $infile +} + +case $1 in + + count) + linecount | head -n 1 + ;; + + split) + split $(linecount | head -n 1) + ;; +esac + diff --git a/foundryVTTRolltablepdf2json/rolltabletxtfix.sh b/foundryVTTRolltablepdf2json/rolltabletxtfix.sh new file mode 100755 index 0000000..e374c63 --- /dev/null +++ b/foundryVTTRolltablepdf2json/rolltabletxtfix.sh @@ -0,0 +1,44 @@ +#!/bin/bash + +# Input and output file names + +input_file="cleanme.txt" +output_file="iamclean.txt" + +# Step 1: Remove unwanted characters and blank lines +# - Remove form feeds (``) +# - Remove empty lines +# - Remove lines that are just numbers (like page numbers) +sed -e 's/\f//g' -e '/^$/d' -e '/^[0-9]\+$/d' "$input_file" | + +# Step 2: Handle lines where numbering and text are split across two lines +awk ' + # If the line ends with a dot (e.g., "100.") without text, store it for the next line + /^[0-9]+\.$/ { + line_number = $1 + next + } + + # Combine the stored number with the current line if there is a pending number + line_number { + $0 = line_number " " $0 + line_number = "" + } + + # Print all valid lines + { print } +' | + +# Step 3: Renumber all lines sequentially +awk ' + BEGIN { count = 1 } + /^[0-9]+\./ { + # Replace the current numbering with sequential numbering + sub(/^[0-9]+\./, count ".") + count++ + } + { print } +' > "$output_file" + +echo "Cleaned and renumbered list saved to $output_file." +