also support additional naming.json file in sketchbook folder

This commit is contained in:
Ben Fry
2022-08-03 06:56:34 -04:00
parent ab0e04a30a
commit 125afc2e0d
2 changed files with 27 additions and 10 deletions
+26 -10
View File
@@ -6,7 +6,6 @@ import processing.data.JSONObject;
import processing.data.StringList;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
@@ -15,6 +14,7 @@ import java.util.Map;
public class SketchName {
static final String FILENAME = "naming.json";
static final String CLASSIC_NAME = "Classic (sketch_220809a)";
static boolean breakTime = false;
@@ -119,26 +119,42 @@ public class SketchName {
File outgoing = null;
if (wl != null) {
do {
outgoing = new File(parentDir, wl.getPair());
// Clean up the name in case a user-supplied word list breaks the rules
String name = Sketch.sanitizeName(wl.getPair());
outgoing = new File(parentDir, name);
} while (outgoing.exists());
}
return outgoing;
}
static private void load(File namingFile) {
JSONArray array = PApplet.loadJSONArray(namingFile);
for (int i = 0; i < array.size(); i++) {
JSONObject obj = array.getJSONObject(i);
WordList wl = new WordList(obj);
wordLists.put(wl.name, wl);
}
}
static Map<String, WordList> getWordLists() {
if (wordLists == null) {
wordLists = new HashMap<>();
try {
File namingFile = Base.getLibFile("naming.json");
JSONArray array = PApplet.loadJSONArray(namingFile);
for (int i = 0; i < array.size(); i++) {
JSONObject obj = array.getJSONObject(i);
WordList wl = new WordList(obj);
wordLists.put(wl.name, wl);
File namingFile = Base.getLibFile(FILENAME);
load(namingFile);
} catch (Exception e) {
Messages.showWarning("Naming Error",
"Could not load word lists from " + FILENAME, e);
}
File sketchbookFile = new File(Base.getSketchbookFolder(), FILENAME);
if (sketchbookFile.exists()) {
try {
load(sketchbookFile);
} catch (Exception e) {
Messages.showWarning("Naming Error",
"Error while reading " + FILENAME + " from sketchbook folder", e);
}
} catch (IOException e) {
Messages.showWarning("Naming Error", "Could not load word lists from naming.json", e);
}
}
return wordLists;
+1
View File
@@ -45,6 +45,7 @@ X how friendly names are used in the p5.js web editor
X https://github.com/processing/p5.js-web-editor/blob/develop/client/utils/generateRandomName.js
X p5.js uses predicate followed by an object
X add other naming options (cooking and classic) and prefs
X also support additional naming.json file in sketchbook folder
bugs
X re-post bugzilla entries at https://download.processing.org/bugzilla/