Skip to content

Commit b7d8f52

Browse files
committed
Allow for (nested) annotations elements
This allows us to set 'interactive' annotations on books, sets, and chapters to signal to the WASM code runner that examples in these areas can be run. The WASM code runner also checks for the 'non-interactive' annotation and selectively disables running code for that example.
1 parent b1e5ac3 commit b7d8f52

File tree

3 files changed

+65
-2
lines changed

3 files changed

+65
-2
lines changed

phpdotnet/phd/Format/Abstract/XHTML.php

+27-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ abstract class Format_Abstract_XHTML extends Format {
66
/** @var array<?string> Last In First Out stack of roles */
77
private array $role = [];
88

9+
/** @var array<?string> Last In First Out stack of annotations */
10+
private array $annotations = [];
11+
912
/* XHTMLPhDFormat */
1013
protected $openPara = 0;
1114
protected $escapedPara = array();
@@ -44,13 +47,19 @@ public function UNDEF($open, $name, $attrs, $props) {
4447
}
4548

4649
public function CDATA($value) {
50+
$annotations = $this->getAnnotations();
51+
$annotationsStr = '';
52+
if (count($annotations) > 0) {
53+
$annotationsStr = 'annotation-' . join(' annotation-', $annotations) . ' ';
54+
}
55+
4756
switch($this->getRole()) {
4857
case '':
49-
return '<div class="cdata"><pre>'
58+
return '<div class="' . $annotationsStr . 'cdata"><pre>'
5059
. htmlspecialchars($value, ENT_QUOTES, "UTF-8")
5160
. '</pre></div>';
5261
default:
53-
return '<div class="' . $this->getRole() . 'code">'
62+
return '<div class="' . $annotationsStr . $this->getRole() . 'code' . '">'
5463
. $this->highlight(trim($value), $this->getRole(), 'xhtml')
5564
. '</div>';
5665
}
@@ -139,4 +148,20 @@ protected function getRole(): ?string {
139148
protected function popRole(): ?string {
140149
return array_pop($this->role);
141150
}
151+
152+
protected function pushAnnotations(?string $annotations): void {
153+
$this->annotations[] = ($annotations != null ? explode(' ', $annotations) : []);
154+
}
155+
156+
protected function getAnnotations() : ?array {
157+
$top = end($this->annotations);
158+
if ($top === false) {
159+
$top = [];
160+
}
161+
return $top;
162+
}
163+
164+
protected function popAnnotations() : ?array {
165+
return array_pop($this->annotations);
166+
}
142167
}

phpdotnet/phd/Package/Generic/XHTML.php

+17
Original file line numberDiff line numberDiff line change
@@ -820,6 +820,8 @@ public function format_name($open, $name, $attrs) {
820820
}
821821

822822
public function format_container_chunk_top($open, $name, $attrs, $props) {
823+
$hasAnnotations = array_key_exists('annotations', $attrs[Reader::XMLNS_DOCBOOK]);
824+
823825
$this->cchunk = $this->dchunk;
824826
$this->cchunk["name"] = $name;
825827
if(isset($attrs[Reader::XMLNS_XML]["id"])) {
@@ -829,12 +831,20 @@ public function format_container_chunk_top($open, $name, $attrs, $props) {
829831
}
830832

831833
if ($open) {
834+
if ($hasAnnotations) {
835+
$this->pushAnnotations($attrs[Reader::XMLNS_DOCBOOK]["annotations"]);
836+
}
837+
832838
$this->CURRENT_CHUNK = $id;
833839
$this->notify(Render::CHUNK, Render::OPEN);
834840

835841
return '<div id="' .$id. '" class="' .$name. '">';
836842
}
837843

844+
if ($hasAnnotations) {
845+
$this->popAnnotations();
846+
}
847+
838848
$this->CURRENT_CHUNK = $id;
839849
$this->notify(Render::CHUNK, Render::CLOSE);
840850
$toc = "";
@@ -1714,10 +1724,17 @@ public function format_example_content($open, $name, $attrs) {
17141724
return "</p></div>";
17151725
}
17161726
public function format_programlisting($open, $name, $attrs) {
1727+
$hasAnnotations = array_key_exists('annotations', $attrs[Reader::XMLNS_DOCBOOK]);
17171728
if ($open) {
17181729
$this->pushRole($attrs[Reader::XMLNS_DOCBOOK]["role"] ?? null);
1730+
if ($hasAnnotations) {
1731+
$this->pushAnnotations($attrs[Reader::XMLNS_DOCBOOK]["annotations"]);
1732+
}
17191733
return '<div class="example-contents">';
17201734
}
1735+
if ($hasAnnotations) {
1736+
$this->popAnnotations();
1737+
}
17211738
$this->popRole();
17221739
return "</div>\n";
17231740
}

phpdotnet/phd/Package/PHP/XHTML.php

+21
Original file line numberDiff line numberDiff line change
@@ -914,13 +914,19 @@ private function isChunkedByAttributes(array $attributes): bool {
914914
}
915915

916916
public function format_container_chunk($open, $name, $attrs, $props) {
917+
$hasAnnotations = array_key_exists('annotations', $attrs[Reader::XMLNS_DOCBOOK]);
918+
917919
$this->CURRENT_CHUNK = $this->CURRENT_ID = $id = $attrs[Reader::XMLNS_XML]["id"] ?? '';
918920

919921
if ($this->isChunkedByAttributes($attrs)) {
920922
$this->cchunk = $this->dchunk;
921923
}
922924

923925
if ($open) {
926+
if ($hasAnnotations) {
927+
$this->pushAnnotations($attrs[Reader::XMLNS_DOCBOOK]["annotations"]);
928+
}
929+
924930
$this->notify(Render::CHUNK, Render::OPEN);
925931
if ($name != "reference") {
926932
$chunks = Format::getChildren($id);
@@ -937,6 +943,10 @@ public function format_container_chunk($open, $name, $attrs, $props) {
937943
}
938944
return '<div id="'.$id.'" class="'.$name.'">';
939945
}
946+
if ($hasAnnotations) {
947+
$this->popAnnotations();
948+
}
949+
940950
$this->notify(Render::CHUNK, Render::CLOSE);
941951

942952
$content = "";
@@ -957,11 +967,22 @@ public function format_container_chunk($open, $name, $attrs, $props) {
957967
}
958968

959969
public function format_root_chunk($open, $name, $attrs) {
970+
$hasAnnotations = array_key_exists('annotations', $attrs[Reader::XMLNS_DOCBOOK]);
971+
960972
$this->CURRENT_CHUNK = $this->CURRENT_ID = $id = $attrs[Reader::XMLNS_XML]["id"] ?? '';
961973
if ($open) {
974+
if ($hasAnnotations) {
975+
$this->pushAnnotations($attrs[Reader::XMLNS_DOCBOOK]["annotations"]);
976+
}
977+
962978
$this->notify(Render::CHUNK, Render::OPEN);
963979
return '<div id="'.$id.'" class="'.$name.'">';
964980
}
981+
982+
if ($hasAnnotations) {
983+
$this->popAnnotations();
984+
}
985+
965986
$this->notify(Render::CHUNK, Render::CLOSE);
966987
$chunks = Format::getChildren($id);
967988
$content = '<ul class="chunklist chunklist_'.$name.'">';

0 commit comments

Comments
 (0)