Ir al contenido

Usuario:Arkin/ltx2wiki

De Wikilibros, la colección de libros de texto de contenido libre.
#This script converts a LaTeX document into a text in Wiki format
# Author: Rub\'en Maguregui
# Licence: GNU

use strict;
use utf8;

print "\nNombre de archivo fuente: ";
my $inFile = <STDIN>;
open MYHANDLE, "<$inFile" or die "No se pudo encontrar $inFile";
print "\nNombre de archivo modificado: ";
my $outFile = <STDIN>;

open MYHANDLEB, ">$outFile";
undef $/;
$_ = <MYHANDLE>;

my %simpcmd = qw(
			smallskip		<br>
			medskip			<br><br>
			bigskip			<br><br><br>
			);

my %open = qw(
			\chapter{		==
			\section{		===
			\subsection{	====
			\textbf{		'''
			\textit{		''
			\texttt{		<tt>
			\emph{			''
			\begin{			\begin{
			\end{			\end{
			\openingBracket{ {
			);
my %close = qw(
			\chapter{		==
			\section{		===
			\subsection{	====
			\textbf{		'''
			\textit{		''
			\textttt{		</tt>
			\emph{			''
			\begin{			\closingBracket
			\end{			\closingBracket
			\openingBracket{ \closingBracket
			);

my %begin = qw(
			center			<center>
			equation		<center><math>
			eqnarray		<center><math>\begin{array}
			align			<center><math>\begin{array}
			);
			
my %end = qw(
			center			</center>
			equation		</math></center>
			eqnarray		\end{array}</math></center>
			align			\end{array}</math></center>
			);
			
my %accented = qw(
			a				á
			A				Á
			e				é
			E				É
			i				í
			I				Í
			o				ó
			O				Ó
			u				ú
			U				Ú
			);

my @mathTags = ('<center><math>','</math></center>','<math>','</math>'); #Math tags
my $mathCount = 0; #Counter for determining correct thags
my $mathCase; # Determine which math tag should be used
my @cTags; #Closing tags

# Math mode substitution

s{(\$)(.)(?#
)(?{$next = ""; if($2 eq '$' && $mathCount % 2 == 0){$mathCase = 0}
elsif($2 eq '$'){$mathCase = 1}
elsif(!($2 eq '$') && $mathCount % 2 == 0){$mathCase = 2; $next = $2}
else{$mathCase = 3; $next = " "}
$mathCount++;
})}
{$mathTags[$mathCase]$next}g;

# Substitution of environments
s/\\begin\{(\w+)\}/$begin{$1}/g;
s/\\end\{(\w+)\}/$end{$1}/g;

#The following code substitues LaTeX commands by their corresponding Wiki tags

s/[^\\\w+]\{/ \\openingBracket\{/g;

s{(\\\w+\{)(?{push @cTags, $close{$1}})}
{$open{$1}}g;

# Substitution of closing brackets by corresponding closing tags
while(s/\}/$cTags[0]/){shift @cTags}

# Substitute any remaining closing brackets
s/\\closingBracket/\}/g;

#Substitute accented bowels
s/\\\'(\w)/$accented{$1}/g;

print MYHANDLEB $_;