diff --git a/cjklib/data.js b/cjklib/data.js index dac2bd3b..3eb85d7a 100644 --- a/cjklib/data.js +++ b/cjklib/data.js @@ -7,6 +7,7 @@ const CHARACTER_FIELDS = ['character', 'decomposition', 'definition', this.cjklib = { characters: {}, gb2312: {}, + promise: undefined, radicals: { primary_radical: {}, index_to_radical_map: {}, @@ -214,7 +215,7 @@ Meteor.startup(() => { readFile('unihan/Unihan_RadicalStrokeCounts.txt').then(getUnihanRows); const readings = readFile('unihan/Unihan_Readings.txt').then(getUnihanRows); - Promise.all([ + cjklib.promise = Promise.all([ // Per-character data. fillDecompositions(decomposition, glyphs, cjklib.characters.decomposition), @@ -229,5 +230,6 @@ Meteor.startup(() => { cjklib.radicals.radical_to_character_map), // Extract the list of characters in the GB2312 character set. readFile('gb2312').then((data) => fillGB2312(data, cjklib.gb2312)), - ]).then(cleanupCJKLibData).catch(console.error.bind(console)); + ]).then(cleanupCJKLibData); + cjklib.promise.catch(console.error.bind(console)); }); diff --git a/lib/glyphs.js b/lib/glyphs.js index 7cf168e5..e9f1c976 100644 --- a/lib/glyphs.js +++ b/lib/glyphs.js @@ -13,13 +13,16 @@ Glyphs.get_svg_path = function(glyph) { var terms = []; for (var i = 0; i < glyph.path.length; i++) { var segment = glyph.path[i]; + assert('LMQZ'.indexOf(segment.type) >= 0, segment.type); terms.push(segment.type); if (segment.x1 !== undefined) { terms.push(segment.x1); terms.push(segment.y1); } - terms.push(segment.x); - terms.push(segment.y); + if (segment.x !== undefined) { + terms.push(segment.x); + terms.push(segment.y); + } } return terms.join(' '); } diff --git a/server/glyphs.js b/server/glyphs.js index d34d6090..5902d59f 100644 --- a/server/glyphs.js +++ b/server/glyphs.js @@ -54,8 +54,3 @@ Meteor.methods({ } }, }); - -Meteor.startup(function() { - Glyphs._ensureIndex({name: 1}, {unique: true}); - Glyphs._ensureIndex({'manual.verified': 1}); -}); diff --git a/server/migration.js b/server/migration.js new file mode 100644 index 00000000..2cb6048a --- /dev/null +++ b/server/migration.js @@ -0,0 +1,48 @@ +// Given an old-form glyph entry, returns the new glyph entry. +migrate_glyph = (glyph) => { + const codepoint = parseInt(glyph.name.substr(3), 16); + const character = String.fromCodePoint(codepoint); + const data = cjklib.getCharacterData(character); + assert(glyph.manual.verified !== undefined); + const result = { + character: character, + codepoint: codepoint, + metadata: { + definition: undefined, + kangxi_index: data.kangxi_index, + pinyin: undefined, + strokes: undefined, + }, + stages: { + path: Glyphs.get_svg_path(glyph), + bridges: glyph.manual.bridges, + strokes: glyph.derived.strokes, + analysis: undefined, + order: undefined, + settled: undefined, + }, + }; + assert(result.stages.path !== undefined); + assert(result.stages.bridges !== undefined); + assert(result.stages.strokes !== undefined); + return result; +} + +migrate_glyphs = () => { + const names = Glyphs.find({}, {fields: {name: 1}, sort: {name: 1}}).fetch(); + names.reverse(); + for (name of names) { + const glyph = Glyphs.findOne({name: name.name}); + const migrated_glyph = migrate_glyph(glyph); + Glyphs.insert(migrated_glyph); + } + console.log('Migration complete.'); +} + +Meteor.startup(() => { + //console.log('Running migration...'); + //cjklib.promise.then(Meteor.bindEnvironment(migrate_glyphs)) + // .catch(console.error.bind(console)); + Glyphs._ensureIndex({character: 1}, {unique: true}); + Glyphs._ensureIndex({codepoint: 1}, {unique: true}); +});