diff --git a/tests/test_small_ui.gd b/tests/test_small_ui.gd index 2721294..7a73e5d 100644 --- a/tests/test_small_ui.gd +++ b/tests/test_small_ui.gd @@ -23,6 +23,7 @@ func _init() -> void: await _test_deck_mode() await _test_batch() await _test_pause_aside() + _test_sheet_export() print("%s: %d failure(s)" % ["PASS" if _failures == 0 else "FAIL", _failures]) quit(1 if _failures else 0) @@ -157,6 +158,18 @@ func _test_pause_aside() -> void: road.entries[i].pipe_pause.clear() +func _test_sheet_export() -> void: + var b := "[b]Name:[/b] [color=#aa0000]Ash[/color] & co\n[i]Brave[/i]" + var md := SheetExport.as_format(b, "Hero", "md") + _check(md.begins_with("# Hero") and md.contains("**Name:**") and md.contains("*Brave*") and not md.contains("[color"), "Markdown: " + md) + var html := SheetExport.as_format(b, "Hero", "html") + _check(html.contains("Name:") and html.contains("color:#aa0000") and html.contains("& co") and html.contains("
"), "HTML keeps bold and colour") + var txt := SheetExport.as_format(b, "Hero", "txt") + _check(txt.contains("Name: Ash & co") and not txt.contains("["), "Text strips the formatting") + main.rolling.start_roll("road", true) + _check(SheetExport.document(main, "txt").contains("Road Encounter"), "the current roll becomes a document") + + func _frames(n: int = 2) -> void: for i in n: await process_frame diff --git a/tools/run_tests.sh b/tools/run_tests.sh index 8d53aa1..de991ad 100755 --- a/tools/run_tests.sh +++ b/tools/run_tests.sh @@ -19,16 +19,27 @@ for t in "${suites[@]}"; do done wait fail=0 +declare -A first_err for t in "${suites[@]}"; do n="$(basename "$t" .gd)" log="$OUT/$n.log" if grep -q "PASS: 0 failure(s)" "$log" && ! grep -q "SCRIPT ERROR" "$log"; then echo "ok $n" + continue + fi + fail=1 + e="$(grep -m1 -E "SCRIPT ERROR|^ *FAIL [^:]" "$log")" + # A shared compile error fails many suites the same way: report it once. + if [[ "$e" == *"SCRIPT ERROR"* ]]; then + first_err["$e"]+="$n " else echo "FAIL $n" grep -E "FAIL|SCRIPT ERROR|ERROR" "$log" | head -8 - fail=1 fi done +for e in "${!first_err[@]}"; do + echo "FAIL ${first_err[$e]}" + echo " $e" +done (( fail )) && echo "SOME SUITES FAILED" || echo "ALL ${#suites[@]} SUITES PASSED" exit $fail diff --git a/ui/inspector_card.gd b/ui/inspector_card.gd index 0b70374..97e64b5 100644 --- a/ui/inspector_card.gd +++ b/ui/inspector_card.gd @@ -577,3 +577,15 @@ static func _sheet(insp: Inspector, box: VBoxContainer, t: RollTable) -> void: tpl.text_changed.connect(func() -> void: insp.apply_settings({"template": tpl.text})) box.add_child(InspectorKit.field("Template", tpl)) box.add_child(InspectorKit.note("Write {@value} where a value goes. The sheet shows the values as they are now.", Style.MUTED, 13)) + # The easy way: pick a value and get a "Label: value" line. + var row := InspectorKit.flow() + row.add_child(InspectorKit.word("+ Add a line for")) + var pick := ValuePicker.new(insp.main, "") + pick.value_chosen.connect(func(n: String) -> void: + var name := n.trim_prefix("@") + var line := "[b]%s:[/b] {@%s}" % [Describe.label(name, insp.main.data), name] + tpl.text = (tpl.text.strip_edges() + "\n" + line).strip_edges() + insp.apply_settings({"template": tpl.text})) + row.add_child(pick) + box.add_child(row) + box.add_child(InspectorKit.note("Save a finished sheet from the tray: Copy ▾ → Save as a file (text, Markdown, or a web page to print as PDF).", Style.MUTED, 12)) diff --git a/ui/result_tray.gd b/ui/result_tray.gd index bdb4ea4..9d31ac2 100644 --- a/ui/result_tray.gd +++ b/ui/result_tray.gd @@ -216,7 +216,7 @@ func set_side(s: int) -> void: func _make_copy_menu() -> MenuButton: var m := MenuButton.new() m.text = "Copy" - m.tooltip_text = "Copy this result" + m.tooltip_text = "Copy this result, or save it as a file" m.flat = false var normal := Style.bordered(Style.box(Color(1, 1, 1, 0.04), 8, [12, 6, 12, 6]), 1, Color(1, 1, 1, 0.16)) var hover := Style.bordered(Style.box(Color(1, 1, 1, 0.1), 8, [12, 6, 12, 6]), 1, Color(1, 1, 1, 0.16)) @@ -233,6 +233,15 @@ func _make_copy_menu() -> MenuButton: pm.add_theme_font_override("font", Style.body()) pm.add_theme_font_size_override("font_size", 15) pm.id_pressed.connect(func(id: int) -> void: main.copy_current(id)) + # Save as a file to use outside the app (SheetExport). + var save := PopupMenu.new() + ModalCard.style_popup(save) + for i in SheetExport.FORMATS.size(): + save.add_item(str(SheetExport.FORMATS[i]["label"]) + "…", i) + save.id_pressed.connect(func(i: int) -> void: SheetExport.save(main, i)) + pm.add_child(save) + pm.add_separator() + pm.add_submenu_node_item("Save as a file", save) return m diff --git a/ui/sheet_export.gd b/ui/sheet_export.gd new file mode 100644 index 0000000..5ca1c96 --- /dev/null +++ b/ui/sheet_export.gd @@ -0,0 +1,87 @@ +class_name SheetExport +extends RefCounted +## Saving a result (or its sheet) as a file to use outside the app: plain +## Text, Markdown, or a Web page (HTML, which any browser can print to PDF). +## Works from the result's BBCode: bold, italics, underline, colour and +## sizes come across where the format has them; anything else is dropped. + +const FORMATS := [ + {"key": "txt", "label": "Text", "filter": "*.txt ; Text"}, + {"key": "md", "label": "Markdown", "filter": "*.md ; Markdown"}, + {"key": "html", "label": "Web page (print it to PDF)", "filter": "*.html ; Web page"}, +] + + +## The current roll as a document: its sheet when it reached one, else one +## line per result. `title` heads the document. +static func document(m: Main, format: String) -> String: + var steps: Array = m.current.get("all", []) + var title := Formatter.strip_tags(str(m.current.get("root_name", "Result"))) + var body := "" + var sheet := "" + for s: Dictionary in steps: + if str(s.get("kind", "")) == RollTable.KIND_SHEET and not s.get("stop", false): + sheet = str(s.get("text", "")) + title = Formatter.strip_tags(str(s.get("table", title))) + if sheet != "": + body = sheet + else: + var lines := PackedStringArray() + for s: Dictionary in steps: + if s.get("inline", false) or s.get("dropped", false): + continue + lines.append("[b]%s:[/b] %s" % [str(s.get("table", "")), str(s.get("text", ""))]) + body = "\n".join(lines) + return as_format(Formatter.to_export_bbcode(body), title, format) + + +static func as_format(bbcode: String, title: String, format: String) -> String: + match format: + "md": + return "# %s\n\n%s\n" % [title, to_markdown(bbcode)] + "html": + return HTML_HEAD % [_esc(title), _esc(title)] + to_html(bbcode) + "\n\n" + return "%s\n%s\n\n%s\n" % [title, "=".repeat(title.length()), Formatter.strip_tags(bbcode)] + + +static func to_markdown(b: String) -> String: + var s := b + for pair: Array in [["[b]", "**"], ["[/b]", "**"], ["[i]", "*"], ["[/i]", "*"], ["[u]", ""], ["[/u]", ""], ["[s]", "~~"], ["[/s]", "~~"]]: + s = s.replace(pair[0], pair[1]) + return Formatter.strip_tags(s).replace("\n", " \n") + + +static func to_html(b: String) -> String: + var s := _esc(b) + for pair: Array in [["[b]", ""], ["[/b]", ""], ["[i]", ""], ["[/i]", ""], ["[u]", ""], ["[/u]", ""], + ["[s]", ""], ["[/s]", ""], ["[/color]", ""], ["[/font_size]", ""], ["[center]", "
"], ["[/center]", "
"]]: + s = s.replace(pair[0], pair[1]) + var re := RegEx.create_from_string("\\[color=([#a-zA-Z0-9]+)\\]") + s = re.sub(s, "", true) + re = RegEx.create_from_string("\\[font_size=(\\d+)\\]") + s = re.sub(s, "", true) + # Any other tag goes; line breaks become
. + re = RegEx.create_from_string("\\[/?[a-z_]+[^\\]]*\\]") + s = re.sub(s, "", true) + return "

" + s.replace("\n\n", "

").replace("\n", "
\n") + "

" + + +static func _esc(s: String) -> String: + return s.replace("&", "&").replace("<", "<").replace(">", ">") + + +const HTML_HEAD := """%s + +

%s

+""" + + +## The tray's "Save as…" submenu entry: asks where, then writes the file. +static func save(m: Main, format_index: int) -> void: + if m.current.is_empty(): + m.toast("Roll something first") + return + var f: Dictionary = FORMATS[format_index] + var name := LibraryStore.slugify(Formatter.strip_tags(str(m.current.get("root_name", "result")))) + "." + str(f["key"]) + m.export_dialog("Save the result", str(f["filter"]), name, func() -> String: return document(m, str(f["key"]))) diff --git a/ui/sheet_export.gd.uid b/ui/sheet_export.gd.uid new file mode 100644 index 0000000..daebf0a --- /dev/null +++ b/ui/sheet_export.gd.uid @@ -0,0 +1 @@ +uid://1wdfwnqqxwhw