Claude commited on
Commit
327d32d
·
1 Parent(s): 12caec6

Editor downloadHTML: emit summary, self-check, glossary, highlights

Browse files

The editor's "Download final HTML" path doesn't go through the
server-side Jinja templates — it builds the final document from the
editor's live DOM via buildFinalHTML(). I taught the server template
(guide.html) about the four learner features but forgot to teach
buildFinalHTML the same vocabulary, so the downloaded file came out
with key terms and math equations but no summary, no self-check
block, no inline term highlights, and no glossary.

Extend buildFinalHTML to inject all four:

- Summary line (.section__summary "In brief: …") inserted right
after each section's <p class="meta-time">.
- Inline highlighting of defined terms in narration paragraphs
via a JS port of the server-side _highlight_terms filter
(longest-first, one mark per term per paragraph, skips text
already inside a <mark> so wraps never nest).
- Self-check questions as a <details class="self-check"> block
at the end of each section, matching guide.html's structure.
- Auto-built glossary as a <section class="glossary"> appended
to <main> — same dedupe rules as the server-side helper
(case-insensitive first-occurrence wins, alphabetised, back-
anchored to the section where each term first appears).

All four features now reach the downloaded HTML, the Preview tab,
and the in-browser editor's view consistently.

Files changed (1) hide show
  1. app/templates/review.html +170 -9
app/templates/review.html CHANGED
@@ -799,18 +799,102 @@
799
  clone.querySelectorAll('script').forEach(el => el.remove());
800
 
801
  // For each remaining (included) section, append rendered key-terms <dl>
802
- // and math <figure> elements built from the captured state.
 
 
 
 
803
  let needsMathJax = false;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
