diff options
author | schnetter <schnetter@17b73243-c579-4c4c-a9d2-2d5706c11dac> | 2005-09-26 00:05:33 +0000 |
---|---|---|
committer | schnetter <schnetter@17b73243-c579-4c4c-a9d2-2d5706c11dac> | 2005-09-26 00:05:33 +0000 |
commit | eb718a1538c53ae843db796af342bc2aedf466c5 (patch) | |
tree | ebf128b4d7815fd972d826f569030f4c1c16a494 | |
parent | d73744780e33053ae162c514e81b404ef2680006 (diff) |
Improve routine CleanForLatex: Really parse the passed strings instead
of only applying a few regexps. This should be a big step towards
correctness.
git-svn-id: http://svn.cactuscode.org/flesh/trunk@4143 17b73243-c579-4c4c-a9d2-2d5706c11dac
-rw-r--r-- | lib/sbin/ThornUtils.pm | 71 |
1 files changed, 42 insertions, 29 deletions
diff --git a/lib/sbin/ThornUtils.pm b/lib/sbin/ThornUtils.pm index acbdca0b..c3ff5b1d 100644 --- a/lib/sbin/ThornUtils.pm +++ b/lib/sbin/ThornUtils.pm @@ -382,39 +382,52 @@ sub CleanFromC #@@*/ sub CleanForLatex { - my $val = shift; - - # escape special characters - $val =~ s,\\,\{\\textbackslash\},g; - $val =~ s,~,\{\\textasciitilde\},g; - $val =~ s,<,\{\\textless\},g; - $val =~ s,>,\{\\textgreater\},g; - - # at start of string, remove spaces before and after: " - $val =~ s,^\s*?\"\s*?,\",; - - # at end of string, remove spaces before and after: " - $val =~ s,\s*?\"\s*?$,\",; - - # escape _ - $val =~ s,\_,\\\_,g; + my $inval = shift; - # escape $ - $val =~ s,\$,\\\$,g; + # at start of string, remove spaces before: " + $inval =~ s,^\s*\",\",; - # escape ^ - $val =~ s,\^,\\\^,g; + # at end of string, remove spaces after: " + $inval =~ s,\"\s*$,\",; - # escape * - $val =~ s,\*,\\\*,g; - - # escape & - $val =~ s,\&,\\\&,g; - - # escape % - $val =~ s,%,\\%,g; + # escape special characters + my $outval = ""; + foreach my $i (0 .. length($inval)-1) + { + my $char = $inval[i]; + if ($char eq '{' or + $char eq '}' or + $char eq '$' or + $char eq '_' or + $char eq '^' or + $char eq '&' or + $char eq '%') + { + $outval .= '\\' . $char; + } + elsif ($char eq '\\') + { + $outval .= '{\\textbackslash}'; + } + elsif ($char eq '~') + { + $outval .= '{\\textasciitilde}'; + } + elsif ($char eq '<') + { + $outval .= '{\\textless}'; + } + elsif ($char eq '>') + { + $outval .= '{\\textgreater}'; + } + else + { + $outval .= $char; + } + } - return $val; + return $outval; } #/*@@ |