summaryrefslogtreecommitdiff
path: root/support/txt2latex/txt2latex
blob: 41a434a18b839b8427ebc47c8f71adafcf5da546 (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
#!/usr/bin/perl
#
# txt2latex -- by Tristan Miller (psychonaut@nothingisreal.com)
# This program is in the public domain.  It comes with absolutely NO
# WARRANTY -- use at your own risk.
#
# Revision history
# ----------------
# 1.0 -- 2002/02/20 -- initial release
#
# Description
# -----------
# txt2latex assists in translation from ASCII text to LaTeX by escaping
# special characters and "fixing" quote marks (using English conventions).
# Reads from standard input and writes to standard output.
# Any "unfixed" double quotes are reported on standard error.
#
# If there is a demand, I will add command-line options allowing
# customization or suppression of the document header/footer, and use of
# non-English quotation marks (e.g., "` and "').

$count = 0;

# Header
print STDOUT "\\documentclass{article}\n\\begin{document}\n";

foreach $line (<>) {

  $count++;

  # Escape special characters
  $line =~ s/\\/{\\textbackslash}/g;
  $line =~ s/{/\\{/g;
  $line =~ s/}/\\}/g;
  $line =~ s/\\{\\textbackslash\\}/{\\textbackslash}/g;
  $line =~ s/\$/\\\$/g;
  $line =~ s/%/\\%/g;
  $line =~ s/_/\\_/g;
  $line =~ s/&/\\&/g;
  $line =~ s/\#/\\\#/g;

  # Ellipses
  $line =~ s/(^|[^.])\.\.\.([^.])/\1\\ldots\2/g;

  # Fix double quotes
  $line =~ s/(^|\s)\"/\1``/g;
  $line =~ s/\"(\W|$)/''\1/g;

  # Fix single quotes
  $line =~ s/(^|\s)'/\1`/g;

  if ($line =~ /\"/) {
    print STDERR "txt2latex: unfixed quote mark on line $count\n";
  }

  print STDOUT $line;
}

# Footer
print STDOUT "\\end{document}\n";