to_latex.ml 25.8 KB
Newer Older
gerd's avatar
gerd committed
1
(* $Id$
gerd's avatar
gerd committed
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
 * ----------------------------------------------------------------------
 *
 *)


open Pxp_types
open Pxp_document
open Pxp_yacc

let map'n'concat f l =
  String.concat "" (List.map f l)
;;

let escape_latex_re = (Pcre.regexp "\\<|\\>|\\&|\\\"|\\$|\\%|\\#|\\{|\\}|\\'|\\||\\~|-|\\`|\\^|\\_|\\\\");;

let escape_latex_ws_re = Pcre.regexp "[ \t\r\n]+";;

let escape_latex ?(tt = false) s =
  (* ~tt: The typewriter fonts are ASCII-compatible, and less control sequences
   * are needed
   *)
  let s' =
    Pcre.substitute
      ~rex:escape_latex_ws_re
      ~subst:(fun s ->
		match s.[0] with
		    ' ' | '\t' -> " "
		  | '\r' | '\n' -> "\n"
		  | _ -> assert false) 
      s
  in
  let s'' =
    Pcre.substitute
      ~rex:escape_latex_re
      ~subst:
        (fun s ->
	   (* ~tt: always use \symbol *)
           match s with
gerd's avatar
gerd committed
40
(*
gerd's avatar
gerd committed
41 42 43 44 45 46 47
               "<" when not tt -> "$<${}"
             | ">" when not tt -> "$>${}"
	     | "\\" when not tt -> "$\\backslash${}"
	     | "_" when not tt -> "\\_"
	     | "{" when not tt ->"\\{"
	     | "|" when not tt ->"$\\vert${}"
	     | "}" when not tt ->"\\}"
gerd's avatar
gerd committed
48
*)
gerd's avatar
gerd committed
49 50 51
             | _ -> "\\symbol{" ^ string_of_int(Char.code s.[0]) ^ "}")
      s'
  in
52
  Netconversion.convert
