ktxt2 exporting
This commit is contained in:
@@ -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
|
||||
|
@@ -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,
|
||||
|
@@ -10,24 +10,7 @@
|
||||
(var outputStarts [unlockedStart lockedStart])
|
||||
(var emptyBlock "${blockStartEnd}${unlockedStart}${blockStartEnd}")
|
||||
|
||||
// Return [sourceFile outputFile]
|
||||
(function :Array<String> 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<KTxt2Element> splitFileElements [:Stream fileStream]
|
||||
(let [elements []]
|
||||
@@ -79,6 +62,23 @@
|
||||
(elements.push (Block block))))
|
||||
elements))
|
||||
|
||||
// Return [sourceFile outputFile]
|
||||
(function :Array<String> 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<String,KTxt2Conversion> validConversions [:Array<KTxt2Conversion> conversions :String sourceFile :String outputFile :String source]
|
||||
(let [validMap (new Map)]
|
||||
(doFor c conversions
|
||||
|
@@ -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)))
|
||||
*/
|
@@ -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,
|
||||
|
@@ -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 @@
|
||||
<body>
|
||||
<script src=\"${scriptUri}\"></script>
|
||||
</body>
|
||||
</html>"))
|
||||
</html>"))
|
||||
|
||||
(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)))
|
Reference in New Issue
Block a user