804
  clone.querySelectorAll('section.section').forEach(section => {
805
  const segState = stateBySegmentId.get(section.id);
806
  if (!segState) return;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
807
  if (segState.key_terms && segState.key_terms.length) {
808
- const dl = clone.ownerDocument.createElement('dl');
 
 
 
 
 
 
 
 
 
 
809
  dl.className = 'key-term';
810
  segState.key_terms.forEach(kt => {
811
- const dt = clone.ownerDocument.createElement('dt');
812
  dt.textContent = kt.term;
813
- const dd = clone.ownerDocument.createElement('dd');
814
  dd.textContent = kt.definition;
815
  dl.appendChild(dt);
816
  dl.appendChild(dd);
@@ -820,38 +904,115 @@
820
  if (segState.math_equations && segState.math_equations.length) {
821
  needsMathJax = true;
822
  segState.math_equations.forEach(eq => {
823
- const fig = clone.ownerDocument.createElement('figure');
824
  fig.className = 'equation';
825
  // Don't set role="math" or aria-label — MathJax's assistiveMml is
826
  // the right accessible representation. aria-label on the wrapper
827
  // would override it. Expose the human-readable description as a
828
  // visually-hidden figcaption so AT users get it without conflict.
829
  if (eq.aria_label && eq.aria_label.trim() && !eq.caption) {
830
- const cap = clone.ownerDocument.createElement('figcaption');
831
  cap.className = 'sr-only';
832
  cap.textContent = eq.aria_label.trim();
833
  fig.appendChild(cap);
834
  }
835
  if (eq.label) {
836
- const lab = clone.ownerDocument.createElement('span');
837
  lab.className = 'equation__label';
838
  lab.textContent = eq.label;
839
  fig.appendChild(lab);
840
  }
841
- const tex = clone.ownerDocument.createTextNode(
842
  eq.inline ? `\\(${eq.latex}\\)` : `\\[${eq.latex}\\]`
843
  );
844
  fig.appendChild(tex);
845
  if (eq.caption) {
846
- const cap = clone.ownerDocument.createElement('figcaption');
847
  cap.textContent = eq.caption;
848
  fig.appendChild(cap);
849
  }
850
  section.appendChild(fig);
851
  });
852
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
853
  });
854
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
855
  // Drop excluded segments.
856
  clone.querySelectorAll('section.section[data-included="false"]').forEach(s => s.remove());
857
 
 
799
  clone.querySelectorAll('script').forEach(el => el.remove());
800
 
801
  // For each remaining (included) section, append rendered key-terms <dl>
802
+ // and math <figure> elements built from the captured state. Same
803
+ // pass also injects the learner-focused features that the review
804
+ // template doesn't render inline (summary above the figure;
805
+ // self-check questions block at the end; inline highlighting of
806
+ // defined key terms inside narration paragraphs).
807
  let needsMathJax = false;
808
+ const doc = clone.ownerDocument;
809
+
810
+ function escapeHtml(s) {
811
+ return String(s)
812
+ .replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;')
813
+ .replace(/"/g, '&quot;').replace(/'/g, '&#39;');
814
+ }
815
+
816
+ function escapeRegex(s) {
817
+ return s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
818
+ }
819
+
820
+ function highlightTerms(paragraphText, keyTerms) {
821
+ // Mirrors the server-side _highlight_terms filter: case-
822
+ // insensitive whole-word match, longest term first, one mark
823
+ // per term per paragraph, no nesting.
824
+ if (!keyTerms || !keyTerms.length) return escapeHtml(paragraphText);
825
+ let escaped = escapeHtml(paragraphText);
826
+ const sorted = keyTerms
827
+ .map(kt => ({ term: (kt.term || '').trim(), definition: (kt.definition || '').trim() }))
828
+ .filter(kt => kt.term.length >= 3)
829
+ .sort((a, b) => b.term.length - a.term.length);
830
+ const markSpan = /<mark class="kt"[^>]*>.*?<\/mark>/g;
831
+ for (const { term, definition } of sorted) {
832
+ const pattern = new RegExp('\\b' + escapeRegex(escapeHtml(term)) + '\\b', 'i');
833
+ const title = escapeHtml(definition || '');
834
+ let lastEnd = 0;
835
+ let replaced = false;
836
+ let out = '';
837
+ let m;
838
+ markSpan.lastIndex = 0;
839
+ while ((m = markSpan.exec(escaped)) !== null) {
840
+ let outside = escaped.slice(lastEnd, m.index);
841
+ if (!replaced && pattern.test(outside)) {
842
+ outside = outside.replace(
843
+ pattern,
844
+ `<mark class="kt" title="${title}">$&</mark>`,
845
+ );
846
+ replaced = true;
847
+ }
848
+ out += outside + m[0];
849
+ lastEnd = m.index + m[0].length;
850
+ }
851
+ let tail = escaped.slice(lastEnd);
852
+ if (!replaced) {
853
+ tail = tail.replace(pattern, `<mark class="kt" title="${title}">$&</mark>`);
854
+ }
855
+ escaped = out + tail;
856
+ }
857
+ return escaped;
858
+ }
859
+
860
  clone.querySelectorAll('section.section').forEach(section => {
861
  const segState = stateBySegmentId.get(section.id);
862
  if (!segState) return;
863
+
864
+ // Summary — advance-organizer line right after the section's
865
+ // meta-time. Mirrors guide.html's .section__summary block.
866
+ if (segState.summary && segState.summary.trim()) {
867
+ const anchor = section.querySelector('p.meta-time')
868
+ || section.querySelector('h2.section__heading');
869
+ if (anchor) {
870
+ const p = doc.createElement('p');
871
+ p.className = 'section__summary';
872
+ const strong = doc.createElement('strong');
873
+ strong.textContent = 'In brief: ';
874
+ p.appendChild(strong);
875
+ p.appendChild(doc.createTextNode(segState.summary.trim()));
876
+ anchor.parentNode.insertBefore(p, anchor.nextSibling);
877
+ }
878
+ }
879
+
880
+ // Inline term highlighting in narration paragraphs.
881
  if (segState.key_terms && segState.key_terms.length) {
882
+ section.querySelectorAll('.narration__body p').forEach(p => {
883
+ const original = p.textContent || '';
884
+ const highlighted = highlightTerms(original, segState.key_terms);
885
+ if (highlighted !== escapeHtml(original)) {
886
+ p.innerHTML = highlighted;
887
+ }
888
+ });
889
+ }
890
+
891
+ if (segState.key_terms && segState.key_terms.length) {
892
+ const dl = doc.createElement('dl');
893
  dl.className = 'key-term';
894
  segState.key_terms.forEach(kt => {
895
+ const dt = doc.createElement('dt');
896
  dt.textContent = kt.term;
897
+ const dd = doc.createElement('dd');
898
  dd.textContent = kt.definition;
899
  dl.appendChild(dt);
900
  dl.appendChild(dd);
 
904
  if (segState.math_equations && segState.math_equations.length) {
905
  needsMathJax = true;
906
  segState.math_equations.forEach(eq => {
907
+ const fig = doc.createElement('figure');
908
  fig.className = 'equation';
909
  // Don't set role="math" or aria-label — MathJax's assistiveMml is
910
  // the right accessible representation. aria-label on the wrapper
911
  // would override it. Expose the human-readable description as a
912
  // visually-hidden figcaption so AT users get it without conflict.
913
  if (eq.aria_label && eq.aria_label.trim() && !eq.caption) {
914
+ const cap = doc.createElement('figcaption');
915
  cap.className = 'sr-only';
916
  cap.textContent = eq.aria_label.trim();
917
  fig.appendChild(cap);
918
  }
919
  if (eq.label) {
920
+ const lab = doc.createElement('span');
921
  lab.className = 'equation__label';
922
  lab.textContent = eq.label;
923
  fig.appendChild(lab);
924
  }
925
+ const tex = doc.createTextNode(
926
  eq.inline ? `\\(${eq.latex}\\)` : `\\[${eq.latex}\\]`
927
  );
928
  fig.appendChild(tex);
929
  if (eq.caption) {
930
+ const cap = doc.createElement('figcaption');
931
  cap.textContent = eq.caption;
932
  fig.appendChild(cap);
933
  }
934
  section.appendChild(fig);
935
  });
936
  }
937
+
938
+ // Self-check questions — collapsible retrieval-practice block.
939
+ if (segState.self_check_questions && segState.self_check_questions.length) {
940
+ const details = doc.createElement('details');
941
+ details.className = 'self-check';
942
+ const summary = doc.createElement('summary');
943
+ summary.textContent = 'Check your understanding';
944
+ details.appendChild(summary);
945
+ const ol = doc.createElement('ol');
946
+ ol.className = 'self-check__list';
947
+ segState.self_check_questions.forEach(q => {
948
+ const li = doc.createElement('li');
949
+ li.textContent = q;
950
+ ol.appendChild(li);
951
+ });
952
+ details.appendChild(ol);
953
+ const hint = doc.createElement('p');
954
+ hint.className = 'self-check__hint';
955
+ hint.textContent =
956
+ 'Try to answer each one in your own words before moving on. ' +
957
+ 'Reviewing the section after a failed attempt is more effective ' +
958
+ 'than re-reading without testing yourself.';
959
+ details.appendChild(hint);
960
+ section.appendChild(details);
961
+ }
962
  });
963
 
964
+ // Auto-built glossary at the end of <main>. Same collection
965
+ // rules as the server-side build_glossary: dedupe case-
966
+ // insensitively (first occurrence wins), sort alphabetically,
967
+ // anchor each entry back to the first section where the term
968
+ // appeared.
969
+ const glossaryMain = clone.querySelector('#main-content') || clone.querySelector('main');
970
+ if (glossaryMain) {
971
+ const seen = new Map();
972
+ state.segments.forEach(seg => {
973
+ if (seg.include === false) return;
974
+ (seg.key_terms || []).forEach(kt => {
975
+ const term = (kt.term || '').trim();
976
+ const definition = (kt.definition || '').trim();
977
+ if (!term) return;
978
+ const key = term.toLowerCase();
979
+ if (seen.has(key)) return;
980
+ seen.set(key, { term, definition, firstSection: seg.index });
981
+ });
982
+ });
983
+ const entries = Array.from(seen.values())
984
+ .sort((a, b) => a.term.toLowerCase().localeCompare(b.term.toLowerCase()));
985
+ if (entries.length) {
986
+ const glossSection = doc.createElement('section');
987
+ glossSection.className = 'glossary';
988
+ glossSection.id = 'glossary';
989
+ glossSection.setAttribute('aria-labelledby', 'glossary-heading');
990
+ const h = doc.createElement('h2');
991
+ h.id = 'glossary-heading';
992
+ h.textContent = 'Glossary';
993
+ glossSection.appendChild(h);
994
+ const dl = doc.createElement('dl');
995
+ entries.forEach((entry, i) => {
996
+ const dt = doc.createElement('dt');
997
+ dt.id = `glossary-${i + 1}`;
998
+ dt.textContent = entry.term;
999
+ const dd = doc.createElement('dd');
1000
+ dd.appendChild(doc.createTextNode(entry.definition + ' '));
1001
+ if (entry.firstSection !== undefined && entry.firstSection !== null) {
1002
+ const back = doc.createElement('a');
1003
+ back.className = 'glossary__back';
1004
+ back.href = `#section-${entry.firstSection}`;
1005
+ back.textContent = '↑ section';
1006
+ dd.appendChild(back);
1007
+ }
1008
+ dl.appendChild(dt);
1009
+ dl.appendChild(dd);
1010
+ });
1011
+ glossSection.appendChild(dl);
1012
+ glossaryMain.appendChild(glossSection);
1013
+ }
1014
+ }
1015
+
1016
  // Drop excluded segments.
1017
  clone.querySelectorAll('section.section[data-included="false"]').forEach(s => s.remove());
1018