gerd's avatar
gerd committed
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
    ~in_enc:`Enc_utf8
    ~out_enc:`Enc_usascii
    ~subst:(fun k -> 
	      (* trial to print the HTML 4 Unicode characters as much as
		 possible *)
	      match k with
		  160 -> "\\ "
		| 161 -> "!`"
		| 162 -> ""  (* CENT *)
		| 163 -> "\\pounds{}"
		| 164 -> ""  (* CURRENCY *)
		| 165 -> ""  (* YEN *)
		| 166 -> ""  (* VERT2 *)
		| 167 -> "\\S{}"
		| 168 -> "\\symbol{127}"
		| 169 -> "\\copyright{}"
		| 170 -> ""  (* ??? *)
gerd's avatar
gerd committed
70
		| 171 -> "\\symbol{19}"  (* LEFT ANGLE QUOT (T1) *)
gerd's avatar
gerd committed
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
		| 172 -> "$\\neg${}"
		| 173 -> "-{}"
		| 174 -> ""  (* REG TRADEMARK *)
		| 175 -> "$\\overline{\\;}${}"
		| 176 -> "${}^{\\circ}${}"   (* degree *)
		| 177 -> "$\\pm${}"
		| 178 -> "${}^2${}"
		| 179 -> "${}^3${}"
		| 180 -> "\\'{ }"
		| 181 -> "$\\mu${}"
		| 182 -> "\\P{}"
		| 183 -> "$\\cdot${}"
		| 184 -> "\\c{ }"
		| 185 -> "${}^1${}"
		| 186 -> ""  (* ??? *)
gerd's avatar
gerd committed
86
		| 187 -> "\\symbol{20}"  (* RIGHT ANGLE QUOT (T1) *)
gerd's avatar
gerd committed
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
		| 188 -> "$\\frac{1}{4}${}"
		| 189 -> "$\\frac{1}{3}${}"
		| 190 -> "$\\frac{3}{4}${}"
		| 191 -> "?`"
		| 192 -> "\\`{A}"
		| 193 -> "\\'{A}"
		| 194 -> "\\^{A}"
		| 195 -> "\\~{A}"
		| 196 -> "\\\"{A}"
		| 197 -> "\\AA{}"
		| 198 -> "\\AE{}"
		| 199 -> "\\c{C}"
		| 200 -> "\\`{E}"
		| 201 -> "\\'{E}"
		| 202 -> "\\^{E}"
		| 203 -> "\\\"{E}"
		| 204 -> "\\`{I}"
		| 205 -> "\\'{I}"
		| 206 -> "\\^{I}"
		| 207 -> "\\\"{I}"
		| 208 -> ""  (* ETH *)
		| 209 -> "\\~{N}"
		| 210 -> "\\`{O}"
		| 211 -> "\\'{O}"
		| 212 -> "\\^{O}"
		| 213 -> "\\~{O}"
		| 214 -> "\\\"{O}"
		| 215 -> "$\\times${}"
		| 216 -> "\\O{}"
		| 217 -> "\\`{U}"
		| 218 -> "\\'{U}"
		| 219 -> "\\^{U}"
		| 220 -> "\\\"{U}"
		| 221 -> "\\`{Y}"
gerd's avatar
gerd committed
121
		| 222 -> "\\symbol{222}" (* THORN (T1) *)
gerd's avatar
gerd committed
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
		| 223 -> "\\ss{}"
		| 224 -> "\\`{a}"
		| 225 -> "\\'{a}"
		| 226 -> "\\^{a}"
		| 227 -> "\\~{a}"
		| 228 -> "\\\"{a}"
		| 229 -> "\\aa{}"
		| 230 -> "\\ae{}"
		| 231 -> "\\c{c}"
		| 232 -> "\\`{e}"
		| 233 -> "\\'{e}"
		| 234 -> "\\^{e}"
		| 235 -> "\\\"{e}"
		| 236 -> "\\`{i}"
		| 237 -> "\\'{i}"
		| 238 -> "\\^{i}"
		| 239 -> "\\\"{i}"
gerd's avatar
gerd committed
139
		| 240 -> "\\symbol{240}"   (* eth (T1) *)
gerd's avatar
gerd committed
140 141 142 143 144 145 146 147 148 149 150 151 152
		| 241 -> "\\~{n}"
		| 242 -> "\\`{o}"
		| 243 -> "\\'{o}"
		| 244 -> "\\^{o}"
		| 245 -> "\\~{o}"
		| 246 -> "\\\"{o}"
		| 247 -> "$\\div${}"
		| 248 -> "\\o{}"
		| 249 -> "\\`{u}"
		| 250 -> "\\'{u}"
		| 251 -> "\\^{u}"
		| 252 -> "\\\"{u}"
		| 253 -> "\\`{y}"
gerd's avatar
gerd committed
153
		| 254 -> "\\symbol{254}" (* thorn (T1) *)
gerd's avatar
gerd committed
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
		| 255 -> "\\\"{y}"
		| 338 -> "\\OE{}"
		| 339 -> "\\oe{}"
		| 352 -> "\\v{S}"
		| 353 -> "\\v{s}"
		| 376 -> "\\\"{Y}"
		| 710 -> "\\symbol{94}"
		| 732 -> "\\symbol{126}"
		| 402 -> "$f${}"   (* fnof *)
		| 913 -> "$A${}"
		| 914 -> "$B${}"
		| 915 -> "$\\Gamma${}"
		| 916 -> "$\\Delta${}"
		| 917 -> "$E${}"
		| 918 -> "$Z${}"
		| 919 -> "$H${}"
		| 920 -> "$\\Theta${}"
		| 921 -> "$I${}"
		| 922 -> "$K${}"
		| 923 -> "$\\Lambda${}"
		| 924 -> "$M${}"
		| 925 -> "$N${}"
		| 926 -> "$\\Xi${}"
		| 927 -> "$O${}"
		| 928 -> "$\\Pi${}"
		| 929 -> "$P${}"
		| 931 -> "$\\Sigma${}"
		| 932 -> "$T${}"
		| 933 -> "$Y${}"
		| 934 -> "$\\Phi${}"
		| 935 -> "$X${}"
		| 936 -> "$\\Psi${}"
		| 937 -> "$\\Omega${}"
		| 945 -> "$\\alpha${}"
		| 946 -> "$\\beta${}"
		| 947 -> "$\\gamma${}"
		| 948 -> "$\\delta${}"
		| 949 -> "$\\epsilon${}"
		| 950 -> "$\\zeta${}"
		| 951 -> "$\\eta${}"
		| 952 -> "$\\theta${}"
		| 953 -> "$\\iota${}"
		| 954 -> "$\\kappa${}"
		| 955 -> "$\\lambda${}"
		| 956 -> "$\\mu${}"
		| 957 -> "$\\nu${}"
		| 958 -> "$\\xi${}"
		| 959 -> "$o${}"
		| 960 -> "$\\pi${}"
		| 961 -> "$\\rho${}"
		| 962 -> "$\\varsigma${}"
		| 963 -> "$\\sigma${}"
		| 964 -> "$\\tau${}"
		| 965 -> "$\\upsilon${}"
		| 966 -> "$\\varphi${}"
		| 967 -> "$\\chi${}"
		| 968 -> "$\\psi${}"
		| 969 -> "$\\omega${}"
		| 977 -> "$\\vartheta${}"
		| 978 -> "$\\Upsilon${}"
		| 982 -> "$\\varpi${}"
		| 8194 -> "\\hspace*{1en}"
		| 8195 -> "\\hspace*{1em}"
		| 8201 -> "\\,"
		| 8204 -> "\\mbox{}"
		| 8205 -> ""
		| 8206 -> ""
		| 8207 -> ""   (* right-to-left not supported *)
		| 8211 -> "--{}"
		| 8212 -> "---{}"
		| 8216 -> "`{}"
		| 8217 -> "'{}"
		| 8218 -> ","
		| 8220 -> "``{}"
		| 8221 -> "''{}"
		| 8222 -> ",,"
		| 8224 -> "$\\dagger${}"
		| 8225 -> "$\\ddagger${}"
		| 8226 -> "$\\bullet${}"
		| 8230 -> "\\ldots{}"
		| 8240 -> ""   (* TODO: permille sign *)
		| 8242 -> "$\\prime${}"
		| 8243 -> "$\\prime\\prime${}"
gerd's avatar
gerd committed
237 238
		| 8249 -> "\\symbol{14}"    (* TODO: single left-pointing angle quot mark (T1) *)  
		| 8250 -> "\\symbol{15}"    (* TODO: single right-pointing angle quot mark (T1) *)  
gerd's avatar
gerd committed
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
		| 8252 -> "$\\overline{\\;}${}"
		| 8260 -> "$/${}"
		| 8472 -> "$\\wp${}"
		| 8465 -> "$\\Im${}"
		| 8476 -> "$\\Re${}"
		| 8482 -> "${}^{\\mbox{\\sc tm}}${}"
		| 8501 -> "$\\aleph${}"
		| 8592 -> "$\\leftarrow${}"
		| 8593 -> "$\\uparrow${}"
		| 8594 -> "$\\rightarrow${}"
		| 8595 -> "$\\downarrow${}"
		| 8596 -> "$\\leftrightarrow${}"
		| 8629 -> "$\\hookleftarrow${}"
		| 8656 -> "$\\Leftarrow${}"
		| 8657 -> "$\\Uparrow${}"
		| 8658 -> "$\\Rightarrow${}"
		| 8659 -> "$\\Downarrow${}"
		| 8660 -> "$\\Leftrightarrow${}"
		| 8704 -> "$\\forall${}"
		| 8706 -> "$\\partial${}"
		| 8707 -> "$\\exists${}"
		| 8709 -> "$\\emptyset${}"
		| 8711 -> "$\\nabla${}"
		| 8712 -> "$\\in${}"
		| 8713 -> "$\\not\\in${}"
		| 8715 -> "$\\ni${}"
		| 8719 -> "$\\prod${}"
		| 8721 -> "$\\sum${}"
		| 8722 -> "$-${}"
		| 8727 -> "$\\star${}"
		| 8730 -> "$\\sqrt{\\;}${}"
		| 8733 -> "$\\propto${}"
		| 8734 -> "$\\infty${}"
		| 8736 -> "$\\angle${}"
		| 8743 -> "$\\wedge${}"
		| 8744 -> "$\\vee${}"
		| 8745 -> "$\\cap${}"
		| 8746 -> "$\\cup${}"
		| 8747 -> "$\\int${}"
		| 8756 -> "$.\\cdot.${}"
		| 8764 -> "$\\sim${}"
		| 8773 -> "$\\cong${}"
		| 8774 -> "$\\approx${}"
		| 8800 -> "$\\not=${}"
		| 8801 -> "$\\equiv${}"
		| 8804 -> "$\\leq${}"
		| 8805 -> "$\\geq${}"
		| 8834 -> "$\\subset${}"
		| 8835 -> "$\\supset${}" 
		| 8836 -> "$\\not\\subset${}"
		| 8838 -> "$\\subseteq${}" 
		| 8839 -> "$\\supseteq${}"
		| 8853 -> "$\\oplus${}"
		| 8855 -> "$\\otimes${}"
		| 8869 -> "$\\perp${}"
		| 8901 -> "$\\cdot${}"
		| 8968 -> "$\\lceil${}"
		| 8969 -> "$\\rceil${}"
		| 8970 -> "$\\lfloor${}"
		| 8971 -> "$\\rfloor${}"
		| 9001 -> "$\\langle${}"
		| 9002 -> "$\\rangle${}"
		| 9674 -> "$\\Diamond${}"
		| 9824 -> "$\\spadesuit${}"
		| 9827 -> "$\\clubsuit${}"
		| 9829 -> "$\\heartsuit${}"
		| 9830 -> "$\\diamondsuit${}"
		| _ -> ""
	   )
    s''
