Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

various bug fixes and features #47

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion lambda/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { spawnSync } from 'child_process';
import { createHmac }from 'crypto';
import { readFileSync, writeFileSync } from 'fs';
import { readFileSync, writeFileSync, existsSync } from 'fs';
import { request } from 'https';

import { APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda';
Expand Down Expand Up @@ -162,13 +162,21 @@ function findUpdated(before: string, after: string, cwd: string): Set<string> {
}

function findDirs(cwd: string): string[] {
if ( ! existsSync(cwd) ) {
console.log(cwd, 'does not exist in git clone, skipping');
return [];
}
const { stdout, error } = spawnSync('/var/task/bin/find', [
'-L', '.', '-mindepth', '1', '-type', 'd' ], { cwd, encoding: 'utf8' });
if (error) { throw error; }
return stdout.split('\n').map(line => line?.match(/^\.\/(.*)/)?.[1]).filter(nonnull);
}

function findFiles(cwd: string): string[] {
if ( ! existsSync(cwd) ) {
console.log(cwd, 'does not exist in git clone, skipping');
return [];
}
const { stdout, error } = spawnSync('/var/task/bin/find', [
'-L', '.', '-type', 'f' ], { cwd, encoding: 'utf8' });
if (error) { throw error; }
Expand Down
2 changes: 1 addition & 1 deletion server/course-setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
const OMNIVORE = 'https://omnivore.example.com';
const COURSE = '6.HANDX';
const SEMESTER = 'ia00';
$HANDOUT_TOC_KINDS = array('classes');
$HANDOUT_TOC_ROOTS = array('home-index');
// for remote delivery: path and secret
// $WWW_FS = '/mit/6.HANDX/www/' . SEMESTER;
// $WWW_SECRET = 'abcd1234';
Expand Down
29 changes: 19 additions & 10 deletions server/structure.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,21 @@
header("Access-Control-Allow-Origin: $origin");
}

function parse($json) {
$configjson = file_get_contents($json.'.json');
$config = json_decode($configjson);
return $config;
function load_handout($handout_id) {
return json_decode(file_get_contents('data/'.$handout_id.'.json'));
}

function incl($config) {
global $HANDOUT_TOC_KINDS;
return in_array($config->kind, $HANDOUT_TOC_KINDS) && ! property_exists($config, 'noindex');
function collect_handouts($handout_ids) {
$handouts = array_map(load_handout, array_values($handout_ids));
$array_of_array_of_handouts = array_map(function($config) {
return collect_handouts($config->handoutsToIndex);
}, $handouts);
foreach ($array_of_array_of_handouts as $array_of_handouts) {
foreach ($array_of_handouts as $config) {
array_push($handouts, $config);
}
}
return $handouts;
}

function entry($config) {
Expand All @@ -27,7 +33,10 @@ function entry($config) {
}

header('Content-Type: application/json');
$configs = array_map(function($name) { return substr($name, 0, -5); }, glob('data/*.json'));
natsort($configs);
print json_encode(array_map(entry, array_values(array_filter(array_map(parse, array_values($configs)), incl))));

$configs = collect_handouts($HANDOUT_TOC_ROOTS);
$configs = array_values(array_filter($configs, function($config) { return ! property_exists($config, 'noindex');
}));
//natsort($configs); // rely on the in-order traversal of handoutsToIndex
print json_encode(array_map(entry, $configs));
?>
16 changes: 16 additions & 0 deletions web/handout/handout-render.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,12 @@ function renderPage() {
id: this.id,
};
}).toArray();
window.handoutsToIndex = $('[data-handx-index] a').map(function() {
const href = $(this).attr('href');
const m = href.match(/([\w-]+\/[\w-]+)\/handout\//);
if (! m) return undefined;
return m[1].replace(/[^\w-]/g, '-');
}).toArray();

if (window.HANDOUT_DID_RENDER) { window.HANDOUT_DID_RENDER(); }
if (window.onHandoutDidRender) { window.onHandoutDidRender(); }
Expand Down Expand Up @@ -436,6 +442,15 @@ function convertExercise(container, category, node) {
choice.attr('data-ex-expected', encodeURIComponent(answerSpec));
}
});

// override answer-checking if we are inside a mark-all-answers-correct element
if ($(this).parents('.exercise-all-answers-correct').length > 0) {
$('.exercise-choice', this).each(function() {
var choice = $(this);
choice.removeAttr('data-ex-expected');
choice.attr('data-ex-regex', encodeURIComponent('/.*/'));
});
}
});

// header
Expand Down Expand Up @@ -492,6 +507,7 @@ function handoutDeliveryCallback() {
handout,
part: part || null,
structure: window.handoutStructure,
handoutsToIndex: window.handoutsToIndex,
exercises: window.handoutExercises,
noindex: document.querySelector('script[data-handx-noindex]') ? true : undefined,
});
Expand Down