Skip to content

Commit 20ee38f

Browse files
remove divisible goods, generate indiv using template
1 parent e92aebe commit 20ee38f

11 files changed

+83
-192
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ __pycache__
66
/cpigjs/*.js.map
77
/cpigjs/*.mjs
88
/cpigjs/*.mjs.map
9+
/fairDiv/*.html
910

1011
/secret
1112
/output
File renamed without changes.

fairDiv/div-family.json

-39
This file was deleted.

fairDiv/div-goods.html

-43
This file was deleted.

fairDiv/div-goods.json

-35
This file was deleted.

fairDiv/htmlContext.json

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"title": "Implications in Fair Division of Indivisible Items",
3+
"funcToFormUrl": "https://sharmaeklavya2.github.io/funcToForm",
4+
"dotvizUrl": "https://cdn.jsdelivr.net/npm/@viz-js/[email protected]/lib/viz-standalone.mjs",
5+
"familyFile": "'setFamily.json'",
6+
"dataFiles": "['data.json']",
7+
"texRefsFile": "'texRefs.json'"
8+
}

fairDiv/index.html

-67
This file was deleted.
File renamed without changes.
File renamed without changes.

scripts/templateFill.js

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#!/usr/bin/env node
2+
3+
import { readFile, writeFile } from 'node:fs/promises';
4+
import yargs from 'yargs';
5+
6+
function templateFill(template, context, openDelim, closeDelim) {
7+
const n = template.length;
8+
let insideVar = false;
9+
let i = 0;
10+
const parts = [];
11+
while(i < n) {
12+
if(insideVar) {
13+
const j = template.indexOf(closeDelim, i);
14+
if(j === -1) {
15+
throw new Error("can't find end of context variable.");
16+
}
17+
const contextVarName = template.slice(i, j);
18+
const contextVarValue = context[contextVarName];
19+
if(contextVarValue === undefined) {
20+
throw new Error(`variable ${contextVarName} is absent from context.`)
21+
}
22+
parts.push(contextVarValue);
23+
insideVar = false;
24+
i = j + 2;
25+
}
26+
else {
27+
const j = template.indexOf(openDelim, i);
28+
if(j === -1) {
29+
parts.push(template.slice(i));
30+
i = n;
31+
}
32+
else {
33+
parts.push(template.slice(i, j));
34+
insideVar = true;
35+
i = j + 2;
36+
}
37+
}
38+
}
39+
return parts;
40+
}
41+
42+
async function main() {
43+
const args = yargs(process.argv.slice(2))
44+
.option('template', {alias: 't', type: 'string', demandOption: true,
45+
describe: "path to template file"})
46+
.option('context', {alias: 'c', type: 'string', demandOption: true,
47+
describe: "path to JSON context file"})
48+
.option('output', {alias: 'o', type: 'string', demandOption: true,
49+
describe: "path to output file"})
50+
.help()
51+
.parse();
52+
53+
// console.log(args);
54+
const encUtf8 = {encoding: 'utf8'};
55+
const contextPromise = readFile(args.context, encUtf8).then(JSON.parse);
56+
const templatePromise = readFile(args.template, encUtf8);
57+
const context = await contextPromise;
58+
const template = await templatePromise;
59+
60+
const parts = templateFill(template, context, '{{', '}}');
61+
const output = parts.join('');
62+
63+
await writeFile(args.output, parts.join(''));
64+
}
65+
66+
await main();

fairDiv/indiv.html renamed to template.html

+8-8
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@
44
<meta charset="utf-8" />
55
<meta name="viewport" content="width=device-width, initial-scale=1" />
66
<meta name="color-scheme" content="dark light" />
7-
<title>Implications in Fair Division of Indivisible Items</title>
7+
<title>{{title}}</title>
88
<script type="importmap">
99
{"imports": {
10-
"funcToForm": "https://sharmaeklavya2.github.io/funcToForm/v2.js",
11-
"dotviz": "https://cdn.jsdelivr.net/npm/@viz-js/[email protected]/lib/viz-standalone.mjs"
10+
"funcToForm": "{{funcToFormUrl}}/v2.js",
11+
"dotviz": "{{dotvizUrl}}"
1212
}}
1313
</script>
14-
<link rel="stylesheet" href="https://sharmaeklavya2.github.io/funcToForm/funcToForm.css" />
14+
<link rel="stylesheet" href="{{funcToFormUrl}}/funcToForm.css" />
1515
<link rel="stylesheet" href="../cpigjs/cpigjs.css" />
16-
<link rel="modulepreload" href="https://sharmaeklavya2.github.io/funcToForm/v2.js" />
17-
<link rel="modulepreload" href="https://cdn.jsdelivr.net/npm/@viz-js/[email protected]/lib/viz-standalone.mjs" />
16+
<link rel="modulepreload" href="{{funcToFormUrl}}/v2.js" />
17+
<link rel="modulepreload" href="{{dotvizUrl}}" />
1818
<link rel="modulepreload" href="../cpigjs/web.js" />
1919
<link rel="modulepreload" href="../cpigjs/main.js" />
2020
<link rel="modulepreload" href="../cpigjs/setFamily.js" />
@@ -23,11 +23,11 @@
2323
<link rel="modulepreload" href="../cpigjs/queue.js" />
2424
<script type="module">
2525
import { setup } from '../cpigjs/web.js';
26-
await setup('indiv-family.json', ['indiv.json'], 'indivRefs.json');
26+
await setup({{familyFile}}, {{dataFiles}}, {{texRefsFile}});
2727
</script>
2828
</head>
2929
<body>
30-
<h1>Implications in Fair Division of Indivisible Items</h1>
30+
<h1>{{title}}</h1>
3131
<div id="form-container"></div>
3232
<div id="output-container"></div>
3333
</body>

0 commit comments

Comments
 (0)