File Coverage

blib/lib/Lingua/WriteLatexFile.pm
Criterion Covered Total %
statement 3 58 5.1
branch n/a
condition n/a
subroutine 1 7 14.2
pod n/a
total 4 65 6.1


line stmt bran cond sub pod time code
1              
2             ########################################################################
3             # Author: Patrik Lambert (lambert@talp.ucp.es)
4             # Description: Provides methods to write a latex file.
5             #
6             #-----------------------------------------------------------------------
7             #
8             # Copyright 2004 by Patrik Lambert
9             #
10             # This program is free software; you can redistribute it and/or modify
11             # it under the terms of the GNU General Public License as published by
12             # the Free Software Foundation; either version 2 of the License, or
13             # (at your option) any later version.
14             #
15             # This program is distributed in the hope that it will be useful,
16             # but WITHOUT ANY WARRANTY; without even the implied warranty of
17             # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18             # GNU General Public License for more details.
19             #
20             # You should have received a copy of the GNU General Public License
21             # along with this program; if not, write to the Free Software
22             # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23             ########################################################################
24              
25             package Lingua::Latex;
26              
27 1     1   5 use strict;
  1         2  
  1         1061  
28              
29             sub new {
30 0     0     my ($pkg) = shift;
31 0           my $this = {};
32              
33 0           return bless $this,$pkg;
34             }
35              
36             sub startFile {
37 0     0     my $this = shift;
38              
39 0           return '\documentclass[10pt]{article}
40             \usepackage[dvips]{graphics}
41             \usepackage{amssymb,amsmath}
42             \topmargin 0pt
43              
44             \headsep 0pt
45             \evensidemargin 0pt
46             \oddsidemargin \evensidemargin
47             \setlength{\headheight}{0cm}
48             \setlength{\textwidth}{17cm}
49             \setlength{\textheight}{24.5cm}
50              
51             \begin{document}
52              
53             \newcommand{\ver}[1]{\rotatebox{90}{#1}}
54              
55             ';
56             }
57              
58             sub endFile {
59 0     0     my $this = shift;
60              
61 0           return '
62             \end{document}
63             ';
64             }
65              
66             sub setTabcolsep {
67 0     0     my ($this,$tabcolsep) = @_;
68 0           return '
69             \tabcolsep'.$tabcolsep.'
70             ';
71             }
72              
73             # fromText: do the conversion from text to latex
74             # INPUT a string in text format
75             # OUTPUT a string in latex-compatible format
76             sub fromText {
77 0     0     my ($this,$txtString) = @_ ;
78 0           my $line = $txtString;
79              
80             # Eliminate windows "^M" character
81 0           $line =~ s/\ //g;
82              
83             # Escape special characters
84 0           $line =~ s/\\/{\\textbackslash}/g;
85 0           $line =~ s/{/\\{/g;
86 0           $line =~ s/}/\\}/g;
87 0           $line =~ s/\\{\\textbackslash\\}/{\\textbackslash}/g;
88 0           $line =~ s/\$/\\\$/g;
89 0           $line =~ s/%/\\%/g;
90 0           $line =~ s/_/\\_/g;
91 0           $line =~ s/&/\\&/g;
92 0           $line =~ s/\#/\\\#/g;
93            
94             # Ellipses
95 0           $line =~ s/(^|[^.])\.\.\.([^.])/\1\\ldots\2/g;
96              
97             # Fix double quotes
98 0           $line =~ s/(^|\s)\"/\1``/g;
99 0           $line =~ s/\"(\W|$)/''\1/g;
100              
101             # Fix single quotes
102 0           $line =~ s/(^|\s)'/\1`/g;
103              
104             # Convert return caracters
105 0           $line =~ s/\n/\n\n/g;
106             # $line =~ s/\n\n/\\\\/g;
107              
108 0           $line =~ s/\^/\$\\hat\{ \}\$/g; #must be before the s/ê/...
109              
110             # Escape accents
111 0           $line =~ s/à/\\`{a}/gi;
112 0           $line =~ s/á/\\'{a}/gi;
113 0           $line =~ s/ä/\\"{a}/gi;
114 0           $line =~ s/â/\\^{a}/gi;
115              
116 0           $line =~ s/è/\\`{e}/gi;
117 0           $line =~ s/é/\\'{e}/gi;
118 0           $line =~ s/ë/\\"{e}/gi;
119 0           $line =~ s/ê/\\^{e}/gi;
120              
121 0           $line =~ s/ì/\\`{i}/gi;
122 0           $line =~ s/í/\\'{i}/gi;
123 0           $line =~ s/ï/\\"{i}/gi;
124 0           $line =~ s/î/\\^{i}/gi;
125              
126 0           $line =~ s/ò/\\`{o}/gi;
127 0           $line =~ s/ó/\\'{o}/gi;
128 0           $line =~ s/ö/\\"{o}/gi;
129 0           $line =~ s/ô/\\^{o}/gi;
130              
131 0           $line =~ s/ù/\\`{u}/gi;
132 0           $line =~ s/ú/\\'{u}/gi;
133 0           $line =~ s/ü/\\"{u}/gi;
134 0           $line =~ s/û/\\^{u}/gi;
135              
136 0           $line =~ s/ñ/\\~{n}/gi;
137              
138             # Escape punctuation
139 0           $line =~ s/¿/?`/gi;
140 0           $line =~ s/¡/!`/gi;
141              
142             # Escape math characters
143 0           $line =~ s/([<>]+)/\$$1\$/g;
144              
145 0           return $line;
146              
147             }
148              
149             sub fromTextFile{
150 0     0     my ($this,$line) = @_;
151 0           chomp $line;
152 0           return $this->fromText($line)."\\\\","\n";
153             }
154              
155             1;