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"])))