wiki-plugin-mech 0.1.10 → 0.1.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/client/mech.js +76 -13
  2. package/package.json +1 -1
package/client/mech.js CHANGED
@@ -8,7 +8,6 @@
8
8
  .replace(/&/g, '&')
9
9
  .replace(/</g, '&lt;')
10
10
  .replace(/>/g, '&gt;')
11
- .replace(/\*(.+?)\*/g, '<i>$1</i>')
12
11
  }
13
12
 
14
13
 
@@ -42,12 +41,12 @@
42
41
  const key = `${unique}.${path.join('.')}`
43
42
  part.key = key
44
43
  if('command' in part)
45
- html.push(`<font color=gray size=small></font><span id=${key}>${expand(part.command)}</span>`)
44
+ html.push(`<font color=gray size=small></font><span style="display: block;" id=${key}>${expand(part.command)}</span>`)
46
45
  else
47
- html.push(`<div id=${key} style="padding-left:15px">${block(part,[...path,0])}</div> `)
46
+ html.push(`<div id=${key} style="padding-left:15px">${block(part,[...path,0])}</div>`)
48
47
  path[path.length-1]++
49
48
  }
50
- return html.join("<br>\n")
49
+ return html.join("\n")
51
50
  }
52
51
  return block(nest,[0])
53
52
  }
@@ -84,13 +83,13 @@
84
83
  }
85
84
  }
86
85
 
87
- function run (nest,state={}) {
86
+ function run (nest,state={},mock) {
88
87
  const scope = nest.slice()
89
88
  while (scope.length) {
90
89
  const code = scope.shift()
91
90
  if ('command' in code) {
92
91
  const command = code.command
93
- const elem = document.getElementById(code.key)
92
+ const elem = mock || document.getElementById(code.key)
94
93
  const [op, ...args] = code.command.split(/ +/)
95
94
  const next = scope[0]
96
95
  const body = next && ('command' in next) ? null : scope.shift()
@@ -345,13 +344,44 @@
345
344
  })
346
345
  }
347
346
 
348
- function kwic_emit ({elem,command,args,state}) {
347
+ function kwic_emit ({elem,command,args,body,state}) {
348
+ const template = body && body[0]?.command
349
+ if(template && !template.match(/\$[KW]/)) return trouble(elem,`KWIK expects $K or $W in link prototype.`)
349
350
  if(!('tsv' in state)) return trouble(elem,`KWIC expects a .tsv file, like from ASSETS .tsv.`)
350
- inspect(elem,'tsv',state)
351
- const lines = state.tsv.split(/\n/)
352
- elem.innerHTML = command + ` ⇒ ${lines.length} lines`
353
- const text = `Yes, we have no bananas. [https://en.wikipedia.org/wiki/Yes!_We_Have_No_Bananas wikipedia]`
354
- state.items = [{type:'paragraph',text}]
351
+ const prefix = args[0] || 1
352
+ const lines = state.tsv.trim().split(/\n/)
353
+
354
+ const stop = new Set(['of','and','in','at'])
355
+ const page = $(elem.closest('.page')).data('data')
356
+ const start = page.story.findIndex(item => item.type=='pagefold' && item.text=='stop')
357
+ if(start >= 0) {
358
+ const finish = page.story.findIndex((item,i) => i>start && item.type=='pagefold')
359
+ page.story.slice(start+1,finish)
360
+ .map(item => item.text.trim().split(/\s+/))
361
+ .flat()
362
+ .forEach(word => stop.add(word))
363
+ }
364
+
365
+ const groups = kwic(prefix,lines,stop)
366
+ elem.innerHTML = command + ` ⇒ ${lines.length} lines, ${groups.length} groups`
367
+ const link = quote => {
368
+ let line = quote.line
369
+ if(template) {
370
+ const substitute = template
371
+ .replaceAll(/\$K\+/g,quote.key.replaceAll(/ /g,'+'))
372
+ .replaceAll(/\$K/g,quote.key)
373
+ .replaceAll(/\$W/g,quote.word)
374
+ const target = template.match(/\$W/) ? quote.word : quote.key
375
+ line = line.replace(target,substitute)
376
+ }
377
+ return line
378
+ }
379
+
380
+ state.items = groups.map(group => {
381
+ text = `# ${group.group}\n\n${group.quotes
382
+ .map(quote=>link(quote))
383
+ .join("\n")}`
384
+ return {type:'markdown',text}})
355
385
  }
356
386
 
357
387
 
@@ -398,7 +428,7 @@
398
428
  }
399
429
 
400
430
  if (typeof module !== "undefined" && module !== null) {
401
- module.exports = {expand}
431
+ module.exports = {expand,tree,format,run}
402
432
  }
403
433
 
404
434
 
@@ -484,6 +514,39 @@
484
514
  })
485
515
  }
486
516
 
517
+
518
+ // adapted from testing-file-mech/testing-kwic.html
519
+ function kwic(prefix,lines,stop) {
520
+ const quotes = lines
521
+ .filter(line => line.match(/\t/))
522
+ .map(quote)
523
+ .flat()
524
+ .sort((a,b) => a.word<b.word ? -1 : 1)
525
+ let current = 'zzz'.slice(0,prefix)
526
+ const groups = []
527
+ for (const quote of quotes) {
528
+ const group = quote.word.toLowerCase().slice(0,prefix)
529
+ if (group != current) {
530
+ groups.push({group,quotes:[]})
531
+ current = group}
532
+ groups[groups.length-1].quotes.push(quote)
533
+ }
534
+ return groups
535
+
536
+ function quote(line) {
537
+ const [key,text] = line.split(/\t/)
538
+ const words = text
539
+ .replaceAll(/'t\b/g,'t')
540
+ .replaceAll(/'s\b/g,'s')
541
+ .split(/[^a-zA-Z]+/)
542
+ .filter(word => word.length>3 && !stop.has(word.toLowerCase()))
543
+ return words
544
+ .map(word => ({word,line,key}))
545
+ }
546
+ }
547
+
548
+
549
+
487
550
  // adapted from graph/src/graph.js
488
551
  class Graph {
489
552
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wiki-plugin-mech",
3
- "version": "0.1.10",
3
+ "version": "0.1.11",
4
4
  "description": "Federated Wiki - Mechanism Scripting Plugin",
5
5
  "keywords": [
6
6
  "mech",