Trust nobody!
I hate having to conform to the limitations of other people’s software. I spent the last two weeks struggling with a stupid problem imposed upon me by other people’s stupidity.
The starting point of my woes was the fact that the output of my editor program is an XML file, but the display program is in JavaScript. So my JavaScript program must be able to read an XML file. That should be easy — there are libraries that provide that capability. The problem is, they are a rat’s nest of special requirements and constraints. I jump through half a dozen hoops, each requiring research and experimentation, and I still can’t get it working.
OK, XML really is a mismatch for JavaScript. Much better is JSON (“JavaScript Object Notation”), designed by my once-employee at Atari, Douglas Crockford. JSON is designed to work with JavaScript. Ergo, it should be much easier to work with. Of course, before I can use it, I must transform my XML file into JSON. Scrounging around the web, I find a number of explanations of how to write a short routine that will transform XML files into JSON. However, they look rather tricky, so when I also find some online resources that will carry out the transformation automatically, I pounce upon those.
Armed with my shiny new JSON file, I set to work figuring out how to read it into the Le Morte D’Arthur web page. Alas, this proves to be almost as messy as working with the XML file. Back in the Stone Age of computing, when I was working on the 8-bit, 1 MHz Atari computer, I could load a file from diskette with about a dozen lines of assembly language code. But that was when life was much simpler. Nowadays loading a file over the Internet is infinitely more complex. First you have to ascertain that your program is authorized to access the given file. Then you have to open the file, then attempt to load the data, but you must also specify whether you want the file loaded synchronously (in one fell swoop) or asynchronously (in the background while your program does other things). Then you must take into account the certainty that there will be delays in the data traveling over the Internet. How long are you willing to wait for the data to come in? Do you want to abort the load process if too much time elapses? That’s where I gave up. That’s just too many hoops to jump through.
So I decided to include the data right there in the same web page. The data will be loaded as part of the process of loading the web page. All I have to do is put all the data into the JavaScript portion of the HTML of the web page. Granted, that’s a little unconventional, given that the data amounts to some 1.5 megabytes of text. But it works, and the other approaches don’t.
So I copied and pasted the contents of the JSON file into my web page. It didn’t work. More gnashing of teeth: it doesn’t work. After several days during which it is a gift from heaven that Douglas Crockford doesn’t come within firearm range of my house, I struggle trying to figure out the problem. Eventually I stumble upon it: JSON (“JavaScript Object Notation”) is not formatted as JavaScript objects. It’s not anything that JavaScript can use — it’s only a notation that can be transformed (with the appropriate software) into JavaScript objects. Apparently my folding the JSON data directly into the JavaScript code was a faux pas worse than holding your tea cup without keeping your little finger raised. JSON isn’t meant to be actually USED in JavaScript — it’s meant only to be READ by JavaScript, at which point you carry out some further computations to actually access the data. Oh. How silly of me to think that JSON is usable.
So I need to convert the JSON (“JavaScript Object Notation”) into actual JavaScript object notation (note the difference between the uppercase letters in the former and the lowercase letters in the latter.) Fortunately, there are also tools on the web that automatically convert JSON to JavaScript objects. Unfortunately, they don’t work very well. Fortunately, there’s a professional tool for sale that also carries out the conversion. Unfortunately, it works only on Windows.
At this point I’m ranting about all this idiocy. Yes, I know, people will say, “Chris, once you learn how to load a file from the Internet, it’s easy!” That’s true. It’s also true that, once you learn how to juggle two cats and three running chain saws, it’s easy. The problem isn’t in the doing, it’s in the learning. A modern programmer, brought up in the realm of DOMs, factories, URLs, and the Internet will have no problem absorbing this stuff. An obsolete dinosaur like me won’t. I feel like a Tyrannosaurus Rex stomping around in a crowd of robots.
In my frustration, I consider developing my own file format and handling everything in that. “Roll your own!” was the battle cry of the early microcomputer people like me. What you wanted wasn’t out there, so you had to be self-reliant. My ranting was generously spiced with obscenities, so my wife Kathy helpfully suggested that I call my creation the “FFF”: “Fucking File Format”. I liked the sound of that.
I came up with a goulash solution. I started with the grossly flawed output of the online JSON-to-JavaScript object converter. I then wrote a little routine in Java that corrected the most common defect, which appeared hundreds of times. I then massaged the file with a variety of corrections, some involving global search and replace in my text editor. The process was so complicated that I wrote down the algorithm for future reference. Every single time that I edit the storyworld XML file, I’ll have to go through this procedure:
Procedure for Converting LMD XML file to JavaScript Object format
1. Convert LMD XML file to JSON file using this tool: https://www…………………...
2. Convert LMD JSON file to JavaScript object file using this tool: https://www……..
3. Replace the first six lines of the file with this:
const actorList = [
4. Change lines 31 and 32 to this: var encounterList = [
5. Run Java correction program on the file.
6. Global search and replace end of IntroText the text ., to .",
(That is, insert a double quote between the period and the comma at the end of each IntroText)
7. Global search and replace \",\n to \"",\n
That is, when \" appears at the end of a line, insert an additional double quote between it and the terminal comma.
8. Global search and replace ?,\n to ?",\n
8. Global search and replace !,\n to !",\n
9. Global search and replace :,\n to :",\n
10. Remove comma from the end of the ActorList
11. Delete this line: Encounter: [
12. Delete the three last curly braces in the file.
13. Save the file.
14. Copy contents of the entire file and paste them into the appropriate section of Le Morte D’Arthur.html.
This entire process — figuring out a new approach to solving the problem, developing the specific solution, writing the Java code, testing and refining the above procedure — took about 22 hours, including time off for meals and sleep. I wasted two weeks trying to do it by other people’s methods. The end result will strike observers as peculiar in the extreme, but it works. That’s all that matters.