;;


let escape_quotes s =
  Pcre.substitute
    ~rex:(Pcre.regexp "[\\\"\\\\]")
    ~subst:
      (fun s ->
	 match s with
             "\"" -> "\\\""
	   | "\\" -> "\\\\"
	   | _    -> assert false)
    s
;;


let latexref s =
326
  let u = Bytes.create (String.length s * 2) in
gerd's avatar
gerd committed
327 328
  for k = 0 to String.length s - 1 do
    let hex = Printf.sprintf "%02x" (Char.code s.[k]) in
329 330
    Bytes.set u (k*2) hex.[0];
    Bytes.set u (k*2+1) hex.[1];
gerd's avatar
gerd committed
331
  done;
332
  Bytes.to_string u
gerd's avatar
gerd committed
333 334 335
;;


336 337 338
type lang =
    [ `Any | `Lang of string ]

gerd's avatar
gerd committed
339 340 341
type page_hierarchy =
    { pageid : string;
      pagelink : string;
342 343
      title : lang:lang -> string;
      web_path : lang:lang -> string;
gerd's avatar
gerd committed
344 345 346 347 348
      children : page_hierarchy list;
    }
;;


349 350 351 352 353 354 355
let string_of_lang =
  function
    | `Any -> "any"
    | `Lang s -> s



