Debian Codegraph @

texmf-dist/source/generic/tex4ht/tex4ht-xhtmml-xtpipes.tex

raw
1 % $Id: tex4ht-xhtmml-xtpipes.tex 740 2020-06-13 22:35:32Z karl $
2 % htlatex tex4ht-xhtmml-xtpipes "xhtml,next,3" "" "-d./"
3
4 % Copyright 2009-2020 TeX Users Group
5 % Copyright 1996-2009 Eitan M. Gurari
6 % Released under LPPL 1.3c+.
7 % See tex4ht-cpright.tex for license text.
8
9 \documentclass{article}
10 \Configure{ProTex}{log,<<<>>>,title,list,`,[[]]}
11 \usepackage{url}
12 \input{common.tex}
13 \begin{document}
14 \input tex4ht-cpright.tex
15 \input tex4ht-dir
16
17 %%%%%%%%%%%%%%%%%%
18 \part{Post Processing for HTML Output Mode}
19 %%%%%%%%%%%%%%%%%%
20
21
22 %%%%%%%%%%%%%%%%%%
23 \section{Outline}
24 %%%%%%%%%%%%%%%%%%
25
26 \AtEndDocument{\OutputCodE\<xhtmml.4xt\>}
27
28 \Needs{"xmllint --valid --noout xhtmml.4xt"}
29
30 \<xhtmml.4xt\><<<
31 <?xml version="1.0" encoding="UTF-8" ?>
32 <!DOCTYPE xtpipes SYSTEM "xtpipes.dtd" >
33 <!-- xhtmml.4xt (`version), generated from `jobname.tex
34 Copyright (C) 2009-2010 TeX Users Group
35 Copyright (C) `CopyYear.2008. Eitan M. Gurari
36 `<TeX4ht copyright`> -->
37 <xtpipes preamble="yes" signature="xhtmml.4xt (`version)">
38 <sax content-handler="xtpipes.util.ScriptsManager"
39 lexical-handler="xtpipes.util.ScriptsManagerLH" >
40 `<p script`>
41 `<li script`>
42 `<table script`>
43 `<mrow script`>
44 `<math script`>
45 `<mstyle script`>
46 </sax>
47 </xtpipes>
48 >>>
49
50
51 %
52 \AtEndDocument{\OutputCodE\<XhtmmlUtilities.java\>}
53
54 \ifdojava
55 \Needs{"
56 if [ ! -d \XTPIPES\space]; then exit 1; fi
57 ;
58 javac XhtmmlUtilities.java -d \XTPIPES
59 "}
60 \fi
61
62 \<XhtmmlUtilities.java\><<<
63 package tex4ht;
64 /* XhtmmlUtilities.java (`version), generated from `jobname.tex
65 Copyright (C) 2009-2010 TeX Users Group
66 Copyright (C) `CopyYear.2008. Eitan M. Gurari
67 `<TeX4ht copyright`> */
68
69 import org.w3c.dom.*;
70 public class XhtmmlUtilities {
71 `<static void p(dom)`>
72 `<static void li(dom)`>
73 `<static void table(dom)`>
74 `<static void mrow(dom)`>
75 `<static void math(dom)`>
76 `<void barsIntoFenced(dom)`>
77 `<void digitsIntoNumbers(dom)`>
78 }
79 >>>
80
81
82
83
84
85 \<mrow script\><<<
86 <script element="mrow" >
87 <dom name="." xml="." method="mrow" class="tex4ht.XhtmmlUtilities" />
88 </script>
89 >>>
90
91
92
93
94 \<static void mrow(dom)\><<<
95 public static void mrow(Node dom) {
96 barsIntoFenced( dom );
97 digitsIntoNumbers( dom );
98 }
99 >>>
100
101
102 \<math script\><<<
103 <script element="math" >
104 <dom name="." xml="." method="math" class="tex4ht.XhtmmlUtilities" />
105 `<math-mstyle script`>
106 </script>
107 >>>
108
109
110
111
112 \<static void math(dom)\><<<
113 public static void math(Node dom) {
114 barsIntoFenced( dom );
115 digitsIntoNumbers( dom );
116 }
117 >>>
118
119
120
121
122
123
124
125
126
127
128
129
130 %%%%%%%%%%%%%
131 \section{Empty Paragraphs}
132 %%%%%%%%%%%%%
133
134 \<p script\><<<
135 <script element="p" >
136 <dom name="." xml="." method="p" class="tex4ht.XhtmmlUtilities" />
137 </script>
138 >>>
139
140 \<p script\><<<
141 <script element="div" >
142 <dom name="." xml="." method="p" class="tex4ht.XhtmmlUtilities" />
143 </script>
144 >>>
145
146
147 \<static void p(dom)\><<<
148 public static void p(Node dom) {
149 Node pNode = dom.getFirstChild();
150 if( pNode.hasChildNodes() ){
151 boolean drop = true;
152 Node child = pNode.getFirstChild();
153 while( child != null ){
154 short type = child.getNodeType();
155 if(
156 (type == Node.ELEMENT_NODE)
157 ||
158 (type == Node.CDATA_SECTION_NODE)
159 ||
160 (type == Node.TEXT_NODE)
161 &&
162 !((Text) child).getWholeText().trim().equals("")
163 ){
164 drop = false; break;
165 }
166 child = child.getNextSibling();
167 }
168 if( drop ){
169 dom.removeChild( pNode );
170 }
171 } else {
172 dom.removeChild( pNode );
173 }
174 }
175 >>>
176
177
178
179 %%%%%%%%%%%%%
180 \section{Tables}
181 %%%%%%%%%%%%%
182
183
184 Remove Empty Trailling Row
185
186 \<table script\><<<
187 <script element="table" >
188 <dom name="." xml="." method="table" class="tex4ht.XhtmmlUtilities" />
189 </script>
190 >>>
191
192
193 \<static void table(dom)\><<<
194 public static void table(Node dom) {
195 Node tableNode = dom.getFirstChild();
196 if( tableNode.hasChildNodes() ){
197 Node trChild = tableNode.getLastChild();
198 while( (trChild != null)
199 &&
200 !trChild.getNodeName().equals("tr") ){
201 trChild = trChild.getPreviousSibling();
202 }
203 if( (trChild != null) && trChild.hasChildNodes() ){
204 Node tdChild = trChild.getLastChild();
205 while( (tdChild != null)
206 &&
207 !tdChild.getNodeName().equals("td") ){
208 tdChild = tdChild.getPreviousSibling();
209 }
210 if( !tdChild.hasChildNodes() ){
211 tableNode.removeChild( trChild );
212 } else {
213 Node child = tdChild.getFirstChild();
214 if(
215 (child.getNodeType() == Node.TEXT_NODE)
216 &&
217 ((Text) child).getWholeText().trim().equals("")
218 &&
219 (child.getNextSibling() == null)
220 ){
221 tableNode.removeChild( trChild );
222 } } } } }
223 >>>
224
225
226
227
228
229
230
231
232 %%%%%%%%%%%%%
233 \section{List Items}
234 %%%%%%%%%%%%%
235
236
237 %%%%%%%%%%%%%
238 \subsection{Outline}
239 %%%%%%%%%%%%%
240
241
242
243 \<li script\><<<
244 <script element="li" >
245 <dom name="." xml="." method="li" class="tex4ht.XhtmmlUtilities" />
246 </script>
247 >>>
248
249
250
251
252 \<static void li(dom)\><<<
253 public static void li(Node dom) {
254 Node liNode = dom.getFirstChild();
255 Node liChild;
256 boolean hasBlock = false, hasInline = false;
257 `<hasBlock, hasInline := exist in li?`>
258 if( hasBlock && hasInline ){
259 `<p := inline code`>
260 `<insert p node as first child`>
261 }
262 }
263 >>>
264
265
266
267
268 %%%%%%%%%%%%%
269 \subsection{Place Inline Code into Paragraphs}
270 %%%%%%%%%%%%%
271
272
273
274 \<p := inline code\><<<
275 liChild = liNode.getLastChild();
276 Element g = ((Document) dom).createElement("p");
277 g.setAttribute("class", "noindent");
278 while( liChild != null ){
279 short type = liChild.getNodeType();
280 if( `<liChild == block?`> ){
281 `<insert p node after block node`>
282 liChild = liChild.getPreviousSibling();
283 } else {
284 Node nextChild = liChild;
285 liChild = liChild.getPreviousSibling();
286 `<move node into p`>
287 } }
288 >>>
289
290
291 \<move node into p\><<<
292 nextChild = liNode.removeChild(nextChild);
293 type = nextChild.getNodeType();
294 if(
295 (type != Node.COMMENT_NODE)
296 &&
297 (
298 (type != Node.TEXT_NODE)
299 ||
300 !((Text) nextChild).getWholeText().trim().equals("")
301 )
302 ){
303 if( g.hasChildNodes() ){
304 g.insertBefore( nextChild, g.getFirstChild() );
305 } else {
306 g.appendChild( nextChild );
307 } }
308 >>>
309
310 %%%%%%%%%%%%%
311 \subsection{Place Paragraphs into List Items}
312 %%%%%%%%%%%%%
313
314 \<insert p node as first child\><<<
315 if( liNode.hasChildNodes() ){
316 liNode.insertBefore( g, liNode.getFirstChild() );
317 } else {
318 liNode.appendChild( g );
319 }
320 >>>
321
322
323 \<insert p node after block node\><<<
324 if( g.hasChildNodes() ){
325 Node nextChild = liChild.getNextSibling();
326 if( nextChild == null ){
327 liNode.appendChild( g );
328 } else {
329 liNode.insertBefore( g, nextChild );
330 }
331 g = ((Document) dom).createElement("p");
332 g.setAttribute("class", "noindent");
333 }
334 >>>
335
336
337
338 %%%%%%%%%%%%%
339 \subsection{Check Nature of Item Content}
340 %%%%%%%%%%%%%
341
342 \<hasBlock, hasInline := exist in li?\><<<
343 liChild = liNode.getFirstChild();
344 while( liChild != null ){
345 short type = liChild.getNodeType();
346 if( `<liChild == block?`> ){ hasBlock = true; }
347 else if( type == Node.TEXT_NODE ){
348 if( !((Text) liChild).getWholeText().trim().equals("") ){
349 hasInline = true;
350 } }
351 else if(
352 (type != Node.COMMENT_NODE)
353 &&
354 (type != Node.PROCESSING_INSTRUCTION_NODE )
355 ){
356 hasInline = true;
357 }
358 liChild = liChild.getNextSibling();
359 }
360 >>>
361
362
363 \<liChild == block?\><<<
364 (type == Node.ELEMENT_NODE)
365 &&
366 (
367 liChild.getNodeName().equals("p")
368 ||
369 liChild.getNodeName().equals("ol")
370 ||
371 liChild.getNodeName().equals("ul")
372 ||
373 liChild.getNodeName().equals("div")
374 ||
375 liChild.getNodeName().equals("table")
376 )
377 >>>
378
379
380
381 %%%%%%%%%%%%%%%%%%
382 \section{Scripted Math |...|}
383 %%%%%%%%%%%%%%%%%%
384
385
386
387
388
389 \<void barsIntoFenced(dom)\><<<
390 static void barsIntoFenced(Node dom) {
391 Node rightBarNode = null;
392 Node mrowNode = dom.getFirstChild();
393 Node mrowChild = mrowNode.getLastChild();
394 while( mrowChild != null ){
395 if(
396 mrowChild.getNodeName().equals("msub")
397 ||
398 mrowChild.getNodeName().equals("msup")
399 ||
400 mrowChild.getNodeName().equals("msubsup")
401 ){
402 Node firstChild = mrowChild.getFirstChild();
403 if(
404 (firstChild.getChildNodes().getLength() == 1)
405 &&
406 firstChild.getTextContent().equals( "|" )
407 ){
408 rightBarNode = mrowChild;
409 } }
410 else
411 if(
412 (rightBarNode != null)
413 &&
414 mrowChild.getNodeName().equals("mo")
415 &&
416 (mrowChild.getChildNodes().getLength() == 1)
417 &&
418 mrowChild.getTextContent().equals( "|" )
419 ){
420 if(mrowChild.getNextSibling() != rightBarNode){
421 `<set mfenced sub,sup`>
422 }
423 rightBarNode = null;
424 }
425 mrowChild = mrowChild.getPreviousSibling();
426 } }
427 >>>
428
429
430
431
432
433
434 \begin{verbatim}
435 A + |B|^2 + C
436 <mrow>
437 <mi>A</mi>
438 <mo class="MathClass-bin">+</mo>
439 <mo class="MathClass-rel">|</mo> #
440 <mi>B</mi> #
441 <msup> #
442 <mrow> #
443 <mo class="MathClass-rel">|</mo> #
444 </mrow> #
445 <mrow> #
446 <mn>2</mn> #
447 </mrow> #
448 </msup> #
449 <mo class="MathClass-bin">+</mo>
450 <mi>C</mi>
451 </mrow>
452 \end{verbatim}
453
454
455 \<set mfenced sub,sup\><<<
456 rightBarNode.removeChild( rightBarNode.getFirstChild() );
457 Node sub = rightBarNode.getFirstChild();
458 Node mfenced = ((Document) dom).createElement( "mfenced" );
459 rightBarNode.insertBefore( mfenced, sub );
460 ((Element) mfenced).setAttribute("open","|");
461 ((Element) mfenced).setAttribute("close","|");
462 ((Element) mfenced).setAttribute("separator","");
463 Node node = mrowChild.getNextSibling();
464 while( node != rightBarNode ){
465 Node next = node.getNextSibling();
466 mrowNode.removeChild( node );
467 mfenced.appendChild( node );
468 node = next;
469 }
470 mrowNode.removeChild( mrowChild );
471 mrowChild = mrowNode.getLastChild();
472 >>>
473
474
475 %%%%%%%%%%%%%%%%%%
476 \section{Digits into Numbers}
477 %%%%%%%%%%%%%%%%%%
478
479 \<void digitsIntoNumbers(dom)\><<<
480 static void digitsIntoNumbers(Node dom){
481 Node mrowNode = dom.getFirstChild();
482 Node mrowChild = mrowNode.getFirstChild();
483 short state = `<init state`>;
484 Node fromNode = null,
485 toNode = null;
486 while( mrowChild != null ){
487 switch( state ){
488 case `<init state`>:
489 if( mrowChild.getNodeName().equals("mn") ){
490 state = `<mn state`>;
491 fromNode = mrowChild;
492 toNode = mrowChild;
493 }
494 break;
495 case `<mn state`>:
496 boolean bool = true;
497 if( mrowChild.getNodeName().equals("mn") ){
498 toNode = mrowChild;
499 bool = ( mrowChild.getNextSibling() == null );
500 }
501 else
502 { `<handle scripted numbers`> }
503 if( bool )
504 { `<bool := just single digits?`>
505 if( bool ){
506 `<num := merge mn's`>
507 node.replaceChild( ((Document) dom).createTextNode(num),
508 node.getFirstChild()
509 );
510 }
511 state = `<init state`>;
512 fromNode = null;
513 toNode = null;
514 } }
515 mrowChild = mrowChild.getNextSibling();
516 } }
517 >>>
518
519
520 \<init state\><<<
521 0
522 >>>
523
524 \<mn state\><<<
525 1
526 >>>
527
528 \<num := merge mn's\><<<
529 Node next;
530 String num = "";
531 Node node = fromNode;
532 while( node != toNode ){
533 num += node.getTextContent();
534 next = node.getNextSibling();
535 mrowNode.removeChild( node );
536 node = next;
537 }
538 num += node.getTextContent();
539 >>>
540
541 \<bool := just single digits?\><<<
542 for( Node node = fromNode; ; node = node.getNextSibling()){
543 String str = node.getTextContent();
544 if( str.length() > 1 ){ bool = false; break; }
545 if( !str.replaceAll("[0-9]","").equals("") ){ bool = false; break; }
546 if( node == toNode ){ break; }
547 }
548 >>>
549
550
551
552 \<handle scripted numbers\><<<
553 String s = null;
554 Node base;
555 if(
556 ( mrowChild.getNodeName().equals("msub")
557 ||
558 mrowChild.getNodeName().equals("msup")
559 ||
560 mrowChild.getNodeName().equals("msubsup")
561 )
562 &&
563 ((s = (base = mrowChild.getFirstChild())
564 . getTextContent()).length() == 1 )
565 &&
566 s.replaceAll("[0-9]","").equals("")
567 ){
568 `<bool := just single digits?`>
569 `<num := merge mn's`>
570 mrowNode.removeChild( node );
571 num += s;
572 Node mn = ((Document) dom).createElement("mn");
573 mn.appendChild( ((Document) dom).createTextNode(num) );
574 mrowChild.replaceChild( mn, base);
575
576 bool = false;
577 state = `<init state`>;
578 fromNode = null;
579 toNode = null;
580 }
581 >>>
582
583
584
585 %%%%%%%%%%%%%%%%%%
586 \section{mstyle}
587 %%%%%%%%%%%%%%%%%%
588
589
590 %%%%%%%%%%%%%
591 \subsection{Mstyle as Parent of an Element}
592 %%%%%%%%%%%%%
593
594 Merge mstyle into its child. For instance
595
596 \begin{verbatim}
597 <mstyle mathvariant="bold">
598 <mi>F</mi>
599 </mstyle>
600 \end{verbatim}
601
602 into
603
604 \begin{verbatim}
605 <mi mathvariant="bold">F</mi>
606 \end{verbatim}
607
608 \<mstyle script\><<<
609 <script element="mstyle" >
610 <set name="mstyle" >
611 `<open xslt script`>
612 `<mstyle templates`>
613 `<close xslt script`>
614 </set>
615 <xslt name="." xml="." xsl="mstyle" />
616 </script>
617 >>>
618
619 \<mstyle templates\><<<
620 <xsl:template match=" m:mstyle [
621 @mathvariant
622 and
623 child::m:*[ not(@mathvariant) ]
624 and
625 (count(child::m:*) = 1)
626 ] " >
627 <xsl:element name="{name(child::m:*[1])}">
628 <xsl:attribute name="mathvariant" >
629 <xsl:value-of select="@mathvariant" />
630 </xsl:attribute>
631 <xsl:apply-templates select="child::m:*/@*" />
632 <xsl:apply-templates select="
633 child::m:*/*
634 | child::m:*/text()
635 | child::m:*/comment()
636 "
637 />
638 </xsl:element>
639 </xsl:template>
640 >>>
641
642 %%%%%%%%%%%%%
643 \subsection{Mstyle as Parent of Text}
644 %%%%%%%%%%%%%
645
646
647 \begin{verbatim}
648 <mi>
649 <mstyle mathvariant="bold">div</mstyle>
650 </mi>
651 \end{verbatim}
652
653 into
654
655 \begin{verbatim}
656 <mi mathvariant="bold">div</mi>
657 \end{verbatim}
658
659 \<math-mstyle script\><<<
660 <set name="math-mstyle" >
661 `<open xslt script`>
662 `<math-mstyle templates`>
663 `<close xslt script`>
664 </set>
665 <xslt name="." xml="." xsl="math-mstyle" />
666 >>>
667
668
669 \<math-mstyle templates\><<<
670 <xsl:template match=" m:* [
671 not(self::m:math)
672 and
673 child::m:mstyle[
674 @mathvariant
675 and
676 child::text()
677 and
678 (count(child::m:*) = 0)
679 ]
680 and
681 (count(child::m:*) = 1)
682 and
683 not(@mathvariant)
684 ] " >
685 <xsl:copy>
686 <xsl:apply-templates select="@*" />
687 <xsl:apply-templates select="m:mstyle/@*" />
688 <xsl:apply-templates select="m:mstyle/text()" />
689 </xsl:copy>
690 </xsl:template>
691 >>>
692
693
694 %%%%%%%%%%%%%%%%%%
695 \section{Shared}
696 %%%%%%%%%%%%%%%%%%
697
698
699
700 \<open xslt script\><<<
701 <![CDATA[
702 <xsl:stylesheet version="1.0"
703 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
704 xmlns:h="http://www.w3.org/1999/xhtml"
705 xmlns:m="http://www.w3.org/1998/Math/MathML"
706 >
707 <xsl:output omit-xml-declaration = "yes" method="xml" />
708 >>>
709
710 \<close xslt script\><<<
711 <xsl:template match="*|@*|text()|comment()" >
712 <xsl:copy>
713 <xsl:apply-templates select="*|@*|text()|comment()" />
714 </xsl:copy>
715 </xsl:template>
716 </xsl:stylesheet>
717 ]]>
718 >>>
719
720
721
722
723
724 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
725 % END END END END END END END END END END END END END END END END
726 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
727
728 \ifdojava
729 \AtEndDocument{\Needs{%
730 "pushd \XTPIPES || exit 1
731 ;
732 jar cf tex4ht.jar *
733 ;
734 popd
735 ;
736 if [ ! -d \TEXMFTEXivBIN\space]; then exit 1; fi
737 ;
738 mv \XTPIPES tex4ht.jar \TEXMFTEXivBIN
739 ;
740 if [ ! -d \TEXMFTEXivXTPIPES\space]; then exit 1; fi
741 ;
742 cp \XTPIPES xtpipes/lib/*
743 \TEXMFTEXivXTPIPES
744 ;
745 "}}
746 \fi
747
748 %%%%%%%%%%%%%%%%%%%%%%%%%%%%
749 \end{document}
750 %%%%%%%%%%%%%%%%%%%%%%%%%%%%
751