summaryrefslogtreecommitdiff
path: root/support/TeX4ht/source/tex4ht-xhtmml-xtpipes.tex
blob: 37461cd573c5f4e723bd4ee062edb049ecb1b2d9 (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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
% $Id: tex4ht-xhtmml-xtpipes.tex 740 2020-06-13 22:35:32Z karl $
% htlatex tex4ht-xhtmml-xtpipes "xhtml,next,3" "" "-d./"

% Copyright 2009-2020 TeX Users Group
% Copyright 1996-2009 Eitan M. Gurari
% Released under LPPL 1.3c+.
% See tex4ht-cpright.tex for license text.

\documentclass{article}
    \Configure{ProTex}{log,<<<>>>,title,list,`,[[]]}
    \usepackage{url}
    \input{common.tex}
\begin{document}
\input tex4ht-cpright.tex
\input tex4ht-dir

%%%%%%%%%%%%%%%%%%
\part{Post Processing for HTML Output Mode}
%%%%%%%%%%%%%%%%%%


%%%%%%%%%%%%%%%%%%
\section{Outline}
%%%%%%%%%%%%%%%%%% 

\AtEndDocument{\OutputCodE\<xhtmml.4xt\>}

\Needs{"xmllint --valid --noout xhtmml.4xt"} 

\<xhtmml.4xt\><<<
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE xtpipes SYSTEM "xtpipes.dtd" >
<!-- xhtmml.4xt (`version), generated from `jobname.tex
     Copyright (C) 2009-2010 TeX Users Group
     Copyright (C) `CopyYear.2008. Eitan M. Gurari
`<TeX4ht copyright`> -->
<xtpipes preamble="yes" signature="xhtmml.4xt (`version)">
   <sax content-handler="xtpipes.util.ScriptsManager" 
        lexical-handler="xtpipes.util.ScriptsManagerLH" >
     `<p script`>
     `<li script`>
     `<table script`>
     `<mrow script`>
     `<math script`>
     `<mstyle script`>
   </sax>
</xtpipes>
>>>


% 
\AtEndDocument{\OutputCodE\<XhtmmlUtilities.java\>}

\ifdojava
  \Needs{"
  if [ ! -d \XTPIPES\space]; then exit 1; fi
  ;
  javac XhtmmlUtilities.java -d \XTPIPES
  "}
\fi

\<XhtmmlUtilities.java\><<<
package tex4ht;
/* XhtmmlUtilities.java (`version), generated from `jobname.tex
   Copyright (C) 2009-2010 TeX Users Group
   Copyright (C) `CopyYear.2008. Eitan M. Gurari
`<TeX4ht copyright`> */

import org.w3c.dom.*;
public class XhtmmlUtilities {
  `<static void p(dom)`>
  `<static void li(dom)`>
  `<static void table(dom)`>
  `<static void mrow(dom)`>
  `<static void math(dom)`>
  `<void barsIntoFenced(dom)`>
  `<void digitsIntoNumbers(dom)`>
}
>>>





\<mrow script\><<<
<script element="mrow" >
   <dom name="." xml="." method="mrow" class="tex4ht.XhtmmlUtilities" />
</script> 
>>>




\<static void mrow(dom)\><<<
public static void mrow(Node dom) {
   barsIntoFenced( dom );
   digitsIntoNumbers( dom );
}
>>>


\<math script\><<<
<script element="math" >
   <dom name="." xml="." method="math" class="tex4ht.XhtmmlUtilities" />
   `<math-mstyle script`>
</script> 
>>>




\<static void math(dom)\><<<
public static void math(Node dom) {
   barsIntoFenced( dom );
   digitsIntoNumbers( dom );
}
>>>












%%%%%%%%%%%%%
\section{Empty Paragraphs}
%%%%%%%%%%%%%

\<p script\><<<
<script element="p" >
   <dom name="." xml="." method="p" class="tex4ht.XhtmmlUtilities" />
</script> 
>>>

\<p script\><<<
<script element="div" >
   <dom name="." xml="." method="p" class="tex4ht.XhtmmlUtilities" />
</script> 
>>>


\<static void p(dom)\><<<
public static void p(Node dom) {
   Node pNode = dom.getFirstChild();
   if( pNode.hasChildNodes() ){
      boolean drop = true;
      Node child = pNode.getFirstChild();
      while( child != null ){
         short type = child.getNodeType();
         if(              
             (type == Node.ELEMENT_NODE)
           ||             
             (type == Node.CDATA_SECTION_NODE)
           ||             
             (type == Node.TEXT_NODE)
           &&
             !((Text) child).getWholeText().trim().equals("")  
         ){         
            drop = false; break;
         }
         child = child.getNextSibling();
      }
      if( drop ){
         dom.removeChild( pNode );
      }        
   } else {
      dom.removeChild( pNode );
   }
}
>>>



%%%%%%%%%%%%%
\section{Tables}
%%%%%%%%%%%%%


Remove Empty Trailling Row

\<table script\><<<
<script element="table" >
   <dom name="." xml="." method="table" class="tex4ht.XhtmmlUtilities" />
</script> 
>>>


\<static void table(dom)\><<<
public static void table(Node dom) {
   Node tableNode = dom.getFirstChild();
   if( tableNode.hasChildNodes() ){
      Node trChild = tableNode.getLastChild();
      while( (trChild != null)
             &&
             !trChild.getNodeName().equals("tr") ){
         trChild = trChild.getPreviousSibling();
      }
      if( (trChild != null) && trChild.hasChildNodes() ){
         Node tdChild = trChild.getLastChild();
         while( (tdChild != null)
                &&
                !tdChild.getNodeName().equals("td") ){
            tdChild = tdChild.getPreviousSibling();
         }
         if( !tdChild.hasChildNodes() ){
            tableNode.removeChild( trChild );
         } else {
            Node child = tdChild.getFirstChild();
            if(
               (child.getNodeType() == Node.TEXT_NODE)
               &&
               ((Text) child).getWholeText().trim().equals("") 
               &&
               (child.getNextSibling() == null)
            ){
               tableNode.removeChild( trChild );
}  }  }  } }
>>>








%%%%%%%%%%%%%
\section{List Items}
%%%%%%%%%%%%%


%%%%%%%%%%%%%
\subsection{Outline}
%%%%%%%%%%%%%



\<li script\><<<
<script element="li" >
   <dom name="." xml="." method="li" class="tex4ht.XhtmmlUtilities" />
</script> 
>>>




\<static void li(dom)\><<<
public static void li(Node dom) {
   Node liNode = dom.getFirstChild();
   Node liChild;
   boolean hasBlock = false, hasInline = false;
   `<hasBlock, hasInline := exist in li?`>
   if( hasBlock && hasInline ){
      `<p := inline code`>
      `<insert p node as first child`>
   }
}
>>>




%%%%%%%%%%%%%
\subsection{Place Inline Code into Paragraphs}
%%%%%%%%%%%%%



\<p := inline code\><<<
liChild = liNode.getLastChild();
Element g = ((Document) dom).createElement("p");
g.setAttribute("class", "noindent");
while( liChild != null ){
   short type = liChild.getNodeType();
   if( `<liChild == block?`> ){
      `<insert p node after block node`>
      liChild = liChild.getPreviousSibling();
   } else {            
      Node nextChild = liChild;
      liChild = liChild.getPreviousSibling();
      `<move node into p`>
}  }
>>>


\<move node into p\><<<
nextChild = liNode.removeChild(nextChild);
type = nextChild.getNodeType();
if(
     (type != Node.COMMENT_NODE)
   &&
   (
     (type != Node.TEXT_NODE)
   ||
     !((Text) nextChild).getWholeText().trim().equals("") 
   )
){
   if( g.hasChildNodes() ){
      g.insertBefore( nextChild, g.getFirstChild() );
   } else {
      g.appendChild( nextChild );
} } 
>>>         

%%%%%%%%%%%%%
\subsection{Place Paragraphs into List Items}
%%%%%%%%%%%%%

\<insert p node as first child\><<<
if( liNode.hasChildNodes() ){
  liNode.insertBefore( g, liNode.getFirstChild() );
} else {
  liNode.appendChild( g );
}
>>>


\<insert p node after block node\><<<
if( g.hasChildNodes() ){
   Node nextChild = liChild.getNextSibling();
   if( nextChild == null  ){
      liNode.appendChild( g );
   } else {
      liNode.insertBefore( g, nextChild );
   }
   g = ((Document) dom).createElement("p");
   g.setAttribute("class", "noindent");
}
>>>



%%%%%%%%%%%%%
\subsection{Check Nature of Item Content}
%%%%%%%%%%%%%

\<hasBlock, hasInline := exist in li?\><<<
liChild = liNode.getFirstChild();
while( liChild != null ){
   short type = liChild.getNodeType();
   if( `<liChild == block?`> ){  hasBlock = true; }
   else if(  type == Node.TEXT_NODE ){ 
      if( !((Text) liChild).getWholeText().trim().equals("") ){
         hasInline = true;
   }  }
   else if( 
       (type != Node.COMMENT_NODE)
       &&
       (type != Node.PROCESSING_INSTRUCTION_NODE )
   ){ 
      hasInline = true;
   }
   liChild = liChild.getNextSibling();
}
>>>


\<liChild == block?\><<<
(type == Node.ELEMENT_NODE)
&&
(
   liChild.getNodeName().equals("p")
   ||
   liChild.getNodeName().equals("ol")
   ||
   liChild.getNodeName().equals("ul")
   ||
   liChild.getNodeName().equals("div")
   ||
   liChild.getNodeName().equals("table")
)
>>>



%%%%%%%%%%%%%%%%%%
\section{Scripted Math |...|}
%%%%%%%%%%%%%%%%%%





\<void barsIntoFenced(dom)\><<<
static void barsIntoFenced(Node dom) {
   Node rightBarNode = null;
   Node mrowNode = dom.getFirstChild();
   Node mrowChild = mrowNode.getLastChild();
   while( mrowChild != null ){
      if(
           mrowChild.getNodeName().equals("msub")
           ||
           mrowChild.getNodeName().equals("msup")
           ||
           mrowChild.getNodeName().equals("msubsup")
      ){
         Node firstChild = mrowChild.getFirstChild();
         if( 
             (firstChild.getChildNodes().getLength() == 1)
             &&
             firstChild.getTextContent().equals( "|" )
         ){
            rightBarNode = mrowChild;
      }  }
      else
      if(
         (rightBarNode != null)
         &&
         mrowChild.getNodeName().equals("mo")
         &&
         (mrowChild.getChildNodes().getLength() == 1)
         &&
         mrowChild.getTextContent().equals( "|" )
      ){
        if(mrowChild.getNextSibling() != rightBarNode){
            `<set mfenced sub,sup`>
        }
        rightBarNode = null;
      }
      mrowChild = mrowChild.getPreviousSibling();
}  }
>>>






\begin{verbatim}
               A + |B|^2 + C
<mrow> 
  <mi>A</mi> 
  <mo class="MathClass-bin">+</mo> 
  <mo class="MathClass-rel">|</mo>      #
  <mi>B</mi>                            #
  <msup>                                #
    <mrow>                              #
      <mo class="MathClass-rel">|</mo>  #
    </mrow>                             #
    <mrow>                              #
      <mn>2</mn>                        #
    </mrow>                             #
  </msup>                               #
  <mo class="MathClass-bin">+</mo> 
  <mi>C</mi> 
</mrow> 
\end{verbatim}


\<set mfenced sub,sup\><<<
rightBarNode.removeChild( rightBarNode.getFirstChild() );
Node sub = rightBarNode.getFirstChild();
Node mfenced = ((Document) dom).createElement( "mfenced" );
rightBarNode.insertBefore( mfenced, sub );
((Element) mfenced).setAttribute("open","|");
((Element) mfenced).setAttribute("close","|");
((Element) mfenced).setAttribute("separator","");
Node node = mrowChild.getNextSibling();
while( node != rightBarNode ){
   Node next = node.getNextSibling();
   mrowNode.removeChild( node );
   mfenced.appendChild( node );
   node = next;
}
mrowNode.removeChild( mrowChild );
mrowChild = mrowNode.getLastChild();
>>>


%%%%%%%%%%%%%%%%%%
\section{Digits into Numbers}
%%%%%%%%%%%%%%%%%%

\<void digitsIntoNumbers(dom)\><<<
static void digitsIntoNumbers(Node dom){
   Node mrowNode = dom.getFirstChild();
   Node mrowChild = mrowNode.getFirstChild();
   short state = `<init state`>;
   Node fromNode = null, 
          toNode = null;
   while( mrowChild != null ){
      switch( state ){
         case `<init state`>:
              if( mrowChild.getNodeName().equals("mn") ){
                 state = `<mn state`>;
                 fromNode = mrowChild;
                 toNode = mrowChild;
              }
            break;
         case `<mn state`>:
              boolean bool = true;
              if( mrowChild.getNodeName().equals("mn") ){
                 toNode = mrowChild;
                 bool = ( mrowChild.getNextSibling() == null );
              }  
              else
              { `<handle scripted numbers`> }
              if( bool )
              { `<bool := just single digits?`>
                if( bool ){
                   `<num := merge mn's`>
                   node.replaceChild( ((Document) dom).createTextNode(num),
                                      node.getFirstChild() 
                                    );
                }
                state = `<init state`>;
                fromNode = null;
                toNode = null;
      }       } 
      mrowChild = mrowChild.getNextSibling();
}  }
>>>


\<init state\><<<
0
>>>

\<mn state\><<<
1
>>>

\<num := merge mn's\><<<
Node next;
String num = "";
Node node = fromNode;
while( node != toNode ){
  num += node.getTextContent();
  next = node.getNextSibling();
  mrowNode.removeChild( node );
  node = next;
} 
num += node.getTextContent();
>>>

\<bool := just single digits?\><<<
for( Node node = fromNode; ; node = node.getNextSibling()){
  String str = node.getTextContent();
  if( str.length() > 1 ){ bool = false; break; }
  if( !str.replaceAll("[0-9]","").equals("") ){ bool = false; break; }    
  if( node == toNode ){ break; }
} 
>>>



\<handle scripted numbers\><<<
String s = null;
Node base;
if(
    ( mrowChild.getNodeName().equals("msub")
      ||
      mrowChild.getNodeName().equals("msup")
      ||
      mrowChild.getNodeName().equals("msubsup")
    )
    &&
    ((s = (base = mrowChild.getFirstChild())
          . getTextContent()).length() == 1 )
    &&
    s.replaceAll("[0-9]","").equals("")
){
   `<bool := just single digits?`>
   `<num := merge mn's`>
   mrowNode.removeChild( node );
   num += s;
   Node mn = ((Document) dom).createElement("mn");
   mn.appendChild( ((Document) dom).createTextNode(num) );
   mrowChild.replaceChild( mn, base);

   bool = false;
   state = `<init state`>;
   fromNode = null;
   toNode = null;
}
>>>



%%%%%%%%%%%%%%%%%%
\section{mstyle}
%%%%%%%%%%%%%%%%%%


%%%%%%%%%%%%%
\subsection{Mstyle as Parent of an Element}
%%%%%%%%%%%%%

Merge mstyle into its child.  For instance

\begin{verbatim}
<mstyle mathvariant="bold">
   <mi>F</mi>
</mstyle>
\end{verbatim}

into

\begin{verbatim}
<mi mathvariant="bold">F</mi>
\end{verbatim}

\<mstyle script\><<<
<script element="mstyle" >
   <set name="mstyle" >
      `<open xslt script`>
      `<mstyle templates`>
      `<close xslt script`>
   </set>
   <xslt name="." xml="." xsl="mstyle" />
</script> 
>>>

\<mstyle templates\><<<
<xsl:template match=" m:mstyle [  
                  @mathvariant  
             and 
                  child::m:*[ not(@mathvariant) ] 
             and
                  (count(child::m:*) = 1)
] " > 
  <xsl:element name="{name(child::m:*[1])}"> 
    <xsl:attribute name="mathvariant" > 
         <xsl:value-of select="@mathvariant" /> 
    </xsl:attribute> 
    <xsl:apply-templates select="child::m:*/@*" /> 
    <xsl:apply-templates select=" 
        child::m:*/*  
        | child::m:*/text() 
        | child::m:*/comment() 
      "  
    /> 
  </xsl:element> 
</xsl:template> 
>>>

%%%%%%%%%%%%%
\subsection{Mstyle as Parent of Text}
%%%%%%%%%%%%%


\begin{verbatim}
<mi> 
  <mstyle mathvariant="bold">div</mstyle> 
</mi> 
\end{verbatim}

into

\begin{verbatim}
<mi mathvariant="bold">div</mi>
\end{verbatim}

\<math-mstyle script\><<<
<set name="math-mstyle" >
   `<open xslt script`>
   `<math-mstyle templates`>
   `<close xslt script`>
</set>
<xslt name="." xml="." xsl="math-mstyle" />
>>>


\<math-mstyle templates\><<<
<xsl:template match=" m:* [  
      not(self::m:math)
    and
      child::m:mstyle[ 
           @mathvariant
         and
           child::text()
         and
           (count(child::m:*) = 0)    
      ] 
    and
      (count(child::m:*) = 1)    
    and
      not(@mathvariant) 
] " > 
  <xsl:copy>
     <xsl:apply-templates select="@*" /> 
     <xsl:apply-templates select="m:mstyle/@*" /> 
     <xsl:apply-templates select="m:mstyle/text()" /> 
  </xsl:copy>
