diff --git a/projects/hollywoo/src/hollywoo/examples/fountain-to-hollywoo/basic.fountain.hollywoo.ktxt2 b/projects/hollywoo/src/hollywoo/examples/fountain-to-hollywoo/basic.fountain.hollywoo.ktxt2 index cb9fc896..f0caffd6 100644 --- a/projects/hollywoo/src/hollywoo/examples/fountain-to-hollywoo/basic.fountain.hollywoo.ktxt2 +++ b/projects/hollywoo/src/hollywoo/examples/fountain-to-hollywoo/basic.fountain.hollywoo.ktxt2 @@ -2,11 +2,16 @@ Stuff outside the thing is a comment which you can edit to your heart's content| Credit: written by Author: Nat Quayle Nelson (she/her) Contact: natquaylenelson@gmail.com -|>||||testing editing another comment|||EXT. CABIN - DAY + +|>|${source}|||testing editing another comment|||EXT. CABIN - DAY + |>|||| |||A swanky cabin sits in a clearing in the Adirondacks. The Autumn colors are beautiful. + |>|||| |||INT. CABIN - SAME + |>|||| |||NAT NELSON (23, femme) sits by a tall window, typing on a laptop. + |>||||Stuff at the end is a different comment diff --git a/projects/kiss-vscode/src/ktxt2/KTxt2.hx b/projects/kiss-vscode/src/ktxt2/KTxt2.hx index dc2ccc23..3e61e095 100644 --- a/projects/kiss-vscode/src/ktxt2/KTxt2.hx +++ b/projects/kiss-vscode/src/ktxt2/KTxt2.hx @@ -2,8 +2,10 @@ package ktxt2; import kiss.Stream; import kiss.Prelude; +import kiss.KissInterp; using haxe.io.Path; +using StringTools; typedef KTxt2Block = { source:String, diff --git a/projects/kiss-vscode/src/ktxt2/KTxt2.kiss b/projects/kiss-vscode/src/ktxt2/KTxt2.kiss index 0869c6c0..abf99d72 100644 --- a/projects/kiss-vscode/src/ktxt2/KTxt2.kiss +++ b/projects/kiss-vscode/src/ktxt2/KTxt2.kiss @@ -10,24 +10,7 @@ (var outputStarts [unlockedStart lockedStart]) (var emptyBlock "${blockStartEnd}${unlockedStart}${blockStartEnd}") -// Return [sourceFile outputFile] -(function :Array splitFileContents [:Stream fileStream] - (let [&mut sourceText "" - &mut outputText ""] - (loop - (case (fileStream.takeUntilAndDrop blockStartEnd) - ((Some _) - (let [sourceBlock (fileStream.expect "A chunk of source text followed by one of $outputStarts" - ->(fileStream.takeUntilOneOf outputStarts))] - (+= sourceText "${sourceBlock}\n")) - (assert (apply = (for outputStart outputStarts outputStart.length)) "all output starts must be the same length!") - (fileStream.dropChars .length (first outputStarts)) - (let [outputBlock (fileStream.expect "A chunk of output text followed by $blockStartEnd" - ->(fileStream.takeUntilAndDrop blockStartEnd))] - (+= outputText "${outputBlock}\n"))) - (None - (break)))) - [sourceText outputText])) + (function :Array splitFileElements [:Stream fileStream] (let [elements []] @@ -79,6 +62,23 @@ (elements.push (Block block)))) elements)) +// Return [sourceFile outputFile] +(function :Array extractFileContents [:Stream fileStream] + (let [&mut sourceText "" + &mut outputText "" + interp (new KissInterp)] + (doFor ktxtElement (splitFileElements fileStream) + (case ktxtElement + ((Block (objectWith source output)) + (+= sourceText source) + (dictSet interp.variables "source" source) + (+= outputText + (let [escapedOutput + (output.replace "\"" "\\\"")] + (interp.evalKiss "\"${escapedOutput}\"")))) + (otherwise))) + [sourceText outputText])) + (function :Map validConversions [:Array conversions :String sourceFile :String outputFile :String source] (let [validMap (new Map)] (doFor c conversions diff --git a/projects/kiss-vscode/src/ktxt2/KTxt2Editor.kiss b/projects/kiss-vscode/src/ktxt2/KTxt2Editor.kiss index c1efa8e3..1e486cd9 100644 --- a/projects/kiss-vscode/src/ktxt2/KTxt2Editor.kiss +++ b/projects/kiss-vscode/src/ktxt2/KTxt2Editor.kiss @@ -115,7 +115,8 @@ :TextAreaElement outputDiv (cast (document.createElement "textarea")) convertLink (document.createElement "a") xLink (document.createElement "a") - lockLink (document.createElement "a")] + lockLink (document.createElement "a") + exportLink (document.createElement "a")] (outerDiv.setAttribute "class" "container") (outerDiv.setAttribute "style" "display: flex;") (sourceDiv.setAttribute "style" "width: 50%; white-space: pre;") @@ -154,6 +155,10 @@ (lockLink.addEventListener "click" ->(changeLockStatus (nth ktxt2Elements idx) !locked)) (outerDiv.appendChild lockLink) + (set exportLink.innerHTML "export") + (exportLink.addEventListener "click" + ->(vscode.postMessage (object type "export"))) + (outerDiv.appendChild exportLink) (content.appendChild outerDiv))) @@ -174,41 +179,3 @@ } (catch [error] (print "Error updating ktxt2 editor: ${error}")))) -/*(function exportSourceAndOutputFiles [document] - (let [[sourceText outputText] (splitFileContents document.fileName) - ktxt2FullFilename document.fileName - ktxt2Directory (haxe.io.Path.directory ktxt2FullFilename) - ktxt2Filename (haxe.io.Path.withoutDirectory ktxt2FullFilename) - [baseFilename sourceExt outputExt ktxt2Ext] (ktxt2Filename.split ".") - sourceFilename (joinPath ktxt2Directory "${baseFilename}.${sourceExt}") - outputFilename (joinPath ktxt2Directory "${baseFilename}.${outputExt}")] - // Use editors instead of File.saveContent for this, so the user can undo the export if - // it overwrites something! - (defMacro overwriteDocument [document content] - `(let [document ,document content ,content] - (awaitLet [editor (Vscode.window.showTextDocument document)] - (editor.edit - ->e (e.replace (new Range (document.positionAt 0) (document.positionAt .length (document.getText))) content))))) - (function uriFor [filename] - (let [uri (Uri.file filename)] - (if (sys.FileSystem.exists filename) - uri - (uri.with (object scheme "untitled"))))) - (awaitLet [sourceDocument - (Vscode.workspace.openTextDocument (uriFor sourceFilename)) - sourceEditSuccess - (overwriteDocument sourceDocument sourceText) - outputDocument - (Vscode.workspace.openTextDocument (uriFor outputFilename)) - outputEditSuccess - (overwriteDocument outputDocument outputText)] - (assert (and sourceEditSuccess outputEditSuccess)) - (awaitLet [saveSourceSuccess - (sourceDocument.save) - saveOutputSuccess - (outputDocument.save)] - (assert (and saveSourceSuccess saveOutputSuccess)))))) - -(function splitBlocks [&opt _] - (let [blocks (splitFileBlocks)] (print blocks))) -*/ \ No newline at end of file diff --git a/projects/kiss-vscode/src/ktxt2/KTxt2EditorProvider.hx b/projects/kiss-vscode/src/ktxt2/KTxt2EditorProvider.hx index af28edba..8a8c4681 100644 --- a/projects/kiss-vscode/src/ktxt2/KTxt2EditorProvider.hx +++ b/projects/kiss-vscode/src/ktxt2/KTxt2EditorProvider.hx @@ -2,8 +2,12 @@ package ktxt2; import kiss.Prelude; import kiss.List; +import kiss.Stream; import vscode.*; import js.lib.Promise; +import sys.io.File; + +using haxe.io.Path; typedef MessageFromEditor = { type:String, diff --git a/projects/kiss-vscode/src/ktxt2/KTxt2EditorProvider.kiss b/projects/kiss-vscode/src/ktxt2/KTxt2EditorProvider.kiss index 6c9fd5da..d9c63b84 100644 --- a/projects/kiss-vscode/src/ktxt2/KTxt2EditorProvider.kiss +++ b/projects/kiss-vscode/src/ktxt2/KTxt2EditorProvider.kiss @@ -55,6 +55,8 @@ (makeEdit ->edit (edit.replace document.uri (rangeFromStartEnd outputStart outputEnd) (chosenConversion.convert source))))))) (errorMessage "No automatic conversions are valid for the chosen block.")))) + ((object type "export") + (exportSourceAndOutputFiles document)) (otherwise (errorMessage "bad message $e from KTxt2Editor")))) @@ -79,4 +81,15 @@ - ")) \ No newline at end of file + ")) + +(function exportSourceAndOutputFiles [document] + (let [:String ktxt2FullFilename document.fileName + [sourceText outputText] (KTxt2.extractFileContents (Stream.fromFile ktxt2FullFilename)) + ktxt2Directory (ktxt2FullFilename.directory) + ktxt2Filename (ktxt2FullFilename.withoutDirectory) + [baseFilename sourceExt outputExt ktxt2Ext] (ktxt2Filename.split ".") + sourceFilename (joinPath ktxt2Directory "${baseFilename}.${sourceExt}") + outputFilename (joinPath ktxt2Directory "${baseFilename}.${outputExt}")] + (File.saveContent sourceFilename sourceText) + (File.saveContent outputFilename outputText))) \ No newline at end of file