(*
* bibtex2html - A BibTeX to HTML translator
* Copyright (C) 1997-2000 Jean-Christophe Filliâtre and Claude Marché
*
* This software is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License version 2, as published by the Free Software Foundation.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* See the GNU General Public License version 2 for more details
* (enclosed in the file GPL).
*)
(*i $Id: latexmacros.ml,v 1.63 2007-07-16 14:34:08 filliatr Exp $ i*)
(*s This code is Copyright (C) 1997 Xavier Leroy. *)
open Printf
open Options
(*s Output functions. *)
let out_channel = ref stdout
let print_s s = output_string !out_channel s
let print_c c = output_char !out_channel c
(*s Actions and translations table. *)
type action =
| Print of string
| Print_arg
| Skip_arg
| Raw_arg of (string -> unit)
| Parameterized of (string -> action list)
| Recursive of string (*r piece of LaTeX to analyze recursively *)
let cmdtable = (Hashtbl.create 19 : (string, action list) Hashtbl.t)
let def name action =
Hashtbl.add cmdtable name action
let find_macro name =
try
Hashtbl.find cmdtable name
with Not_found ->
if not !quiet then eprintf "Unknown macro: %s\n" name;
[]
;;
(*s Translations of general LaTeX macros. *)
(* Sectioning *)
def "\\part"
[Print "
"];
def "\\end{alltt}" [Print ""];
def "\\textbf" [Print "" ; Print_arg ; Print ""];
def "\\mathbf" [Print "" ; Print_arg ; Print ""];
def "\\texttt" [Print "" ; Print_arg ; Print ""];
def "\\mathtt" [Print "" ; Print_arg ; Print ""];
def "\\textit" [Print "" ; Print_arg ; Print ""];
def "\\mathit" [Print "" ; Print_arg ; Print ""];
def "\\textsl" [Print "" ; Print_arg ; Print ""];
def "\\textem" [Print "" ; Print_arg ; Print ""];
def "\\textrm" [Print_arg];
def "\\mathrm" [Print_arg];
def "\\textmd" [Print_arg];
def "\\textup" [Print_arg];
def "\\textnormal" [Print_arg];
def "\\mathnormal" [Print "" ; Print_arg ; Print ""];
def "\\mathcal" [Print_arg];
def "\\mathbb" [Print_arg];
def "\\mathfrak" [Print_arg];
def "\\textin" [Print ""; Print_arg; Print ""];
def "\\textsu" [Print ""; Print_arg; Print ""];
def "\\textsi" [Print "" ; Print_arg ; Print ""];
(* Basic color support. *)
def "\\textcolor" [ Parameterized (function name ->
match String.lowercase name with
(* At the moment, we support only the 16 named colors defined in HTML 4.01. *)
| "black" | "silver" | "gray" | "white" | "maroon" | "red" | "purple" | "fuchsia"
| "green" | "lime" | "olive" | "yellow" | "navy" | "blue" | "teal" | "aqua" ->
[ Print (Printf.sprintf "" name);
Print_arg ;
Print ""
]
(* Other, unknown colors have no effect. *)
| _ ->
[ Print_arg ]
)];
(* Fonts without HTML equivalent *)
def "\\textsf" [Print "" ; Print_arg ; Print ""];
def "\\mathsf" [Print "" ; Print_arg ; Print ""];
def "\\textsc" [Print_arg];
def "\\textln" [Print_arg];
def "\\textos" [Print_arg];
def "\\textdf" [Print_arg];
def "\\textsw" [Print_arg];
def "\\rm" [];
def "\\cal" [];
def "\\emph" [Print "" ; Print_arg ; Print ""];
def "\\mbox" [Print_arg];
def "\\footnotesize" [];
def "\\etalchar" [ Print "" ; Raw_arg print_s ; Print "" ];
def "\\newblock" [Print " "];
(* Environments *)
def "\\begin{itemize}" [Print ""]; def "\\end{center}" [Print ""]; def "\\begin{htmlonly}" []; def "\\end{htmlonly}" []; def "\\begin{flushleft}" [Print "
"]; def "\\end{flushleft}" [Print ""]; (* Special characters *) def "\\ " [Print " "]; def "\\\n" [Print " "]; def "\\{" [Print "{"]; def "\\}" [Print "}"]; def "\\l" [Print "l"]; def "\\oe" [Print "œ"]; def "\\OE" [Print "Œ"]; def "\\o" [Print "ø"]; def "\\O" [Print "Ø"]; def "\\ae" [Print "æ"]; def "\\AE" [Print "Æ"]; def "\\aa" [Print "å"]; def "\\AA" [Print "Å"]; def "\\&" [Print "&"]; def "\\$" [Print "$"]; def "\\%" [Print "%"]; def "\\_" [Print "_"]; def "\\slash" [Print "/"]; def "\\copyright" [Print "(c)"]; def "\\th" [Print "þ"]; def "\\TH" [Print "Þ"]; def "\\dh" [Print "ð"]; def "\\DH" [Print "Ð"]; def "\\ss" [Print "ß"]; def "\\'" [Raw_arg(function "e" -> print_s "é" | "E" -> print_s "É" | "a" -> print_s "á" | "A" -> print_s "Á" | "o" -> print_s "ó" | "O" -> print_s "Ó" | "i" -> print_s "í" | "\\i" -> print_s "í" | "I" -> print_s "Í" | "u" -> print_s "ú" | "U" -> print_s "Ú" | "'" -> print_s "”" | "c" -> print_s "ć" | "y" -> print_s "ý" | "Y" -> print_s "Ý" | "" -> print_c '\'' | s -> print_s s)]; def "\\`" [Raw_arg(function "e" -> print_s "è" | "E" -> print_s "È" | "a" -> print_s "à" | "A" -> print_s "À" | "o" -> print_s "ò" | "O" -> print_s "Ò" | "i" -> print_s "ì" | "\\i" -> print_s "ì" | "I" -> print_s "Ì" | "u" -> print_s "ù" | "U" -> print_s "Ù" | "`" -> print_s "“" | "" -> print_s "‘" | s -> print_s s)]; def "\\~" [Raw_arg(function "n" -> print_s "ñ" | "N" -> print_s "Ñ" | "o" -> print_s "õ" | "O" -> print_s "Õ" | "a" -> print_s "ã" | "A" -> print_s "Ã" | "" -> print_s "˜" | s -> print_s s)]; def "\\c" [Raw_arg(function "c" -> print_s "ç" | s -> print_s s)]; def "\\^" [Raw_arg(function "a" -> print_s "â" | "A" -> print_s "Â" | "e" -> print_s "ê" | "E" -> print_s "Ê" | "i" -> print_s "î" | "\\i" -> print_s "î" | "I" -> print_s "Î" | "o" -> print_s "ô" | "O" -> print_s "Ô" | "u" -> print_s "û" | "U" -> print_s "Û" | "" -> print_c '^' | s -> print_s s)]; def "\\hat" [Raw_arg(function "a" -> print_s "â" | "A" -> print_s "Â" | "e" -> print_s "ê" | "E" -> print_s "Ê" | "i" -> print_s "î" | "\\i" -> print_s "î" | "I" -> print_s "Î" | "o" -> print_s "ô" | "O" -> print_s "Ô" | "u" -> print_s "û" | "U" -> print_s "Û" | "" -> print_c '^' | s -> print_s s)]; def "\\\"" [Raw_arg(function "e" -> print_s "ë" | "E" -> print_s "Ë" | "a" -> print_s "ä" | "A" -> print_s "Ä" | "\\i" -> print_s "ï" | "i" -> print_s "ï" | "I" -> print_s "Ï" | "o" -> print_s "ö" | "O" -> print_s "Ö" | "u" -> print_s "ü" | "U" -> print_s "Ü" | s -> print_s s)]; def "\\u" [Raw_arg print_s ]; def "\\v" [Raw_arg(function | "C" -> print_s "Č" | "c" -> print_s "č" | "D" -> print_s "Ď" | "d" -> print_s "ď" | "E" -> print_s "Ě" | "e" -> print_s "ě" | "N" -> print_s "Ň" | "n" -> print_s "ň" | "r" -> print_s "ř" | "R" -> print_s "Ř" | "s" -> print_s "š" | "S" -> print_s "Š" | "T" -> print_s "Ť" | "t" -> print_s "ť" | "\\i" -> print_s "ĭ" | "i" -> print_s "ĭ" | "I" -> print_s "Ĭ" | "Z" -> print_s "Ž" | "z" -> print_s "ž" | s -> print_s s)]; def "\\H" [Raw_arg (function | "O" -> print_s "Ő" | "o" -> print_s "ő" | "U" -> print_s "Ű" | "u" -> print_s "ű" | s -> print_s s)]; def "\\r" [Raw_arg (function | "U" -> print_s "Ů" | "u" -> print_s "ů" | s -> print_s s)]; (* Math macros *) def "\\[" [Print "
"]; def "\\]" [Print "\n"]; def "\\le" [Print "<="]; def "\\leq" [Print "<="]; def "\\log" [Print "log"]; def "\\ge" [Print ">="]; def "\\geq" [Print ">="]; def "\\neq" [Print "<>"]; def "\\circ" [Print "o"]; def "\\bigcirc" [Print "O"]; def "\\sim" [Print "~"]; def "\\(" [Print ""]; def "\\)" [Print ""]; def "\\mapsto" [Print "|->"]; def "\\times" [Print "×"]; def "\\neg" [Print "¬"]; def "\\frac" [Print "("; Print_arg; Print ")/("; Print_arg; Print ")"]; (* Math symbols printed as texts (could we do better?) *) def "\\ne" [Print "=/="]; def "\\in" [Print "in"]; def "\\forall" [Print "for all"]; def "\\exists" [Print "there exists"]; def "\\vdash" [Print "|-"]; def "\\ln" [Print "ln"]; def "\\gcd" [Print "gcd"]; def "\\min" [Print "min"]; def "\\max" [Print "max"]; def "\\exp" [Print "exp"]; def "\\rightarrow" [Print "->"]; def "\\to" [Print "->"]; def "\\longrightarrow" [Print "-->"]; def "\\Rightarrow" [Print "=>"]; def "\\leftarrow" [Print "<-"]; def "\\longleftarrow" [Print "<--"]; def "\\Leftarrow" [Print "<="]; def "\\leftrightarrow" [Print "<->"]; def "\\sqrt" [Print "sqrt("; Print_arg; Print ")"]; def "\\vee" [Print "V"]; def "\\lor" [Print "V"]; def "\\wedge" [Print "/\\"]; def "\\land" [Print "/\\"]; def "\\parallel" [Print "||"]; def "\\mid" [Print "|"]; def "\\cup" [Print "U"]; def "\\inf" [Print "inf"]; (* Misc. macros. *) def "\\TeX" [Print "TEX"]; def "\\LaTeX" [Print "LATEX"]; def "\\LaTeXe" [Print "LATEX 2e"]; def "\\tm" [Print "TM"]; def "\\par" [Print "
"];
def "\\@" [];
def "\\#" [Print "#"];
def "\\/" [];
def "\\-" [];
def "\\left" [];
def "\\right" [];
def "\\smallskip" [];
def "\\medskip" [];
def "\\bigskip" [];
def "\\relax" [];
def "\\markboth" [Skip_arg; Skip_arg];
def "\\dots" [Print "..."];
def "\\dot" [Print "."];
def "\\simeq" [Print "˜="];
def "\\approx" [Print "˜"];
def "\\^circ" [Print "°"];
def "\\ldots" [Print "..."];
def "\\cdot" [Print "·"];
def "\\cdots" [Print "..."];
def "\\newpage" [];
def "\\hbox" [Print_arg];
def "\\noindent" [];
def "\\label" [Print ""];
def "\\ref" [Print "(ref)"];
def "\\index" [Skip_arg];
def "\\\\" [Print "
"];
def "\\," [];
def "\\;" [];
def "\\!" [];
def "\\hspace" [Skip_arg; Print " "];
def "\\symbol"
[Raw_arg (function s ->
try let n = int_of_string s in print_c (Char.chr n)
with _ -> ())];
def "\\html" [Raw_arg print_s];
(* hyperref *)
def "\\href"
[Print ""; Print_arg; Print ""];
(* Bibliography *)
def "\\begin{thebibliography}" [Print "