Sheets: a line-per-value helper and Save as a file (Text, Markdown, web page)
The Sheet card's inspector adds a 'Label: {@value}' line for any value
picked. The tray's Copy ▾ gets Save as a file: the roll's sheet (or one
line per result) as Text, Markdown, or an HTML page a browser can print
to PDF, with bold, italics and colours kept where the format allows
(SheetExport). The test runner prints a compile error shared by many
suites once, with the suite names.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
61395fa573
commit
8fd2b2b8c9
6 changed files with 135 additions and 2 deletions
|
|
@ -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("<b>Name:</b>") and html.contains("color:#aa0000") and html.contains("& co") and html.contains("<br>"), "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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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))
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
||||
|
||||
|
|
|
|||
87
ui/sheet_export.gd
Normal file
87
ui/sheet_export.gd
Normal file
|
|
@ -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</body></html>\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>"], ["[/b]", "</b>"], ["[i]", "<i>"], ["[/i]", "</i>"], ["[u]", "<u>"], ["[/u]", "</u>"],
|
||||
["[s]", "<s>"], ["[/s]", "</s>"], ["[/color]", "</span>"], ["[/font_size]", "</span>"], ["[center]", "<div style=\"text-align:center\">"], ["[/center]", "</div>"]]:
|
||||
s = s.replace(pair[0], pair[1])
|
||||
var re := RegEx.create_from_string("\\[color=([#a-zA-Z0-9]+)\\]")
|
||||
s = re.sub(s, "<span style=\"color:$1\">", true)
|
||||
re = RegEx.create_from_string("\\[font_size=(\\d+)\\]")
|
||||
s = re.sub(s, "<span style=\"font-size:$1px\">", true)
|
||||
# Any other tag goes; line breaks become <br>.
|
||||
re = RegEx.create_from_string("\\[/?[a-z_]+[^\\]]*\\]")
|
||||
s = re.sub(s, "", true)
|
||||
return "<p>" + s.replace("\n\n", "</p><p>").replace("\n", "<br>\n") + "</p>"
|
||||
|
||||
|
||||
static func _esc(s: String) -> String:
|
||||
return s.replace("&", "&").replace("<", "<").replace(">", ">")
|
||||
|
||||
|
||||
const HTML_HEAD := """<!doctype html><html><head><meta charset="utf-8"><title>%s</title>
|
||||
<style>body{font-family:Georgia,serif;max-width:42em;margin:2em auto;padding:0 1em;line-height:1.5;color:#1c0329}
|
||||
h1{font-size:1.6em;border-bottom:2px solid #823d08;padding-bottom:.2em}</style></head><body>
|
||||
<h1>%s</h1>
|
||||
"""
|
||||
|
||||
|
||||
## 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"])))
|
||||
1
ui/sheet_export.gd.uid
Normal file
1
ui/sheet_export.gd.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://1wdfwnqqxwhw
|
||||
Loading…
Reference in a new issue