diff --git a/server/course-setup.php b/server/course-setup.php index 64faf1d..e098c11 100644 --- a/server/course-setup.php +++ b/server/course-setup.php @@ -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'; diff --git a/server/structure.php b/server/structure.php index 99be46f..f6368b3 100644 --- a/server/structure.php +++ b/server/structure.php @@ -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) { @@ -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)); ?> diff --git a/web/handout/handout-render.js b/web/handout/handout-render.js index dec12f0..cfc4578 100644 --- a/web/handout/handout-render.js +++ b/web/handout/handout-render.js @@ -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(); } @@ -501,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, });