/* * codeView.aus - Reusable syntax-highlighted code block component. * * Inherits from Highlighter and adds an optional "Try in Playground" * button implemented as a native HTML form POST (no JavaScript). */ include highlighter; /** * CodeView extends Highlighter to produce a styled code block with an * optional form-based "Try in Playground" button. The button submits the * source code to the Playground via multipart/form-data in a new tab. */ class CodeView : Highlighter { public showPlayground = false; public playgroundAction = 'https://playground.aussom-lang.com/load'; /** * Constructs a new CodeView. * @p Theme is an optional theme name ("dark" or "light") or a custom * map of {tokenType: colorString}. Defaults to "dark". * @p ShowLineNumbers is an optional bool to show a line-number gutter. * @p ShowCopyButton is an optional bool to show a "Copy" button in the header. * @p ShowPlayground is an optional bool to show the "Try in Playground" button. */ public CodeView(Theme = null, bool ShowLineNumbers = false, bool ShowCopyButton = false, bool ShowPlayground = false) { this.Highlighter(Theme, ShowLineNumbers, ShowCopyButton); this.showPlayground = ShowPlayground; } /** * Highlights the provided Aussom source code and returns a styled * HNode Div. When ShowPlayground is true, a form-based footer with * a submit button is appended to the code block. * @p Code is the Aussom source string to highlight. * @r A Div HNode containing the complete code view. */ public render(string Code) { codeBlock = this.highlight(Code); if (this.showPlayground) { /* Build an HTML form that POSTs the code as multipart/form-data */ form = new Form(this.playgroundAction, 'post'); form.setAttr('enctype', 'multipart/form-data'); form.setAttr('target', '_blank'); /* Invisible textarea carries the source code */ codeField = new Textarea('code', Code); codeField.setAttr('style', 'display:none;'); /* Submit button styled to match the site theme */ submitBtn = new Button('Try in Playground'); submitBtn.setAttr('type', 'submit'); submitBtn.setAttr('class', 'aus-play-btn'); submitBtn.setAttr('title', 'Open this code in the Aussom Playground'); form.add(codeField); form.add(submitBtn); footer = new Div(); footer.setAttr('class', 'aus-play-footer'); footer.add(form); codeBlock.add(footer); } return codeBlock; } }