| 1 | # /=====================================================================\ # | ||
| 2 | # | LaTeXML::Common::Model::RelaxNG | # | ||
| 3 | # | Extract Model information from a RelaxNG schema | # | ||
| 4 | # |=====================================================================| # | ||
| 5 | # | Part of LaTeXML: | # | ||
| 6 | # | Public domain software, produced as part of work done by the | # | ||
| 7 | # | United States Government & not subject to copyright in the US. | # | ||
| 8 | # |---------------------------------------------------------------------| # | ||
| 9 | # | Bruce Miller <bruce.miller@nist.gov> #_# | # | ||
| 10 | # | http://dlmf.nist.gov/LaTeXML/ (o o) | # | ||
| 11 | # \=========================================================ooo==U==ooo=/ # | ||
| 12 | package LaTeXML::Common::Model::RelaxNG; | ||
| 13 | use strict; | ||
| 14 | use warnings; | ||
| 15 | use LaTeXML::Util::Pathname; | ||
| 16 | use LaTeXML::Common::XML; | ||
| 17 | use LaTeXML::Global; | ||
| 18 | use LaTeXML::Common::Object; | ||
| 19 | use LaTeXML::Common::Error; | ||
| 20 | use Scalar::Util qw(weaken); | ||
| 21 | |||
| 22 | my $XMLPARSER = LaTeXML::Common::XML::Parser->new(); # [CONSTANT] | ||
| 23 | |||
| 24 | # $schema->documentModules; | ||
| 25 | |||
| 26 | # NOTE: Pending problem; | ||
| 27 | # Once we've got multiple namespaces in the schema, | ||
| 28 | # we haven't provided a means to specify the namespace <=> prefix mapping! | ||
| 29 | # It may be that rnc supplies that info, however! | ||
| 30 | # Alternatively, we could extract a symbol from the url, | ||
| 31 | # but it needs a sanity/collision check! | ||
| 32 | |||
| 33 | # NOTE: When a schema is composed from various modules, | ||
| 34 | # some elements may not be "reachable" and (perhaps) should be removed | ||
| 35 | |||
| 36 | # Scan a RelaxNG schema into an internal summary of modules. | ||
| 37 | sub new { | ||
| 38 | my ($class, $model, $name) = @_; | ||
| 39 | my $self = { name => $name, model => $model, | ||
| 40 | modules => [], elementdefs => {}, defs => {}, elements => {}, | ||
| 41 | internal_grammars => 0 }; | ||
| 42 | weaken($$self{model}); # circular back ref; weaked so can be garbage collected. | ||
| 43 | bless $self, $class; | ||
| 44 | return $self; } | ||
| 45 | |||
| 46 | sub addSchemaDeclaration { | ||
| 47 | my ($self, $document, $tag) = @_; | ||
| 48 | $document->insertPI('latexml', RelaxNGSchema => $$self{name}); | ||
| 49 | return; } | ||
| 50 | |||
| 51 | sub loadSchema { | ||
| 52 | my ($self) = @_; | ||
| 53 | ProgressSpinup("Loading RelaxNG $$self{name}"); | ||
| 54 | # Scan the schema file(s), and extract the info | ||
| 55 | my @schema = $self->scanExternal($$self{name}); | ||
| 56 | if ($LaTeXML::DEBUG{relaxng}) { | ||
| 57 | Debug("========================\nRaw Schema"); | ||
| 58 | map { showSchema($_) } @schema; } | ||
| 59 | @schema = map { $self->simplify($_) } @schema; | ||
| 60 | if ($LaTeXML::DEBUG{relaxng}) { | ||
| 61 | Debug("========================\nSimplified Schema"); | ||
| 62 | map { showSchema($_) } @schema; | ||
| 63 | Debug("========================\nElements"); | ||
| 64 | foreach my $tag (sort keys %{ $$self{elements} }) { | ||
| 65 | showSchema(['element', $tag, @{ $$self{elements}{$tag} }]); } | ||
| 66 | Debug("========================\nModules"); | ||
| 67 | foreach my $mod (@{ $$self{modules} }) { | ||
| 68 | showSchema($mod); } } | ||
| 69 | |||
| 70 | # The resulting @schema should contain the "start" of the grammar. | ||
| 71 | my ($startcontent) = $self->extractContent('#Document', @schema); | ||
| 72 | $$self{model}->addTagContent('#Document', sort keys %$startcontent); | ||
| 73 | if ($LaTeXML::DEBUG{relaxng}) { | ||
| 74 | Debug("========================\nStart\n" . join(', ', sort keys %$startcontent)); } | ||
| 75 | |||
| 76 | # NOTE: Do something automatic about this too!?! | ||
| 77 | # We'll need to generate namespace prefixes for all namespaces found in the doc! | ||
| 78 | $$self{model}->registerDocumentNamespace(undef, "http://dlmf.nist.gov/LaTeXML"); | ||
| 79 | |||
| 80 | # Distill the info into allowed children & attributes for each element. | ||
| 81 | foreach my $tag (sort keys %{ $$self{elements} }) { | ||
| 82 | if ($tag eq '*:*') { | ||
| 83 | # Ignore any internal structure (side effect of restricted names) | ||
| 84 | $$self{model}->addTagContent($tag, '*:*'); | ||
| 85 | next; } | ||
| 86 | my @body = @{ $$self{elements}{$tag} }; | ||
| 87 | my ($content, $attributes) = $self->extractContent($tag, @body); | ||
| 88 | $$self{model}->addTagContent($tag, sort keys %$content); | ||
| 89 | $$self{model}->addTagAttribute($tag, sort keys %$attributes); } | ||
| 90 | # Extract definitions of symbols that define Schema Classes, too | ||
| 91 | foreach my $symbol (sort keys %{ $$self{defs} }) { | ||
| 92 | if ($symbol =~ /^grammar\d+:(.+?)\.class$/) { | ||
| 93 | my $name = $1; | ||
| 94 | my ($content, $attributes) = $self->extractContent($symbol, $$self{defs}{$symbol}); | ||
| 95 | $$self{model}->setSchemaClass($name, $content); } } | ||
| 96 | ProgressSpindown("Loading RelaxNG $$self{name}"); | ||
| 97 | return; } | ||
| 98 | |||
| 99 | # Return two hashrefs for content & attributes | ||
| 100 | sub extractContent { | ||
| 101 | my ($self, $tag, @body) = @_; | ||
| 102 | my (%attr, %child); | ||
| 103 | my @savebody = @body; | ||
| 104 | while (@body) { | ||
| 105 | my $item = shift(@body); | ||
| 106 | if (ref $item eq 'ARRAY') { | ||
| 107 | my ($op, $name, @args) = @$item; | ||
| 108 | if ($op eq 'attribute') { $attr{$name} = 1; } | ||
| 109 | elsif ($op eq 'elementref') { $child{$name} = 1; } | ||
| 110 | elsif ($op eq 'doc') { } | ||
| 111 | elsif ($op eq 'combination') { push(@body, @args); } | ||
| 112 | elsif ($op eq 'grammar') { push(@body, $self->extractStart(@args)); } | ||
| 113 | elsif ($op eq 'module') { push(@body, $self->extractStart(@args)); } | ||
| 114 | elsif (($op eq 'ref') || ($op eq 'parentref')) { | ||
| 115 | |||
| 116 | if (my $el = $$self{elementdefs}{$name}) { | ||
| 117 | push(@body, ['elementref', $el]); } | ||
| 118 | elsif (my $expansion = $$self{defs}{$name}) { | ||
| 119 | push(@body, $expansion); } | ||
| 120 | } | ||
| 121 | elsif ($op eq 'element') { | ||
| 122 | $child{$name} = 1; } # ??? | ||
| 123 | elsif (($op eq 'value') || ($op eq 'data')) { | ||
| 124 | $child{'#PCDATA'} = 1; } | ||
| 125 | else { Warn('unexpected', $name, undef, | ||
| 126 | "Unknown child $op [$name] of element $tag in extractContent\n"); } } | ||
| 127 | elsif ($item eq '#PCDATA') { $child{'#PCDATA'} = 1; } } | ||
| 128 | return ({%child}, {%attr}); } | ||
| 129 | |||
| 130 | #====================================================================== | ||
| 131 | # Internal Representation of a RelaxNG Schema | ||
| 132 | |||
| 133 | # This should build a usable intermediate structure | ||
| 134 | # WITHOUT side effects so that an (eventual) rnc parser | ||
| 135 | # can create the same intermediate! | ||
| 136 | |||
| 137 | # Intermediate structure is a list of | ||
| 138 | # strings (representing the raw leave data) | ||
| 139 | # and recursive items of the following form: | ||
| 140 | # [$op, $name, @forms] | ||
| 141 | # where $op is one of: | ||
| 142 | # ref : defines or references a symbol | ||
| 143 | # parentref : references a symbol in parent's context [converted to ref by simplify] | ||
| 144 | # def,defchoice,definterleave : defines $name to be @forms. | ||
| 145 | # the last 2 combine w/existing values. | ||
| 146 | # elementref : references an element [Added by expand] | ||
| 147 | # grammar : Collects the a grammar's specifications; defined names are scoped (to $name), | ||
| 148 | # and the start is the effective pattern. | ||
| 149 | # (due to <grammar> or <externalRef> [Replaced by it's start by simplify] | ||
| 150 | # override : The @forms consist of a module, and the rest are replacement rules. | ||
| 151 | # (due to <include>) [Reduced to 'module' by simplify] | ||
| 152 | # element : $name is the tag QName, @forms are the content/attribute patterns | ||
| 153 | # attribute : $name is the attribute's QName, @forms are the patterns for the value. | ||
| 154 | # start : the grammar's start pattern ($name is undef) | ||
| 155 | # value : a literal value (typically for attributes) | ||
| 156 | # data : a data type | ||
| 157 | # doc : An annotation ($name is undef), the @forms are documentation strings. | ||
| 158 | # combination: combines various other patterns in @forms, | ||
| 159 | # $name is one of group, interleave, choice, optional, zeroOrMore, oneOrMore, list | ||
| 160 | # module : collects the specifications coming from a separate schema file | ||
| 161 | # for documentation purposes; $name is the name of the file | ||
| 162 | # [Recored in the modules list and replaced by its content in simplify] | ||
| 163 | |||
| 164 | # Tricky is getting the thing scanned and creating blocks | ||
| 165 | # that should be separately documentable. | ||
| 166 | # Each external schema (whether include or external)? | ||
| 167 | |||
| 168 | #====================================================================== | ||
| 169 | # SCAN: Walk through the XML Representation compiling information about | ||
| 170 | # modules, definitions and elements. | ||
| 171 | # | ||
| 172 | # The representation built here has minimal processing done, | ||
| 173 | # so that hopefully it will be feasable to generate the same structure | ||
| 174 | # from a parsed RelaxNG Compact, without duplicating the processing. | ||
| 175 | #====================================================================== | ||
| 176 | my %RNGNSMAP = ( # [CONSTANT] | ||
| 177 | "http://relaxng.org/ns/structure/1.0" => 'rng', | ||
| 178 | "http://relaxng.org/ns/compatibility/annotations/1.0" => 'rnga'); | ||
| 179 | |||
| 180 | local @LaTeXML::Common::Model::RelaxNG::PATHS = (); | ||
| 181 | |||
| 182 | sub scanExternal { | ||
| 183 | my ($self, $name, $inherit_ns) = @_; | ||
| 184 | my $modname = $name; $modname =~ s/\.rn(g|c)$//; | ||
| 185 | my $paths = [@LaTeXML::Common::Model::RelaxNG::PATHS, @{ $STATE->lookupValue('SEARCHPATHS') }]; | ||
| 186 | if (my $schemadoc = LaTeXML::Common::XML::RelaxNG->new($name, searchpaths => $paths)) { | ||
| 187 | my $uri = $schemadoc->URI; | ||
| 188 | ProgressSpinup("Loading RelaxNG schema from $uri"); | ||
| 189 | local @LaTeXML::Common::Model::RelaxNG::PATHS | ||
| 190 | = (pathname_directory($schemadoc->URI), @LaTeXML::Common::Model::RelaxNG::PATHS); | ||
| 191 | my $node = $schemadoc->documentElement; | ||
| 192 | # Fetch any additional namespaces | ||
| 193 | foreach my $ns ($node->getNamespaces) { | ||
| 194 | my ($prefix, $nsuri) = ($ns->getLocalName, $ns->getData); | ||
| 195 | next if $nsuri =~ m|^http://relaxng.org|; # Ignore RelaxNG namespaces(!!) | ||
| 196 | $$self{model}->registerDocumentNamespace($prefix, $nsuri); } | ||
| 197 | my $mod = (['module', $modname, $self->scanPattern($node, $inherit_ns)]); | ||
| 198 | ProgressSpindown("Loading RelaxNG schema from $uri"); | ||
| 199 | return $mod; } | ||
| 200 | else { | ||
| 201 | Warn('expected', $name, undef, "Failed to find RelaxNG schema for name '$name'"); | ||
| 202 | return (); } } | ||
| 203 | |||
| 204 | sub getRelaxOp { | ||
| 205 | my ($node) = @_; | ||
| 206 | return unless $node->nodeType == XML_ELEMENT_NODE; | ||
| 207 | my $ns = $node->namespaceURI; | ||
| 208 | my $prefix = $ns && $RNGNSMAP{$ns}; | ||
| 209 | return ($prefix ? $prefix : "{$ns}") . ":" . $node->localname; } | ||
| 210 | |||
| 211 | sub getElements { | ||
| 212 | my ($node) = @_; | ||
| 213 | return grep { $_->nodeType == XML_ELEMENT_NODE } $node->childNodes; } | ||
| 214 | |||
| 215 | my $COMBINER_re = # [CONSTANT] | ||
| 216 | qr/group|interleave|choice|optional|zeroOrMore|oneOrMore|list/; | ||
| 217 | |||
| 218 | sub scanPattern { | ||
| 219 | my ($self, $node, $inherit_ns) = @_; | ||
| 220 | if (my $relaxop = getRelaxOp($node)) { | ||
| 221 | my $ns = $node->getAttribute('ns') || $inherit_ns; # Possibly bind new namespace | ||
| 222 | # Element description | ||
| 223 | if ($relaxop eq 'rng:element') { | ||
| 224 | return $self->scanPattern_element($ns, $node); } | ||
| 225 | # Attribute description | ||
| 226 | elsif ($relaxop eq 'rng:attribute') { | ||
| 227 | return $self->scanPattern_attribute($ns, $node); } | ||
| 228 | # Various combiners | ||
| 229 | elsif ($relaxop =~ /^rng:($COMBINER_re)$/) { | ||
| 230 | my $op = $1; | ||
| 231 | return (['combination', $op, $self->scanChildren($ns, getElements($node))]); } | ||
| 232 | # Mixed is a combiner but includes #PCDATA | ||
| 233 | elsif ($relaxop eq 'rng:mixed') { | ||
| 234 | return (['combination', 'interleave', '#PCDATA', $self->scanChildren($ns, getElements($node))]); } | ||
| 235 | # Reference to a defined symbol, or parent grammar's defined symbol | ||
| 236 | elsif ($relaxop =~ /^rng:(ref|parentRef)$/) { | ||
| 237 | my $op = lc($1); | ||
| 238 | return ([$op, $node->getAttribute('name')]); } | ||
| 239 | elsif ($relaxop =~ /^rng:(empty|notAllowed)$/) { # Ignorable here | ||
| 240 | return (); } | ||
| 241 | elsif ($relaxop eq 'rng:text') { | ||
| 242 | return ('#PCDATA'); } | ||
| 243 | elsif ($relaxop eq 'rng:value') { # Not interested in details here. | ||
| 244 | return (['value', undef, $node->textContent]); } | ||
| 245 | elsif ($relaxop eq 'rng:data') { # Not interested in details here. | ||
| 246 | return (['data', undef, $node->getAttribute('type')]); } | ||
| 247 | # Include an external grammar | ||
| 248 | elsif ($relaxop eq 'rng:externalRef') { | ||
| 249 | return $self->scanExternal($node->getAttribute('href'), $ns); } | ||
| 250 | # Include an internal grammar | ||
| 251 | elsif ($relaxop eq 'rng:grammar') { | ||
| 252 | return (['grammar', "grammar" . (++$$self{internal_grammars}), | ||
| 253 | $self->scanGrammarContent($ns, getElements($node))]); } | ||
| 254 | elsif ($relaxop =~ /^rnga:documentation$/) { | ||
| 255 | return (['doc', undef, $node->textContent]); } | ||
| 256 | else { | ||
| 257 | Warn('misdefined', $relaxop, undef, "Didn't expect '$relaxop' in RelaxNG Schema (scanPattern)"); | ||
| 258 | return (); } } | ||
| 259 | else { | ||
| 260 | return (); } } | ||
| 261 | |||
| 262 | sub scanPattern_element { | ||
| 263 | my ($self, $ns, $node) = @_; | ||
| 264 | my @children = getElements($node); | ||
| 265 | if (my $name = $node->getAttribute('name')) { | ||
| 266 | return (['element', $$self{model}->encodeQName($ns, $name), | ||
| 267 | $self->scanChildren($ns, @children)]); } | ||
| 268 | else { | ||
| 269 | my $namenode = shift(@children); | ||
| 270 | my @names = $self->scanNameClass($namenode, 0, $ns); | ||
| 271 | return map { ['element', $_, $self->scanChildren($ns, @children)] } @names; } } | ||
| 272 | |||
| 273 | sub scanPattern_attribute { | ||
| 274 | my ($self, $ns, $node) = @_; | ||
| 275 | my $xns = $node->getAttribute('ns'); # explicit declaration! | ||
| 276 | my @children = getElements($node); | ||
| 277 | if (my $name = $node->getAttribute('name')) { | ||
| 278 | return (['attribute', $$self{model}->encodeQName($xns, $name), | ||
| 279 | $self->scanChildren($ns, @children)]); } | ||
| 280 | else { | ||
| 281 | my $namenode = shift(@children); | ||
| 282 | my @names = $self->scanNameClass($namenode, 1, $ns); | ||
| 283 | return (map { ['attribute', $_, $self->scanChildren($ns, @children)] } @names); } } | ||
| 284 | |||
| 285 | sub scanChildren { | ||
| 286 | my ($self, $ns, @children) = @_; | ||
| 287 | return grep { $_ } map { ($self->scanPattern($_, $ns)) } @children; } | ||
| 288 | |||
| 289 | sub scanGrammarContent { | ||
| 290 | my ($self, $ns, @content) = @_; | ||
| 291 | return map { $self->scanGrammarItem($_, $ns) } @content; } | ||
| 292 | |||
| 293 | sub scanGrammarItem { | ||
| 294 | my ($self, $node, $inherit_ns) = @_; | ||
| 295 | if (my $relaxop = getRelaxOp($node)) { | ||
| 296 | my @children = getElements($node); | ||
| 297 | my $ns = $node->getAttribute('ns') || $inherit_ns; # Possibly bind new namespace | ||
| 298 | # The start element's content is returned | ||
| 299 | if ($relaxop eq 'rng:start') { | ||
| 300 | return (['start', undef, $self->scanChildren($ns, @children)]); } | ||
| 301 | elsif ($relaxop eq 'rng:define') { | ||
| 302 | my $name = $node->getAttribute('name'); | ||
| 303 | my $op = $node->getAttribute('combine') || ''; | ||
| 304 | return (['def' . $op, $name, $self->scanChildren($ns, @children)]); } | ||
| 305 | elsif ($relaxop eq 'rng:div') { | ||
| 306 | return $self->scanGrammarContent($ns, @children); } | ||
| 307 | elsif ($relaxop eq 'rng:include') { | ||
| 308 | my $name = $node->getAttribute('href'); | ||
| 309 | my $paths = [@LaTeXML::Common::Model::RelaxNG::PATHS, @{ $STATE->lookupValue('SEARCHPATHS') }]; | ||
| 310 | if (my $schemadoc = LaTeXML::Common::XML::RelaxNG->new($name, searchpaths => $paths)) { | ||
| 311 | local @LaTeXML::Common::Model::RelaxNG::PATHS | ||
| 312 | = (pathname_directory($schemadoc->URI), @LaTeXML::Common::Model::RelaxNG::PATHS); | ||
| 313 | my @patterns; | ||
| 314 | # Hopefully, just a file, not a URL? | ||
| 315 | my $doc = $schemadoc->documentElement; | ||
| 316 | # Ignore the grammar level, if any, since we do NOT establish a binding with include | ||
| 317 | if (getRelaxOp($doc) eq 'rng:grammar') { | ||
| 318 | my $nns = $doc->getAttribute('ns') || $inherit_ns; # Possibly bind new namespace | ||
| 319 | @patterns = $self->scanGrammarContent($nns, getElements($doc)); } | ||
| 320 | else { | ||
| 321 | @patterns = $self->scanPattern($doc, undef); } | ||
| 322 | # The rule is "includeContent", same as grammarContent | ||
| 323 | # except that it shouldn't have nested rng:include; | ||
| 324 | # we'll just assume there aren't any. | ||
| 325 | my $mod = $name; $mod =~ s/\.rn(g|c)$//; | ||
| 326 | if (my @replacements = $self->scanGrammarContent($ns, @children)) { | ||
| 327 | return (['override', undef, ['module', $mod, @patterns], @replacements]); } | ||
| 328 | else { | ||
| 329 | return (['module', $mod, @patterns]); } } | ||
| 330 | else { | ||
| 331 | return (); } } } | ||
| 332 | else { | ||
| 333 | return (); } } | ||
| 334 | |||
| 335 | sub scanNameClass { | ||
| 336 | my ($self, $node, $forattr, $ns) = @_; | ||
| 337 | my $relaxop = getRelaxOp($node); | ||
| 338 | if ($relaxop eq 'rng:name') { | ||
| 339 | my ($decns, $name) = $$self{model}->decodeQName($node->textContent); | ||
| 340 | $ns = $decns if $decns; | ||
| 341 | return ($$self{model}->encodeQName(($forattr ? undef : $ns), $name)); } | ||
| 342 | elsif ($relaxop eq 'rng:anyName') { | ||
| 343 | my @exceptions = (); # Check for exceptions! | ||
| 344 | if (my @children = getElements($node)) { | ||
| 345 | @exceptions = map { $self->scanNameClass($_, $forattr, $ns) } @children; } | ||
| 346 | return filterNames('*', '*:*', @exceptions); } | ||
| 347 | elsif ($relaxop eq 'rng:nsName') { | ||
| 348 | my @exceptions = (); # Check for exceptions! | ||
| 349 | if (my @children = getElements($node)) { | ||
| 350 | @exceptions = map { $self->scanNameClass($_, $forattr, $ns) } @children; } | ||
| 351 | return filterNames($$self{model}->encodeQName($node->getAttribute('ns') // $ns, '*'), @exceptions); } | ||
| 352 | elsif ($relaxop eq 'rng:choice') { | ||
| 353 | my %names = (); | ||
| 354 | foreach my $choice ($node->childNodes) { | ||
| 355 | map { $names{$_} = 1 } $self->scanNameClass($choice, $forattr, $ns); } | ||
| 356 | return (sort keys %names); } | ||
| 357 | elsif ($relaxop eq 'rng:except') { | ||
| 358 | my %names = (); | ||
| 359 | foreach my $choice (getElements($node)) { | ||
| 360 | map { $names{$_} = 1 } $self->scanNameClass($choice, $forattr, $ns); } | ||
| 361 | return map { "!$_" } (sort keys %names); } | ||
| 362 | else { | ||
| 363 | my $op = $node->nodeName; | ||
| 364 | Fatal('misdefined', $op, undef, | ||
| 365 | "Expected a RelaxNG name element (rng:name|rng:anyName|rng:nsName|rng:choice|rng:except), got '$op'"); | ||
| 366 | return; } } | ||
| 367 | |||
| 368 | # collapse redundancies like having both *:* and !*:* | ||
| 369 | sub filterNames { | ||
| 370 | my (@names) = @_; | ||
| 371 | my %include = (); | ||
| 372 | my %exclude = (); | ||
| 373 | foreach my $name (@names) { | ||
| 374 | if ($name =~ /^\!(.*)$/) { $exclude{$name} = $1; } | ||
| 375 | else { $include{$name} = $name; } } | ||
| 376 | foreach my $name (keys %exclude) { | ||
| 377 | if ($include{ $exclude{$name} }) { | ||
| 378 | delete $include{ $exclude{$name} }; delete $exclude{$name}; } } | ||
| 379 | return ((sort keys %include), (sort keys %exclude)); } # Put exclusions last, for documentation | ||
| 380 | |||
| 381 | #====================================================================== | ||
| 382 | # Simplify | ||
| 383 | # Various simplifications: | ||
| 384 | # grammar : the binding of the separate space of defines is applied. | ||
| 385 | # and the result is simplified, and replaced by the start. | ||
| 386 | # module : stored for any documentation purposes, and simplified content returned. | ||
| 387 | # ref, parentref : replaced by a ref of appropriately scoped symbol. | ||
| 388 | # def : store the combined (but unexpanded) definitions | ||
| 389 | |||
| 390 | # and | ||
| 391 | # symbols, elements are recorded. | ||
| 392 | |||
| 393 | sub eqOp { | ||
| 394 | my ($form, $op) = @_; | ||
| 395 | return (ref $form eq 'ARRAY') && ($$form[0] eq $op); } | ||
| 396 | |||
| 397 | sub extractStart { | ||
| 398 | my ($self, @items) = @_; | ||
| 399 | my @starts = (); | ||
| 400 | foreach my $item (@items) { | ||
| 401 | if (ref $item eq 'ARRAY') { | ||
| 402 | my ($op, $name, @args) = @$item; | ||
| 403 | if ($op eq 'start') { push(@starts, @args); } | ||
| 404 | elsif ($op eq 'module') { push(@starts, $self->extractStart(@args)); } | ||
| 405 | elsif ($op eq 'grammar') { push(@starts, $self->extractStart(@args)); } | ||
| 406 | } } | ||
| 407 | return @starts; } | ||
| 408 | |||
| 409 | # NOTE: Reconsider this process. | ||
| 410 | # In particular, how we're returning throwing away stuff (after it gets recorded). | ||
| 411 | # Mainly it's an issue for being able to document a schema, | ||
| 412 | # having separate sections for each "module". | ||
| 413 | |||
| 414 | # What order should we be simplifing and expanding? | ||
| 415 | # For documentable modules we want: | ||
| 416 | # the content, grammars NOT yet replaced by start, | ||
| 417 | # elementdef's sorted out | ||
| 418 | # For model extraction we also want | ||
| 419 | # models flattened, grammars replaced by start, all symbols joined & expanded | ||
| 420 | ##### | ||
| 421 | # In Simplify | ||
| 422 | # grammar: extract & return start | ||
| 423 | # [for doc of a module, shouldn't do this, but for doc of an element, should!] | ||
| 424 | # [Actually, I'm not even sure how to document an embedded grammar] | ||
| 425 | # override: make replacements in module, return module | ||
| 426 | # [this should always happen] | ||
| 427 | # element : store in elements table | ||
| 428 | # [OK] | ||
| 429 | # ref : adjust name | ||
| 430 | # [OK] | ||
| 431 | # parentref : adjust name, convert to ref | ||
| 432 | # [OK] | ||
| 433 | # defchoice, definterleave, def: add to defns table, possibly combining with existing | ||
| 434 | # [OK] | ||
| 435 | # module : store in modules list, return contents | ||
| 436 | # [for doc,we'd like to return nothing? but for getting grammar start we want content?] | ||
| 437 | |||
| 438 | sub simplify { | ||
| 439 | my ($self, $form, $binding, $parentbinding, $container) = @_; | ||
| 440 | if (ref $form eq 'ARRAY') { | ||
| 441 | my ($op, $name, @args) = @$form; | ||
| 442 | if ($op eq 'grammar') { | ||
| 443 | return (['grammar', $name, $self->simplify_args($name, $binding, $container, @args)]); } | ||
| 444 | elsif ($op eq 'override') { | ||
| 445 | return $self->simplify_override($binding, $parentbinding, $container, @args); } | ||
| 446 | elsif ($op eq 'module') { | ||
| 447 | my $module = ['module', $name]; | ||
| 448 | push(@{ $$self{modules} }, $module); # Keep in order: push first, then scan contents | ||
| 449 | push(@$module, $self->simplify_args($binding, $parentbinding, $container, @args)); | ||
| 450 | return ($module); } | ||
| 451 | elsif ($op eq 'element') { | ||
| 452 | @args = $self->simplify_args($binding, $parentbinding, "element:$name", @args); | ||
| 453 | push(@{ $$self{elements}{$name} }, @args); | ||
| 454 | return (['element', $name, @args]); } | ||
| 455 | elsif ($op =~ /^(ref|parentref)$/) { | ||
| 456 | my $qname = ($1 eq 'parentref' ? $parentbinding : $binding) . ":" . $name; | ||
| 457 | @args = $self->simplify_args($binding, $parentbinding, $container, @args); | ||
| 458 | $$self{usesname}{$qname}{$container} = 1 if $container; | ||
| 459 | return (['ref', $qname]); } | ||
| 460 | elsif ($op =~ /^def(choice|interleave|)$/) { | ||
| 461 | my $combination = $1 || 'group'; | ||
| 462 | my $qname = $binding . ":" . $name; | ||
| 463 | $$self{usesname}{$qname}{$container} = 1 if $container; | ||
| 464 | @args = $self->simplify_args($binding, $parentbinding, "pattern:$qname", @args); | ||
| 465 | |||
| 466 | # Special case: simple definition of an element | ||
| 467 | if (($combination eq 'group') && (scalar(@args) == 1) && eqOp($args[0], 'element')) { | ||
| 468 | $$self{elementdefs}{$qname} = $args[0][1]; | ||
| 469 | $$self{elementreversedefs}{ $args[0][1] } = $qname; | ||
| 470 | return @args; } | ||
| 471 | else { | ||
| 472 | my $prev = $$self{defs}{$qname}; | ||
| 473 | my $prevc = $$self{def_combiner}{$qname}; | ||
| 474 | my @xargs = grep { !eqOp($_, 'doc') } @args; # Remove annotations | ||
| 475 | my @prevargs = (); | ||
| 476 | if ($prev) { # Previoud definition? | ||
| 477 | if ((ref $prev) && ($$prev[0] eq 'combination')) { | ||
| 478 | @prevargs = @$prev[2 .. $#{$prev}]; } | ||
| 479 | else { | ||
| 480 | @prevargs = ($prev); } | ||
| 481 | if (($combination eq 'group') && ($prevc eq 'group')) { # apparently RE-defining $qname? | ||
| 482 | $prev = undef; } | ||
| 483 | elsif (($combination eq 'group') && ($prevc ne 'group')) { # Use old combination!?!?!?!? | ||
| 484 | $combination = $prevc; } } | ||
| 485 | $$self{defs}{$qname} = simplifyCombination(['combination', $combination, @prevargs, @xargs]); | ||
| 486 | $$self{def_combiner}{$qname} = $combination; | ||
| 487 | return ([$op, $qname, @args]); } } | ||
| 488 | else { | ||
| 489 | return ([$op, $name, $self->simplify_args($binding, $parentbinding, $container, @args)]); } } | ||
| 490 | else { | ||
| 491 | return ($form); } } | ||
| 492 | |||
| 493 | sub simplify_args { | ||
| 494 | my ($self, $binding, $parentbinding, $container, @forms) = @_; | ||
| 495 | return map { $self->simplify($_, $binding, $parentbinding, $container) } @forms; } | ||
| 496 | |||
| 497 | sub simplify_override { | ||
| 498 | my ($self, $binding, $parentbinding, $container, @args) = @_; | ||
| 499 | # Note that we do NOT simplify till we've made the replacements! | ||
| 500 | my ($module, @replacements) = @args; | ||
| 501 | my ($modop, $modname, @patterns) = @$module; | ||
| 502 | # Replace any start from @patterns by that from @replacement, if any. | ||
| 503 | if (my @replacement_start = grep { eqOp($_, 'start') } @replacements) { | ||
| 504 | @patterns = grep { !eqOp($_, 'start') } @patterns; } | ||
| 505 | foreach my $def (grep { (ref $_ eq 'ARRAY') && ($$_[0] =~ /^def/) } @replacements) { | ||
| 506 | my ($defop, $symbol) = @$def; | ||
| 507 | @patterns = grep { !(eqOp($_, $defop) && ($$_[1] eq $symbol)) } @patterns; } | ||
| 508 | # Recurse on the overridden module | ||
| 509 | return $self->simplify(['module', "$modname (overridden)", @patterns, @replacements], | ||
| 510 | $binding, $parentbinding, $container); } | ||
| 511 | |||
| 512 | sub simplifyCombination { | ||
| 513 | my ($combination) = @_; | ||
| 514 | if ((ref $combination) && ($$combination[0] eq 'combination')) { | ||
| 515 | my ($c, $op, @stuff) = @$combination; | ||
| 516 | @stuff = map { simplifyCombination($_) } @stuff; | ||
| 517 | if ($op =~ /^(group|choice)$/) { # These can be flattened. | ||
| 518 | @stuff = map { ((ref $_) && ($$_[0] eq 'combination') && ($$_[1] eq $op) | ||
| 519 | ? @$_[2 .. $#$_] : ($_)) } | ||
| 520 | @stuff; } | ||
| 521 | return (($op eq 'group') && (scalar(@stuff) == 1) ? $stuff[0] : [$c, $op, @stuff]); } | ||
| 522 | else { | ||
| 523 | return $combination; } } | ||
| 524 | |||
| 525 | #====================================================================== | ||
| 526 | # For debugging... | ||
| 527 | sub showSchema { | ||
| 528 | my ($item, $level) = @_; | ||
| 529 | $level = 0 unless defined $level; | ||
| 530 | if (ref $item eq 'ARRAY') { | ||
| 531 | my ($op, $name, @args) = @$item; | ||
| 532 | if ($op eq 'doc') { $name = "..."; @args = (); } | ||
| 533 | Debug("" . (' ' x (2 * $level)) . $op . ($name ? " " . $name : '')); | ||
| 534 | |||
| 535 | foreach my $arg (@args) { | ||
| 536 | showSchema($arg, $level + 1); } } | ||
| 537 | else { | ||
| 538 | Debug("" . (' ' x (2 * $level)) . $item); } | ||
| 539 | return; } | ||
| 540 | |||
| 541 | #====================================================================== | ||
| 542 | # Generate TeX documentation for a Schema | ||
| 543 | #====================================================================== | ||
| 544 | # The svg schema can only just barely be read in and recognized, | ||
| 545 | # but it is structured in a way that makes a joke of our attempt at automatic documentation | ||
| 546 | my $SKIP_SVG = 1; # [CONFIGURABLE?] | ||
| 547 | my $SKIP_ARIA = 1; | ||
| 548 | my $SKIP_XHTML = 1; | ||
| 549 | |||
| 550 | sub documentModules { | ||
| 551 | my ($self) = @_; | ||
| 552 | my $docs = ""; | ||
| 553 | $$self{defined_patterns} = {}; | ||
| 554 | foreach my $module (@{ $$self{modules} }) { | ||
| 555 | my ($op, $name, @content) = @$module; | ||
| 556 | next if $SKIP_SVG && $name =~ /:svg:/; # !!!! | ||
| 557 | $name =~ s/^urn:x-LaTeXML:RelaxNG://; # Remove the urn part. | ||
| 558 | $docs = join("\n", $docs, | ||
| 559 | "\\begin{schemamodule}{$name}", | ||
| 560 | (map { $self->toTeX($_) } @content), | ||
| 561 | "\\end{schemamodule}"); } | ||
| 562 | foreach my $name (sort keys %{ $$self{defined_patterns} }) { | ||
| 563 | if ($$self{defined_patterns}{$name} < 0) { | ||
| 564 | $docs =~ s/\\patternadd\{$name\}/\\patterndefadd{$name}/s; } } | ||
| 565 | return $docs; } | ||
| 566 | |||
| 567 | sub cleanTeX { | ||
| 568 | my ($string) = @_; | ||
| 569 | return '\typename{text}' if $string eq '#PCDATA'; | ||
| 570 | $string =~ s/^urn:x-LaTeXML:RelaxNG://; # Remove the urn part, if any | ||
| 571 | $string =~ s/\#/\\#/g; | ||
| 572 | $string =~ s/<([^>]*)>/\\texttt{$1}/g; # An apparent convention <sometext> == ttfont? | ||
| 573 | $string =~ s/_/\\_/g; | ||
| 574 | return $string; } | ||
| 575 | |||
| 576 | sub cleanTeXName { | ||
| 577 | my ($string) = @_; | ||
| 578 | $string = cleanTeX($string); | ||
| 579 | $string =~ s/^ltx://; | ||
| 580 | # $string =~ s/:/../; | ||
| 581 | return $string; } | ||
| 582 | |||
| 583 | sub toTeX { | ||
| 584 | my ($self, $object) = @_; | ||
| 585 | if (ref $object eq 'HASH') { | ||
| 586 | return join(', ', map { "$_=" . $self->toTeX($$object{$_}) } sort keys %$object); } | ||
| 587 | elsif (ref $object eq 'ARRAY') { # an object? | ||
| 588 | my ($op, $name, @data) = @$object; | ||
| 589 | if ($op eq 'doc') { | ||
| 590 | return join(' ', map { cleanTeX($_) } @data) . "\n"; } | ||
| 591 | elsif ($op eq 'ref') { | ||
| 592 | return $self->toTeX_ref($op, $name); } | ||
| 593 | elsif ($op =~ /^def(choice|interleave|)$/) { | ||
| 594 | return $self->toTeX_def($1, $name, @data); } | ||
| 595 | elsif ($op eq 'element') { | ||
| 596 | return $self->toTeX_element($name, @data); } | ||
| 597 | elsif ($op eq 'attribute') { | ||
| 598 | return $self->toTeX_attribute($name, @data); } | ||
| 599 | elsif ($op eq 'combination') { | ||
| 600 | return $self->toTeX_combination($name, @data); } | ||
| 601 | elsif ($op eq 'data') { | ||
| 602 | return "\\typename{" . cleanTeX($data[0]) . "}"; } | ||
| 603 | elsif ($op eq 'value') { | ||
| 604 | return '\attrval{' . cleanTeX($data[0]) . "}"; } | ||
| 605 | elsif ($op eq 'start') { | ||
| 606 | my ($docs, @spec) = $self->toTeXExtractDocs(@data); | ||
| 607 | my $content = join(' ', map { $self->toTeX($_) } @spec); | ||
| 608 | return "\\item[\\textit{Start}]\\textbf{==}\\ $content" . ($docs ? " \\par$docs" : ''); } | ||
| 609 | elsif ($op eq 'grammar') { # Don't otherwise mention it? | ||
| 610 | my (@mods, @rest); # Collapse any iniitial module inclusions for brevity | ||
| 611 | foreach my $datum (@data) { | ||
| 612 | if ((ref $datum eq 'ARRAY') && ($$datum[0] eq 'module')) { | ||
| 613 | push(@mods, $$datum[1]); } | ||
| 614 | else { push(@rest, $datum); } } | ||
| 615 | return join("\n", | ||
| 616 | (@mods | ||
| 617 | ? '\item[\textit{Included}]' . join(', ', map { '\moduleref{' . cleanTeX($_) . '}'; } @mods) | ||
| 618 | : ()), | ||
| 619 | map { $self->toTeX($_) } @rest); } | ||
| 620 | elsif ($op eq 'module') { | ||
| 621 | if (($name =~ /:svg:/) && $SKIP_SVG) { | ||
| 622 | return '\item[\textit{Module }' . cleanTeX($name) . '] included.'; } | ||
| 623 | else { | ||
| 624 | return '\item[\textit{Module }\moduleref{' . cleanTeX($name) . '}] included.'; } } | ||
| 625 | else { | ||
| 626 | Warn('unexpected', $op, undef, "RelaxNG->toTeX: Unrecognized item $op"); | ||
| 627 | return "[$op: " . join(', ', map { $self->toTeX($_) } @data) . "]"; } } | ||
| 628 | else { | ||
| 629 | return cleanTeX($object); } } | ||
| 630 | |||
| 631 | sub toTeX_ref { | ||
| 632 | my ($self, $op, $name) = @_; | ||
| 633 | if (my $el = $$self{elementdefs}{$name}) { | ||
| 634 | $el = cleanTeXName($el); | ||
| 635 | return ($SKIP_XHTML && ($el eq 'xhtml:*') ? '\texttt{xhtml:*}' : "\\elementref{$el}"); } | ||
| 636 | elsif ($name =~ /_(?:attributes|model)$/) { | ||
| 637 | return $self->toTeX($$self{defs}{$name}); } | ||
| 638 | else { | ||
| 639 | $name =~ s/^\w+://; # Strip off qualifier!!!! (watch for clash in docs?) | ||
| 640 | return ($SKIP_SVG && ($name eq 'svg') ? '\texttt{svg:svg}' | ||
| 641 | : "\\patternref{" . cleanTeX($name) . "}"); } } | ||
| 642 | |||
| 643 | sub toTeX_def { | ||
| 644 | my ($self, $combiner, $name, @data) = @_; | ||
| 645 | my $qname = $name; | ||
| 646 | return "" if ($name =~ /\baria\b/) && $SKIP_ARIA; | ||
| 647 | return "" if $name =~ /_(?:attributes|model)$/; | ||
| 648 | $name =~ s/^\w+://; # Strip off qualifier!!!! (watch for clash in docs?) | ||
| 649 | return "" if $SKIP_SVG && ($name =~ /^svg/); | ||
| 650 | $name = cleanTeX($name); | ||
| 651 | my ($docs, @spec) = $self->toTeXExtractDocs(@data); | ||
| 652 | my ($attr, $content) = $self->toTeXBody(@spec); | ||
| 653 | |||
| 654 | if ($combiner) { | ||
| 655 | my $body = $attr; | ||
| 656 | $body .= '\item[' . ($combiner eq 'choice' ? '\textbar=' : '\&=') . '] ' . $content if $content; | ||
| 657 | $$self{defined_patterns}{$name} = -1 unless defined $$self{defined_patterns}{$name}; | ||
| 658 | return "\\patternadd{$name}{$docs}{$body}\n"; } | ||
| 659 | else { | ||
| 660 | $attr = '\item[\textit{Attributes:}] \textit{empty}' if !$attr && ($name =~ /\\_attributes/); | ||
| 661 | $content = '\textit{empty}' if !$content && ($name =~ /\\_model/); | ||
| 662 | my $body = $attr; | ||
| 663 | $body .= '\item[\textit{Content}:] ' . $content if $content; | ||
| 664 | if (!$attr && $self->toTeX_isContent($$self{defs}{$qname})) { | ||
| 665 | my ($xattr, $xcontent) = $self->toTeXBody($$self{defs}{$qname}); | ||
| 666 | $body .= '\item[\textit{Expansion}:] ' . $xcontent | ||
| 667 | if !$xattr && $xcontent && ($xcontent ne $content); } | ||
| 668 | if (my $uses = $self->getSymbolUses($qname)) { | ||
| 669 | $body .= '\item[\textit{Used by}:] ' . $uses; } | ||
| 670 | if ((defined $$self{defined_patterns}{$name}) && ($$self{defined_patterns}{$name} > 0)) { # Already been defined??? | ||
| 671 | return ''; } | ||
| 672 | else { | ||
| 673 | $$self{defined_patterns}{$name} = 1; | ||
| 674 | return "\\patterndef{$name}{$docs}{$body}\n"; } } } | ||
| 675 | |||
| 676 | sub toTeX_element { | ||
| 677 | my ($self, $name, @data) = @_; | ||
| 678 | my $qname = $name; | ||
| 679 | $name =~ s/^ltx://; | ||
| 680 | return "" if $SKIP_XHTML && ($name eq 'xhtml:*'); | ||
| 681 | $name = cleanTeXName($name); | ||
| 682 | my ($docs, @spec) = $self->toTeXExtractDocs(@data); | ||
| 683 | my ($attr, $content) = $self->toTeXBody(@spec); | ||
| 684 | $content = "\\typename{empty}" unless $content; | ||
| 685 | # Shorten display for element-specific attributes & model, ASSUMING they immediately folllow! | ||
| 686 | my $body = $attr; | ||
| 687 | $body .= '\item[\textit{Content}:] ' . $content if $content; | ||
| 688 | if (my $ename = $$self{elementreversedefs}{$qname}) { | ||
| 689 | if (my $uses = $self->getSymbolUses($ename)) { | ||
| 690 | $body .= '\item[\textit{Used by}:] ' . $uses; } } | ||
| 691 | return "\\elementdef{$name}{$docs}{$body}\n"; } | ||
| 692 | |||
| 693 | sub toTeX_attribute { | ||
| 694 | my ($self, $name, @data) = @_; | ||
| 695 | $name = cleanTeXName($name); | ||
| 696 | my ($docs, @spec) = $self->toTeXExtractDocs(@data); | ||
| 697 | my $content = join(' ', map { $self->toTeX($_) } @spec) || '\typename{text}'; | ||
| 698 | if ($name =~ /^!(.*)/) { # Excluded attribute? | ||
| 699 | return "\\item[\\textit{Exluding attribute }]\\texttt{$1}"; } | ||
| 700 | return "\\attrdef{$name}{$docs}{$content}"; } | ||
| 701 | |||
| 702 | sub toTeX_combination { | ||
| 703 | my ($self, $name, @data) = @_; | ||
| 704 | if ($name eq 'group') { | ||
| 705 | return (scalar(@data) == 1 ? $self->toTeX($data[0]) | ||
| 706 | : "(" . join(', ', map { $self->toTeX($_) } @data) . ")"); } | ||
| 707 | elsif ($name eq 'interleave') { | ||
| 708 | return "(" . join(' ~\&~ ', map { $self->toTeX($_) } @data) . ")"; } # ? | ||
| 709 | elsif ($name eq 'choice') { | ||
| 710 | return "(" . join(' ~\textbar~ ', map { $self->toTeX($_) } @data) . ")"; } | ||
| 711 | elsif ($name eq 'optional') { | ||
| 712 | if ((@data == 1) && eqOp($data[0], 'attribute')) { | ||
| 713 | return $self->toTeX($data[0]); } | ||
| 714 | else { | ||
| 715 | return $self->toTeX($data[0]) . '\textsuperscript{?}'; } } | ||
| 716 | elsif ($name eq 'zeroOrMore') { | ||
| 717 | return $self->toTeX($data[0]) . '\textsuperscript{*}'; } | ||
| 718 | elsif ($name eq 'oneOrMore') { | ||
| 719 | return $self->toTeX($data[0]) . '\textsuperscript{*}'; } | ||
| 720 | elsif ($name eq 'list') { | ||
| 721 | return "(" . join(', ', map { $self->toTeX($_) } @data) . ")"; } # ? | ||
| 722 | else { | ||
| 723 | Warn('unexpected', $name, undef, "RelaxNG->toTeX: Unrecognized combination $name"); | ||
| 724 | return; } } | ||
| 725 | |||
| 726 | sub getSymbolUses { | ||
| 727 | my ($self, $qname) = @_; | ||
| 728 | if (my $uses = $$self{usesname}{$qname}) { | ||
| 729 | my @uses = sort keys %$uses; | ||
| 730 | @uses = grep { !/\bSVG./ } @uses if $SKIP_SVG; # !!! | ||
| 731 | @uses = map { (/pattern:[^:]*:(.*?)_(?:attributes|model)$/ ? "element:$1" : $_); } @uses; | ||
| 732 | return join(', ', | ||
| 733 | (map { /^pattern:[^:]*:(.*)$/ ? ('\patternref{' . cleanTeX($1) . '}') : () } @uses), | ||
| 734 | (map { /^element:(.*)$/ ? ('\elementref{' . cleanTeXName($1) . '}') : () } @uses)); } | ||
| 735 | else { | ||
| 736 | return ''; } } | ||
| 737 | |||
| 738 | # Extract any documentation nodes from @data | ||
| 739 | sub toTeXExtractDocs { | ||
| 740 | my ($self, @data) = @_; | ||
| 741 | my $docs = ""; | ||
| 742 | my @rest = (); | ||
| 743 | while (my $item = shift(@data)) { | ||
| 744 | if ((ref $item eq 'ARRAY') && ($$item[0] eq 'doc')) { | ||
| 745 | $docs .= $self->toTeX($item); } | ||
| 746 | else { | ||
| 747 | push(@rest, $item); } } | ||
| 748 | return ($docs, @rest); } | ||
| 749 | |||
| 750 | # Partition & format the attributes & content model of a named pattern or element. | ||
| 751 | # This generates a sequence of \item's to be put in a definition list. | ||
| 752 | sub toTeXBody { | ||
| 753 | my ($self, @data) = @_; | ||
| 754 | my (@attributes, @content, @attrpatterns); | ||
| 755 | while (my $item = shift(@data)) { | ||
| 756 | if (ref $item eq 'ARRAY') { | ||
| 757 | my ($op, $name, @args) = @$item; | ||
| 758 | if ($op eq 'attribute') { | ||
| 759 | push(@attributes, $self->toTeX($item)); } | ||
| 760 | elsif (($op eq 'combination') && $self->toTeX_isAttributes($item)) { | ||
| 761 | unshift(@data, @args); } | ||
| 762 | # Directly include <element>_attributes or _model values | ||
| 763 | elsif (($op eq 'ref') && ($name =~ /_(?:attributes|model)$/)) { | ||
| 764 | unshift(@data, $$self{defs}{$name}); } | ||
| 765 | elsif (($op eq 'ref') && ($name =~ /[^a-zA-Z]attributes$/)) { | ||
| 766 | push(@attrpatterns, $self->toTeX($item)); } | ||
| 767 | else { | ||
| 768 | push(@content, $self->toTeX($item)); } } | ||
| 769 | else { | ||
| 770 | push(@content, $self->toTeX($item)); } } | ||
| 771 | return | ||
| 772 | (join('', (@attrpatterns ? '\item[\textit{Attributes}:] ' . join(', ', @attrpatterns) : ''), | ||
| 773 | @attributes), | ||
| 774 | join(', ', @content)); } | ||
| 775 | |||
| 776 | sub toTeX_isAttributes { | ||
| 777 | my ($self, $item) = @_; | ||
| 778 | if (ref $item eq 'ARRAY') { | ||
| 779 | my ($op, $name, @args) = @$item; | ||
| 780 | if ($op eq 'attribute') { return 1; } | ||
| 781 | elsif ($op eq 'ref') { | ||
| 782 | return $self->toTeX_isAttributes($$self{defs}{$name}); } | ||
| 783 | elsif (($op eq 'combination') && ($name =~ /^(?:optional|choice|group|zeroOrMore|oneOrMore)$/)) { | ||
| 784 | return !grep { !$self->toTeX_isAttributes($_); } @args; } } | ||
| 785 | return 0; } | ||
| 786 | |||
| 787 | sub toTeX_isContent { | ||
| 788 | my ($self, $item) = @_; | ||
| 789 | if (ref $item eq 'ARRAY') { | ||
| 790 | my ($op, $name, @args) = @$item; | ||
| 791 | if ($op eq 'element') { return 1; } | ||
| 792 | elsif ($op eq 'grammar') { return 1; } | ||
| 793 | # elsif (($op eq 'ref') && ($name =~ /[^a-zA-Z](?:model|class)$/)) { return 1; } | ||
| 794 | elsif ($op eq 'ref') { | ||
| 795 | return $$self{elementdefs}{$name} || $self->toTeX_isContent($$self{defs}{$name}); } | ||
| 796 | elsif (($op eq 'combination') && ($name =~ /^(?:optional|choice|group|zeroOrMore|oneOrMore)$/)) { | ||
| 797 | return !grep { !$self->toTeX_isContent($_); } @args; } } | ||
| 798 | return ($item && ($item eq '#PCDATA')); } | ||
| 799 | |||
| 800 | #====================================================================== | ||
| 801 | 1; | ||
| 802 | |||
| 803 | __END__ |