update scripts to automatic add headline
This commit is contained in:
parent
e3812613b1
commit
73aac4e6e6
2 changed files with 58 additions and 49 deletions
|
@ -1,37 +1,39 @@
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
INPUT_FILE="input.txt"
|
# Konfiguration
|
||||||
OUTPUT_FILE="output.md"
|
INPUT_FILE=$1
|
||||||
|
OUTPUT_FILE=$2
|
||||||
|
SPACES_PER_LEVEL=4
|
||||||
|
LIST_INDENTATION=2
|
||||||
|
HEADER_LEVELS=3
|
||||||
|
|
||||||
# Schritt 1: Tabs durch 4 Leerzeichen ersetzen (ohne expand)
|
# Tabs zu Leerzeichen konvertieren
|
||||||
sed 's/\t/ /g' "$INPUT_FILE" > temp_processed.txt
|
sed 's/\t/ /g' "$INPUT_FILE" > temp_processed.txt
|
||||||
|
|
||||||
# Schritt 2: Hierarchische Listen generieren
|
# Ausgabedatei leeren
|
||||||
|
> "$OUTPUT_FILE"
|
||||||
|
|
||||||
|
# Verarbeitung jeder Zeile
|
||||||
while IFS= read -r line; do
|
while IFS= read -r line; do
|
||||||
# Zähle führende Leerzeichen
|
# Führende Leerzeichen ermitteln
|
||||||
spaces=$(echo "$line" | grep -o '^ *' | wc -c)
|
leading_spaces=${line%%[! ]*}
|
||||||
spaces=$((spaces - 1)) # wc -c zählt Nullbyte
|
total_spaces=${#leading_spaces}
|
||||||
|
|
||||||
# Berechne Ebene
|
# Ebene berechnen
|
||||||
level=$((spaces / 4))
|
level=$((total_spaces / SPACES_PER_LEVEL))
|
||||||
|
content="${line:total_spaces}"
|
||||||
|
|
||||||
# Generiere Einrückung
|
if (( level <= HEADER_LEVELS )); then
|
||||||
indent=""
|
# Überschriften formatieren
|
||||||
for ((i=0; i<level; i++)); do
|
hashtags=$(printf "%0.s#" $(seq 0 $level))
|
||||||
indent+=" "
|
echo "${hashtags} ${content}" >> "$OUTPUT_FILE"
|
||||||
done
|
|
||||||
|
|
||||||
# Konstruiere neue Zeile
|
|
||||||
if (( level > 0 )); then
|
|
||||||
# Entferne originale Einrückung und füge MD-Formatierung hinzu
|
|
||||||
clean_line="${line:$spaces}"
|
|
||||||
new_line="${indent}- ${clean_line}"
|
|
||||||
else
|
else
|
||||||
new_line="$line"
|
# Listen formatieren
|
||||||
|
list_level=$((level - HEADER_LEVELS - 1))
|
||||||
|
indent=$(printf "%*s" $((LIST_INDENTATION * list_level)) "")
|
||||||
|
echo "${indent}- ${content}" >> "$OUTPUT_FILE"
|
||||||
fi
|
fi
|
||||||
|
done < temp_processed.txt
|
||||||
|
|
||||||
echo "$new_line"
|
# Temporäre Datei aufräumen
|
||||||
done < temp_processed.txt > "$OUTPUT_FILE"
|
|
||||||
|
|
||||||
# Aufräumen
|
|
||||||
rm temp_processed.txt
|
rm temp_processed.txt
|
||||||
|
|
|
@ -1,32 +1,39 @@
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
INPUT_FILE="input.txt"
|
# Variablen definieren
|
||||||
OUTPUT_FILE="output.md"
|
INPUT_FILE=$1
|
||||||
|
OUTPUT_FILE=$2
|
||||||
|
SPACES_PER_LEVEL=4
|
||||||
|
LIST_INDENTATION=2
|
||||||
|
HEADER_LEVELS=3
|
||||||
|
|
||||||
# Tabs zu 4 Leerzeichen konvertieren OHNE andere Leerzeichen zu verändern
|
|
||||||
sed 's/\t/ /g' "$INPUT_FILE" > temp_processed.txt
|
sed 's/\t/ /g' "$INPUT_FILE" > temp_processed.txt
|
||||||
|
|
||||||
# AWK-Verarbeitung mit exakter Ebenenerkennung
|
awk -v spaces_per_level=$SPACES_PER_LEVEL -v list_indentation=$LIST_INDENTATION -v header_levels=$HEADER_LEVELS '
|
||||||
awk '
|
|
||||||
{
|
{
|
||||||
# Gesamtzahl der führenden Leerzeichen ermitteln
|
|
||||||
match($0, /^ */)
|
match($0, /^ */)
|
||||||
total_spaces = RLENGTH
|
total_spaces = RLENGTH
|
||||||
|
level = int(total_spaces / spaces_per_level)
|
||||||
|
|
||||||
# Ebene berechnen (1 Ebene = 4 Leerzeichen)
|
# Überschriften (bis zur angegebenen Ebene)
|
||||||
level = int(total_spaces / 4)
|
if (level <= header_levels) {
|
||||||
|
hashtags = ""
|
||||||
# Neue Einrückung generieren
|
for (i = 0; i <= level; i++) {
|
||||||
indent = ""
|
hashtags = hashtags "#"
|
||||||
for (i = 0; i < level; i++) {
|
}
|
||||||
indent = indent " "
|
$0 = hashtags " " substr($0, total_spaces + 1)
|
||||||
|
}
|
||||||
|
# Listen (ab der nächsten Ebene)
|
||||||
|
else {
|
||||||
|
list_level = level - header_levels - 1
|
||||||
|
# Anpassung der Einrückung basierend auf LIST_INDENTATION
|
||||||
|
adjusted_indent = ""
|
||||||
|
for (i = 0; i < list_indentation * list_level; i++) {
|
||||||
|
adjusted_indent = adjusted_indent " "
|
||||||
|
}
|
||||||
|
$0 = adjusted_indent "- " substr($0, total_spaces + 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
# Zeile neu aufbauen
|
|
||||||
if (level > 0) {
|
|
||||||
# Original-Leerzeichen entfernen und durch MD-konforme ersetzen
|
|
||||||
$0 = indent "- " substr($0, total_spaces + 1)
|
|
||||||
}
|
|
||||||
print
|
print
|
||||||
}' temp_processed.txt > "$OUTPUT_FILE"
|
}' temp_processed.txt > "$OUTPUT_FILE"
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue