summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/latex/stex/omtext/omtext.sty.ltxml
blob: 7d781b74e53266d56d163443561e5e4700e67d8e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package LaTeXML::Package::Pool;
use strict;
use LaTeXML::Package;
use Cwd qw(cwd abs_path);
DeclareOption('noindex','');
RequirePackage('sref');
Tag('omdoc:metadata',afterOpen=>\&numberIt,afterClose=>\&locateIt,autoClose=>1,autoOpen=>1);
sub declareFunctions{
  my ($stomach,$whatsit) = @_;
  my $keyval = $whatsit->getArg(1);
  my $funval = KeyVal($keyval,'functions') if KeyVal($keyval,'functions');
  my @funsymbs = ParseKeyValList($funval);
  #Unread the function declarations at the Gullet
  foreach (@funsymbs) {
    $stomach->getGullet->unread(Tokenize('\lxDeclare[role=FUNCTION]{$'.$_.'$}')->unlist);
  }
  return;
}
Tag('omdoc:CMP', afterOpen => sub {AssignValue('_LastSeenCMP', $_[1], 'global');return;});#$
DefParameterType('IfBeginFollows', sub {
   my ($gullet) = @_;
   $gullet->skipSpaces;
                   my $next = $gullet->readToken;
                   $gullet->unread($next);
                   $next = ToString($next);
                   #Hm, falling back to regexp handling, the $gullet->ifNext approach didn't work properly
                   return 1 unless ($next=~/^\\begin/);
                   return;
                 },
 reversion=>'', optional=>1);
DefKeyVal('omtext','functions','Undigested');
DefKeyVal('omtext','display','Semiverbatim');
DefKeyVal('omtext','for','Semiverbatim');
DefKeyVal('omtext','from','Semiverbatim');
DefKeyVal('omtext','type','Semiverbatim');
DefKeyVal('omtext','title','Plain'); #Math mode in titles.
DefKeyVal('omtext','start','Plain'); #Math mode in start phrases
DefKeyVal('omtext','theory','Semiverbatim');
DefKeyVal('omtext','continues','Semiverbatim');
DefKeyVal('omtext','verbalizes','Semiverbatim');
DefEnvironment('{omtext} OptionalKeyVals:omtext',
  "<omdoc:omtext "
     . "?&KeyVal(#1,'id')(xml:id='&KeyVal(#1,'id')')() "
     . "?&KeyVal(#1,'type')(type='&KeyVal(#1,'type')')() "
     . "?&KeyVal(#1,'for')(for='&KeyVal(#1,'for')')() "
             . "?&KeyVal(#1,'from')(from='&KeyVal(#1,'from')')()>"
  . "?&KeyVal(#1,'title')(<dc:title>&KeyVal(#1,'title')</dc:title>)()"
  . "<omdoc:CMP>"
  .     "?&KeyVal(#1,'start')(<ltx:text class='startemph'>&KeyVal(#1,'start')</ltx:text>)()"
                  .     "#body"
                 ."</omdoc:omtext>");
sub DefCMPEnvironment {
  my ($proto, $replacement, %options) = @_;
  my @before = $options{beforeDigest} ? ($options{beforeDigest}) : ();
  push(@before, \&useCMPItemizations);
  $options{beforeDigest} = \@before;
  my @after = $options{afterDigestBegin} ? ($options{afterDigestBegin}) : ();
  push(@after, \&declareFunctions);
  $options{afterDigestBegin} = \@after;
  DefEnvironment($proto, $replacement, %options);
}
sub DefCMPConstructor {
  my ($proto, $replacement, %options) = @_;
  my @before = $options{beforeDigest} ? ($options{beforeDigest}) : ();
  push(@before, \&useCMPItemizations);
  $options{beforeDigest} = \@before;
  DefConstructor($proto, $replacement, %options);
}#$
DefKeyVal('phrase','id','Semiverbatim');
DefKeyVal('phrase','style','Semiverbatim');
DefKeyVal('phrase','class','Semiverbatim');
DefKeyVal('phrase','index','Semiverbatim');
DefKeyVal('phrase','verbalizes','Semiverbatim');
DefKeyVal('phrase','type','Semiverbatim');
DefConstructor('\phrase OptionalKeyVals:phrase {}',
       "<ltx:text %&KeyVals(#1)>#2</ltx:text>");
DefConstructor('\nlex{}',
  "<ltx:text class='nlex'>#1</ltx:text>");
DefConstructor('\nlcex{}',
  "<ltx:text type='nlcex'>#1</ltx:text>");
DefConstructor('\sinlinequote [] {}',
              "<ltx:quote type='inlinequote'>"
               . "?#1(<dc:source>#1</dc:source>\n)()"
               . "#2"
            . "</ltx:quote>");
DefEnvironment('{sblockquote} []',
               "?#1(<omdoc:omtext type='quote'>"
               .     "<dc:source>#1</dc:source>"
               .     "#body"
             . "    </omdoc:omtext>)"
            .    "(<ltx:quote>#body</ltx:quote>)");
DefConstructor('\lec{}',
   "\n<omdoc:note type='line-end-comment'>#1</omdoc:note>");
RawTeX('
\newcommand\mygraphics[2][]{\includegraphics[#1]{#2}}
\newcommand\mycgraphics[2][]{\begin{center}\includegraphics[#1]{#2}\end{center}}
\newcommand\mybgraphics[2][]{\fbox{\includegraphics[#1]{#2}\end{center}}}
');
DefConstructor('\indextoo[]{}',
       "<omdoc:idx>"
      .  "<omdoc:idt>#2</omdoc:idt>"
      .  "<omdoc:ide ?#1(sort-by='#1')()>"
      .    "<omdoc:idp>#2</omdoc:idp>"
      .  "</omdoc:ide>"
      ."</omdoc:idx>");
DefConstructor('\indexalt[]{}{}',
       "<omdoc:idx>"
      .  "<omdoc:idt>#2</omdoc:idt>"
      .  "<omdoc:ide ?#1(sort-by='#1')()>"
      .    "<omdoc:idp>#3</omdoc:idp>"
      .  "</omdoc:ide>"
      ."</omdoc:idx>");
DefConstructor('\twintoo[]{}{}',
       "<omdoc:idx>"
      .  "<omdoc:idt>#2 #3</omdoc:idt>"
      .  "<omdoc:ide ?#1(sort-by='#1')()>"
      .    "<omdoc:idp>#2</omdoc:idp>"
      .    "<omdoc:idp>#3</omdoc:idp>"
      .  "</omdoc:ide>"
      ."</omdoc:idx>");
DefConstructor('\twinalt[]{}{}{}',
       "<omdoc:idx>"
      .  "<omdoc:idt>#2</omdoc:idt>"
      .  "<omdoc:ide ?#1(sort-by='#1')()>"
      .    "<omdoc:idp>#2</omdoc:idp>"
      .    "<omdoc:idp>#3</omdoc:idp>"
      .  "</omdoc:ide>"
      ."</omdoc:idx>");
DefConstructor('\atwintoo[]{}{}{}',
       "<omdoc:idx>"
      .  "<omdoc:idt>#2 #3</omdoc:idt>"
      .  "<omdoc:ide ?#1(sort-by='#1')()>"
      .    "<omdoc:idp>#2</omdoc:idp>"
      .    "<omdoc:idp>#3</omdoc:idp>"
      .    "<omdoc:idp>#4</omdoc:idp>"
      .  "</omdoc:ide>"
      ."</omdoc:idx>");

DefConstructor('\atwinalt[]{}{}{}{}',
       "<omdoc:idx>"
      .  "<omdoc:idt>#2</omdoc:idt>"
      .  "<omdoc:ide ?#1(sort-by='#1')()>"
      .    "<omdoc:idp>#2</omdoc:idp>"
      .    "<omdoc:idp>#3</omdoc:idp>"
      .    "<omdoc:idp>#4</omdoc:idp>"
      .  "</omdoc:ide>"
      ."</omdoc:idx>");
# needed? DefConstructor('\par',sub { $_[0]->maybeCloseElement('ltx:p'); },alias=>"\\par\n");
Tag('omdoc:CMP', autoClose=>1, autoOpen=>1);
Tag('omdoc:omtext', autoClose=>1, autoOpen=>1);
DefConstructor('\footnote[]{}',
       "<omdoc:note type='foot' ?#1(mark='#1')>#2</omdoc:note>");
DefConstructor('\footnotemark[]',"");
DefConstructor('\footnotetext[]{}',
       "<omdoc:note class='foot' ?#1(mark='#1')>#2</omdoc:note>");
Tag('omdoc:omtext',afterOpen=>\&numberIt,afterClose=>\&locateIt);
Tag('omdoc:omgroup',afterOpen=>\&numberIt,afterClose=>\&locateIt);
Tag('omdoc:CMP',afterOpen=>\&numberIt,afterClose=>\&locateIt);
Tag('omdoc:idx',afterOpen=>\&numberIt,afterClose=>\&locateIt);
Tag('omdoc:ide',afterOpen=>\&numberIt,afterClose=>\&locateIt);
Tag('omdoc:idt',afterOpen=>\&numberIt,afterClose=>\&locateIt);
Tag('omdoc:note',afterOpen=>\&numberIt,afterClose=>\&locateIt);
Tag('omdoc:metadata',afterOpen=>\&numberIt,afterClose=>\&locateIt);
Tag('omdoc:meta',afterOpen=>\&numberIt,afterClose=>\&locateIt);
Tag('omdoc:resource',afterOpen=>\&numberIt,afterClose=>\&locateIt);
Tag('ltx:p',afterOpen=>\&numberIt,afterClose=>\&locateIt);
Tag('ltx:tabular',afterOpen=>\&numberIt,afterClose=>\&locateIt);
Tag('ltx:thead',afterOpen=>\&numberIt,afterClose=>\&locateIt);
Tag('ltx:td',afterOpen=>\&numberIt,afterClose=>\&locateIt);
Tag('ltx:tr',afterOpen=>\&numberIt,afterClose=>\&locateIt);
Tag('ltx:caption',afterOpen=>\&numberIt,afterClose=>\&locateIt);
sub numberIt {
  my($document,$node,$whatsit)=@_;
  my(@parents)=$document->findnodes('ancestor::*[@xml:id]',$node);
  my $prefix= (@parents ? $parents[$#parents]->getAttribute('xml:id')."." : '');
  my(@siblings)=$document->findnodes('preceding-sibling::*[@xml:id]',$node);
  my $n = scalar(@siblings)+1;
  my $id = ($node -> getAttribute('xml:id'));
  my $localname = $node->localname;
  $node->setAttribute('xml:id'=>$prefix."$localname$n") unless $id;
  my $about = $node -> getAttribute('about');
  $node->setAttribute('about'=>'#'.$node->getAttribute('xml:id')) unless $about;
  #Also, provide locators:
  my $locator = $whatsit->getProperty('locator');
  #Need to inherit locators if missing:
  $locator = (@parents ? $parents[$#parents]->getAttribute('stex:srcref') : '') unless $locator;
  if ($locator) {
    # There is a BUG with namespace declarations (or am I using the API wrongly??) which
    # does not recognize the stex namespace. Hence, I need to redeclare it...
    my $parent=$document->getNode;
    if(! defined $parent->lookupNamespacePrefix("http://kwarc.info/ns/sTeX"))
      { # namespace not already declared?
        $document->getDocument->documentElement->setNamespace("http://kwarc.info/ns/sTeX","stex",0);
      }
    $node->setAttribute('stex:srcref'=>$locator);
  }return;}

sub locateIt {
  my($document,$node,$whatsit)=@_;
  #Estimate trailer locator:
  my $trailer = $whatsit->getProperty('trailer');
  return unless $trailer; #Nothing we can do if the trailer isn't defined
  $trailer = $trailer->getLocator;
  return unless ($trailer &&  $trailer!~/^\s*$/); #Useless if broken
  my $locator = $node->getAttribute('stex:srcref');
  if ($locator) {
    $locator =~ /^(.+from=\d+;\d+)/;
    my $from = $1;
    $trailer =~ /(,to=\d+;\d+.+)$/;
    my $to = $1;
    $locator = $from.$to;
  } else {
    $locator = $trailer; #This should never happen
  }
  my $parent = $document->getNode;
  if(! defined $parent->lookupNamespacePrefix("http://kwarc.info/ns/sTeX"))
    { # namespace not already declared?
      $document->getDocument->documentElement->setNamespace("http://kwarc.info/ns/sTeX","stex",0);
    }
  $node->setAttribute('stex:srcref' => $locator);
  return;
}
1;