OVH Cloud OVH Cloud

LaTeX->XML->SQL - critique

3 réponses
Avatar
Sean McIlroy
bonjour remi
voici quelques idees a moi

peace
sean

"""
file_nom =3D '/home/remi/droit_tice/faq_gest.tex'
file_lignes =3D [x for x in open(file_nom,'r').readlines() if x[0]<>'%']
## ignore toutes les lignes commen=E7ant par au moins un %
## en plus, il n'y a aucun besoin de << FICHIER.close() >>
file_string =3D ''.join(file_lignes)
"""

###################################################################

def remplacer(t,c,f):
while True:
try:
i =3D t.index(c)
j =3D i + t[i:].index('{')
k =3D j + t[j:].index('}')
t =3D t.replace(t[i:k+1],f(t[j+1:k]))
except: break
return t

c1 =3D '\\url'
f1 =3D lambda x: '\n<a href =3D "' + x + '">' + x + '</a>'

c2 =3D '\\textit'
f2 =3D lambda x: '\n<i>' + x + '</i>'

###################################################################

texte1 =3D '''
tralalala nil nisi divinum stabile est
\\url{adresse_web_0}
the naming of cats is a difficult matter
\\url{adresse_web_1}
i shall wear the bottoms of my trousers rolled
\\url{adresse_web_2}
headpiece filled with straw, alas
'''

texte2 =3D '''
tralalala nil nisi divinum stabile est
\\textit{titre_0}
the naming of cats is a difficult matter
\\textit{titre_1}
i shall wear the bottoms of my trousers rolled
\\textit{titre_2}
headpiece filled with straw, alas
'''

print texte1
print remplacer(texte1,c1,f1)
print texte2
print remplacer(texte2,c2,f2)

3 réponses

Avatar
Sean McIlroy
# -*- coding: Latin-1 -*-

from cStringIO import StringIO
import re

def xReplace(t,a,b):
if not a: return t
return xReplace(t.replace(a[0],b[0]),a[1:],b[1:])

def remplacer(t,c,f):
while True:
try:
i = t.index(c)
j = i + t[i:].index('{')
k = j + t[j:].index('}')
t = t.replace(t[i:k+1],f(t[j+1:k]))
except: break
return t

def pardeux(iterable, n=2):
it = iter(iterable)
next = it.next
while True:
yield next(), next()

def normalize_whitespace(text): return ' '.join(text.split())

file_nom = '/home/remi/droit_tice/faq_gest.tex'
file_string = ''.join([x for x in open(file_nom,'r').readlines() if
x[0]<>'%'])

corps = r"""begin{document}(?P<contents>.*)end{document}"""
r0 =
['begin{enumerate}','end{enumerate}','begin{itemize}','end{itemize}']
r1 = ['<ol>','</ol>','<ul>','</ul>']

contents = re.compile(corps, re.IGNORECASE|
re.DOTALL).search(file_string).group('contents')
contents = xReplace(contents,r0,r1)
contents = remplacer(contents, 'url', lambda x: 'n<a href = "' + x +
'">' + x + '</a>')
contents = re.compile(r'item
(.*?)''n').sub(r'<li>1</li>n',contents)
contents = contents.replace(''','\'')
contents = remplacer(contents, 'textit', lambda x: 'n<i>' + x +
'</i>')

contents_list = re.compile(r'section{(.*)}').split(contents)

fout = StringIO()
fout.write('<?xml version="1.0" encoding="ISO-8859-1"?>n')
fout.write('<!--fichier *.tex "xmlisé" par montex2xml.py-->n')
fout.write('<document>n')
fout.write('<introduction>%s</introduction>n' %
normalize_whitespace(contents_list[0]))

i = j = 1
for titre, contenu in pardeux(contents_list[1:]):
contenus_sections =
re.split(re.compile(r'subsection{(.*)}'),contenu)
fout.write('<section id="%s" name="%s" description="%s">n' %
(str(i),titre,str(normalize_whitespace(contenus_sections[0])) ))
for titre_ssect, contenu_sect in pardeux(contenus_sections[1:]):
fout.write('<sousSection id="%s" thema="%s">' %
(str(j),normalize_whitespace(titre_ssect)))
fout.write('<![CDATA[%s]]>' %
normalize_whitespace(contenu_sect))
fout.write('</sousSection>n')
j=j+1
fout.write('</section>n')
i=i+1
fout.write('</document>')

open('out.xml','w').write(fout.getvalue())
Avatar
remi
Bonjour,

# -*- coding: Latin-1 -*-
[...]


Merci beaucoup pour toutes ces remarques !!
J'étudie tout ça...
Bonne soirée !
@+
Rémi.

Avatar
remi
Bonjour,

# -*- coding: Latin-1 -*-

contents = re.compile(corps, re.IGNORECASE|
re.DOTALL).search(file_string).group('contents')


Un joli "one-liner" ;-) Comme il y en a plusieurs.
il manque plus qu'une écriture plus OO comme un autre intervenant l'a
proposé.
:-)
@+ et merci !
Vive fclp !
Rémi