</xsl:template> 
>>>


%%%%%%%%%%%%%%%%%%
\section{Shared}
%%%%%%%%%%%%%%%%%%



\<open xslt script\><<<
<![CDATA[ 
   <xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:h="http://www.w3.org/1999/xhtml"
      xmlns:m="http://www.w3.org/1998/Math/MathML"
   >
      <xsl:output omit-xml-declaration = "yes" method="xml" />
>>>

\<close xslt script\><<<
      <xsl:template match="*|@*|text()|comment()" >
        <xsl:copy>
          <xsl:apply-templates select="*|@*|text()|comment()" />
        </xsl:copy>
      </xsl:template>
   </xsl:stylesheet> 
]]>
>>>





%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% END END END END END END END END END END END END END END END END 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\ifdojava
\AtEndDocument{\Needs{%
    "pushd \XTPIPES || exit 1
     ;  
     jar cf tex4ht.jar *
     ;
     popd
     ;
     if [ ! -d \TEXMFTEXivBIN\space]; then exit 1; fi
     ;
     mv \XTPIPES tex4ht.jar \TEXMFTEXivBIN
     ;
     if [ ! -d \TEXMFTEXivXTPIPES\space]; then exit 1; fi
     ;
     cp \XTPIPES  xtpipes/lib/* 
     \TEXMFTEXivXTPIPES
     ;
"}}
\fi

%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\end{document}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%