Skip to content

Commit

Permalink
Topics & Terms is now populated only with handouts published on the h…
Browse files Browse the repository at this point in the history
…omepage: replaced HANDOUT_TOC_KINDS with HANDOUT_TOC_ROOTS, and added data-handx-index to mark links to handouts that should be included in the TOC by recursive traversal from the roots
  • Loading branch information
rcmiller committed May 27, 2023
1 parent 6bf9f77 commit 1c4d9dd
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 11 deletions.
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));
?>
7 changes: 7 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 @@ -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,
});
Expand Down

0 comments on commit 1c4d9dd

Please sign in to comment.