2026-07-26 21:45:22 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
2026-07-27 20:09:31 +02:00
|
|
|
"encoding/json"
|
2026-07-26 21:45:22 +02:00
|
|
|
"flag"
|
|
|
|
|
"fmt"
|
2026-07-27 20:09:31 +02:00
|
|
|
"io"
|
2026-07-26 21:45:22 +02:00
|
|
|
"net/http"
|
|
|
|
|
"os"
|
2026-07-27 18:40:05 +02:00
|
|
|
"os/exec"
|
2026-07-26 21:45:22 +02:00
|
|
|
"path/filepath"
|
|
|
|
|
"regexp"
|
|
|
|
|
"strings"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Choice struct {
|
|
|
|
|
TargetID string `json:"targetId"`
|
|
|
|
|
Text string `json:"text"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type StoryNode struct {
|
2026-07-27 20:09:31 +02:00
|
|
|
ID string `json:"id"`
|
|
|
|
|
Title string `json:"title"`
|
2026-07-28 18:31:05 +02:00
|
|
|
Content string `json:"content"`
|
2026-07-27 20:09:31 +02:00
|
|
|
Choices []Choice `json:"choices"`
|
|
|
|
|
IsReboot bool `json:"isReboot"`
|
|
|
|
|
IsEnd bool `json:"isEnd"`
|
|
|
|
|
FileSource string `json:"fileSource"`
|
|
|
|
|
Metadata map[string]string `json:"metadata"`
|
2026-07-26 21:45:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type StoryGraph struct {
|
|
|
|
|
Nodes map[string]*StoryNode `json:"nodes"`
|
|
|
|
|
StartNodeID string `json:"startNodeId"`
|
|
|
|
|
BrokenLinks []string `json:"brokenLinks"`
|
|
|
|
|
OrphanNodes []string `json:"orphanNodes"`
|
|
|
|
|
DeadEndNodes []string `json:"deadEndNodes"`
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-27 20:09:31 +02:00
|
|
|
var (
|
|
|
|
|
adocNodeRegex = regexp.MustCompile(`\[\[([a-zA-Z0-9_\-]+)\]\]`)
|
|
|
|
|
adocChoiceRegex = regexp.MustCompile(`<<([a-zA-Z0-9_\-]+)\s*,\s*([^>]+)>>`)
|
|
|
|
|
mdChoiceRegex = regexp.MustCompile(`\[([^\]]+)\]\((?:#)?([a-zA-Z0-9_\-]+)\)`)
|
|
|
|
|
headingRegex = regexp.MustCompile(`^(?:={1,4}|#{1,4})\s+(.+)`)
|
|
|
|
|
metaRegex = regexp.MustCompile(`(?i)\[meta:\s*([a-zA-Z0-9_]+)\s*=\s*([^\]]+)\]`)
|
|
|
|
|
)
|
|
|
|
|
|
2026-07-26 21:45:22 +02:00
|
|
|
func main() {
|
2026-07-28 17:54:22 +02:00
|
|
|
dirPtr := flag.String("dir", "./stories", "Verzeichnis mit AsciiDoc/Markdown Dateien")
|
|
|
|
|
frontendPtr := flag.String("frontend", "./frontend", "Pfad zum Verzeichnis der statischen Frontend-Dateien")
|
2026-07-28 18:31:05 +02:00
|
|
|
outPtr := flag.String("out", "export", "Ziel-Ordner für den Dateiexport (SSG Modus)")
|
|
|
|
|
servePtr := flag.Bool("serve", false, "Startet den Live-Preview-Server (API Modus)")
|
2026-07-28 17:54:22 +02:00
|
|
|
portPtr := flag.Int("port", 3344, "Port für den Server")
|
2026-07-26 21:45:22 +02:00
|
|
|
flag.Parse()
|
|
|
|
|
|
2026-07-28 18:31:05 +02:00
|
|
|
fmt.Println("\033[1;36m=== Computer Historyz :: CYOA SSG & API ===\033[0m")
|
|
|
|
|
|
2026-07-28 17:54:22 +02:00
|
|
|
if info, err := os.Stat(*frontendPtr); os.IsNotExist(err) || !info.IsDir() {
|
|
|
|
|
fmt.Printf("\033[1;33m[WARNUNG]\033[0m Frontend-Verzeichnis '%s' nicht gefunden!\n", *frontendPtr)
|
|
|
|
|
}
|
2026-07-27 20:09:31 +02:00
|
|
|
|
|
|
|
|
if *servePtr {
|
2026-07-28 17:54:22 +02:00
|
|
|
startServer(*portPtr, *dirPtr, *frontendPtr)
|
2026-07-27 20:09:31 +02:00
|
|
|
return
|
|
|
|
|
}
|
2026-07-26 21:45:22 +02:00
|
|
|
|
2026-07-28 18:31:05 +02:00
|
|
|
// --- EXPORT MODUS (Static Site Generation) ---
|
|
|
|
|
fmt.Printf("\033[1;34m[EXPORT]\033[0m Kompiliere Story-Graph via Asciidoctor...\n")
|
2026-07-27 20:09:31 +02:00
|
|
|
graph := loadStoryGraphFromDir(*dirPtr)
|
2026-07-26 21:45:22 +02:00
|
|
|
validateGraph(graph)
|
|
|
|
|
|
2026-07-28 18:31:05 +02:00
|
|
|
// Export Ordner anlegen
|
|
|
|
|
os.MkdirAll(*outPtr, 0755)
|
|
|
|
|
|
|
|
|
|
// 1. Statische Daten als JSON schreiben
|
|
|
|
|
graphPath := filepath.Join(*outPtr, "graph.json")
|
|
|
|
|
graphJson, _ := json.MarshalIndent(graph, "", " ")
|
|
|
|
|
os.WriteFile(graphPath, graphJson, 0644)
|
|
|
|
|
|
|
|
|
|
// 2. Frontend-Dateien 1:1 kopieren
|
|
|
|
|
copyFile(filepath.Join(*frontendPtr, "index.html"), filepath.Join(*outPtr, "index.html"))
|
|
|
|
|
copyFile(filepath.Join(*frontendPtr, "style.css"), filepath.Join(*outPtr, "style.css"))
|
|
|
|
|
copyFile(filepath.Join(*frontendPtr, "engine_logic.js"), filepath.Join(*outPtr, "engine_logic.js"))
|
|
|
|
|
copyFile(filepath.Join(*frontendPtr, "engine_ui.js"), filepath.Join(*outPtr, "engine_ui.js"))
|
|
|
|
|
|
|
|
|
|
fmt.Printf("\033[1;32m[ERFOLG]\033[0m Web-App erfolgreich exportiert nach -> \033[1;33m%s/\033[0m\n", *outPtr)
|
|
|
|
|
}
|
2026-07-26 21:45:22 +02:00
|
|
|
|
2026-07-28 18:31:05 +02:00
|
|
|
func copyFile(src, dst string) {
|
|
|
|
|
input, err := os.ReadFile(src)
|
2026-07-28 17:54:22 +02:00
|
|
|
if err != nil {
|
2026-07-28 18:31:05 +02:00
|
|
|
fmt.Printf("\033[1;33m[WARNUNG]\033[0m Frontend-Datei %s fehlt.\n", src)
|
|
|
|
|
return
|
2026-07-26 21:45:22 +02:00
|
|
|
}
|
2026-07-28 18:31:05 +02:00
|
|
|
os.WriteFile(dst, input, 0644)
|
2026-07-26 21:45:22 +02:00
|
|
|
}
|
|
|
|
|
|
2026-07-28 17:54:22 +02:00
|
|
|
func startServer(port int, watchDir string, frontendDir string) {
|
|
|
|
|
addr := fmt.Sprintf(":%d", port)
|
2026-07-28 18:31:05 +02:00
|
|
|
fmt.Printf("\n\033[1;32m[SERVER LÄUFT]\033[0m http://localhost:%d\n", port)
|
2026-07-28 17:54:22 +02:00
|
|
|
|
2026-07-28 18:31:05 +02:00
|
|
|
// Dynamischer API-Endpunkt für graph.json
|
|
|
|
|
http.HandleFunc("/graph.json", func(w http.ResponseWriter, r *http.Request) {
|
2026-07-28 17:54:22 +02:00
|
|
|
w.Header().Set("Access-Control-Allow-Origin", "*")
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
|
2026-07-28 18:31:05 +02:00
|
|
|
var graph *StoryGraph
|
2026-07-28 17:54:22 +02:00
|
|
|
if r.Method == http.MethodPost {
|
2026-07-28 18:31:05 +02:00
|
|
|
bodyBytes, _ := io.ReadAll(r.Body)
|
|
|
|
|
graph = &StoryGraph{Nodes: make(map[string]*StoryNode)}
|
2026-07-28 17:54:22 +02:00
|
|
|
parseContent(string(bodyBytes), "API_POST_Stream", graph)
|
2026-07-28 18:31:05 +02:00
|
|
|
} else {
|
|
|
|
|
graph = loadStoryGraphFromDir(watchDir)
|
2026-07-28 17:54:22 +02:00
|
|
|
}
|
2026-07-28 18:31:05 +02:00
|
|
|
validateGraph(graph)
|
|
|
|
|
json.NewEncoder(w).Encode(graph)
|
2026-07-28 17:54:22 +02:00
|
|
|
})
|
|
|
|
|
|
2026-07-28 18:31:05 +02:00
|
|
|
// Liefert alles andere aus dem /frontend Ordner
|
2026-07-28 17:54:22 +02:00
|
|
|
fs := http.FileServer(http.Dir(frontendDir))
|
|
|
|
|
http.Handle("/", fs)
|
|
|
|
|
|
|
|
|
|
if err := http.ListenAndServe(addr, nil); err != nil {
|
|
|
|
|
fmt.Printf("\033[1;31m[FEHLER]\033[0m %v\n", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// renderAsciidoctor piped rohen AsciiDoc Text in den lokalen Container-Prozess
|
|
|
|
|
func renderAsciidoctor(text string) string {
|
|
|
|
|
if strings.TrimSpace(text) == "" {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2026-07-27 20:09:31 +02:00
|
|
|
cmd := exec.Command("asciidoctor", "--embedded", "-")
|
|
|
|
|
cmd.Stdin = strings.NewReader(text)
|
|
|
|
|
out, err := cmd.Output()
|
|
|
|
|
if err != nil {
|
2026-07-28 17:54:22 +02:00
|
|
|
fmt.Printf("\033[1;33m[WARNUNG]\033[0m Asciidoctor Rendering fehlgeschlagen: %v\n", err)
|
|
|
|
|
return strings.ReplaceAll(text, "\n", "<br>")
|
2026-07-27 20:09:31 +02:00
|
|
|
}
|
2026-07-28 17:54:22 +02:00
|
|
|
return string(out)
|
2026-07-27 20:09:31 +02:00
|
|
|
}
|
2026-07-26 21:45:22 +02:00
|
|
|
|
2026-07-27 20:09:31 +02:00
|
|
|
func cleanBodyText(raw string) string {
|
|
|
|
|
cleaned := adocChoiceRegex.ReplaceAllString(raw, "")
|
|
|
|
|
cleaned = mdChoiceRegex.ReplaceAllString(cleaned, "")
|
|
|
|
|
return strings.TrimSpace(cleaned)
|
|
|
|
|
}
|
2026-07-26 21:45:22 +02:00
|
|
|
|
2026-07-27 20:09:31 +02:00
|
|
|
func parseContent(rawContent string, sourceName string, graph *StoryGraph) {
|
|
|
|
|
lines := strings.Split(rawContent, "\n")
|
|
|
|
|
var currentNode *StoryNode = nil
|
|
|
|
|
var currentBody strings.Builder
|
|
|
|
|
|
|
|
|
|
for _, line := range lines {
|
|
|
|
|
if matches := adocNodeRegex.FindStringSubmatch(line); len(matches) > 1 {
|
|
|
|
|
if currentNode != nil {
|
|
|
|
|
currentNode.Content = renderAsciidoctor(cleanBodyText(currentBody.String()))
|
|
|
|
|
graph.Nodes[currentNode.ID] = currentNode
|
|
|
|
|
currentBody.Reset()
|
|
|
|
|
}
|
|
|
|
|
nodeID := matches[1]
|
2026-07-28 18:31:05 +02:00
|
|
|
currentNode = &StoryNode{ ID: nodeID, Title: nodeID, Choices: make([]Choice, 0), FileSource: sourceName, Metadata: make(map[string]string) }
|
|
|
|
|
if graph.StartNodeID == "" || nodeID == "start" || nodeID == "node_start" { graph.StartNodeID = nodeID }
|
2026-07-27 20:09:31 +02:00
|
|
|
continue
|
2026-07-26 21:45:22 +02:00
|
|
|
}
|
2026-07-28 17:54:22 +02:00
|
|
|
|
2026-07-27 20:09:31 +02:00
|
|
|
if currentNode == nil {
|
|
|
|
|
if strings.TrimSpace(line) != "" {
|
|
|
|
|
baseID := "start"
|
2026-07-28 18:31:05 +02:00
|
|
|
if sourceName != "API_POST_Stream" { baseID = strings.TrimSuffix(filepath.Base(sourceName), filepath.Ext(sourceName)) }
|
|
|
|
|
currentNode = &StoryNode{ ID: baseID, Title: baseID, Choices: make([]Choice, 0), FileSource: sourceName, Metadata: make(map[string]string) }
|
|
|
|
|
if graph.StartNodeID == "" { graph.StartNodeID = baseID }
|
|
|
|
|
} else { continue }
|
2026-07-27 20:09:31 +02:00
|
|
|
}
|
2026-07-26 21:45:22 +02:00
|
|
|
|
2026-07-27 20:09:31 +02:00
|
|
|
if mMatches := metaRegex.FindStringSubmatch(line); len(mMatches) > 2 {
|
2026-07-28 17:54:22 +02:00
|
|
|
currentNode.Metadata[strings.ToLower(mMatches[1])] = strings.TrimSpace(mMatches[2])
|
|
|
|
|
continue
|
2026-07-27 20:09:31 +02:00
|
|
|
}
|
2026-07-28 17:54:22 +02:00
|
|
|
|
2026-07-27 20:09:31 +02:00
|
|
|
if hMatches := headingRegex.FindStringSubmatch(line); len(hMatches) > 1 && currentNode.Title == currentNode.ID {
|
|
|
|
|
currentNode.Title = strings.TrimSpace(hMatches[1])
|
|
|
|
|
}
|
2026-07-28 17:54:22 +02:00
|
|
|
|
2026-07-27 20:09:31 +02:00
|
|
|
if cMatches := adocChoiceRegex.FindAllStringSubmatch(line, -1); len(cMatches) > 0 {
|
2026-07-28 18:31:05 +02:00
|
|
|
for _, match := range cMatches { currentNode.Choices = append(currentNode.Choices, Choice{TargetID: match[1], Text: strings.TrimSpace(match[2])}) }
|
2026-07-27 20:09:31 +02:00
|
|
|
}
|
2026-07-28 17:54:22 +02:00
|
|
|
|
2026-07-27 20:09:31 +02:00
|
|
|
if mMatches := mdChoiceRegex.FindAllStringSubmatch(line, -1); len(mMatches) > 0 {
|
2026-07-28 18:31:05 +02:00
|
|
|
for _, match := range mMatches { currentNode.Choices = append(currentNode.Choices, Choice{TargetID: match[2], Text: strings.TrimSpace(match[1])}) }
|
2026-07-26 21:45:22 +02:00
|
|
|
}
|
|
|
|
|
|
2026-07-27 20:09:31 +02:00
|
|
|
lowerLine := strings.ToLower(line)
|
2026-07-28 18:31:05 +02:00
|
|
|
if strings.Contains(lowerLine, "[reboot]") || strings.Contains(lowerLine, "speicherabbild") { currentNode.IsReboot = true }
|
|
|
|
|
if strings.Contains(lowerLine, "[ende]") || strings.Contains(lowerLine, "[end]") { currentNode.IsEnd = true }
|
2026-07-26 21:45:22 +02:00
|
|
|
|
2026-07-27 20:09:31 +02:00
|
|
|
currentBody.WriteString(line + "\n")
|
2026-07-26 21:45:22 +02:00
|
|
|
}
|
2026-07-28 17:54:22 +02:00
|
|
|
|
2026-07-27 20:09:31 +02:00
|
|
|
if currentNode != nil {
|
|
|
|
|
currentNode.Content = renderAsciidoctor(cleanBodyText(currentBody.String()))
|
|
|
|
|
graph.Nodes[currentNode.ID] = currentNode
|
2026-07-27 18:40:05 +02:00
|
|
|
}
|
2026-07-27 20:09:31 +02:00
|
|
|
}
|
2026-07-27 18:40:05 +02:00
|
|
|
|
2026-07-27 20:09:31 +02:00
|
|
|
func loadStoryGraphFromDir(rootDir string) *StoryGraph {
|
|
|
|
|
graph := &StoryGraph{Nodes: make(map[string]*StoryNode)}
|
|
|
|
|
filepath.Walk(rootDir, func(path string, info os.FileInfo, err error) error {
|
|
|
|
|
if err != nil || info.IsDir() { return nil }
|
|
|
|
|
ext := strings.ToLower(filepath.Ext(path))
|
|
|
|
|
if ext != ".adoc" && ext != ".md" { return nil }
|
|
|
|
|
contentBytes, err := os.ReadFile(path)
|
2026-07-28 17:54:22 +02:00
|
|
|
if err == nil {
|
|
|
|
|
relPath, _ := filepath.Rel(rootDir, path)
|
|
|
|
|
parseContent(string(contentBytes), relPath, graph)
|
|
|
|
|
}
|
2026-07-27 20:09:31 +02:00
|
|
|
return nil
|
|
|
|
|
})
|
|
|
|
|
return graph
|
2026-07-26 21:45:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func validateGraph(g *StoryGraph) {
|
|
|
|
|
referenced := make(map[string]bool)
|
2026-07-27 20:09:31 +02:00
|
|
|
if g.StartNodeID != "" { referenced[g.StartNodeID] = true }
|
2026-07-26 21:45:22 +02:00
|
|
|
for id, node := range g.Nodes {
|
|
|
|
|
for _, choice := range node.Choices {
|
|
|
|
|
referenced[choice.TargetID] = true
|
2026-07-28 17:54:22 +02:00
|
|
|
if _, exists := g.Nodes[choice.TargetID]; !exists {
|
|
|
|
|
g.BrokenLinks = append(g.BrokenLinks, fmt.Sprintf("%s -> %s (in %s)", id, choice.TargetID, node.FileSource))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if len(node.Choices) == 0 && !node.IsReboot && !node.IsEnd {
|
|
|
|
|
g.DeadEndNodes = append(g.DeadEndNodes, id)
|
2026-07-26 21:45:22 +02:00
|
|
|
}
|
|
|
|
|
}
|
2026-07-28 17:54:22 +02:00
|
|
|
for id := range g.Nodes {
|
|
|
|
|
if !referenced[id] { g.OrphanNodes = append(g.OrphanNodes, id) }
|
2026-07-26 21:45:22 +02:00
|
|
|
}
|
2026-07-28 16:19:17 +02:00
|
|
|
}
|