gerd's avatar
gerd committed
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
class virtual shared =
  object (self)

    (* --- default_ext --- *)

    val mutable node = (None : shared node option)

    method clone = {< >}
    method node =
      match node with
          None ->
            assert false
        | Some n -> n
    method set_node n =
      node <- Some n


    (* --- for all --- *)

    (* First, 'enumerate_pages' is invoked on all 'layout' and 'page' objects.
     * All preprocessing should happen here. After that, 'print_pages' is
     * invoked on all 'layout' and 'page' objects. The latter will
     * subsequently invoke 'to_html' to compute the page contents.
     *)

    method enumerate_pages (n : int) =
      ()

    method enumerate_pictures (n : int) =
      (* Iterates over all elements: *)
      List.fold_left
	(fun n0 x ->
	   x # extension # enumerate_pictures n0)
	n
      	(self # node # sub_nodes)

392
    method print_pages (out:out_channel) (lang:lang) (idx:shared index) (hier:page_hierarchy) =
gerd's avatar
gerd committed
393 394
      ()

395
    method collect_hierarchy (idx : shared index) (path : lang:lang -> string) =
gerd's avatar
gerd committed
396 397 398 399 400 401 402
      (failwith "collect_hierarchy" : page_hierarchy)

    (* --- special methods --- *)

    method refname_of_object =      (* only for objects with ID attribute *)
      (failwith "refname_of_object" : string)

403
    method toc_title_of_object (_ : lang) =
gerd's avatar
gerd committed
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
      (failwith "toc_title_of_object" : string)

    method object_number =
      (failwith "object_number" : string)

    method set_dirname (d:string) = 
      (failwith "set_dirname" : unit)

    method dirname =
      (self # node # root # extension # dirname : string)

    method set_option (name:string) (value:string) = 
      (failwith "set_option" : unit)

    (* --- auxiliary --- *)

    method get_page =
      (* Get the page containing this node *)
      let p = ref (self # node) in
      begin
	try
	  while !p # node_type <> T_element "page" do
	    p := !p # parent;
	  done
	with
	    Not_found -> assert false
      end;
      !p

    (* --- virtual --- *)

435
    method virtual to_latex : lang:lang -> (shared index) -> string
gerd's avatar
gerd committed
436 437 438 439 440 441 442 443 444

  end
;;


class only_data =
  object (self)
    inherit shared

445
    method to_latex ~lang idx =
gerd's avatar
gerd committed
446 447 448 449 450 451 452 453 454
      escape_latex (self # node # data)
  end
;;


class html =
  object (self)
    inherit shared

455
    method to_latex ~lang idx =
gerd's avatar
gerd committed
456 457 458 459 460 461 462 463 464
      ""
  end
;;


class latex =
  object (self)
    inherit shared

465
    method to_latex ~lang idx =
gerd's avatar
gerd committed
466 467 468 469 470 471 472 473 474
      self # node # data
  end
;;


class no_markup =
  object (self)
    inherit shared

475
    method to_latex ~lang idx =
gerd's avatar
gerd committed
476
      map'n'concat
477
	(fun n -> n # extension # to_latex ~lang idx)
gerd's avatar
gerd committed
478 479 480 481 482
	(self # node # sub_nodes)
  end
;;


483 484 485 486 487 488 489 490 491 492
class empty =
  object (self)
    inherit shared

    method to_latex ~lang idx =
      ""
  end
;;


gerd's avatar
gerd committed
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
class presentation =
  object (self)
    inherit no_markup

    val mutable dirname = "."
    val mutable use_ocamldoc = false
    val mutable front_file = None

    method set_dirname d =
      dirname <- d

    method dirname = 
      dirname

    method set_option name v =
      match name with
	  "use_ocamldoc" ->
	    use_ocamldoc <- (v = "true")
	| "front_file" ->
	    front_file <- Some v
	| _ ->
	    ()

    method enumerate_pages n =
      let rec enum k l =
	match l with
	    [] -> ()
	  | p :: l' ->
	      let k' =
		if p # node_type = T_element "page" then k+1 else k in
	      p # extension # enumerate_pages k;
	      enum k' l'
      in
      enum 0 (self # node # sub_nodes)

528
    method print_pages out lang idx hier =
gerd's avatar
gerd committed
529 530 531 532 533
      let rec print hier =
	let pg = 
	  try idx # find hier.pageid 
	  with
	      Not_found -> failwith("Page not found: " ^ hier.pageid) in
534
	pg # extension # print_pages out lang idx hier;
gerd's avatar
gerd committed
535 536 537 538 539
	List.iter print hier.children
      in

      output_string out "\\documentclass{article}\n";
      output_string out "\\usepackage[a4paper,lmargin=2cm,rmargin=2cm,tmargin=3cm,bmargin=4cm]{geometry}\n";
gerd's avatar
gerd committed
540
      output_string out "\\usepackage[T1]{fontenc}\n";
gerd's avatar
gerd committed
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
      output_string out "\\usepackage{pslatex}\n";
      output_string out "\\usepackage[pdftex]{color}\n";
      output_string out "\\usepackage{calc}\n";
      output_string out "\\usepackage{fancyhdr}\n";
      output_string out "\\usepackage{graphicx}\n";
      if use_ocamldoc then
	output_string out "\\usepackage{ocamldoc}\n";
      output_string out "\\parindent=0pt\n";
      output_string out "\\parskip=1.5ex\n";
      output_string out "\\fboxrule=2pt\n";
      output_string out "\\fboxsep=5mm\n";
      output_string out "\\newlength{\\prboxwidth}\n";
      output_string out "\\setlength{\\prboxwidth}{\\textwidth - 1cm - 8pt}\n";

      output_string out "\\newcounter{prpage}\n";
      output_string out "\\newcounter{prpic}\n";
      output_string out "\\pagestyle{fancy}\n";
      output_string out "\\lhead{\\slshape WDialog Manual}\n";
      output_string out "\\chead{}\n";
      output_string out "\\rhead{\\slshape \\rightmark}\n";
      output_string out "\\lfoot{}\n";
      output_string out "\\cfoot{\\thepage}\n";
      output_string out "\\rfoot{}\n";
      (* output_string out "\\renewcommand{\\chaptermark}[1]{}\n"; *)
      output_string out "\\renewcommand{\\sectionmark}[1]{}\n";
      output_string out "\\renewcommand{\\subsectionmark}[1]{}\n";
      output_string out "\\sloppy\n";
      output_string out "\\begin{document}\n";
      ( match front_file with
	    Some f ->
	      output_string out ("\\include{" ^ f ^ "}\n");
	  | None ->
	      ()
      );
      print hier;
      output_string out "\\end{document}\n";

    method collect_hierarchy idx path =
      let rec find_hier l =
	match l with
	    [] -> assert false
	  | x :: l' ->
	      if x # node_type = T_element "hierarchy" then
		x # extension # collect_hierarchy idx path
	      else
		find_hier l'
      in
      find_hier (self # node # sub_nodes)

  end
;;


class layout =
  object (self)
    inherit shared

598
    method to_latex ~lang idx =
gerd's avatar
gerd committed
599 600
      ""

601
    method print_pages out lang idx hier =
gerd's avatar
gerd committed
602 603 604 605 606 607 608 609 610 611
      ()

  end
;;


class headline_layout =
  object (self)
    inherit shared

612
    method to_latex ~lang idx =
gerd's avatar
gerd committed
613 614
      ""

615
    method print_pages out lang idx hier =
gerd's avatar
gerd committed
616 617 618 619 620 621 622 623 624
      ()
  end
;;


class hierarchy =
  object (self)
    inherit shared

625
    method to_latex ~lang idx = ""
gerd's avatar
gerd committed
626 627 628 629 630 631 632 633 634 635 636 637

    method collect_hierarchy idx path =
      (List.hd (self # node # sub_nodes)) # extension # collect_hierarchy idx path
  end
;;


class related =
  object (self)

    inherit shared

638
    method to_latex ~lang idx =
gerd's avatar
gerd committed
639 640 641 642 643 644 645 646 647 648
      ""

  end
;;


class plevel =
  object (self)
    inherit shared

649
    method to_latex ~lang idx = ""
gerd's avatar
gerd committed
650 651 652 653 654 655 656 657 658 659 660 661 662 663 664

    method collect_hierarchy idx path =
      let pageid =
	match self # node # attribute "idref" with
	    Value s -> s
	  | _ -> assert false
      in
      let page = 
	try
	  idx # find pageid
	with
	    Not_found ->
	      failwith ("Cannot find object with ID: " ^ pageid)
      in
      let pagelink = page # extension # refname_of_object in
665 666 667 668
      let title ~lang = page # extension # toc_title_of_object lang in
      let path' ~lang = 
	let p = path lang in
	if p = "" then title lang else p  ^ " / " ^ title lang in
gerd's avatar
gerd committed
669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689
      let children =
	List.flatten
	  (List.map
	     (fun n ->
		if n # node_type = T_element "plevel" then
		  [n # extension # collect_hierarchy idx path']
		else
		  []
	     )
	     (self # node # sub_nodes)) in

      { pageid = pageid;
	pagelink = pagelink;
	title = title;
	web_path = path';
	children = children;
      }
  end
;;


690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711
let title_of_node ~lang n =
  let rec find_title best_title l =
    match l with
      | n :: l' when n # node_type = (T_element "title") ->
	  ( match n # optional_string_attribute "lang" with
	      | None -> find_title n#data l'
	      | Some t ->
		  if (`Lang t) = lang then
		    n#data
		  else
		    find_title best_title l'
	  )
      | _ -> best_title
  in
  match n # optional_string_attribute "title" with
    | None ->
	find_title "" n#sub_nodes
    | Some t ->
	find_title t n#sub_nodes
;;


gerd's avatar
gerd committed
712 713 714 715 716 717 718 719 720
exception Skip_page

class page =
  object (self)
    inherit shared

    val mutable this_page = (-1)
    val mutable web_path = ""

721
    method to_latex ~lang idx =
gerd's avatar
gerd committed
722 723
      try
	(* output header *)
724
	let title = title_of_node ~lang self#node in
gerd's avatar
gerd committed
725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744
	let id =
	  match self # node # optional_string_attribute "id" with
	      None -> "ANONYMOUS"
	    | Some x -> x
	in
	let key = "page-" ^ latexref id in
	let header = 
	  "\\clearpage\n" ^
	  "\\thispagestyle{plain}\n" ^ 
	  "\\markright{" ^ escape_latex web_path ^ "}\n" ^
	  "\\fcolorbox[gray]{0.0}{0.9}{" ^
	  "\\makebox[\\prboxwidth][l]{\\huge\\sf\\bfseries " ^ escape_latex title ^ "}}\n" ^
	  "\\refstepcounter{prpage}\n" ^
	  "\\label{" ^ key ^ "}\n" ^ 
	  "\\par\\hspace*{5.1mm}{\\it Web Path: " ^ escape_latex web_path ^ "}\n"
	in

	(* process main content: *)
	let main =
          map'n'concat
745
	    (fun n -> n # extension # to_latex ~lang idx)
gerd's avatar
gerd committed
746 747 748 749 750 751 752
	    (self # node # sub_nodes) in

	header ^ main

      with
	  Skip_page -> ""

753 754
    method toc_title_of_object lang =
      title_of_node ~lang self#node
gerd's avatar
gerd committed
755 756 757 758 759 760 761 762

    method enumerate_pages n =
      this_page <- n

    method object_number =
      assert(this_page >= 0);
      string_of_int this_page

763
    method print_pages out lang idx hier =
gerd's avatar
gerd committed
764 765
      let do_print = 
	self # node # required_string_attribute "for-latex" = "yes" in
766
      web_path <- hier.web_path ~lang;
gerd's avatar
gerd committed
767
      if do_print then begin
768
	output_string out (self # to_latex ~lang idx);
gerd's avatar
gerd committed
769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788
	output_string out "\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
      end

    method refname_of_object =
      match self # node # attribute "id" with
	  Value s -> 
	    "page-" ^ latexref s
	| Implied_value -> 
	    "page-UNKNOWN"
	| _ -> assert false
  end
;;


class section the_tag =
  object (self)
    inherit shared

    val tag = the_tag

789 790 791 792 793 794 795 796 797 798 799 800 801 802
    method to_latex ~lang idx =
      let title = title_of_node ~lang self # node in
      let children =
	map'n'concat
	  (fun n -> n # extension # to_latex ~lang idx)
	  self#node#sub_nodes
      in
      let latex_cmd =
	match tag with
	    "sect1" -> "\\section"
	  | "sect2" -> "\\subsection"
	  | "sect3" -> "\\subsubsection" 
	  | _ -> assert false in
      latex_cmd ^ "{" ^ title ^ "}\n" ^ children
gerd's avatar
gerd committed
803 804 805 806 807 808 809 810 811 812 813 814
  end
;;

class sect1 = section "sect1";;
class sect2 = section "sect2";;
class sect3 = section "sect3";;


class map_tag pre_cmd post_cmd =
  object (self)
    inherit shared

815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831
    method private language =
      (* Determine the language of the element *)
      match self # node # optional_string_attribute "lang" with
	| None -> `Any
	| Some l -> `Lang l

    method to_latex ~lang idx =
      let taglang = self # language in
      if taglang = `Any || taglang = lang then (
	let children =
	  map'n'concat
	    (fun n -> n # extension # to_latex ~lang idx)
	    (self # node # sub_nodes)
	in
	pre_cmd ^ children ^ post_cmd
      )
      else ""
gerd's avatar
gerd committed
832 833 834 835 836 837 838 839 840 841 842 843 844 845 846
  end
;;

class p = map_tag "\n\n" "\n\n";;
class em = map_tag "{\\it " "}";;
class ul = map_tag "\n\\begin{itemize}\n" "\n\\end{itemize}\n";;
class li = map_tag "\n\\item{}" "";;
class br = map_tag "\\mbox{}\\\\\\relax{}\n" "";;
class footnote = map_tag "\\footnote{" "}";;


class code =
  object (self)
    inherit shared

847
    method to_latex ~lang idx =
gerd's avatar
gerd committed
848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881
      let data = self # node # data in
      (* convert tabs *)
      let l = String.length data in
      let rec preprocess i0 i column =
	(* this is very ineffective but comprehensive: *)
	if i < l then
	  match data.[i] with
	      '\t' ->
		let n = 8 - (column mod 8) in
		let s = String.make n '~' in 
		escape_latex ~tt:true (String.sub data i0 (i-i0)) ^
		s ^ preprocess (i+1) (i+1) (column + n)
	    | ' ' ->
		escape_latex ~tt:true (String.sub data i0 (i-i0)) ^
		"\\ " ^ preprocess (i+1) (i+1) (column + 1)
	    | '\n' ->
		escape_latex ~tt:true (String.sub data i0 (i-i0)) ^
		"\\mbox{}\\\\\\mbox{}\n" ^ preprocess (i+1) (i+1) 0
	    | c ->
		preprocess i0 (i+1) (column + 1)
	else
	  escape_latex ~tt:true (String.sub data i0 (i-i0))
      in
      "\\begin{quote}\n" ^
      "\\tt\\mbox{}\n" ^
      preprocess 0 0 0 ^
      "\\end{quote}\n"
  end
;;

class c =
  object (self)
    inherit shared

882
    method to_latex ~lang idx =
gerd's avatar
gerd committed
883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909
      let data = self # node # data in
      (* convert tabs and spaces *)
      let l = String.length data in
      let rec preprocess i0 i =
	(* this is very ineffective but comprehensive: *)
	if i < l then
	  match data.[i] with
	      '\t' | ' ' ->
		escape_latex ~tt:true (String.sub data i0 (i-i0)) ^
		"\\ " ^ preprocess (i+1) (i+1)
	    | '\n' ->
		escape_latex ~tt:true (String.sub data i0 (i-i0)) ^
		"\\mbox{}\\\\\\mbox{}\n" ^ preprocess (i+1) (i+1)
	    | c ->
		preprocess i0 (i+1)
	else
	  escape_latex ~tt:true (String.sub data i0 (i-i0))
      in
      "{\\tt " ^ preprocess 0 0 ^ "}"
  end
;;


class a =
  object (self)
    inherit shared

910
    method to_latex ~lang idx =
911
      let _langref =
912 913 914 915
	match self # node # optional_string_attribute "langref" with
	  | None -> lang
	  | Some l -> (`Lang l)
      in
gerd's avatar
gerd committed
916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933
      let href =
	match self # node # attribute "href" with
	    Value v -> 
	      "\\footnote{(URL: " ^ escape_latex v ^ ")}"
	  | Valuelist _ -> assert false
	  | Implied_value ->
	      begin match self # node # attribute "idref" with
		  Value v ->
		    let obj =
		      try idx # find v
		      with
			  Not_found ->
			    failwith ("Cannot find object with ID: " ^ v)
		    in
		    let key = obj # extension # refname_of_object in
		    " ($\\rightarrow$ \\pageref{" ^ key ^ "})"
		| Valuelist _ -> assert false
		| Implied_value ->
934 935 936 937
		    (* Link to this page: *)
		    let p = self # get_page in
		    let key = p # extension # refname_of_object in
		    " ($\\rightarrow$ \\pageref{" ^ key ^ "})"
gerd's avatar
gerd committed
938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960
	      end
      in
      "{\\it " ^ escape_latex(self # node # data) ^ "}" ^ href

  end
;;


class picture =
  object (self)
    inherit shared

    val mutable picture_nr = (-1)     (* not yet specified *)

    method object_number =
      assert( picture_nr >= 1);
      string_of_int picture_nr

    method refname_of_object =
      let page = self # get_page in
      let pageref = page # extension # refname_of_object in
      pageref ^ "-pic-" ^ string_of_int picture_nr

961 962 963 964 965 966 967
     method private language =
      (* Determine the language of the picture *)
      match self # node # optional_string_attribute "lang" with
	| None -> `Any
	| Some l -> `Lang l

   method toc_title_of_object _ =
gerd's avatar
gerd committed
968 969 970 971 972 973
      self # node # required_string_attribute "caption"

    method enumerate_pictures n =
      picture_nr <- n;
      n + 1

974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002
    method to_latex ~lang idx = 
      let plang = self # language in 
      if plang = `Any || plang = lang then (
	let page = self # get_page in
	let pageref = page # extension # refname_of_object in
	let key = pageref ^ "-pic-" ^ string_of_int picture_nr in
	let header = 
	  "\\refstepcounter{prpic}\n" ^
	    "\\label{" ^ key ^ "}"
	in
	let src = self # node # required_string_attribute "src" in
	let caption = self # node # required_string_attribute "caption" in
	let filename = Filename.chop_extension 
	  (Filename.concat (self # dirname) src) ^ ".pdf" in
	let s =
	  "\\begin{figure}[bp]\n" ^
	    "\\fboxsep=2mm\n" ^
	    "\\raggedleft\n" ^ 
	    "\\fcolorbox[gray]{0.0}{0.9}{\\begin{minipage}[l]{5cm}\n" ^
	    "{\\bfseries Picture " ^ string_of_int picture_nr ^ ":} " ^
	    escape_latex caption ^
	    "\\end{minipage}}\n" ^ 
	    "\\vspace{1.5ex}\n" ^
	    "\\includegraphics[width=\\textwidth]{" ^ filename ^ "}\n" ^
	    "\\end{figure}\n" 
	in
	header ^ s
      )
      else ""
gerd's avatar
gerd committed
1003 1004 1005 1006 1007 1008 1009 1010
  end
;;


class numref =
  object (self)
    inherit shared

1011
    method to_latex ~lang idx =
gerd's avatar
gerd committed
1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027
      let idref = self # node # required_string_attribute "idref" in
      let obj =
	try idx # find idref
	with
	    Not_found ->
	      failwith ("Cannot find object with ID: " ^ idref)
      in
      escape_latex (obj # extension # object_number)
  end
;;


class nameref =
  object (self)
    inherit shared

1028
    method to_latex ~lang idx =
gerd's avatar
gerd committed
1029 1030 1031 1032 1033 1034 1035 1036 1037
      let idref = self # node # required_string_attribute "idref" in
      let obj =
	try idx # find idref
	with
	    Not_found ->
	      failwith ("Cannot find object with ID: " ^ idref)
      in
      let key = obj # extension # refname_of_object in
      let href = " ($\\rightarrow$ \\pageref{" ^ key ^ "})" in
1038
      let link = obj # extension # toc_title_of_object lang in
gerd's avatar
gerd committed
1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060

      "{\\it " ^ escape_latex link ^ "}" ^ href
  end
;;


(**********************************************************************)

let tag_map =
  make_spec_from_alist
    ~data_exemplar:             (new data_impl (new only_data))
    ~default_element_exemplar:  (new element_impl (new no_markup))
    ~element_alist:
      [ "presentation",    (new element_impl (new presentation));
        "page",            (new element_impl (new page));
	"layout",          (new element_impl (new layout));
        "headline-layout", (new element_impl (new headline_layout));
        "hierarchy",       (new element_impl (new hierarchy));
        "plevel",          (new element_impl (new plevel));
        "sect1",           (new element_impl (new sect1));
        "sect2",           (new element_impl (new sect2));
        "sect3",           (new element_impl (new sect3));
1061
        "title",           (new element_impl (new empty));
gerd's avatar
gerd committed
1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075
        "p",               (new element_impl (new p));
        "br",              (new element_impl (new br));
        "code",            (new element_impl (new code));
        "c",               (new element_impl (new c));
        "em",              (new element_impl (new em));
        "ul",              (new element_impl (new ul));
        "li",              (new element_impl (new li));
        "footnote",        (new element_impl (new footnote));
	"picture",         (new element_impl (new picture));
        "a",               (new element_impl (new a));
	"numref",          (new element_impl (new numref));
	"nameref",         (new element_impl (new nameref));
	"html",            (new element_impl (new html));
	"latex",           (new element_impl (new latex));
1076 1077 1078
	"languages",       (new element_impl (new empty));
	"msg",             (new element_impl (new empty));

gerd's avatar
gerd committed
1079 1080 1081 1082
      ]
    ()
;;