Skip to content

Commit

Permalink
Merge pull request #9058 from keymanapp/chore/developer/kmw-support-e…
Browse files Browse the repository at this point in the history
…mbed-files

chore(developer): support embedded files in kmw compiler 🗜
  • Loading branch information
mcdurdin authored Jun 23, 2023
2 parents fd64028 + 165dcbe commit 3730f2d
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 67 deletions.
8 changes: 6 additions & 2 deletions developer/src/kmc-kmn/src/kmw-compiler/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ export class KmwCompilerMessages extends KmnCompilerMessages {

static Error_InvalidBegin = () => m(this.ERROR_InvalidBegin,
`A "begin unicode" statement is required to compile a KeymanWeb keyboard`);
static Error_TouchLayoutFileInvalid = () => m(this.ERROR_InvalidTouchLayoutFile,
`Touch layout file is not valid`);
static Error_TouchLayoutFileInvalid = (o:{filename:string}) => m(this.ERROR_InvalidTouchLayoutFile,
`Touch layout file ${o.filename} is not valid`);
static Warn_DontMixChiralAndNonChiralModifiers = () => m(this.WARN_DontMixChiralAndNonChiralModifiers,
`This keyboard contains Ctrl,Alt and LCtrl,LAlt,RCtrl,RAlt sets of modifiers. Use only one or the other set for web target.`);
static Warn_OptionStoreNameInvalid = (o:{name:string}) => m(this.WARN_OptionStoreNameInvalid,
Expand All @@ -39,6 +39,10 @@ export class KmwCompilerMessages extends KmnCompilerMessages {
`Statement ${o.code} is not currently supported in context`);
static Error_NotSupportedInKeymanWebOutput = (o:{code:string}) => m(this.ERROR_NotSupportedInKeymanWebOutput,
`Statement ${o.code} is not currently supported in output`);
static Warn_HelpFileMissing = (o:{filename: string, e:any}) => m(this.WARN_HelpFileMissing,
`File ${o.filename} could not be loaded: ${(o.e??'').toString()}`);
static Warn_EmbedJsFileMissing = (o:{filename: string, e:any}) => m(this.WARN_EmbedJsFileMissing,
`File ${o.filename} could not be loaded: ${(o.e??'').toString()}`);

// Following messages are kmw-compiler only, so use KmwCompiler error namespace

Expand Down
107 changes: 42 additions & 65 deletions developer/src/kmc-kmn/src/kmw-compiler/write-compiled-keyboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ export function WriteCompiledKeyboard(callbacks: CompilerCallbacks, kmnfile: str

let vMnemonic: number = 0;
let /*s: string,*/ sRTL: string = "", sHelp: string = "''", sHelpFile: string = "",
sEmbedJS: string = "", sEmbedCSS: string = "";
sEmbedJSFilename: string = "", sEmbedCSSFilename: string = "";
let sVisualKeyboardFilename: string = "", sFullName: string = "";
let sBegin_NewContext: string = "", sBegin_PostKeystroke: string = "";
let sLayoutFile: string = "", sVKDictionary: string = "";
let sLayoutFilename: string = "", sVKDictionary: string = "";
let linecomment: string; // I3438
// let HasRules: boolean;
let sModifierBitmask: string;
Expand Down Expand Up @@ -84,10 +84,10 @@ export function WriteCompiledKeyboard(callbacks: CompilerCallbacks, kmnfile: str
sVisualKeyboardFilename = fsp.dpString;
}
else if (fsp.dpName == 'EmbedJS' || fsp.dwSystemID == KMX.KMXFile.TSS_KMW_EMBEDJS) {
sEmbedJS = fsp.dpString;
sEmbedJSFilename = fsp.dpString;
}
else if (fsp.dpName == 'EmbedCSS' || fsp.dwSystemID == KMX.KMXFile.TSS_KMW_EMBEDCSS) { // I4368
sEmbedCSS = fsp.dpString;
sEmbedCSSFilename = fsp.dpString;
}
else if (fsp.dpName == 'RTL' || fsp.dwSystemID == KMX.KMXFile.TSS_KMW_RTL) {
sRTL = fsp.dpString == '1' ? FTabStop+'this.KRTL=1;'+nl : ''; // I3681
Expand All @@ -99,7 +99,7 @@ export function WriteCompiledKeyboard(callbacks: CompilerCallbacks, kmnfile: str
sVKDictionary = fsp.dpString;
}
else if (fsp.dwSystemID == KMX.KMXFile.TSS_LAYOUTFILE) { // I3483
sLayoutFile = fsp.dpString;
sLayoutFilename = fsp.dpString;
}
else if (fsp.dwSystemID == KMX.KMXFile.TSS_BEGIN_NEWCONTEXT) {
sBegin_NewContext = fsp.dpString;
Expand All @@ -113,75 +113,52 @@ export function WriteCompiledKeyboard(callbacks: CompilerCallbacks, kmnfile: str

if (sHelpFile != '') {
sHelp = '';
//TODO: load sHelpFile from file
/*with TStringList.Create do
try
try
LoadFromFile(ExtractFilePath(FInFile) + sHelpFile, TEncoding.UTF8); // I3337
for n := 0 to Count - 1 do
sHelp := sHelp + Strings[n] + ' ';
except
on E:EFOpenError do
begin
ReportError(0, CWARN_HelpFileMissing, E.Message); // I1971 // I4061
sHelp := '';
end;
end;
finally
Free;
end;*/

sHelp = requote(sHelp);
}

if (sEmbedJS != '') {
//TODO: load sEmbedJS from file
/*try
with TStringList.Create do
try
LoadFromFile(ExtractFilePath(FInFile) + sEmbedJS, TEncoding.UTF8); // I3337
sEmbedJS := Text;
finally
Free;
end;
except
on E:EFOpenError do // I3683
begin
ReportError(0, CWARN_EmbedJsFileMissing, E.Message); // I4061
sEmbedJS := '';
end;
end;*/
sHelpFile = callbacks.resolveFilename(kmnfile, sHelpFile);
try {
const data = callbacks.loadFile(sHelpFile);
sHelp = new TextDecoder().decode(data).replace(/[\r\n]/g, ' ');
sHelp = requote(sHelp);
} catch(e) {
callbacks.reportMessage(KmwCompilerMessages.Warn_HelpFileMissing({filename: sHelpFile, e}));
sHelp = '';
}
}

if (sEmbedCSS != '') { // I4368
//TODO: load sEmbedCSS from file
/*try
with TStringList.Create do
try
LoadFromFile(ExtractFilePath(FInFile) + sEmbedCSS, TEncoding.UTF8); // I3337
sEmbedCSS := Text;
finally
Free;
end;
except
on E:EFOpenError do // I3683
begin
ReportError(0, CWARN_EmbedJsFileMissing, E.Message); // I4061
sEmbedCSS := '';
end;
end;*/
let sEmbedJS = '';
if (sEmbedJSFilename != '') {
sEmbedJSFilename = callbacks.resolveFilename(kmnfile, sEmbedJSFilename);
try {
const data = callbacks.loadFile(sEmbedJSFilename);
sEmbedJS = new TextDecoder().decode(data);
} catch(e) {
callbacks.reportMessage(KmwCompilerMessages.Warn_EmbedJsFileMissing({filename: sEmbedJSFilename, e}));
sEmbedJS = '';
}
}

if (sLayoutFile != '') { // I3483
let path = callbacks.resolveFilename(kmnfile, sLayoutFile);
let sEmbedCSS = '';
if (sEmbedCSSFilename != '') { // I4368
sEmbedCSSFilename = callbacks.resolveFilename(kmnfile, sEmbedCSSFilename);
try {
const data = callbacks.loadFile(sEmbedCSSFilename);
sEmbedCSS = new TextDecoder().decode(data);
} catch(e) {
// TODO(lowpri): rename error constant to Warn_EmbedFileMissing
callbacks.reportMessage(KmwCompilerMessages.Warn_EmbedJsFileMissing({filename: sEmbedCSSFilename, e}));
sEmbedCSS = '';
}
}

let sLayoutFile = '';
if (sLayoutFilename != '') { // I3483
sLayoutFilename = callbacks.resolveFilename(kmnfile, sLayoutFilename);

let result = ValidateLayoutFile(keyboard, options.saveDebug, path, sVKDictionary, displayMap);
let result = ValidateLayoutFile(keyboard, options.saveDebug, sLayoutFilename, sVKDictionary, displayMap);
if(!result.result) {
sLayoutFile = '';
callbacks.reportMessage(KmwCompilerMessages.Error_TouchLayoutFileInvalid());
callbacks.reportMessage(KmwCompilerMessages.Error_TouchLayoutFileInvalid({filename:sLayoutFilename}));
return null;
} else {
// TODO: reusing the same variable here is ugly
sLayoutFile = result.output;
}
}
Expand Down

0 comments on commit 3730f2d

Please sign in to comment.